diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-19 10:34:59 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-19 10:34:59 +0100 |
| commit | 093475dc772277b1c2681568982bec7e4bed8cd0 (patch) | |
| tree | 7022aeaad39d5b774dea99d92f3afdbd189accc0 | |
| parent | 8fd64c80700bee629ef1882f011dffb0d69566c6 (diff) | |
| parent | 1b73b14a6b2acfcb663c98c67474bd39b4a2449d (diff) | |
| download | perlweeklychallenge-club-093475dc772277b1c2681568982bec7e4bed8cd0.tar.gz perlweeklychallenge-club-093475dc772277b1c2681568982bec7e4bed8cd0.tar.bz2 perlweeklychallenge-club-093475dc772277b1c2681568982bec7e4bed8cd0.zip | |
Merge pull request #10650 from pme/challenge-283
challenge-283
| -rwxr-xr-x | challenge-283/peter-meszaros/perl/ch-1.pl | 65 | ||||
| -rwxr-xr-x | challenge-283/peter-meszaros/perl/ch-2.pl | 62 | ||||
| -rwxr-xr-x | challenge-283/peter-meszaros/tcl/ch-1.tcl | 62 | ||||
| -rwxr-xr-x | challenge-283/peter-meszaros/tcl/ch-2.tcl | 65 |
4 files changed, 254 insertions, 0 deletions
diff --git a/challenge-283/peter-meszaros/perl/ch-1.pl b/challenge-283/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..c11860ce34 --- /dev/null +++ b/challenge-283/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl +# +=head1 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. + +=head2 Example 1 + + Input: @ints = (3, 3, 1) + Output: 1 + +=head2 Example 2 + + Input: @ints = (3, 2, 4, 2, 4) + Output: 3 + +=head2 Example 3 + + Input: @ints = (1) + Output: 1 + +=head2 Example 4 + + Input: @ints = (4, 3, 1, 1, 1, 4) + Output: 3 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[3, 3, 1], 1, 'Example 1'], + [[3, 2, 4, 2, 4], 3, 'Example 2'], + [[1], 1, 'Example 3'], + [[4, 3, 1, 1, 1, 4], 3, 'Example 4'], + [[3, 2, 4, 3, 2, 4], undef, 'Example 5'], +]; + +sub unique_number +{ + my $l = shift; + + my %h; + $h{$_}++ for @$l; + + for my $k (keys %h) { + return $k if $h{$k} == 1; + } + return; +} + +for (@$cases) { + is(unique_number($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-283/peter-meszaros/perl/ch-2.pl b/challenge-283/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..2867457ce0 --- /dev/null +++ b/challenge-283/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl +# +=head1 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. + +=head2 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. + +=head2 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. + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[1, 2, 1, 0], 1, 'Example 1'], + [[0, 3, 0], 0, 'Example 2'], +]; + +sub digit_count_value +{ + my $ints = shift; + + my %h; + $h{$_}++ for @$ints; + + for my $i (0 .. $#$ints) { + return 0 if $ints->[$i] != ($h{$i} // 0); + } + return 1; +} + +for (@$cases) { + is(digit_count_value($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-283/peter-meszaros/tcl/ch-1.tcl b/challenge-283/peter-meszaros/tcl/ch-1.tcl new file mode 100755 index 0000000000..cdaeed11b9 --- /dev/null +++ b/challenge-283/peter-meszaros/tcl/ch-1.tcl @@ -0,0 +1,62 @@ +#!/usr/bin/env tclsh +# +# 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. +# +# 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 +# + +package require tcltest + +set cases { + {{3 3 1} 1 "Example 1"} + {{3 2 4 2 4} 3 "Example 2"} + {{1} 1 "Example 3"} + {{4 3 1 1 1 4} 3 "Example 4"} + {{3 2 4 3 2 4} null "Example 5"} +} + +proc unique_number {ints} { + foreach i $ints { + dict incr d $i + } + foreach k [dict keys $d] { + if {[dict get $d $k] == 1} { + return $k + } + } + return null +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + unique_number [lindex $case 0] + } [lindex $case 1] +} + +exit 0 diff --git a/challenge-283/peter-meszaros/tcl/ch-2.tcl b/challenge-283/peter-meszaros/tcl/ch-2.tcl new file mode 100755 index 0000000000..ebc06d4897 --- /dev/null +++ b/challenge-283/peter-meszaros/tcl/ch-2.tcl @@ -0,0 +1,65 @@ +#!/usr/bin/env tclsh +# +# 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. +# +# 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. +# + +package require tcltest + +set cases { + {{1 2 1 0} 1 "Example 1"} + {{0 3 0} 0 "Example 2"} +} + +proc digit_count_value {ints} { + foreach i $ints { + dict incr d $i + } + set l [llength $ints] + for {set i 0} {$i < $l} {incr i} { + if {[dict keys $d $i] == $i} { + set v [dict get $d $i] + } else { + set v 0 + } + if {[lindex $ints $i] != $v} { + return 0 + } + } + return 1 +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + digit_count_value [lindex $case 0] + } [lindex $case 1] +} + +exit 0 |
