The weekly challenge 276 - Task 1: Complete Day
1 #!/usr/bin/env perl 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-276/#TASK1 3 # 4 # Task 1: Complete Day 5 # ==================== 6 # 7 # You are given an array of integers, @hours. 8 # 9 # Write a script to return the number of pairs that forms a complete day. 10 # 11 ### A complete day is defined as a time duration that is an exact multiple of 12 ### 24 hours. 13 # 14 ## Example 1 15 ## 16 ## Input: @hours = (12, 12, 30, 24, 24) 17 ## Output: 2 18 ## 19 ## Pair 1: (12, 12) 20 ## Pair 2: (24, 24) 21 # 22 ## Example 2 23 ## 24 ## Input: @hours = (72, 48, 24, 5) 25 ## Output: 3 26 ## 27 ## Pair 1: (72, 48) 28 ## Pair 2: (72, 24) 29 ## Pair 3: (48, 24) 30 # 31 ## Example 3 32 ## 33 ## Input: @hours = (12, 18, 24) 34 ## Output: 0 35 # 36 ############################################################ 37 ## 38 ## discussion 39 ## 40 ############################################################ 41 # 42 # Create all possible pairs and check which ones are multiples 43 # of 24. 44 45 use strict; 46 use warnings; 47 48 complete_day(12, 12, 30, 24, 24); 49 complete_day(72, 48, 24, 5); 50 complete_day(12, 18, 24); 51 52 sub complete_day { 53 my @hours = @_; 54 print "Input: (", join(", ", @hours), ")\n"; 55 my $output = 0; 56 foreach my $i (0..$#hours) { 57 foreach my $j ($i+1..$#hours) { 58 $output++ unless (($hours[$i]+$hours[$j]) % 24); 59 } 60 } 61 print "Output: $output\n"; 62 }