From 9518d58f43543361c49af92b9d919d8b8c4c10f0 Mon Sep 17 00:00:00 2001 From: Humberto Massa Date: Wed, 8 Nov 2023 19:43:03 -0300 Subject: Some more oneliners --- challenge-242/massa/raku/ch-1.raku | 64 ++++++++++++++++++++++++++++++++ challenge-242/massa/raku/ch-2.raku | 75 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 challenge-242/massa/raku/ch-1.raku create mode 100644 challenge-242/massa/raku/ch-2.raku diff --git a/challenge-242/massa/raku/ch-1.raku b/challenge-242/massa/raku/ch-1.raku new file mode 100644 index 0000000000..7691ced71b --- /dev/null +++ b/challenge-242/massa/raku/ch-1.raku @@ -0,0 +1,64 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:17:32 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task 1: Missing Members + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given two arrays of integers. + +Write a script to find out the missing members in each other arrays. + +=head3 Example 1: + + Input: @arr1 = (1, 2, 3) + @arr2 = (2, 4, 6) + Output: ([1, 3], [4, 6]) + + (1, 2, 3) has 2 members (1, 3) missing in the array (2, 4, 6). + (2, 4, 6) has 2 members (4, 6) missing in the array (1, 2, 3). + +=head3 Example 2: + + Input: @arr1 = (1, 2, 3, 3) + @arr2 = (1, 1, 2, 2) + Output: ([3]) + + (1, 2, 3, 3) has 2 members (3, 3) missing in the array (1, 1, 2, 2). Since they are same, keep just one. + (1, 1, 2, 2) has 0 member missing in the array (1, 2, 3, 3). + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub SOLUTION(@a, @b) { + keys(@a ∖ @b).sort.Array, keys(@b ∖ @a).sort.Array +} + +multi MAIN (Bool :$test!) { + use Test; + + my @tests = [ + %{ input => ([1, 2, 3], [2, 4, 6]), output => ([1, 3], [4, 6]) }, + %{ input => ([1, 2, 3, 3], [1, 1, 2, 2]), output => ([3], []) }, + ]; + + for @tests { + SOLUTION( |. ).&is-deeply: ., .; + } # end of for @tests +} # end of multi MAIN (Bool :$test!) + + diff --git a/challenge-242/massa/raku/ch-2.raku b/challenge-242/massa/raku/ch-2.raku new file mode 100644 index 0000000000..58801dd5e4 --- /dev/null +++ b/challenge-242/massa/raku/ch-2.raku @@ -0,0 +1,75 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:17:32 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task 2: Flip Matrix + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given n x n binary matrix. + +Write a script to flip the given matrix as below. + + 1 1 0 + 0 1 1 + 0 0 1 + + a) Reverse each row + + 0 1 1 + 1 1 0 + 1 0 0 + + b) Invert each member + + 1 0 0 + 0 0 1 + 0 1 1 + +=head3 Example 1: + + Input: @matrix = ([1, 1, 0], [1, 0, 1], [0, 0, 0]) + Output: ([1, 0, 0], [0, 1, 0], [1, 1, 1]) + +=head3 Example 2: + + Input: @matrix = ([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]) + Output: ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]) + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub SOLUTION(@_) { + @_».reverse».map({ $_ ?? 0 !! 1 })».Array +} + +multi MAIN (Bool :$test!) { + use Test; + + my @tests = [ + %{ input => ([1, 1, 0], [1, 0, 1], [0, 0, 0]), + output => ([1, 0, 0], [0, 1, 0], [1, 1, 1]) }, + %{ input => ([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]), + output => ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]) }, + ]; + + for @tests { + SOLUTION( . ).&is-deeply: ., .; + + } # end of for @tests +} # end of multi MAIN (:$test!) + + -- cgit From c7cd3e2ea5883404c6ae18ef6621a075e24210d4 Mon Sep 17 00:00:00 2001 From: Humberto Massa Date: Thu, 9 Nov 2023 06:35:35 -0300 Subject: unifying test syntax --- challenge-242/massa/raku/ch-1.raku | 18 +++++++++--------- challenge-242/massa/raku/ch-2.raku | 21 +++++++++------------ 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/challenge-242/massa/raku/ch-1.raku b/challenge-242/massa/raku/ch-1.raku index 7691ced71b..6f66804868 100644 --- a/challenge-242/massa/raku/ch-1.raku +++ b/challenge-242/massa/raku/ch-1.raku @@ -44,21 +44,21 @@ Write a script to find out the missing members in each other arrays. # always use the latest version of Raku use v6.*; -sub SOLUTION(@a, @b) { - keys(@a ∖ @b).sort.Array, keys(@b ∖ @a).sort.Array +sub SOLUTION(@ (@a, @b)) { + (@a ∖ @b, @b ∖ @a)».keys».sort } multi MAIN (Bool :$test!) { use Test; - my @tests = [ - %{ input => ([1, 2, 3], [2, 4, 6]), output => ([1, 3], [4, 6]) }, - %{ input => ([1, 2, 3, 3], [1, 1, 2, 2]), output => ([3], []) }, - ]; + my @tests = + %{ input => ((1, 2, 3), (2, 4, 6)), + output => ((1, 3), (4, 6)) }, + %{ input => ((1, 2, 3, 3), (1, 1, 2, 2)), + output => ((3,), ()) }, + ; - for @tests { - SOLUTION( |. ).&is-deeply: ., .; - } # end of for @tests + ..&SOLUTION.gist.&is: ..gist, . for @tests } # end of multi MAIN (Bool :$test!) diff --git a/challenge-242/massa/raku/ch-2.raku b/challenge-242/massa/raku/ch-2.raku index 58801dd5e4..bb3033c7a2 100644 --- a/challenge-242/massa/raku/ch-2.raku +++ b/challenge-242/massa/raku/ch-2.raku @@ -53,23 +53,20 @@ Write a script to flip the given matrix as below. use v6.*; sub SOLUTION(@_) { - @_».reverse».map({ $_ ?? 0 !! 1 })».Array + @_».reverse».map: 1 - * } multi MAIN (Bool :$test!) { use Test; - my @tests = [ - %{ input => ([1, 1, 0], [1, 0, 1], [0, 0, 0]), - output => ([1, 0, 0], [0, 1, 0], [1, 1, 1]) }, - %{ input => ([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]), - output => ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]) }, - ]; + my @tests = + %{ input => ((1, 1, 0), (1, 0, 1), (0, 0, 0)), + output => ((1, 0, 0), (0, 1, 0), (1, 1, 1)) }, + %{ input => ((1, 1, 0, 0), (1, 0, 0, 1), (0, 1, 1, 1), (1, 0, 1, 0)), + output => ((1, 1, 0, 0), (0, 1, 1, 0), (0, 0, 0, 1), (1, 0, 1, 0)) }, + ; - for @tests { - SOLUTION( . ).&is-deeply: ., .; - - } # end of for @tests -} # end of multi MAIN (:$test!) + ..&SOLUTION.gist.&is: ..gist, . for @tests +} # end of multi MAIN (Bool :$test!) -- cgit From df3151eb4b96d34e7c23a99fa9cd4cffe29839cd Mon Sep 17 00:00:00 2001 From: Humberto Massa Date: Thu, 9 Nov 2023 06:48:13 -0300 Subject: unifying test syntax (quirk in is-deeply) --- challenge-242/massa/raku/ch-1.raku | 2 +- challenge-242/massa/raku/ch-2.raku | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-242/massa/raku/ch-1.raku b/challenge-242/massa/raku/ch-1.raku index 6f66804868..fd3b1ccefd 100644 --- a/challenge-242/massa/raku/ch-1.raku +++ b/challenge-242/massa/raku/ch-1.raku @@ -58,7 +58,7 @@ multi MAIN (Bool :$test!) { output => ((3,), ()) }, ; - ..&SOLUTION.gist.&is: ..gist, . for @tests + ..&SOLUTION».cache.&is-deeply: .».cache, . for @tests } # end of multi MAIN (Bool :$test!) diff --git a/challenge-242/massa/raku/ch-2.raku b/challenge-242/massa/raku/ch-2.raku index bb3033c7a2..997ddd791b 100644 --- a/challenge-242/massa/raku/ch-2.raku +++ b/challenge-242/massa/raku/ch-2.raku @@ -66,7 +66,7 @@ multi MAIN (Bool :$test!) { output => ((1, 1, 0, 0), (0, 1, 1, 0), (0, 0, 0, 1), (1, 0, 1, 0)) }, ; - ..&SOLUTION.gist.&is: ..gist, . for @tests + ..&SOLUTION».cache.&is-deeply: .».cache, . for @tests } # end of multi MAIN (Bool :$test!) -- cgit From c59c47a9c3579f4edfd0bf7d736a25e845a8a1aa Mon Sep 17 00:00:00 2001 From: Humberto Massa Date: Thu, 9 Nov 2023 06:58:13 -0300 Subject: unifying test syntax (a more perfect workaround) --- challenge-242/massa/raku/ch-1.raku | 2 +- challenge-242/massa/raku/ch-2.raku | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-242/massa/raku/ch-1.raku b/challenge-242/massa/raku/ch-1.raku index fd3b1ccefd..9eacb018ce 100644 --- a/challenge-242/massa/raku/ch-1.raku +++ b/challenge-242/massa/raku/ch-1.raku @@ -58,7 +58,7 @@ multi MAIN (Bool :$test!) { output => ((3,), ()) }, ; - ..&SOLUTION».cache.&is-deeply: .».cache, . for @tests + ..&SOLUTION.deepmap({$_}).&is-deeply: ., . for @tests } # end of multi MAIN (Bool :$test!) diff --git a/challenge-242/massa/raku/ch-2.raku b/challenge-242/massa/raku/ch-2.raku index 997ddd791b..3241fa21c6 100644 --- a/challenge-242/massa/raku/ch-2.raku +++ b/challenge-242/massa/raku/ch-2.raku @@ -66,7 +66,7 @@ multi MAIN (Bool :$test!) { output => ((1, 1, 0, 0), (0, 1, 1, 0), (0, 0, 0, 1), (1, 0, 1, 0)) }, ; - ..&SOLUTION».cache.&is-deeply: .».cache, . for @tests + ..&SOLUTION.deepmap({$_}).&is-deeply: ., . for @tests } # end of multi MAIN (Bool :$test!) -- cgit