aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-327/peter-meszaros/perl/ch-1.pl62
-rwxr-xr-xchallenge-327/peter-meszaros/perl/ch-2.pl67
-rwxr-xr-xchallenge-327/peter-meszaros/tcl/ch-1.tcl64
-rwxr-xr-xchallenge-327/peter-meszaros/tcl/ch-2.tcl68
4 files changed, 261 insertions, 0 deletions
diff --git a/challenge-327/peter-meszaros/perl/ch-1.pl b/challenge-327/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..89c574c906
--- /dev/null
+++ b/challenge-327/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+#
+=head1 Task 1: Missing Integers
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given an array of n integers.
+
+Write a script to find all the missing integers in the range 1..n in the given
+array.
+
+=head2 Example 1
+
+ Input: @ints = (1, 2, 1, 3, 2, 5)
+ Output: (4, 6)
+
+ The given array has 6 elements.
+ So we are looking for integers in the range 1..6 in the given array.
+ The missing integers: (4, 6)
+
+=head2 Example 2
+
+ Input: @ints = (1, 1, 1)
+ Output: (2, 3)
+
+=head2 Example 3
+
+ Input: @ints = (2, 2, 1)
+ Output: (3)
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[1, 2, 1, 3, 2, 5], [4, 6], "Example 1"],
+ [[1, 1, 1], [2, 3], "Example 2"],
+ [[2, 2, 1], [3], "Example 3"],
+];
+
+sub missing_integers
+{
+ my $ints = shift;
+
+ my $n = @$ints;
+ my %seen = map { $_ => 1 } @$ints;
+ my @missing;
+ for my $i (1 .. $n) {
+ push @missing, $i unless exists $seen{$i};
+ }
+ return \@missing;
+}
+
+for (@$cases) {
+ is(missing_integers($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-327/peter-meszaros/perl/ch-2.pl b/challenge-327/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..086306cbbd
--- /dev/null
+++ b/challenge-327/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/env perl
+#
+=head1 Task 2: MAD
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given an array of distinct integers.
+
+Write a script to find all pairs of elements with minimum absolute difference
+(MAD) of any two elements.
+
+=head2 Example 1
+
+ Input: @ints = (4, 1, 2, 3)
+ Output: [1,2], [2,3], [3,4]
+
+ The minimum absolute difference is 1.
+ Pairs with MAD: [1,2], [2,3], [3,4]
+
+=head2 Example 2
+
+ Input: @ints = (1, 3, 7, 11, 15)
+ Output: [1,3]
+
+=head2 Example 3
+
+ Input: @ints = (1, 5, 3, 8)
+ Output: [1,3], [3,5]
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[4, 1, 2, 3], [[1,2], [2,3], [3,4]], "Example 1"],
+ [[1, 3, 7, 11, 15], [[1,3]], "Example 2"],
+ [[1, 5, 3, 8], [[1,3], [3,5]], "Example 3"],
+];
+
+sub mad
+{
+ my $ints = shift;
+
+ my @sorted = sort { $a <=> $b } @$ints;
+ my $min_diff;
+ my @pairs;
+ for (my $i = 0; $i < @sorted - 1; $i++) {
+ my $diff = $sorted[$i + 1] - $sorted[$i];
+ if (!defined($min_diff) || $diff < $min_diff) {
+ $min_diff = $diff;
+ @pairs = ([$sorted[$i], $sorted[$i + 1]]);
+ } elsif ($diff == $min_diff) {
+ push @pairs, [$sorted[$i], $sorted[$i + 1]];
+ }
+ }
+ return \@pairs;
+}
+
+for (@$cases) {
+ is(mad($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-327/peter-meszaros/tcl/ch-1.tcl b/challenge-327/peter-meszaros/tcl/ch-1.tcl
new file mode 100755
index 0000000000..d26e7e09fb
--- /dev/null
+++ b/challenge-327/peter-meszaros/tcl/ch-1.tcl
@@ -0,0 +1,64 @@
+#!/usr/bin/env tclsh
+#
+# Task 1: Missing Integers
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given an array of n integers.
+#
+# Write a script to find all the missing integers in the range 1..n in the given
+# array.
+#
+# Example 1
+#
+# Input: @ints = (1, 2, 1, 3, 2, 5)
+# Output: (4, 6)
+#
+# The given array has 6 elements.
+# So we are looking for integers in the range 1..6 in the given array.
+# The missing integers: (4, 6)
+#
+# Example 2
+#
+# Input: @ints = (1, 1, 1)
+# Output: (2, 3)
+#
+# Example 3
+#
+# Input: @ints = (2, 2, 1)
+# Output: (3)
+#
+
+package require tcltest
+
+set cases {
+ {{1 2 1 3 2 5} {4 6} "Example 1"}
+ {{1 1 1} {2 3} "Example 2"}
+ {{2 2 1} {3} "Example 3"}
+}
+
+proc missing_integers {ints} {
+
+ set n [llength $ints]
+ set seen {}
+ foreach i $ints {
+ dict set seen $i 1
+ }
+ set missing {}
+ for {set i 1} {$i <= $n} {incr i} {
+ if {![dict exists $seen $i]} {
+ lappend missing $i
+ }
+ }
+ return $missing
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ missing_integers [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+
diff --git a/challenge-327/peter-meszaros/tcl/ch-2.tcl b/challenge-327/peter-meszaros/tcl/ch-2.tcl
new file mode 100755
index 0000000000..f8f7834124
--- /dev/null
+++ b/challenge-327/peter-meszaros/tcl/ch-2.tcl
@@ -0,0 +1,68 @@
+#!/usr/bin/env tclsh
+#
+# Task 2: MAD
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given an array of distinct integers.
+#
+# Write a script to find all pairs of elements with minimum absolute difference
+# (MAD) of any two elements.
+#
+# Example 1
+#
+# Input: @ints = (4, 1, 2, 3)
+# Output: [1,2], [2,3], [3,4]
+#
+# The minimum absolute difference is 1.
+# Pairs with MAD: [1,2], [2,3], [3,4]
+#
+# Example 2
+#
+# Input: @ints = (1, 3, 7, 11, 15)
+# Output: [1,3]
+#
+# Example 3
+#
+# Input: @ints = (1, 5, 3, 8)
+# Output: [1,3], [3,5]
+#
+
+package require tcltest
+
+set cases {
+ {{4 1 2 3} {{1 2} {2 3} {3 4}} "Example 1"}
+ {{1 3 7 11 15} {{1 3}} "Example 2"}
+ {{1 5 3 8} {{1 3} {3 5}} "Example 3"}
+}
+
+proc mad {ints} {
+
+ set sorted [lsort -integer $ints]
+ set min_diff {}
+ set pairs {}
+
+ for {set i 0} {$i < [llength $sorted] - 1} {incr i} {
+ set sorted_i1 [lindex $sorted [expr $i + 1]]
+ set sorted_i [lindex $sorted $i]
+ set diff [expr $sorted_i1 - $sorted_i]
+
+ if {$min_diff eq "" || $diff < $min_diff} {
+ set min_diff $diff
+ set pairs [list [list $sorted_i $sorted_i1]]
+ } elseif {$diff == $min_diff} {
+ lappend pairs [list $sorted_i $sorted_i1]
+ }
+ }
+ return $pairs
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ mad [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+