aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpme <hauptadler@gmail.com>2024-08-26 21:15:18 +0200
committerpme <hauptadler@gmail.com>2024-08-26 21:15:18 +0200
commit6b1f8b4fda8d0700040d151fbc0671fffa4f02a8 (patch)
treea0d6b221bf7e628eb27946f5e5f1c83a374aa84c
parentca254bf2754bcd6602cd190c4497529c3aa7ff74 (diff)
downloadperlweeklychallenge-club-6b1f8b4fda8d0700040d151fbc0671fffa4f02a8.tar.gz
perlweeklychallenge-club-6b1f8b4fda8d0700040d151fbc0671fffa4f02a8.tar.bz2
perlweeklychallenge-club-6b1f8b4fda8d0700040d151fbc0671fffa4f02a8.zip
challenge-284
-rwxr-xr-xchallenge-284/peter-meszaros/perl/ch-1.pl61
-rwxr-xr-xchallenge-284/peter-meszaros/perl/ch-2.pl74
-rwxr-xr-xchallenge-284/peter-meszaros/tcl/ch-1.tcl61
-rwxr-xr-xchallenge-284/peter-meszaros/tcl/ch-2.tcl87
4 files changed, 283 insertions, 0 deletions
diff --git a/challenge-284/peter-meszaros/perl/ch-1.pl b/challenge-284/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..9740163fbe
--- /dev/null
+++ b/challenge-284/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,61 @@
+#!/usr/bin/env perl
+#
+=head1 Task 1: Lucky Integer
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given an array of integers, @ints.
+
+Write a script to find the lucky integer if found otherwise return -1. If there
+are more than one then return the largest.
+
+ A lucky integer is an integer that has a frequency in the array equal to
+ its value.
+
+=head2 Example 1
+
+ Input: @ints = (2, 2, 3, 4)
+ Output: 2
+
+=head2 Example 2
+
+ Input: @ints = (1, 2, 2, 3, 3, 3)
+ Output: 3
+
+=head2 Example 3
+
+ Input: @ints = (1, 1, 1, 3)
+ Output: -1
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[2, 2, 3, 4], 2, 'Example 1'],
+ [[1, 2, 2, 3, 3, 3], 3, 'Example 2'],
+ [[1, 1, 1, 3], -1, 'Example 3'],
+];
+
+sub lucky_integer
+{
+ my $l = shift;
+
+ my %h;
+ $h{$_}++ for @$l;
+
+ for my $k (sort {$b <=> $a} keys %h) {
+ return $k if $k == $h{$k};
+ }
+ return -1;
+}
+
+for (@$cases) {
+ is(lucky_integer($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-284/peter-meszaros/perl/ch-2.pl b/challenge-284/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..ba3887a70f
--- /dev/null
+++ b/challenge-284/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+#
+=head1 Task 2: Relative Sort
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given two list of integers, @list1 and @list2. The elements in the
+@list2 are distinct and also in the @list1.
+
+Write a script to sort the elements in the @list1 such that the relative order
+of items in @list1 is same as in the @list2. Elements that is missing in @list2
+should be placed at the end of @list1 in ascending order.
+
+=head2 Example 1
+
+ Input: @list1 = (2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5)
+ @list2 = (2, 1, 4, 3, 5, 6)
+ Ouput: (2, 2, 1, 4, 3, 3, 5, 6, 7, 8, 9)
+
+=head2 Example 2
+
+ Input: @list1 = (3, 3, 4, 6, 2, 4, 2, 1, 3)
+ @list2 = (1, 3, 2)
+ Ouput: (1, 3, 3, 3, 2, 2, 4, 4, 6)
+
+=head2 Example 3
+
+ Input: @list1 = (3, 0, 5, 0, 2, 1, 4, 1, 1)
+ @list2 = (1, 0, 3, 2)
+ Ouput: (1, 1, 1, 0, 0, 3, 2, 4, 5)
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[[2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5], [2, 1, 4, 3, 5, 6]],
+ [2, 2, 1, 4, 3, 3, 5, 6, 7, 8, 9], 'Example 1'],
+ [[[3, 3, 4, 6, 2, 4, 2, 1, 3], [1, 3, 2]],
+ [1, 3, 3, 3, 2, 2, 4, 4, 6], 'Example 2'],
+ [[[3, 0, 5, 0, 2, 1, 4, 1, 1], [1, 0, 3, 2]],
+ [1, 1, 1, 0, 0, 3, 2, 4, 5], 'Example 3'],
+];
+
+sub relative_sort
+{
+ my $l1 = $_[0]->[0];
+ my $l2 = $_[0]->[1];
+
+ my %h;
+ $h{$l2->[$_]} = $_ for 0 .. $#$l2;
+
+ my (@res, @rem);
+ for my $i (@$l1) {
+ if (exists $h{$i}) {
+ push @res, $i;
+ } else {
+ push @rem, $i;
+ }
+ }
+ @res = sort { $h{$a} <=> $h{$b} } @res;
+ push @res, sort {$a <=> $b} @rem;
+ return \@res;
+}
+
+for (@$cases) {
+ is(relative_sort($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-284/peter-meszaros/tcl/ch-1.tcl b/challenge-284/peter-meszaros/tcl/ch-1.tcl
new file mode 100755
index 0000000000..890dc1917a
--- /dev/null
+++ b/challenge-284/peter-meszaros/tcl/ch-1.tcl
@@ -0,0 +1,61 @@
+#!/usr/bin/env tclsh
+#
+# Task 1: Lucky Integer
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given an array of integers, @ints.
+#
+# Write a script to find the lucky integer if found otherwise return -1. If there
+# are more than one then return the largest.
+#
+# A lucky integer is an integer that has a frequency in the array equal to
+# its value.
+#
+# Example 1
+#
+# Input: @ints = (2, 2, 3, 4)
+# Output: 2
+#
+# Example 2
+#
+# Input: @ints = (1, 2, 2, 3, 3, 3)
+# Output: 3
+#
+# Example 3
+#
+# Input: @ints = (1, 1, 1, 3)
+# Output: -1
+#
+
+package require tcltest
+
+set cases {
+ {{2 2 3 4} 2 "Example 1"}
+ {{1 2 2 3 3 3} 3 "Example 2"}
+ {{1 1 1 3} -1 "Example 3"}
+}
+
+proc lucky_integer {ints} {
+
+ set d {}
+ foreach i $ints {
+ dict incr d $i
+ }
+
+ foreach i [lsort -integer -decreasing [dict key $d]] {
+ if {$i == [dict get $d $i]} {
+ return $i
+ }
+ }
+ return -1
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ lucky_integer [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
diff --git a/challenge-284/peter-meszaros/tcl/ch-2.tcl b/challenge-284/peter-meszaros/tcl/ch-2.tcl
new file mode 100755
index 0000000000..0785dcb49a
--- /dev/null
+++ b/challenge-284/peter-meszaros/tcl/ch-2.tcl
@@ -0,0 +1,87 @@
+#!/usr/bin/env tclsh
+#
+# Task 2: Relative Sort
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given two list of integers, @list1 and @list2. The elements in the
+# @list2 are distinct and also in the @list1.
+#
+# Write a script to sort the elements in the @list1 such that the relative order
+# of items in @list1 is same as in the @list2. Elements that is missing in @list2
+# should be placed at the end of @list1 in ascending order.
+#
+# Example 1
+#
+# Input: @list1 = (2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5)
+# @list2 = (2, 1, 4, 3, 5, 6)
+# Ouput: (2, 2, 1, 4, 3, 3, 5, 6, 7, 8, 9)
+#
+# Example 2
+#
+# Input: @list1 = (3, 3, 4, 6, 2, 4, 2, 1, 3)
+# @list2 = (1, 3, 2)
+# Ouput: (1, 3, 3, 3, 2, 2, 4, 4, 6)
+#
+# Example 3
+#
+# Input: @list1 = (3, 0, 5, 0, 2, 1, 4, 1, 1)
+# @list2 = (1, 0, 3, 2)
+# Ouput: (1, 1, 1, 0, 0, 3, 2, 4, 5)
+#
+
+package require tcltest
+
+set cases {
+ {{{2 3 9 3 1 4 6 7 2 8 5} {2 1 4 3 5 6}}
+ {2 2 1 4 3 3 5 6 7 8 9} "Example 1"}
+ {{{3 3 4 6 2 4 2 1 3} {1 3 2}}
+ {1 3 3 3 2 2 4 4 6} "Example 2"}
+ {{{3 0 5 0 2 1 4 1 1} {1 0 3 2}}
+ {1 1 1 0 0 3 2 4 5} "Example 3"}
+}
+
+proc compare {a b} {
+ upvar 1 d dlocal
+
+ set v1 [dict get $dlocal $a]
+ set v2 [dict get $dlocal $b]
+
+ if {$v1 < $v2} {
+ return -1
+ } elseif {$v1 > $v2} {
+ return +1
+ }
+ return 0
+}
+
+proc relative_sort {lists} {
+ set l1 [lindex $lists 0]
+ set l2 [lindex $lists 1]
+
+ set d {}
+ for {set i 0} {$i < [llength $l2]} {incr i} {
+ dict set d [lindex $l2 $i] $i
+ }
+
+ foreach i $l1 {
+ if {[dict exists $d $i]} {
+ lappend res $i
+ } else {
+ lappend rem $i
+ }
+ }
+ set rem [lsort -integer $rem]
+ set res [lsort -command compare $res]
+ lappend res {*}$rem
+ return $res
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ relative_sort [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0