diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-08-01 18:33:16 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-01 18:33:16 +0100 |
| commit | a5c8c3696920a816b2f4779f0b30fe2558f35551 (patch) | |
| tree | 7ed3b9191a3488192a5410ebebae2ea95b3a876b | |
| parent | 9959149ddd64c90c8881472e5024dc5336088745 (diff) | |
| parent | 1d872fa52443d78a60754e94b51debf616bf1f05 (diff) | |
| download | perlweeklychallenge-club-a5c8c3696920a816b2f4779f0b30fe2558f35551.tar.gz perlweeklychallenge-club-a5c8c3696920a816b2f4779f0b30fe2558f35551.tar.bz2 perlweeklychallenge-club-a5c8c3696920a816b2f4779f0b30fe2558f35551.zip | |
Merge pull request #8489 from spadacciniweb/PWC-228
PWC 228 - Perl and Python
| -rw-r--r-- | challenge-228/spadacciniweb/perl/ch-1.pl | 43 | ||||
| -rw-r--r-- | challenge-228/spadacciniweb/python/ch-1.py | 38 |
2 files changed, 81 insertions, 0 deletions
diff --git a/challenge-228/spadacciniweb/perl/ch-1.pl b/challenge-228/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..e3b5dae77f --- /dev/null +++ b/challenge-228/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# Task 1: Unique Sum +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers. +# Write a script to find out the sum of unique elements in the given array. +# +# Example 1 +# Input: @int = (2, 1, 3, 2) +# Output: 4 +# +# In the given array we have 2 unique elements (1, 3). +# +# Example 2 +# Input: @int = (1, 1, 1, 1) +# Output: 0 +# +# In the given array no unique element found. +# +# Example 3 +# Input: @int = (2, 1, 3, 4) +# Output: 10 +# +# In the given array every element is unique. + +use strict; +use warnings; +use List::Util qw/ sum /; + +my @input = @ARGV; +die "Input error\n" + if scalar @input < 1 + or + (scalar map { $_ =~ /[\-\d]/ ? () : 1 } + @input) != 0; + +my %int; +foreach my $i (@input) { + $int{$i}++; +} + +printf "Output: %d\n", sum map { $int{$_} == 1 ? $_ : () } keys %int; diff --git a/challenge-228/spadacciniweb/python/ch-1.py b/challenge-228/spadacciniweb/python/ch-1.py new file mode 100644 index 0000000000..81c0fc8200 --- /dev/null +++ b/challenge-228/spadacciniweb/python/ch-1.py @@ -0,0 +1,38 @@ +# Task 1: Unique Sum +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers. +# Write a script to find out the sum of unique elements in the given array. +# +# Example 1 +# Input: @int = (2, 1, 3, 2) +# Output: 4 +# +# In the given array we have 2 unique elements (1, 3). +# +# Example 2 +# Input: @int = (1, 1, 1, 1) +# Output: 0 +# +# In the given array no unique element found. +# +# Example 3 +# Input: @int = (2, 1, 3, 4) +# Output: 10 +# +# In the given array every element is unique. + +import re +import sys + +if __name__ == "__main__": + input = sys.argv[1:] + if (len(input) < 1 + or + len(list(filter(lambda x: re.search(r'[^\-\d]', x), input))) != 0): + sys.exit("Input error\n") + + ints = list(map(int, input)) + + freq = dict((i, ints.count(i)) for i in set(ints)) + print( sum( list(filter(lambda x: freq.get(x) == 1, freq.keys())) ) ) |
