/
ch-2.pl
executable file
·68 lines (64 loc) · 1.85 KB
/
ch-2.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
#!/usr/bin/perl
# https://theweeklychallenge.org/blog/perl-weekly-challenge-229/#TASK2
#
# Task 2: Two out of Three
# ========================
#
# You are given three array of integers.
#
# Write a script to return all the elements that are present in at least 2 out of 3 given arrays.
#
## Example 1
##
## Input: @array1 = (1, 1, 2, 4)
## @array2 = (2, 4)
## @array3 = (4)
## Ouput: (2, 4)
#
## Example 2
##
## Input: @array1 = (4, 1)
## @array2 = (2, 4)
## @array3 = (1, 2)
## Ouput: (1, 2, 4)
#
############################################################
##
## discussion
##
############################################################
#
# First, we generate a list of unique elements per array.
# Second, we count how many times each element in those unique
# lists appear altogether. If that happens to be at least 2
# times we found an element for our result. Since sorting those
# elements can easily be done when enumerating them, let's
# just do that as well.
use strict;
use warnings;
two_out_of_three( [1, 1, 2, 4], [2, 4], [4] );
two_out_of_three( [4, 1], [2, 4], [1, 2] );
sub two_out_of_three {
my ($array1, $array2, $array3) = @_;
print "Input: \@array1 = (" . join(", ", @$array1) . ")\n";
print " \@array2 = (" . join(", ", @$array2) . ")\n";
print " \@array3 = (" . join(", ", @$array3) . ")\n";
my @uniq_array1 = my_uniq(@$array1);
my @uniq_array2 = my_uniq(@$array2);
my @uniq_array3 = my_uniq(@$array3);
my %seen;
my @output = ();
foreach my $elem ( (@uniq_array1, @uniq_array2, @uniq_array3) ) {
$seen{$elem}++;
}
foreach my $found (sort {$a <=> $b} keys %seen) {
push @output, $found if $seen{$found} >= 2;
}
print "Output: (" . join(", ", @output) . ")\n";
}
sub my_uniq {
my @array = @_;
my $found = {};
map { $found->{$_}++ } @array;
return keys %$found;
}