-
Notifications
You must be signed in to change notification settings - Fork 320
/
ch-1.pl
74 lines (68 loc) · 1.92 KB
/
ch-1.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env perl
# Task 1: Minimum Index Sum
# Submitted by: Mohammad S Anwar
#
# You are given two arrays of strings.
# Write a script to find out all common strings in the given two arrays with minimum index sum. If no common strings found returns an empty list.
#
# Example 1
# Input: @list1 = ("Perl", "Raku", "Love")
# @list2 = ("Raku", "Perl", "Hate")
# Output: ("Perl", "Raku")
#
# There are two common strings "Perl" and "Raku".
# Index sum of "Perl": 0 + 1 = 1
# Index sum of "Raku": 1 + 0 = 1
#
# Example 2
# Input: @list1 = ("A", "B", "C")
# @list2 = ("D", "E", "F")
# Output: ()
#
# No common string found, so no result.
#
# Example 3
# Input: @list1 = ("A", "B", "C")
# @list2 = ("C", "A", "B")
# Output: ("A")
#
# There are three common strings "A", "B" and "C".
# Index sum of "A": 0 + 1 = 1
# Index sum of "B": 1 + 2 = 3
# Index sum of "C": 2 + 0 = 2
use strict;
use warnings;
use feature 'say';
say get_minimum_index_sum(["Perl", "Raku", "Love"],
["Raku", "Perl", "Hate"]
);
say get_minimum_index_sum(["A", "B", "C"],
["D", "E", "F"]
);
say get_minimum_index_sum(["A", "B", "C"],
["C", "A", "B"]
);
exit 0;
sub get_minimum_index_sum {
my ($list1, $list2) = @_;
my @words;
my $value;
foreach my $i (0..scalar(@$list1)-1) {
foreach my $j (0..scalar(@$list2)-1) {
last
if defined($value) and $value < $i + $j;
if ($list1->[$i] eq $list2->[$j]) {
if (!defined($value)
or
$value > $i+$j
) {
@words = ();
$value = $i+$j;
}
push @words, "\"$list1->[$i]\"";
last;
}
}
}
return sprintf "(%s)", join ', ', @words;
}