diff options
| author | andrezgz <andrezgz@gmail.com> | 2024-08-31 17:01:50 -0300 |
|---|---|---|
| committer | andrezgz <andrezgz@gmail.com> | 2024-08-31 17:01:50 -0300 |
| commit | 5d688d7e924aebd7310a25190732992ce6242882 (patch) | |
| tree | 88aef856ceaab9e94715401fab78c9536c21c1ed | |
| parent | 1d9c15f31a1dd6ff6f81bcd1f30d0f9d90c9b607 (diff) | |
| download | perlweeklychallenge-club-5d688d7e924aebd7310a25190732992ce6242882.tar.gz perlweeklychallenge-club-5d688d7e924aebd7310a25190732992ce6242882.tar.bz2 perlweeklychallenge-club-5d688d7e924aebd7310a25190732992ce6242882.zip | |
challenge-283 andrezgz solution
| -rwxr-xr-x | challenge-283/andrezgz/perl/ch-1.pl | 36 | ||||
| -rwxr-xr-x | challenge-283/andrezgz/perl/ch-2.pl | 46 |
2 files changed, 82 insertions, 0 deletions
diff --git a/challenge-283/andrezgz/perl/ch-1.pl b/challenge-283/andrezgz/perl/ch-1.pl new file mode 100755 index 0000000000..9576d5f48e --- /dev/null +++ b/challenge-283/andrezgz/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +# https://theweeklychallenge.org/blog/perl-weekly-challenge-283/ +# TASK #1 > Unique Number +# +# You are given an array of integers, @ints, where every elements appears more than once except one element. +# +# Write a script to find the one element that appears exactly one time. +# +# Example 1 +# Input: @ints = (3, 3, 1) +# Output: 1 +# Example 2 +# Input: @ints = (3, 2, 4, 2, 4) +# Output: 3 +# Example 3 +# Input: @ints = (1) +# Output: 1 +# Example 4 +# Input: @ints = (4, 3, 1, 1, 1, 4) +# Output: 3 + +use strict; +use warnings; +use feature 'say'; + +my @ints = @ARGV; +my %seen; +$seen{$_}++ for @ints; + +say grep { $seen{$_} == 1 } @ints; + +__END__ + +$ ./ch-1.pl 3 1 1 3 5 2 4 2 4 +5 diff --git a/challenge-283/andrezgz/perl/ch-2.pl b/challenge-283/andrezgz/perl/ch-2.pl new file mode 100755 index 0000000000..17b744d3e9 --- /dev/null +++ b/challenge-283/andrezgz/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/usr/bin/perl + +# https://theweeklychallenge.org/blog/perl-weekly-challenge-283/ +# Task #2 > Digit Count Value +# +# You are given an array of positive integers, @ints. +# Write a script to return true if for every index i in the range 0 <= i < size of array, +# the digit i occurs exactly the $ints[$i] times in the given array otherwise return false. +# +# Example 1 +# Input: @ints = (1, 2, 1, 0) +# Ouput: true +# +# $ints[0] = 1, the digit 0 occurs exactly 1 time. +# $ints[1] = 2, the digit 1 occurs exactly 2 times. +# $ints[2] = 1, the digit 2 occurs exactly 1 time. +# $ints[3] = 0, the digit 3 occurs 0 time. +# +# Example 2 +# Input: @ints = (0, 3, 0) +# Ouput: false +# +# $ints[0] = 0, the digit 0 occurs 2 times rather than 0 time. +# $ints[1] = 3, the digit 1 occurs 0 time rather than 3 times. +# $ints[2] = 0, the digit 2 occurs exactly 0 time. + +use strict; +use warnings; +use feature 'say'; + +my @ints = @ARGV; +say digit_count_match(@ints); +exit 0; + +sub digit_count_match { + my @ints = @_; + + my %seen; + $seen{$_}++ for @ints; + + for my $i (0..$#ints) { + next if $ints[$i] == ($seen{$i} // 0); + return 'false'; + } + return 'true'; +} |
