From 66dd53d55fa5f82657ca4424019b8a8e6b1c8a16 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 19 Aug 2024 06:38:06 +0000 Subject: w283 - Task 1 & 2 --- challenge-283/perlboy1967/perl/ch1.pl | 42 +++++++++++++++++++++++++++++++++++ challenge-283/perlboy1967/perl/ch2.pl | 38 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100755 challenge-283/perlboy1967/perl/ch1.pl create mode 100755 challenge-283/perlboy1967/perl/ch2.pl 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 + +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 + +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; -- cgit