/
ch-1.pl
62 lines (50 loc) · 1.06 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
#!/usr/bin/env perl
=begin commnt
-----------------------------------------
AUTHOR: Robert DiCicco
DATE : 2023-07-31
Challenge 228 Task 1 Unique Sum ( Perl )
-----------------------------------------
=cut
use v5.38;
my %hash;
my @ints = ([2, 1, 3, 2],[1, 1, 1, 1],[2, 1, 3, 4]);
sub CreateHashValues($h) {
%hash = ();
my $ln = scalar(@$h);
my $cnt = 0;
while ($cnt < scalar @$h) {
if(! exists $hash{$h->[$cnt]}) {
$hash{$h->[$cnt]} = 1;
} else {
$hash{$h->[$cnt]}++;
}
$cnt++;
}
}
sub FindUniqueSum() {
my $flag = 0;
my $sum = 0;
for my $key (keys %hash) {
$sum += $key if ($hash{$key} == 1);
}
say "Output: ",$sum,"\n";
%hash = ();
}
for my $x (@ints) {
say "Input: \@int = [@$x]";
CreateHashValues(\@$x);
FindUniqueSum;
}
=begin commnt
-----------------------------------------
SAMPLE OUTPUT
perl .\UniqueSum.pl
Input: @int = [2 1 3 2]
Output: 4
Input: @int = [1 1 1 1]
Output: 0
Input: @int = [2 1 3 4]
Output: 10
-----------------------------------------
=cut