diff options
| author | pme <hauptadler@gmail.com> | 2025-08-30 10:37:48 +0200 |
|---|---|---|
| committer | pme <hauptadler@gmail.com> | 2025-08-30 10:37:48 +0200 |
| commit | 5b67dab43c2572037562d4e0358cf8ff32a6cb71 (patch) | |
| tree | 26a4c84b188eabfb7959ca3bd9c803477a70e757 | |
| parent | abe00897569078e604adaecef75941fa70176dd1 (diff) | |
| download | perlweeklychallenge-club-5b67dab43c2572037562d4e0358cf8ff32a6cb71.tar.gz perlweeklychallenge-club-5b67dab43c2572037562d4e0358cf8ff32a6cb71.tar.bz2 perlweeklychallenge-club-5b67dab43c2572037562d4e0358cf8ff32a6cb71.zip | |
challenge-336
| -rwxr-xr-x | challenge-336/peter-meszaros/perl/ch-1.pl | 92 | ||||
| -rwxr-xr-x | challenge-336/peter-meszaros/perl/ch-2.pl | 128 | ||||
| -rwxr-xr-x | challenge-336/peter-meszaros/tcl/ch-1.tcl | 95 | ||||
| -rwxr-xr-x | challenge-336/peter-meszaros/tcl/ch-2.tcl | 124 |
4 files changed, 439 insertions, 0 deletions
diff --git a/challenge-336/peter-meszaros/perl/ch-1.pl b/challenge-336/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..1442513de5 --- /dev/null +++ b/challenge-336/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,92 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Equal Group + +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers. + +Write a script to return true if the given array can be divided into one or +more groups: each group must be of the same size as the others, with at least +two members, and with all members having the same value. + +=head2 Example 1 + + Input: @ints = (1,1,2,2,2,2) + Output: true + + Groups: (1,1), (2,2), (2,2) + +=head2 Example 2 + + Input: @ints = (1,1,1,2,2,2,3,3) + Output: false + + Groups: (1,1,1), (2,2,2), (3,3) + +=head2 Example 3 + + Input: @ints = (5,5,5,5,5,5,7,7,7,7,7,7) + Output: true + + Groups: (5,5,5,5,5,5), (7,7,7,7,7,7) + +=head2 Example 4 + + Input: @ints = (1,2,3,4) + Output: false + +=head2 Example 5 + + Input: @ints = (8,8,9,9,10,10,11,11) + Output: true + + Groups: (8,8), (9,9), (10,10), (11,11) + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[1,1,2,2,2,2], 1, "Example 1"], + [[1,1,1,2,2,2,3,3], 0, "Example 2"], + [[5,5,5,5,5,5,7,7,7,7,7,7], 1, "Example 3"], + [[1,2,3,4], 0, "Example 4"], + [[8,8,9,9,10,10,11,11], 1, "Example 5"], +]; + +sub equal_group +{ + my ($ints) = @_; + + my %count; + $count{$_}++ for @$ints; + my @values = values %count; + my $min = $values[0]; + for my $v (@values[1 .. $#values]) { + $min = $v if $v < $min; + } + return 0 if $min < 2; + + for my $size (2 .. $min) { + my $ok = 1; + for my $v (@values) { + if ($v % $size != 0) { + $ok = 0; + last; + } + } + return 1 if $ok; + } + return 0; +} + +for (@$cases) { + is(equal_group($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-336/peter-meszaros/perl/ch-2.pl b/challenge-336/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..1187ace468 --- /dev/null +++ b/challenge-336/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,128 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Final Score + +Submitted by: Mohammad Sajid Anwar + +You are given an array of scores by a team. + +Write a script to find the total score of the given team. The score can be any +integer, +, C or D. The + adds the sum of previous two scores. The score C +invalidates the previous score. The score D will double the previous score. + +=head2 Example 1 + + Input: @scores = ("5","2","C","D","+") + Output: 30 + + Round 1: 5 + Round 2: 5 + 2 + Round 3: 5 (invalidate the previous score 2) + Round 4: 5 + 10 (double the previous score 5) + Round 5: 5 + 10 + 15 (sum of previous two scores) + + Total Scores: 30 + +=head2 Example 2 + + Input: @scores = ("5","-2","4","C","D","9","+","+") + Output: 27 + + Round 1: 5 + Round 2: 5 + (-2) + Round 3: 5 + (-2) + 4 + Round 4: 5 + (-2) (invalidate the previous score 4) + Round 5: 5 + (-2) + (-4) (double the previous score -2) + Round 6: 5 + (-2) + (-4) + 9 + Round 7: 5 + (-2) + (-4) + 9 + 5 (sum of previous two scores) + Round 8: 5 + (-2) + (-4) + 9 + 5 + 14 (sum of previous two scores) + + Total Scores: 27 + +=head2 Example 3 + + Input: @scores = ("7","D","D","C","+","3") + Output: 45 + + Round 1: 7 + Round 2: 7 + 14 (double the previous score 7) + Round 3: 7 + 14 + 28 (double the previous score 14) + Round 4: 7 + 14 (invalidate the previous score 28) + Round 5: 7 + 14 + 21 (sum of previous two scores) + Round 6: 7 + 14 + 21 + 3 + + Total Scores: 45 + +=head2 Example 4 + + Input: @scores = ("-5","-10","+","D","C","+") + Output: -55 + + Round 1: (-5) + Round 2: (-5) + (-10) + Round 3: (-5) + (-10) + (-15) (sum of previous two scores) + Round 4: (-5) + (-10) + (-15) + (-30) (double the previous score -15) + Round 5: (-5) + (-10) + (-15) (invalidate the previous score -30) + Round 6: (-5) + (-10) + (-15) + (-25) (sum of previous two scores) + + Total Scores: -55 + +=head2 Example 5 + + Input: @scores = ("3","6","+","D","C","8","+","D","-2","C","+") + Output: 128 + + Round 1: 3 + Round 2: 3 + 6 + Round 3: 3 + 6 + 9 (sum of previous two scores) + Round 4: 3 + 6 + 9 + 18 (double the previous score 9) + Round 5: 3 + 6 + 9 (invalidate the previous score 18) + Round 6: 3 + 6 + 9 + 8 + Round 7: 3 + 6 + 9 + 8 + 17 (sum of previous two scores) + Round 8: 3 + 6 + 9 + 8 + 17 + 34 (double the previous score 17) + Round 9: 3 + 6 + 9 + 8 + 17 + 34 + (-2) + Round 10: 3 + 6 + 9 + 8 + 17 + 34 (invalidate the previous score -2) + Round 11: 3 + 6 + 9 + 8 + 17 + 34 + 51 (sum of previous two scores) + + Total Scores: 128 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [["5", "2", "C", "D", "+"], 30, "Example 1"], + [["5", "-2", "4", "C", "D", "9", "+", "+"], 27, "Example 2"], + [["7", "D", "D", "C", "+", "3"], 45, "Example 3"], + [["-5","-10", "+", "D", "C", "+"], -55, "Example 4"], + [["3", "6", "+", "D", "C", "8", "+", "D", "-2", "C", "+"], 128, "Example 5"], +]; + +sub final_score +{ + my ($scores) = @_; + + my @stack; + for my $s (@$scores) { + if ($s eq 'C') { + pop @stack; + } elsif ($s eq 'D') { + push @stack, 2 * $stack[-1]; + } elsif ($s eq '+') { + push @stack, $stack[-1] + $stack[-2]; + } else { + push @stack, $s; + } + } + return eval join '+', @stack; +} + +for (@$cases) { + is(final_score($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-336/peter-meszaros/tcl/ch-1.tcl b/challenge-336/peter-meszaros/tcl/ch-1.tcl new file mode 100755 index 0000000000..574a69045d --- /dev/null +++ b/challenge-336/peter-meszaros/tcl/ch-1.tcl @@ -0,0 +1,95 @@ +#!/usr/bin/env tclsh +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers. +# +# Write a script to return true if the given array can be divided into one or +# more groups: each group must be of the same size as the others, with at least +# two members, and with all members having the same value. +# +# Example 1 +# +# Input: @ints = (1,1,2,2,2,2) +# Output: true +# +# Groups: (1,1), (2,2), (2,2) +# +# Example 2 +# +# Input: @ints = (1,1,1,2,2,2,3,3) +# Output: false +# +# Groups: (1,1,1), (2,2,2), (3,3) +# +# Example 3 +# +# Input: @ints = (5,5,5,5,5,5,7,7,7,7,7,7) +# Output: true +# +# Groups: (5,5,5,5,5,5), (7,7,7,7,7,7) +# +# Example 4 +# +# Input: @ints = (1,2,3,4) +# Output: false +# +# Example 5 +# +# Input: @ints = (8,8,9,9,10,10,11,11) +# Output: true +# +# Groups: (8,8), (9,9), (10,10), (11,11) +# + +package require tcltest + +set cases { + {{1 1 2 2 2 2} 1 "Example 1"} + {{1 1 1 2 2 2 3 3} 0 "Example 2"} + {{5 5 5 5 5 5 7 7 7 7 7 7} 1 "Example 3"} + {{1 2 3 4} 0 "Example 4"} + {{8 8 9 9 10 10 11 11} 1 "Example 5"} +} + +proc equal_group {ints} { + array set count {} + foreach i $ints { + incr count($i) + } + foreach key [lsort -integer [array names count]] { + lappend values $count($key) + } + set min [lindex $values 0] + foreach v [lrange $values 1 end] { + if {$v < $min} { + set min $v + } + } + if {$min < 2} { + return 0 + } + for {set size 2} {$size <= $min} {incr size} { + set ok 1 + foreach v [lrange $values 1 end] { + if {$v % $size != 0} { + set ok 0 + break + } + } + if {$ok} { + return 1 + } + } + return 0 +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + equal_group [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + diff --git a/challenge-336/peter-meszaros/tcl/ch-2.tcl b/challenge-336/peter-meszaros/tcl/ch-2.tcl new file mode 100755 index 0000000000..35112a0dda --- /dev/null +++ b/challenge-336/peter-meszaros/tcl/ch-2.tcl @@ -0,0 +1,124 @@ +#!/usr/bin/env tclsh +# +# Task 2: Final Score +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of scores by a team. +# +# Write a script to find the total score of the given team. The score can be any +# integer, +, C or D. The + adds the sum of previous two scores. The score C +# invalidates the previous score. The score D will double the previous score. +# +# Example 1 +# +# Input: @scores = ("5","2","C","D","+") +# Output: 30 +# +# Round 1: 5 +# Round 2: 5 + 2 +# Round 3: 5 (invalidate the previous score 2) +# Round 4: 5 + 10 (double the previous score 5) +# Round 5: 5 + 10 + 15 (sum of previous two scores) +# +# Total Scores: 30 +# +# Example 2 +# +# Input: @scores = ("5","-2","4","C","D","9","+","+") +# Output: 27 +# +# Round 1: 5 +# Round 2: 5 + (-2) +# Round 3: 5 + (-2) + 4 +# Round 4: 5 + (-2) (invalidate the previous score 4) +# Round 5: 5 + (-2) + (-4) (double the previous score -2) +# Round 6: 5 + (-2) + (-4) + 9 +# Round 7: 5 + (-2) + (-4) + 9 + 5 (sum of previous two scores) +# Round 8: 5 + (-2) + (-4) + 9 + 5 + 14 (sum of previous two scores) +# +# Total Scores: 27 +# +# Example 3 +# +# Input: @scores = ("7","D","D","C","+","3") +# Output: 45 +# +# Round 1: 7 +# Round 2: 7 + 14 (double the previous score 7) +# Round 3: 7 + 14 + 28 (double the previous score 14) +# Round 4: 7 + 14 (invalidate the previous score 28) +# Round 5: 7 + 14 + 21 (sum of previous two scores) +# Round 6: 7 + 14 + 21 + 3 +# +# Total Scores: 45 +# +# Example 4 +# +# Input: @scores = ("-5","-10","+","D","C","+") +# Output: -55 +# +# Round 1: (-5) +# Round 2: (-5) + (-10) +# Round 3: (-5) + (-10) + (-15) (sum of previous two scores) +# Round 4: (-5) + (-10) + (-15) + (-30) (double the previous score -15) +# Round 5: (-5) + (-10) + (-15) (invalidate the previous score -30) +# Round 6: (-5) + (-10) + (-15) + (-25) (sum of previous two scores) +# +# Total Scores: -55 +# +# Example 5 +# +# Input: @scores = ("3","6","+","D","C","8","+","D","-2","C","+") +# Output: 128 +# +# Round 1: 3 +# Round 2: 3 + 6 +# Round 3: 3 + 6 + 9 (sum of previous two scores) +# Round 4: 3 + 6 + 9 + 18 (double the previous score 9) +# Round 5: 3 + 6 + 9 (invalidate the previous score 18) +# Round 6: 3 + 6 + 9 + 8 +# Round 7: 3 + 6 + 9 + 8 + 17 (sum of previous two scores) +# Round 8: 3 + 6 + 9 + 8 + 17 + 34 (double the previous score 17) +# Round 9: 3 + 6 + 9 + 8 + 17 + 34 + (-2) +# Round 10: 3 + 6 + 9 + 8 + 17 + 34 (invalidate the previous score -2) +# Round 11: 3 + 6 + 9 + 8 + 17 + 34 + 51 (sum of previous two scores) +# +# Total Scores: 128 +# + +package require tcltest + +set cases { + {{"5" "2" "C" "D" "+"} 30 "Example 1"} + {{"5" "-2" "4" "C" "D" "9" "+" "+"} 27 "Example 2"} + {{"7" "D" "D" "C" "+" "3"} 45 "Example 3"} + {{"-5" "-10" "+" "D" "C" "+"} -55 "Example 4"} + {{"3" "6" "+" "D" "C" "8" "+" "D" "-2" "C" "+"} 128 "Example 5"} +} + +proc final_score {scores} { + set stack {} + foreach s $scores { + if {$s eq "C"} { + set stack [lrange $stack 0 end-1] + } elseif {$s eq "D"} { + lappend stack [expr {2 * [lindex $stack end]}] + } elseif {$s eq "+"} { + lappend stack [expr {[lindex $stack end] + [lindex $stack end-1]}] + } else { + lappend stack [expr $s + 0] + } + } + return [expr [join $stack +]] +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + final_score [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + |
