diff options
| -rwxr-xr-x | challenge-337/peter-meszaros/perl/ch-1.pl | 77 | ||||
| -rwxr-xr-x | challenge-337/peter-meszaros/perl/ch-2.pl | 283 | ||||
| -rwxr-xr-x | challenge-337/peter-meszaros/tcl/ch-1.tcl | 75 | ||||
| -rwxr-xr-x | challenge-337/peter-meszaros/tcl/ch-2.tcl | 285 |
4 files changed, 720 insertions, 0 deletions
diff --git a/challenge-337/peter-meszaros/perl/ch-1.pl b/challenge-337/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..c4174fc073 --- /dev/null +++ b/challenge-337/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,77 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Smaller Than Current + +Submitted by: Mohammad Sajid Anwar + +You are given an array of numbers, @num1. + +Write a script to return an array, @num2, where $num2[i] is the count of all +numbers less than or equal to $num1[i]. + +=head2 Example 1 + + Input: @num1 = (6, 5, 4, 8) + Output: (2, 1, 0, 3) + + index 0: numbers <= 6 are 5, 4 => 2 + index 1: numbers <= 5 are 4 => 1 + index 2: numbers <= 4, none => 0 + index 3: numbers <= 8 are 6, 5, 4 => 3 + +=head2 Example 2 + + Input: @num1 = (7, 7, 7, 7) + Output: (3, 3, 3, 3) + +=head2 Example 3 + + Input: @num1 = (5, 4, 3, 2, 1) + Output: (4, 3, 2, 1, 0) + +=head2 Example 4 + + Input: @num1 = (-1, 0, 3, -2, 1) + Output: (1, 2, 4, 0, 3) + +=head2 Example 5 + + Input: @num1 = (0, 1, 1, 2, 0) + Output: (1, 3, 3, 4, 1) + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[ 6, 5, 4, 8], [2, 1, 0, 3], "Example 1"], + [[ 7, 7, 7, 7], [3, 3, 3, 3], "Example 2"], + [[ 5, 4, 3, 2, 1], [4, 3, 2, 1, 0], "Example 3"], + [[-1, 0, 3, -2, 1], [1, 2, 4, 0, 3], "Example 4"], + [[ 0, 1, 1, 2, 0], [1, 3, 3, 4, 1], "Example 5"], +]; + +sub smaller_than_current +{ + my ($nums) = @_; + + my @result; + for my $i (0 .. $#$nums) { + my $count = 0; + for my $j (0 .. $#$nums) { + $count++ if $nums->[$j] <= $nums->[$i]; + } + push @result, $count - 1; + } + return \@result; +} + +for (@$cases) { + is(smaller_than_current($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-337/peter-meszaros/perl/ch-2.pl b/challenge-337/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..45e3183caa --- /dev/null +++ b/challenge-337/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,283 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Odd Matrix + +Submitted by: Mohammad Sajid Anwar + +You are given row and col, also a list of positions in the matrix. + +Write a script to perform action on each location (0-indexed) as provided in +the list and find out the total odd valued cells. + +For each location (r, c), do both of the following: + + a) Increment by 1 all the cells on row r. + b) Increment by 1 all the cells on column c. + +=head2 Example 1 + + Input: $row = 2, $col = 3, @locations = ([0,1],[1,1]) + Output: 6 + + Initial: + [ 0 0 0 ] + [ 0 0 0 ] + + Apply [0,1]: + Increment row 0: + Before After + [ 0 0 0 ] [ 1 1 1 ] + [ 0 0 0 ] [ 0 0 0 ] + Increment col 1: + Before After + [ 1 1 1 ] [ 1 2 1 ] + [ 0 0 0 ] [ 0 1 0 ] + + Apply [1,1]: + Increment row 1: + Before After + [ 1 2 1 ] [ 1 2 1 ] + [ 0 1 0 ] [ 1 2 1 ] + Increment col 1: + Before After + [ 1 2 1 ] [ 1 3 1 ] + [ 1 2 1 ] [ 1 3 1 ] + + Final: + [ 1 3 1 ] + [ 1 3 1 ] + +=head2 Example 2 + + Input: $row = 2, $col = 2, @locations = ([1,1],[0,0]) + Output: 0 + + Initial: + [ 0 0 ] + [ 0 0 ] + + Apply [1,1]: + Increment row 1: + Before After + [ 0 0 ] [ 0 0 ] + [ 0 0 ] [ 1 1 ] + Increment col 1: + Before After + [ 0 0 ] [ 0 1 ] + [ 1 1 ] [ 1 2 ] + + Apply [0,0]: + Increment row 0: + Before After + [ 0 1 ] [ 1 2 ] + [ 1 2 ] [ 1 2 ] + Increment col 0: + Before After + [ 1 2 ] [ 2 2 ] + [ 1 2 ] [ 2 2 ] + + Final: + [ 2 2 ] + [ 2 2 ] + +=head2 Example 3 + + Input: $row = 3, $col = 3, @locations = ([0,0],[1,2],[2,1]) + Output: 0 + + Initial: + [ 0 0 0 ] + [ 0 0 0 ] + [ 0 0 0 ] + + Apply [0,0]: + Increment row 0: + Before After + [ 0 0 0 ] [ 1 1 1 ] + [ 0 0 0 ] [ 0 0 0 ] + [ 0 0 0 ] [ 0 0 0 ] + Increment col 0: + Before After + [ 1 1 1 ] [ 2 1 1 ] + [ 0 0 0 ] [ 1 0 0 ] + [ 0 0 0 ] [ 1 0 0 ] + + Apply [1,2]: + Increment row 1: + Before After + [ 2 1 1 ] [ 2 1 1 ] + [ 1 0 0 ] [ 2 1 1 ] + [ 1 0 0 ] [ 1 0 0 ] + Increment col 2: + Before After + [ 2 1 1 ] [ 2 1 2 ] + [ 2 1 1 ] [ 2 1 2 ] + [ 1 0 0 ] [ 1 0 1 ] + + Apply [2,1]: + Increment row 2: + Before After + [ 2 1 2 ] [ 2 1 2 ] + [ 2 1 2 ] [ 2 1 2 ] + [ 1 0 1 ] [ 2 1 2 ] + Increment col 1: + Before After + [ 2 1 2 ] [ 2 2 2 ] + [ 2 1 2 ] [ 2 2 2 ] + [ 2 1 2 ] [ 2 2 2 ] + + Final: + [ 2 2 2 ] + [ 2 2 2 ] + [ 2 2 2 ] + +=head2 Example 4 + + Input: $row = 1, $col = 5, @locations = ([0,2],[0,4]) + Output: 2 + + Initial: + [ 0 0 0 0 0 ] + + Apply [0,2]: + Increment row 0: + Before After + [ 0 0 0 0 0 ] [ 1 1 1 1 1 ] + Increment col 2: + Before After + [ 1 1 1 1 1 ] [ 1 1 2 1 1 ] + + Apply [0,4]: + Increment row 0: + Before After + [ 1 1 2 1 1 ] [ 2 2 3 2 2 ] + Increment col 4: + Before After + [ 2 2 3 2 2 ] [ 2 2 3 2 3 ] + + Final: + [ 2 2 3 2 3 ] + +=head2 Example 5 + + Input: $row = 4, $col = 2, @locations = ([1,0],[3,1],[2,0],[0,1]) + Output: 8 + + Initial: + [ 0 0 ] + [ 0 0 ] + [ 0 0 ] + [ 0 0 ] + + Apply [1,0]: + Increment row 1: + Before After + [ 0 0 ] [ 0 0 ] + [ 0 0 ] [ 1 1 ] + [ 0 0 ] [ 0 0 ] + [ 0 0 ] [ 0 0 ] + Increment col 0: + Before After + [ 0 0 ] [ 1 0 ] + [ 1 1 ] [ 2 1 ] + [ 0 0 ] [ 1 0 ] + [ 0 0 ] [ 1 0 ] + + Apply [3,1]: + Increment row 3: + Before After + [ 1 0 ] [ 1 0 ] + [ 2 1 ] [ 2 1 ] + [ 1 0 ] [ 1 0 ] + [ 1 0 ] [ 2 1 ] + Increment col 1: + Before After + [ 1 0 ] [ 1 1 ] + [ 2 1 ] [ 2 2 ] + [ 1 0 ] [ 1 1 ] + [ 2 1 ] [ 2 2 ] + + Apply [2,0]: + Increment row 2: + Before After + [ 1 1 ] [ 1 1 ] + [ 2 2 ] [ 2 2 ] + [ 1 1 ] [ 2 2 ] + [ 2 2 ] [ 2 2 ] + Increment col 0: + Before After + [ 1 1 ] [ 2 1 ] + [ 2 2 ] [ 3 2 ] + [ 2 2 ] [ 3 2 ] + [ 2 2 ] [ 3 2 ] + + Apply [0,1]: + Increment row 0: + Before After + [ 2 1 ] [ 3 2 ] + [ 3 2 ] [ 3 2 ] + [ 3 2 ] [ 3 2 ] + [ 3 2 ] [ 3 2 ] + Increment col 1: + Before After + [ 3 2 ] [ 3 3 ] + [ 3 2 ] [ 3 3 ] + [ 3 2 ] [ 3 3 ] + [ 3 2 ] [ 3 3 ] + + Final: + [ 3 3 ] + [ 3 3 ] + [ 3 3 ] + [ 3 3 ] + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[2, 3, [[0,1],[1,1]]], 6, "Example 1"], + [[2, 2, [[1,1],[0,0]]], 0, "Example 2"], + [[3, 3, [[0,0],[1,2],[2,1]]], 0, "Example 3"], + [[1, 5, [[0,2],[0,4]]], 2, "Example 4"], + [[4, 2, [[1,0],[3,1],[2,0],[0,1]]], 8, "Example 5"], +]; + +sub odd_matrix +{ + my ($r, $c, $loc) = @{shift()}; + + my @matrix; + + for my $i (0 .. $r-1) { + for my $j (0 .. $c-1) { + $matrix[$i][$j] = 0; + } + } + for my $l (@$loc) { + my ($rr, $cc) = @$l; + for my $i (0 .. $c-1) { + $matrix[$rr][$i]++; + } + for my $i (0 .. $r-1) { + $matrix[$i][$cc]++; + } + } + my $odd_count = 0; + for my $i (0 .. $r-1) { + for my $j (0 .. $c-1) { + $odd_count++ if $matrix[$i][$j] % 2; + } + } + return $odd_count; +} + +for (@$cases) { + is(odd_matrix($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-337/peter-meszaros/tcl/ch-1.tcl b/challenge-337/peter-meszaros/tcl/ch-1.tcl new file mode 100755 index 0000000000..0e725f8a4e --- /dev/null +++ b/challenge-337/peter-meszaros/tcl/ch-1.tcl @@ -0,0 +1,75 @@ +#!/usr/bin/env tclsh +# +# Task 1: Smaller Than Current +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of numbers, @num1. +# +# Write a script to return an array, @num2, where $num2[i] is the count of all +# numbers less than or equal to $num1[i]. +# +# Example 1 +# +# Input: @num1 = (6, 5, 4, 8) +# Output: (2, 1, 0, 3) +# +# index 0: numbers <= 6 are 5, 4 => 2 +# index 1: numbers <= 5 are 4 => 1 +# index 2: numbers <= 4, none => 0 +# index 3: numbers <= 8 are 6, 5, 4 => 3 +# +# Example 2 +# +# Input: @num1 = (7, 7, 7, 7) +# Output: (3, 3, 3, 3) +# +# Example 3 +# +# Input: @num1 = (5, 4, 3, 2, 1) +# Output: (4, 3, 2, 1, 0) +# +# Example 4 +# +# Input: @num1 = (-1, 0, 3, -2, 1) +# Output: (1, 2, 4, 0, 3) +# +# Example 5 +# +# Input: @num1 = (0, 1, 1, 2, 0) +# Output: (1, 3, 3, 4, 1) +# + +package require tcltest + +set cases { + {{ 6 5 4 8} {2 1 0 3} "Example 1"} + {{ 7 7 7 7} {3 3 3 3} "Example 2"} + {{ 5 4 3 2 1} {4 3 2 1 0} "Example 3"} + {{-1 0 3 -2 1} {1 2 4 0 3} "Example 4"} + {{ 0 1 1 2 0} {1 3 3 4 1} "Example 5"} +} + +proc smaller_than_current {nums} { + set result {} + foreach i $nums { + set count 0 + foreach j $nums { + if {$j <= $i} { + incr count + } + } + lappend result [expr $count - 1] + } + return $result +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + smaller_than_current [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + diff --git a/challenge-337/peter-meszaros/tcl/ch-2.tcl b/challenge-337/peter-meszaros/tcl/ch-2.tcl new file mode 100755 index 0000000000..685fe9031b --- /dev/null +++ b/challenge-337/peter-meszaros/tcl/ch-2.tcl @@ -0,0 +1,285 @@ +#!/usr/bin/env tclsh +# +# Task 2: Odd Matrix +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given row and col, also a list of positions in the matrix. +# +# Write a script to perform action on each location (0-indexed) as provided in +# the list and find out the total odd valued cells. +# +# For each location (r, c), do both of the following: +# +# a) Increment by 1 all the cells on row r. +# b) Increment by 1 all the cells on column c. +# +# Example 1 +# +# Input: $row = 2, $col = 3, @locations = ([0,1],[1,1]) +# Output: 6 +# +# Initial: +# [ 0 0 0 ] +# [ 0 0 0 ] +# +# Apply [0,1]: +# Increment row 0: +# Before After +# [ 0 0 0 ] [ 1 1 1 ] +# [ 0 0 0 ] [ 0 0 0 ] +# Increment col 1: +# Before After +# [ 1 1 1 ] [ 1 2 1 ] +# [ 0 0 0 ] [ 0 1 0 ] +# +# Apply [1,1]: +# Increment row 1: +# Before After +# [ 1 2 1 ] [ 1 2 1 ] +# [ 0 1 0 ] [ 1 2 1 ] +# Increment col 1: +# Before After +# [ 1 2 1 ] [ 1 3 1 ] +# [ 1 2 1 ] [ 1 3 1 ] +# +# Final: +# [ 1 3 1 ] +# [ 1 3 1 ] +# +# Example 2 +# +# Input: $row = 2, $col = 2, @locations = ([1,1],[0,0]) +# Output: 0 +# +# Initial: +# [ 0 0 ] +# [ 0 0 ] +# +# Apply [1,1]: +# Increment row 1: +# Before After +# [ 0 0 ] [ 0 0 ] +# [ 0 0 ] [ 1 1 ] +# Increment col 1: +# Before After +# [ 0 0 ] [ 0 1 ] +# [ 1 1 ] [ 1 2 ] +# +# Apply [0,0]: +# Increment row 0: +# Before After +# [ 0 1 ] [ 1 2 ] +# [ 1 2 ] [ 1 2 ] +# Increment col 0: +# Before After +# [ 1 2 ] [ 2 2 ] +# [ 1 2 ] [ 2 2 ] +# +# Final: +# [ 2 2 ] +# [ 2 2 ] +# +# Example 3 +# +# Input: $row = 3, $col = 3, @locations = ([0,0],[1,2],[2,1]) +# Output: 0 +# +# Initial: +# [ 0 0 0 ] +# [ 0 0 0 ] +# [ 0 0 0 ] +# +# Apply [0,0]: +# Increment row 0: +# Before After +# [ 0 0 0 ] [ 1 1 1 ] +# [ 0 0 0 ] [ 0 0 0 ] +# [ 0 0 0 ] [ 0 0 0 ] +# Increment col 0: +# Before After +# [ 1 1 1 ] [ 2 1 1 ] +# [ 0 0 0 ] [ 1 0 0 ] +# [ 0 0 0 ] [ 1 0 0 ] +# +# Apply [1,2]: +# Increment row 1: +# Before After +# [ 2 1 1 ] [ 2 1 1 ] +# [ 1 0 0 ] [ 2 1 1 ] +# [ 1 0 0 ] [ 1 0 0 ] +# Increment col 2: +# Before After +# [ 2 1 1 ] [ 2 1 2 ] +# [ 2 1 1 ] [ 2 1 2 ] +# [ 1 0 0 ] [ 1 0 1 ] +# +# Apply [2,1]: +# Increment row 2: +# Before After +# [ 2 1 2 ] [ 2 1 2 ] +# [ 2 1 2 ] [ 2 1 2 ] +# [ 1 0 1 ] [ 2 1 2 ] +# Increment col 1: +# Before After +# [ 2 1 2 ] [ 2 2 2 ] +# [ 2 1 2 ] [ 2 2 2 ] +# [ 2 1 2 ] [ 2 2 2 ] +# +# Final: +# [ 2 2 2 ] +# [ 2 2 2 ] +# [ 2 2 2 ] +# +# Example 4 +# +# Input: $row = 1, $col = 5, @locations = ([0,2],[0,4]) +# Output: 2 +# +# Initial: +# [ 0 0 0 0 0 ] +# +# Apply [0,2]: +# Increment row 0: +# Before After +# [ 0 0 0 0 0 ] [ 1 1 1 1 1 ] +# Increment col 2: +# Before After +# [ 1 1 1 1 1 ] [ 1 1 2 1 1 ] +# +# Apply [0,4]: +# Increment row 0: +# Before After +# [ 1 1 2 1 1 ] [ 2 2 3 2 2 ] +# Increment col 4: +# Before After +# [ 2 2 3 2 2 ] [ 2 2 3 2 3 ] +# +# Final: +# [ 2 2 3 2 3 ] +# +# Example 5 +# +# Input: $row = 4, $col = 2, @locations = ([1,0],[3,1],[2,0],[0,1]) +# Output: 8 +# +# Initial: +# [ 0 0 ] +# [ 0 0 ] +# [ 0 0 ] +# [ 0 0 ] +# +# Apply [1,0]: +# Increment row 1: +# Before After +# [ 0 0 ] [ 0 0 ] +# [ 0 0 ] [ 1 1 ] +# [ 0 0 ] [ 0 0 ] +# [ 0 0 ] [ 0 0 ] +# Increment col 0: +# Before After +# [ 0 0 ] [ 1 0 ] +# [ 1 1 ] [ 2 1 ] +# [ 0 0 ] [ 1 0 ] +# [ 0 0 ] [ 1 0 ] +# +# Apply [3,1]: +# Increment row 3: +# Before After +# [ 1 0 ] [ 1 0 ] +# [ 2 1 ] [ 2 1 ] +# [ 1 0 ] [ 1 0 ] +# [ 1 0 ] [ 2 1 ] +# Increment col 1: +# Before After +# [ 1 0 ] [ 1 1 ] +# [ 2 1 ] [ 2 2 ] +# [ 1 0 ] [ 1 1 ] +# [ 2 1 ] [ 2 2 ] +# +# Apply [2,0]: +# Increment row 2: +# Before After +# [ 1 1 ] [ 1 1 ] +# [ 2 2 ] [ 2 2 ] +# [ 1 1 ] [ 2 2 ] +# [ 2 2 ] [ 2 2 ] +# Increment col 0: +# Before After +# [ 1 1 ] [ 2 1 ] +# [ 2 2 ] [ 3 2 ] +# [ 2 2 ] [ 3 2 ] +# [ 2 2 ] [ 3 2 ] +# +# Apply [0,1]: +# Increment row 0: +# Before After +# [ 2 1 ] [ 3 2 ] +# [ 3 2 ] [ 3 2 ] +# [ 3 2 ] [ 3 2 ] +# [ 3 2 ] [ 3 2 ] +# Increment col 1: +# Before After +# [ 3 2 ] [ 3 3 ] +# [ 3 2 ] [ 3 3 ] +# [ 3 2 ] [ 3 3 ] +# [ 3 2 ] [ 3 3 ] +# +# Final: +# [ 3 3 ] +# [ 3 3 ] +# [ 3 3 ] +# [ 3 3 ] +# + +package require tcltest + +set cases { + {{2 3 {{0 1} {1 1}}} 6 "Example 1"} + {{2 2 {{1 1} {0 0}}} 0 "Example 2"} + {{3 3 {{0 0} {1 2} {2 1}}} 0 "Example 3"} + {{1 5 {{0 2} {0 4}}} 2 "Example 4"} + {{4 2 {{1 0} {3 1} {2 0} {0 1}}} 8 "Example 5"} +} + +proc odd_matrix {p} { + set r [lindex $p 0] + set c [lindex $p 1] + set loc [lindex $p 2] + set matrix {} + for {set i 0} {$i < $r} {incr i} { + lappend matrix [list] + for {set j 0} {$j < $c} {incr j} { + lset matrix $i $j 0 + } + } + foreach l $loc { + set rr [lindex $l 0] + set cc [lindex $l 1] + for {set i 0} {$i < $c} {incr i} { + lset matrix $rr $i [expr [lindex [lindex $matrix $rr] $i] + 1] + } + for {set i 0} {$i < $r} {incr i} { + lset matrix $i $cc [expr [lindex [lindex $matrix $i] $cc] + 1] + } + } + set odd_count 0 + for {set i 0} {$i < $r} {incr i} { + for {set j 0} {$j < $c} {incr j} { + if {[expr [lindex [lindex $matrix $i] $j] % 2]} { + incr odd_count + } + } + } + return $odd_count +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + odd_matrix [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + |
