The weekly challenge 239 - Task 1: Same String

 1 #!/usr/bin/perl
 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-239/#TASK1
 3 #
 4 # Task 1: Same String
 5 # ===================
 6 #
 7 # You are given two arrays of strings.
 8 #
 9 # Write a script to find out if the word created by concatenating the array
10 # elements is the same.
11 #
12 ## Example 1
13 ##
14 ## Input: @arr1 = ("ab", "c")
15 ##        @arr2 = ("a", "bc")
16 ## Output: true
17 ##
18 ## Using @arr1, word1 => "ab" . "c" => "abc"
19 ## Using @arr2, word2 => "a" . "bc" => "abc"
20 #
21 ## Example 2
22 ##
23 ## Input: @arr1 = ("ab", "c")
24 ##        @arr2 = ("ac", "b")
25 ## Output: false
26 ##
27 ## Using @arr1, word1 => "ab" . "c" => "abc"
28 ## Using @arr2, word2 => "ac" . "b" => "acb"
29 #
30 ## Example 3
31 ##
32 ## Input: @arr1 = ("ab", "cd", "e")
33 ##        @arr2 = ("abcde")
34 ## Output: true
35 ##
36 ## Using @arr1, word1 => "ab" . "cd" . "e" => "abcde"
37 ## Using @arr2, word2 => "abcde"
38 #
39 ############################################################
40 ##
41 ## discussion
42 ##
43 ############################################################
44 #
45 # This one is simple, just compare the strings after putting them
46 # together from the arrays' content.
47 #
48 use strict;
49 use warnings;
50 
51 same_string( [ "ab", "c" ], ["a", "bc"]);
52 same_string( [ "ab", "c" ], ["ac", "b"]);
53 same_string( [ "ab", "cd", "e" ], [ "abcde" ]);
54 
55 sub same_string {
56    my ($arr1, $arr2) = @_;
57    print "Input: (\"" . join("\", \"", @$arr1) . "\"), (\"" . join("\", \"", @$arr2) . "\")\n";
58    if( join("", @$arr1) eq join("", @$arr2) ) {
59       print "Output: true\n";
60    } else {
61       print "Output: false\n";
62    }
63 }