diff options
| -rwxr-xr-x | challenge-343/peter-meszaros/perl/ch-1.pl | 79 | ||||
| -rwxr-xr-x | challenge-343/peter-meszaros/perl/ch-2.pl | 151 | ||||
| -rwxr-xr-x | challenge-343/peter-meszaros/tcl/ch-1.tcl | 77 | ||||
| -rwxr-xr-x | challenge-343/peter-meszaros/tcl/ch-2.tcl | 150 |
4 files changed, 457 insertions, 0 deletions
diff --git a/challenge-343/peter-meszaros/perl/ch-1.pl b/challenge-343/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..a0bd8d47d9 --- /dev/null +++ b/challenge-343/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,79 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Zero Friend + +Submitted by: Mohammad Sajid Anwar + +You are given a list of numbers. + +Find the number that is closest to zero and return its distance to zero. + +=head2 Example 1 + + Input: @nums = (4, 2, -1, 3, -2) + Output: 1 + + Values closest to 0: -1 and 2 (distance = 1 and 2) + +=head2 Example 2 + + Input: @nums = (-5, 5, -3, 3, -1, 1) + Output: 1 + + Values closest to 0: -1 and 1 (distance = 1) + +=head2 Example 3 + + Input: @ums = (7, -3, 0, 2, -8) + Output: 0 + + Values closest to 0: 0 (distance = 0) + Exact zero wins regardless of other close values. + +=head2 Example 4 + + Input: @nums = (-2, -5, -1, -8) + Output: 1 + + Values closest to 0: -1 and -2 (distance = 1 and 2) + +=head2 Example 5 + + Input: @nums = (-2, 2, -4, 4, -1, 1) + Output: 1 + + Values closest to 0: -1 and 1 (distance = 1) + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[ 4, 2, -1, 3, -2], 1, "Example 1"], + [[-5, 5, -3, 3, -1, 1], 1, "Example 2"], + [[ 7, -3, 0, 2, -8], 0, "Example 3"], + [[-2, -5, -1, -8], 1, "Example 4"], + [[-2, 2, -4, 4, -1, 1], 1, "Example 5"], +]; + +sub zero_friend +{ + my $nums = shift; + + my $min = undef; + for my $n (@$nums) { + my $abs = abs($n); + $min = $abs if !defined $min || $abs < $min; + } + return $min; +} + +for (@$cases) { + is(zero_friend($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-343/peter-meszaros/perl/ch-2.pl b/challenge-343/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..d6805610a9 --- /dev/null +++ b/challenge-343/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,151 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Champion Team + +Submitted by: Mohammad Sajid Anwar + +You have n teams in a tournament. A matrix grid tells you which team is +stronger between any two teams: + + If grid[i][j] == 1, then team i is stronger than team j + If grid[i][j] == 0, then team j is stronger than team i + +Find the champion team - the one with most wins, or if there is no single such +team, the strongest of the teams with most wins. (You may assume that there is +a definite answer.) + +=head2 Example 1 + + Input: @grid = ( + [0, 1, 1], + [0, 0, 1], + [0, 0, 0], + ) + Output: Team 0 + + [0, 1, 1] => Team 0 beats Team 1 and Team 2 + [0, 0, 1] => Team 1 beats Team 2 + [0, 0, 0] => Team 2 loses to all + +=head2 Example 2 + + Input: @grid = ( + [0, 1, 0, 0], + [0, 0, 0, 0], + [1, 1, 0, 0], + [1, 1, 1, 0], + ) + Output: Team 3 + + [0, 1, 0, 0] => Team 0 beats only Team 1 + [0, 0, 0, 0] => Team 1 loses to all + [1, 1, 0, 0] => Team 2 beats Team 0 and Team 1 + [1, 1, 1, 0] => Team 3 beats everyone + +=head2 Example 3 + + Input: @grid = ( + [0, 1, 0, 1], + [0, 0, 1, 1], + [1, 0, 0, 0], + [0, 0, 1, 0], + ) + Output: Team 0 + + [0, 1, 0, 1] => Team 0 beats teams 1 and 3 + [0, 0, 1, 1] => Team 1 beats teams 2 and 3 + [1, 0, 0, 0] => Team 2 beats team 0 + [0, 0, 1, 0] => Team 3 beats team 2 + + Of the teams with 2 wins, Team 0 beats team 1. + +=head2 Example 4 + + Input: @grid = ( + [0, 1, 1], + [0, 0, 0], + [0, 1, 0], + ) + Output: Team 0 + + [0, 1, 1] => Team 0 beats Team 1 and Team 2 + [0, 0, 0] => Team 1 loses to Team 2 + [0, 1, 0] => Team 2 beats Team 1 but loses to Team 0 + + +=head2 Example 5 + + Input: @grid = ( + [0, 0, 0, 0, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 1, 1], + [1, 1, 0, 0, 0], + [1, 1, 0, 1, 0], + ) + Output: Team 2 + + [0, 0, 0, 0, 0] => Team 0 loses to all + [1, 0, 0, 0, 0] => Team 1 beats only Team 0 + [1, 1, 0, 1, 1] => Team 2 beats everyone except self + [1, 1, 0, 0, 0] => Team 3 loses to Team 2 + [1, 1, 0, 1, 0] => Team 4 loses to Team 2 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[[0, 1, 1], + [0, 0, 1], + [0, 0, 0], + ], 0, "Example 1"], + [[[0, 1, 0, 0], + [0, 0, 0, 0], + [1, 1, 0, 0], + [1, 1, 1, 0], + ], 3, "Example 2"], + [[[0, 1, 0, 1], + [0, 0, 1, 1], + [1, 0, 0, 0], + [0, 0, 1, 0], + ], 0, "Example 3"], + [[[0, 1, 1], + [0, 0, 0], + [0, 1, 0], + ], 0, "Example 4"], + [[[0, 0, 0, 0, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 1, 1], + [1, 1, 0, 0, 0], + [1, 1, 0, 1, 0], + ], 2, "Example 5"], +]; + +sub champion_team +{ + my $grid = shift; + + my $champ; + my $wins = 0; + my $n = @$grid; + for (my $i=0; $i < $n; $i++) { + my $w = 0; + $w++ for grep { $_ == 1 } $grid->[$i]->@*; + if (!defined($champ) || $w > $wins) { + $champ = $i; + $wins = $w; + } + } + + return $champ; +} + +for (@$cases) { + is(champion_team($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-343/peter-meszaros/tcl/ch-1.tcl b/challenge-343/peter-meszaros/tcl/ch-1.tcl new file mode 100755 index 0000000000..e481115a9d --- /dev/null +++ b/challenge-343/peter-meszaros/tcl/ch-1.tcl @@ -0,0 +1,77 @@ +#!/usr/bin/env tclsh +# +# Task 1: Zero Friend +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given a list of numbers. +# +# Find the number that is closest to zero and return its distance to zero. +# +# Example 1 +# +# Input: @nums = (4, 2, -1, 3, -2) +# Output: 1 +# +# Values closest to 0: -1 and 2 (distance = 1 and 2) +# +# Example 2 +# +# Input: @nums = (-5, 5, -3, 3, -1, 1) +# Output: 1 +# +# Values closest to 0: -1 and 1 (distance = 1) +# +# Example 3 +# +# Input: @ums = (7, -3, 0, 2, -8) +# Output: 0 +# +# Values closest to 0: 0 (distance = 0) +# Exact zero wins regardless of other close values. +# +# Example 4 +# +# Input: @nums = (-2, -5, -1, -8) +# Output: 1 +# +# Values closest to 0: -1 and -2 (distance = 1 and 2) +# +# Example 5 +# +# Input: @nums = (-2, 2, -4, 4, -1, 1) +# Output: 1 +# +# Values closest to 0: -1 and 1 (distance = 1) +# + +package require tcltest + +set cases { + {{ 4 2 -1 3 -2} 1 "Example 1"} + {{-5 5 -3 3 -1 1} 1 "Example 2"} + {{ 7 -3 0 2 -8} 0 "Example 3"} + {{-2 -5 -1 -8} 1 "Example 4"} + {{-2 2 -4 4 -1 1} 1 "Example 5"} +} + +proc zero_friend {nums} { + set min {} + foreach n $nums { + set abs [expr abs($n)] + if {$min eq "" || $abs < $min} { + set min $abs + } + } + return $min +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + zero_friend [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + diff --git a/challenge-343/peter-meszaros/tcl/ch-2.tcl b/challenge-343/peter-meszaros/tcl/ch-2.tcl new file mode 100755 index 0000000000..2245f47697 --- /dev/null +++ b/challenge-343/peter-meszaros/tcl/ch-2.tcl @@ -0,0 +1,150 @@ +#!/usr/bin/env tclsh +# +# Task 2: Champion Team +# +# Submitted by: Mohammad Sajid Anwar +# +# You have n teams in a tournament. A matrix grid tells you which team is +# stronger between any two teams: +# +# If grid[i][j] == 1, then team i is stronger than team j +# If grid[i][j] == 0, then team j is stronger than team i +# +# Find the champion team - the one with most wins, or if there is no single such +# team, the strongest of the teams with most wins. (You may assume that there is +# a definite answer.) +# +# Example 1 +# +# Input: @grid = ( +# [0, 1, 1], +# [0, 0, 1], +# [0, 0, 0], +# ) +# Output: Team 0 +# +# [0, 1, 1] => Team 0 beats Team 1 and Team 2 +# [0, 0, 1] => Team 1 beats Team 2 +# [0, 0, 0] => Team 2 loses to all +# +# Example 2 +# +# Input: @grid = ( +# [0, 1, 0, 0], +# [0, 0, 0, 0], +# [1, 1, 0, 0], +# [1, 1, 1, 0], +# ) +# Output: Team 3 +# +# [0, 1, 0, 0] => Team 0 beats only Team 1 +# [0, 0, 0, 0] => Team 1 loses to all +# [1, 1, 0, 0] => Team 2 beats Team 0 and Team 1 +# [1, 1, 1, 0] => Team 3 beats everyone +# +# Example 3 +# +# Input: @grid = ( +# [0, 1, 0, 1], +# [0, 0, 1, 1], +# [1, 0, 0, 0], +# [0, 0, 1, 0], +# ) +# Output: Team 0 +# +# [0, 1, 0, 1] => Team 0 beats teams 1 and 3 +# [0, 0, 1, 1] => Team 1 beats teams 2 and 3 +# [1, 0, 0, 0] => Team 2 beats team 0 +# [0, 0, 1, 0] => Team 3 beats team 2 +# +# Of the teams with 2 wins, Team 0 beats team 1. +# +# Example 4 +# +# Input: @grid = ( +# [0, 1, 1], +# [0, 0, 0], +# [0, 1, 0], +# ) +# Output: Team 0 +# +# [0, 1, 1] => Team 0 beats Team 1 and Team 2 +# [0, 0, 0] => Team 1 loses to Team 2 +# [0, 1, 0] => Team 2 beats Team 1 but loses to Team 0 +# +# +# Example 5 +# +# Input: @grid = ( +# [0, 0, 0, 0, 0], +# [1, 0, 0, 0, 0], +# [1, 1, 0, 1, 1], +# [1, 1, 0, 0, 0], +# [1, 1, 0, 1, 0], +# ) +# Output: Team 2 +# +# [0, 0, 0, 0, 0] => Team 0 loses to all +# [1, 0, 0, 0, 0] => Team 1 beats only Team 0 +# [1, 1, 0, 1, 1] => Team 2 beats everyone except self +# [1, 1, 0, 0, 0] => Team 3 loses to Team 2 +# [1, 1, 0, 1, 0] => Team 4 loses to Team 2 +# + +package require tcltest + +set cases { + {{{0 1 1} + {0 0 1} + {0 0 0} + } 0 "Example 1"} + {{{0 1 0 0} + {0 0 0 0} + {1 1 0 0} + {1 1 1 0} + } 3 "Example 2"} + {{{0 1 0 1} + {0 0 1 1} + {1 0 0 0} + {0 0 1 0} + } 0 "Example 3"} + {{{0 1 1} + {0 0 0} + {0 1 0} + } 0 "Example 4"} + {{{0 0 0 0 0} + {1 0 0 0 0} + {1 1 0 1 1} + {1 1 0 0 0} + {1 1 0 1 0} + } 2 "Example 5"} +} + +proc champion_team {grid} { + set n [llength $grid] + set champ -1 + set wins -1 + for {set i 0} {$i < $n} {incr i} { + set w 0 + foreach r [lindex $grid $i] { + if {$r == 1} { + incr w + } + } + if {$w > $wins} { + set wins $w + set champ $i + } + } + return $champ +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + champion_team [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + |
