diff options
| -rwxr-xr-x | challenge-320/peter-meszaros/perl/ch-1.pl | 72 | ||||
| -rwxr-xr-x | challenge-320/peter-meszaros/perl/ch-2.pl | 68 | ||||
| -rwxr-xr-x | challenge-320/peter-meszaros/tcl/ch-1.tcl | 71 | ||||
| -rwxr-xr-x | challenge-320/peter-meszaros/tcl/ch-2.tcl | 70 |
4 files changed, 281 insertions, 0 deletions
diff --git a/challenge-320/peter-meszaros/perl/ch-1.pl b/challenge-320/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..f1d47d195a --- /dev/null +++ b/challenge-320/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Maximum Count + +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers. + +Write a script to return the maximum between the number of positive and +negative integers. Zero is neither positive nor negative. + +=head2 Example 1 + + Input: @ints = (-3, -2, -1, 1, 2, 3) + Output: 3 + + There are 3 positive integers. + There are 3 negative integers. + The maximum between 3 and 3 is 3. + +=head2 Example 2 + + Input: @ints = (-2, -1, 0, 0, 1) + Output: 2 + + There are 1 positive integers. + There are 2 negative integers. + The maximum between 2 and 1 is 2. + +=head2 Example 3 + + Input: @ints = (1, 2, 3, 4) + Output: 4 + + There are 4 positive integers. + There are 0 negative integers. + The maximum between 4 and 0 is 4. + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[-3, -2, -1, 1, 2, 3], 3, "Example 1"], + [[-2, -1, 0, 0, 1], 2, "Example 2"], + [[ 1, 2, 3, 4], 4, "Example 3"], +]; + +sub maximum_count +{ + my $ints = shift; + + my ($pos, $neg) = (0, 0); + for my $i (@$ints) { + if ($i > 0) { + $pos++; + } elsif ($i < 0) { + $neg++; + } + } + return $pos > $neg ? $pos : $neg; +} + +for (@$cases) { + is(maximum_count($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-320/peter-meszaros/perl/ch-2.pl b/challenge-320/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..07a5f4925b --- /dev/null +++ b/challenge-320/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,68 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Sum Difference + +Submitted by: Mohammad Sajid Anwar + +You are given an array of positive integers. + +Write a script to return the absolute difference between digit sum and element +sum of the given array. + +=head2 Example 1 + + Input: @ints = (1, 23, 4, 5) + Output: 18 + + Element sum: 1 + 23 + 4 + 5 => 33 + Digit sum: 1 + 2 + 3 + 4 + 5 => 15 + Absolute difference: | 33 - 15 | => 18 + +=head2 Example 2 + + Input: @ints = (1, 2, 3, 4, 5) + Output: 0 + + Element sum: 1 + 2 + 3 + 4 + 5 => 15 + Digit sum: 1 + 2 + 3 + 4 + 5 => 15 + Absolute difference: | 15 - 15 | => 0 + +=head2 Example 3 + + Input: @ints = (1, 2, 34) + Output: 27 + + Element sum: 1 + 2 + 34 => 37 + Digit sum: 1 + 2 + 3 + 4 => 10 + Absolute difference: | 37 - 10 | => 27 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; +use List::Util qw/sum/; + +my $cases = [ + [[1, 23, 4, 5], 18, "Example 1"], + [[1, 2, 3, 4, 5], 0, "Example 2"], + [[1, 2, 34], 27, "Example 3"], +]; + +sub sum_difference +{ + my $ints = shift; + + my $esum = sum @$ints; + my $dsum = sum(split(//, join('', @$ints))); + + return abs($esum - $dsum); +} + +for (@$cases) { + is(sum_difference($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-320/peter-meszaros/tcl/ch-1.tcl b/challenge-320/peter-meszaros/tcl/ch-1.tcl new file mode 100755 index 0000000000..22d50a42a2 --- /dev/null +++ b/challenge-320/peter-meszaros/tcl/ch-1.tcl @@ -0,0 +1,71 @@ +#!/usr/bin/env tclsh +# +# Task 1: Maximum Count +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers. +# +# Write a script to return the maximum between the number of positive and +# negative integers. Zero is neither positive nor negative. +# +# Example 1 +# +# Input: @ints = (-3, -2, -1, 1, 2, 3) +# Output: 3 +# +# There are 3 positive integers. +# There are 3 negative integers. +# The maximum between 3 and 3 is 3. +# +# Example 2 +# +# Input: @ints = (-2, -1, 0, 0, 1) +# Output: 2 +# +# There are 1 positive integers. +# There are 2 negative integers. +# The maximum between 2 and 1 is 2. +# +# Example 3 +# +# Input: @ints = (1, 2, 3, 4) +# Output: 4 +# +# There are 4 positive integers. +# There are 0 negative integers. +# The maximum between 4 and 0 is 4. +# + +package require tcltest + +set cases { + {{-3 -2 -1 1 2 3} 3 {Example 1}} + {{-2 -1 0 0 1} 2 {Example 2}} + {{ 1 2 3 4} 4 {Example 3}} +} + +proc maximum_count {ints} { + set pos 0 + set neg 0 + + foreach i $ints { + if {$i > 0} { + incr pos + } elseif {$i < 0} { + incr neg + } + } + return [expr $pos > $neg ? $pos : $neg] + +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + maximum_count [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + diff --git a/challenge-320/peter-meszaros/tcl/ch-2.tcl b/challenge-320/peter-meszaros/tcl/ch-2.tcl new file mode 100755 index 0000000000..d62e19222b --- /dev/null +++ b/challenge-320/peter-meszaros/tcl/ch-2.tcl @@ -0,0 +1,70 @@ +#!/usr/bin/env tclsh +# +# Task 2: Sum Difference +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of positive integers. +# +# Write a script to return the absolute difference between digit sum and element +# sum of the given array. +# +# Example 1 +# +# Input: @ints = (1, 23, 4, 5) +# Output: 18 +# +# Element sum: 1 + 23 + 4 + 5 => 33 +# Digit sum: 1 + 2 + 3 + 4 + 5 => 15 +# Absolute difference: | 33 - 15 | => 18 +# +# Example 2 +# +# Input: @ints = (1, 2, 3, 4, 5) +# Output: 0 +# +# Element sum: 1 + 2 + 3 + 4 + 5 => 15 +# Digit sum: 1 + 2 + 3 + 4 + 5 => 15 +# Absolute difference: | 15 - 15 | => 0 +# +# Example 3 +# +# Input: @ints = (1, 2, 34) +# Output: 27 +# +# Element sum: 1 + 2 + 34 => 37 +# Digit sum: 1 + 2 + 3 + 4 => 10 +# Absolute difference: | 37 - 10 | => 27 +# + +package require tcltest + +set cases { + {{1 23 4 5} 18 {Example 1}} + {{1 2 3 4 5} 0 {Example 2}} + {{1 2 34} 27 {Example 3}} +} + +proc sum_difference {ints} { + set esum 0 + set dsum 0 + + foreach i $ints { + incr esum $i + foreach j [split $i ""] { + incr dsum $j + } + } + + return [expr abs($esum - $dsum)] +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + sum_difference [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + |
