aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2024-08-19 06:38:06 +0000
committerNiels van Dijke <perlboy@cpan.org>2024-08-19 06:38:06 +0000
commit66dd53d55fa5f82657ca4424019b8a8e6b1c8a16 (patch)
tree188c90fac4af696d3159b8966d19cf1d1d20c8f9
parent1ec5389949cde3e7cae71c1c6298759adc5b9b1b (diff)
downloadperlweeklychallenge-club-66dd53d55fa5f82657ca4424019b8a8e6b1c8a16.tar.gz
perlweeklychallenge-club-66dd53d55fa5f82657ca4424019b8a8e6b1c8a16.tar.bz2
perlweeklychallenge-club-66dd53d55fa5f82657ca4424019b8a8e6b1c8a16.zip
w283 - Task 1 & 2
-rwxr-xr-xchallenge-283/perlboy1967/perl/ch1.pl42
-rwxr-xr-xchallenge-283/perlboy1967/perl/ch2.pl38
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-283/perlboy1967/perl/ch1.pl b/challenge-283/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..d6286a2883
--- /dev/null
+++ b/challenge-283/perlboy1967/perl/ch1.pl
@@ -0,0 +1,42 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 283
+- L<https://theweeklychallenge.org/blog/perl-weekly-challenge-283>
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Unique Number
+Submitted by: Mohammad Sajid Anwar
+
+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.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+no warnings qw(experimental::signatures);
+use common::sense;
+
+use Test2::V0 qw(-no_srand);
+
+use List::MoreUtils qw(singleton);
+
+sub uniqueNumber (@ints) {
+ my @s = singleton(@ints);
+ @s == 1 ? $s[0] : undef;
+}
+
+is(uniqueNumber(3,3,1),1,'Example 1');
+is(uniqueNumber(3,2,4,2,4),3,'Example 2');
+is(uniqueNumber(1),1,'Example 3');
+is(uniqueNumber(4,3,1,1,1,4),3,'Example 4');
+is(uniqueNumber(),undef,'Own test 1');
+is(uniqueNumber(1,1),undef,'Own test 2');
+is(uniqueNumber(1,2),undef,'Own test 3');
+
+done_testing;
diff --git a/challenge-283/perlboy1967/perl/ch2.pl b/challenge-283/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..f6e882fd98
--- /dev/null
+++ b/challenge-283/perlboy1967/perl/ch2.pl
@@ -0,0 +1,38 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 283
+- L<https://theweeklychallenge.org/blog/perl-weekly-challenge-283>
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Digit Count Value
+Submitted by: Mohammad Sajid Anwar
+
+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.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+no warnings qw(experimental::signatures);
+use common::sense;
+
+use Test2::V0 qw(-no_srand);
+
+use List::MoreUtils qw(all frequency);
+
+sub digitCountValue (@ints) {
+ my ($i,%f) = (0,frequency(@ints));
+ 0 + all { ($f{$i++} // 0) == $_ } (@ints);
+}
+
+is(digitCountValue(1,2,1,0),1,'Example 1');
+is(digitCountValue(0,3,0),0,'Example 2');
+
+done_testing;