aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUtil <bruce.gray@acm.org>2024-01-13 19:20:39 -0600
committerUtil <bruce.gray@acm.org>2024-01-13 19:20:39 -0600
commitb123b884d800a28ce11ac6a42f3e757a16e377e0 (patch)
treee6dfca98d52ca4b216ccff9a86eec25ad2f8908b
parentda0d5dcf50f8fa8def91abaafce1fd5624ec1a61 (diff)
downloadperlweeklychallenge-club-b123b884d800a28ce11ac6a42f3e757a16e377e0.tar.gz
perlweeklychallenge-club-b123b884d800a28ce11ac6a42f3e757a16e377e0.tar.bz2
perlweeklychallenge-club-b123b884d800a28ce11ac6a42f3e757a16e377e0.zip
Add TWC 251 solutions by Bruce Gray (in Raku and Scala).
-rw-r--r--challenge-251/bruce-gray/raku/ch-1.raku62
-rw-r--r--challenge-251/bruce-gray/raku/ch-2.raku28
-rw-r--r--challenge-251/bruce-gray/scala/ch-1.scala42
-rw-r--r--challenge-251/bruce-gray/scala/ch-2.scala45
4 files changed, 177 insertions, 0 deletions
diff --git a/challenge-251/bruce-gray/raku/ch-1.raku b/challenge-251/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..62fbe9c8c7
--- /dev/null
+++ b/challenge-251/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,62 @@
+sub task1a ( @ns --> Numeric ) {
+ my $half = +@ns div 2;
+ my $odd = +@ns !%% 2;
+
+ my @left = @ns.head($half);
+ my $middle = @ns.skip($half)[0] if $odd;
+ my @right = @ns.skip($half + (1 if $odd));
+
+ return (@left »~« @right.reverse).sum + ($middle // 0);
+}
+sub task1b ( @ns --> Numeric ) {
+ my @r = @ns;
+ my @l = @r.splice(0, * div 2);
+ my $m = @r.splice(0, 1)[0] if @ns % 2;
+
+ die "Cannot happen" if +@l != +@r;
+
+ return (@l »~« @r.reverse).sum
+ + ($m // 0);
+}
+sub task1c ( @ns --> Numeric ) {
+ return (@ns.head(@ns/2) »~« @ns.tail(@ns/2).reverse).sum
+ + (@ns.skip(@ns/2)[0] if @ns % 2);
+}
+sub task1d ( @ns_in --> Numeric ) {
+ my @ns = @ns_in;
+
+ my $ret = 0;
+ $ret += @ns.shift ~ @ns.pop while @ns > 1;
+ $ret += @ns.shift if @ns;
+
+ return $ret;
+}
+sub task1e ( @ns --> Numeric ) {
+ my ($i, $j) = 0, @ns.end;
+
+ my $ret = 0;
+ $ret += @ns[$i++, $j--].join while $i < $j;
+ $ret += @ns[$i] if $i == $j;
+
+ return $ret;
+}
+
+my @subs =
+ :&task1a,
+ :&task1b,
+ :&task1c,
+ :&task1d,
+ :&task1e,
+;
+my @tests =
+ ( 1286, ( 6, 12, 25, 1 ) ),
+ ( 489, ( 10, 7, 31, 5, 2, 2 ) ),
+ ( 112, ( 1, 2, 10 ) ),
+ ( 0, [] ),
+;
+use Test; plan +@tests * @subs;
+for @subs -> ( :key($sub_name), :value(&task1) ) {
+ for @tests -> ( $expected, @in ) {
+ is task1(@in), $expected, "$sub_name : $expected";
+ }
+}
diff --git a/challenge-251/bruce-gray/raku/ch-2.raku b/challenge-251/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..8346d47756
--- /dev/null
+++ b/challenge-251/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,28 @@
+sub task2 ( @in --> Numeric ) {
+ # Prevents recalculation inside `.first`
+ my @column_maxes = @in[0].keys.map({ @in».[$_].max });
+
+ for @in -> @row {
+ return .value with @row.minpairs.first: { @column_maxes[.key] == .value };
+ }
+ return -1;
+}
+
+
+my @tests =
+ ( 15, ( ( 3, 7, 8 ), ( 9, 11, 13 ), (15, 16, 17 ) ) ),
+ ( 12, ( ( 1, 10, 4, 2), ( 9, 3, 8, 7), (15, 16, 17, 12) ) ),
+ ( 7, ( ( 7, 8 ), ( 1, 2 ) ) ),
+
+ ( 3, ( ( 3, 3, 3 ), ( 3, 9, 9 ), ( 3, 3, 3 ) ) ),
+
+ ( 3, ( ( 9, 3, 15 ), ( 9, 3, 15 ), ( 9, 3, 15 ) ) ),
+
+ ( -1, ( ( 1, 5, 8 ), ( 2, 6, 4 ), ( 7, 3, 9 ) ) ),
+
+ ( -1, ( (0, 1, 2), (1, 2, 0), (2, 0, 1) ) ),
+;
+use Test; plan +@tests;
+for @tests -> ( $expected, @in ) {
+ is task2(@in), $expected;
+}
diff --git a/challenge-251/bruce-gray/scala/ch-1.scala b/challenge-251/bruce-gray/scala/ch-1.scala
new file mode 100644
index 0000000000..032f375379
--- /dev/null
+++ b/challenge-251/bruce-gray/scala/ch-1.scala
@@ -0,0 +1,42 @@
+// Run with `scala scala/ch-1.scala`
+val task1 = ( ns: Array[Int] ) =>
+ var i = 0
+ var j = ns.length - 1
+ var ret = 0
+
+ while ( i < j ) {
+ ret += s"${ns(i)}${ns(j)}".toInt
+ i += 1
+ j -= 1
+ }
+
+ if i == j then
+ ret += ns(i)
+
+ ret
+
+
+// Below this point is test harness and data
+var test_number = 0
+val is = ( got:Int, expected:Int, test_name:String ) =>
+ test_number += 1
+ val ok_msg = if (got == expected) then "ok" else "not ok"
+ println(s"$ok_msg $test_number $test_name")
+
+@main def TWC251_1(args: String*): Unit = {
+ if (args.length > 0) {
+ println(task1(args.map(_.toInt).toArray))
+ }
+ else {
+ val arr = List(
+ ( 1286, Array( 6, 12, 25, 1) , "Task example 1" ),
+ ( 489, Array(10, 7, 31, 5, 2, 2) , "Task example 2" ),
+ ( 112, Array( 1, 2, 10) , "Task example 3" ),
+ ( 0, Array[Int]() , "Empty --> 0" )
+ )
+
+ for (((expected, in, test_name)) <- arr) {
+ is( task1( in ), expected, test_name )
+ }
+ }
+}
diff --git a/challenge-251/bruce-gray/scala/ch-2.scala b/challenge-251/bruce-gray/scala/ch-2.scala
new file mode 100644
index 0000000000..ae19ab47eb
--- /dev/null
+++ b/challenge-251/bruce-gray/scala/ch-2.scala
@@ -0,0 +1,45 @@
+// Run with `scala scala/ch-2.scala`
+val task2 = ( matrix: List[List[Int]] ) =>
+
+ val column_maxes = matrix(0).indices.map(j => {
+ matrix.map{_(j)}.max
+ })
+
+ var ret = -1
+ import scala.util.control.Breaks._
+ breakable {
+ for (row <- matrix) {
+ // Minimums can occur more than once in a row
+ val min_col_value = row.min
+ val min_col_indexes = row.indices.filter(j => row(j) == min_col_value)
+
+ if ( min_col_indexes.exists(j => min_col_value == column_maxes(j)) ) {
+ ret = min_col_value
+ break
+ }
+ }
+ }
+ ret
+
+
+// Below this point is test harness and data
+var test_number = 0
+val is = ( got:Int, expected:Int, test_name:String ) =>
+ test_number += 1
+ val ok_msg = if (got == expected) then "ok" else "not ok"
+ println(s"$ok_msg $test_number $test_name")
+
+@main def TWC251_2(): Unit = {
+ val arr = List(
+ ( 15, List( List( 3, 7, 8 ), List( 9, 11, 13 ), List(15, 16, 17 ) ), "Task example 1" ),
+ ( 12, List( List( 1, 10, 4, 2), List( 9, 3, 8, 7), List(15, 16, 17, 12) ), "Task example 2" ),
+ ( 7, List( List( 7, 8 ), List( 1, 2 ) ), "Task example 3" ),
+ ( 3, List( List( 3, 3, 3 ), List( 3, 9, 9 ), List( 3, 3, 3 ) ), "3&9" ),
+ ( 3, List( List( 9, 3, 15 ), List( 9, 3, 15 ), List( 9, 3, 15 ) ), "3,9,15" ),
+ ( -1, List( List( 1, 5, 8 ), List( 2, 6, 4 ), List( 7, 3, 9 ) ), "-1" ),
+ )
+
+ for (((expected, in, test_name)) <- arr) {
+ is( task2(in), expected, test_name )
+ }
+}