The weekly challenge 383 - Task 1: Similar List
1 #!/usr/bin/env perl 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-383/#TASK1 3 # 4 # Task 1: Similar List 5 # ==================== 6 # 7 # You are given three list of strings. 8 # 9 # Write a script to find out if the first two list are similar with the help 10 # the third list. The third list contains the similar words map. 11 # 12 ## Example 1 13 ## 14 ## Input: $list1 = ("great", "acting") 15 ## $list2 = ("fine", "drama") 16 ## $list3 = (["great", "fine"], ["acting", "drama"]) 17 ## Output: true 18 # 19 ## Example 2 20 ## 21 ## Input: $list1 = ("apple", "pie") 22 ## $list2 = ("banana", "pie") 23 ## $list3 = (["apple", "peach"], ["peach", "banana"]) 24 ## Output: false 25 # 26 ## Example 3 27 ## 28 ## Input: $list1 = ("perl4", "python") 29 ## $list2 = ("raku", "python") 30 ## $list3 = (["perl4", "perl5", "raku"]) 31 ## Output: true 32 # 33 ## Example 4 34 ## 35 ## Input: $list1 = ("enjoy", "challenge") 36 ## $list2 = ("love", "weekly", "challenge") 37 ## $list3 = (["enjoy", "love"]) 38 ## Output: false 39 # 40 ## Example 5 41 ## 42 ## Input: $list1 = ("fast", "car") 43 ## $list2 = ("quick", "vehicle") 44 ## $list3 = (["quick", "fast"], ["vehicle", "car"]) 45 ## Output: true 46 # 47 ############################################################ 48 ## 49 ## discussion 50 ## 51 ############################################################ 52 # 53 # If both list1 and list2 have different lengths, they are not similar, so 54 # handle that case first. Then we return false for every index in the lists 55 # where the two values from the two lists don't map to similar words. 56 # In the end we know that for each index, the two elements map to similar 57 # words, so we can return true. 58 59 use v5.36; 60 61 sub similar_list($list1, $list2, $list3) { 62 say "Input: list1 = (" . join(", ", @$list1) . "),"; 63 say " list2 = (" . join(", ", @$list2) . "),"; 64 say " list3 = (" . join(", ", map {"[" . join(", ", @{$_}) . "]"} @$list3) . ")"; 65 66 return say "Output: false" unless scalar(@$list1) == scalar(@$list2); 67 my $last_index = scalar(@$list1) - 1; 68 69 foreach my $i (0..$last_index) { 70 return say "Output: false" unless maps_the_same($list1->[$i], $list2->[$i], $list3); 71 } 72 say "Output: true"; 73 } 74 75 sub maps_the_same($elem1, $elem2, $list) { 76 return 1 if $elem1 eq $elem2; 77 foreach my $map (@$list) { 78 my $hash = {}; 79 map { $hash->{$_} = 1, } @$map; 80 return 1 if $hash->{$elem1} && $hash->{$elem2}; 81 } 82 return 0; 83 } 84 85 similar_list(["great", "acting"], ["fine", "drama"], [["great", "fine"], ["acting", "drama"]]); 86 similar_list(["apple", "pie"], ["banana", "pie"], [["apple", "peach"], ["peach", "banana"]]); 87 similar_list(["perl4", "python"], ["raku", "python"], [["perl4", "perl5", "raku"]]); 88 similar_list(["enjoy", "challenge"], ["love", "weekly", "challenge"], [["enjoy", "love"]]); 89 similar_list(["fast", "car"], ["quick", "vehicle"], [["quick", "fast"], ["vehicle", "car"]]); 90