From 835102b726e19165cf2b3f3d373ed9af1ef88954 Mon Sep 17 00:00:00 2001 From: deoac Date: Mon, 15 May 2023 21:19:03 -0400 Subject: Started 217-2 --- challenge-217/shimon-ben-avraham/raku/ch-1.md | 160 ++++++++------------------ 1 file changed, 47 insertions(+), 113 deletions(-) diff --git a/challenge-217/shimon-ben-avraham/raku/ch-1.md b/challenge-217/shimon-ben-avraham/raku/ch-1.md index 280ffd2275..e52e8592a8 100644 --- a/challenge-217/shimon-ben-avraham/raku/ch-1.md +++ b/challenge-217/shimon-ben-avraham/raku/ch-1.md @@ -1,13 +1,5 @@ -<<<<<<< HEAD -TITLE -===== - -======= ```raku -# Perl Weekly Challenge #216, Challenge 1 -# © 2023 Shimon Bollinger. All rights reserved. -# Last modified: Thu 11 May 2023 07:47:20 PM EDT -# Version 0.0.1 + ``` TITLE @@ -15,9 +7,8 @@ TITLE ->>>>>>> 0c0e20987dc5677805c95f8e2471492515be32c3 -Task 1: Registration Number -=========================== +Task 2: Max Number +------------------ SUBTITLE ======== @@ -27,128 +18,71 @@ SUBTITLE Submitted by: Mohammad S Anwar ------------------------------ -You are given a list of words and a random registration number. +CHALLENGE +========= -Write a script to find all the words in the given list that has every letter in the given registration number. -### Example 1 - Input: @words = ('abc', 'abcd', 'bcd'), $reg = 'AB1 2CD' - Output: ('abcd') +You are given a list of positive integers. Write a script to concatenate the integers to form the highest possible value. +------------------------------------------------------------------------------------------------------------------------- -The only word that matches every alphabets in the given registration number is 'abcd'. +### Example 1: -### Example 2 + Input: @list = (1, 23) - Input: @words = ('job', 'james', 'bjorg'), $reg = '007 JB' - Output: ('job', 'bjorg') + Output: 231 -### Example 3 +### Example 2: - Input: @words = ('crack', 'road', 'rac'), $reg = 'C7 RA2' - Output: ('crack', 'rac') - -SOLUTION -======== + Input: @list = (10, 3, 2) + Output: 3210 +### Example 3: -```raku -multi MAIN (Str $reg, *@words) { - my Str @result; -``` + Input: @list = (31, 2, 4, 10) -We're going to use the `comb` method to determine if any of the input words fulfill the requirements. + Output: 431210 -First, remove all the non-alphabetics from the registration number. +### Example 4: -```raku - my Junction $reg-letters = $reg.subst(/<-alpha>/, :g) -``` + Input: @list = (5, 11, 4, 1, 2) -We don't care about the case of the letters. + Output: 542111 -```raku - .lc -``` +### Example 5: -Now create an array of the letters remaing in the registration number. + Input: @list = (1, 10) -```raku - .comb -``` - -Create an `any` junction from the letters. - -```raku - .any; -``` - -In example 3, above, `$reg-letters` will be - -```raku -any("a", "r", "c") -``` - -Now, collect the words that match the registration in the `@result` array. - -```raku - @result.push($_) if -``` + Output: 110 -We don't care about the case of the words being tested. - -```raku - .lc -``` - -Here's the heart of the algorithm. `comb` the word with the previously made `Junction`. This will create a `Junction` of *n* `Seq`uences, where *n* is the number of letters in the registration number. - -```raku - .comb($reg-letters) -``` - -When we `comb` the words in the third example, we will get these `Junction`s: - -```raku -# for "crack" -any(("c", "c").Seq, ("r",).Seq, ("a",).Seq) - -# for "road" -any(().Seq, ("r",).Seq, ("a",).Seq) - -# for "rac" -any(("c",).Seq, ("r",).Seq, ("a",).Seq) -``` - -The word "road" does not meet the challenge's requirements, because it lacks the letter 'c'. This is reflected in the empty `Seq`uence: - -```raku -().Seq -``` - -The other two examples pass the requirements, and so do not include an empty `Seq`uence in their `any Junction`s. =end pod - -so to find the good words, match the `Junction` against an empty list; those that **don't** match are the good ones. +SOLUTION +======== -```raku - !~~ () -``` -Finally, make sure we test every word in the `@words` array... ```raku - for @words; + 1| use v6.*; + 2| + 3| sub max-number (@list where .all ~~ UInt) { + 4| my UInt $retval = 0; + 5| + 6| return $retval + 7| } + 8| + 9| multi MAIN (:$test! ) { + 10| use Test; + 11| + 12| my @tests = [ + 13| %{ input => (1, 23), output => 231, text => 'Example 1' }, + 14| %{ input => (10, 3, 2), output => 3210, text => 'Example 2' }, + 15| %{ input => (31, 2, 4, 10), output => 431210, text => 'Example 3' }, + 16| %{ input => (5, 11, 4, 1, 2), output => 542111, text => 'Example 4' }, + 17| %{ input => (1, 10), output => 110, text => 'Example 5' }, + 18| ]; + 19| + 20| for @tests { + 21| is max-number( . ), ., .; + 22| } # end of for @tests + 23| } # end of multi MAIN (:$test! ) ``` - -...and print the results! - -```raku -<<<<<<< HEAD - say @result.List; -} # end of multi MAIN (Str $reg, *@words) - -======= - say @result.map({"'$_'"}).join(', ').map({ ($_) } ); -} # end of multi MAIN (Str $reg, *@words) ->>>>>>> 0c0e20987dc5677805c95f8e2471492515be32c3 -- cgit From beec22198794f698ba762790a81be7633610bbae Mon Sep 17 00:00:00 2001 From: deoac Date: Mon, 15 May 2023 21:21:18 -0400 Subject: Started 217-2 --- challenge-217/shimon-ben-avraham/raku/ch-1.raku | 33 +++++++++ .../shimon-ben-avraham/raku/max-number.raku | 79 ++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 challenge-217/shimon-ben-avraham/raku/ch-1.raku create mode 100755 challenge-217/shimon-ben-avraham/raku/max-number.raku diff --git a/challenge-217/shimon-ben-avraham/raku/ch-1.raku b/challenge-217/shimon-ben-avraham/raku/ch-1.raku new file mode 100644 index 0000000000..4329c14604 --- /dev/null +++ b/challenge-217/shimon-ben-avraham/raku/ch-1.raku @@ -0,0 +1,33 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge 217-2 +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:07:42 PM EDT +# Version 0.0.1 + +# always use the latest version of Raku +use v6.*; + +sub max-number (@list where .all ~~ UInt) { + my UInt $retval = 0; + + return $retval +} # end of sub max-number (UInt @list) + +multi MAIN (:$test! ) { + use Test; + + my @tests = [ + %{ input => (1, 23), output => 231, text => 'Example 1' }, + %{ input => (10, 3, 2), output => 3210, text => 'Example 2' }, + %{ input => (31, 2, 4, 10), output => 431210, text => 'Example 3' }, + %{ input => (5, 11, 4, 1, 2), output => 542111, text => 'Example 4' }, + %{ input => (1, 10), output => 110, text => 'Example 5' }, + ]; + + for @tests { + is max-number( . ), ., .; + } # end of for @tests +} # end of multi MAIN ( ) + + diff --git a/challenge-217/shimon-ben-avraham/raku/max-number.raku b/challenge-217/shimon-ben-avraham/raku/max-number.raku new file mode 100755 index 0000000000..c0f67173f6 --- /dev/null +++ b/challenge-217/shimon-ben-avraham/raku/max-number.raku @@ -0,0 +1,79 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge 217-2 +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:13:54 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task 2: Max Number + +=SUBTITLE +=head2 Submitted by: Mohammad S Anwar + +=CHALLENGE +=head2 +You are given a list of positive integers. +Write a script to concatenate the integers to form the highest possible value. + +=head3 Example 1: + + Input: @list = (1, 23) + + Output: 231 + +=head3 Example 2: + + Input: @list = (10, 3, 2) + + Output: 3210 + +=head3 Example 3: + + Input: @list = (31, 2, 4, 10) + + Output: 431210 + +=head3 Example 4: + + Input: @list = (5, 11, 4, 1, 2) + + Output: 542111 + +=head3 Example 5: + + Input: @list = (1, 10) + + Output: 110 + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub max-number (@list where .all ~~ UInt) { + my UInt $retval = 0; + + return $retval +} # end of sub max-number (UInt @list) + +multi MAIN (:$test! ) { + use Test; + + my @tests = [ + %{ input => (1, 23), output => 231, text => 'Example 1' }, + %{ input => (10, 3, 2), output => 3210, text => 'Example 2' }, + %{ input => (31, 2, 4, 10), output => 431210, text => 'Example 3' }, + %{ input => (5, 11, 4, 1, 2), output => 542111, text => 'Example 4' }, + %{ input => (1, 10), output => 110, text => 'Example 5' }, + ]; + + for @tests { + is max-number( . ), ., .; + } # end of for @tests +} # end of multi MAIN (:$test! ) + + -- cgit From cb2d6dbbdb61d677500032118ba3b43a67a18c74 Mon Sep 17 00:00:00 2001 From: deoac Date: Mon, 15 May 2023 21:21:52 -0400 Subject: Created template file for Challenges --- template.raku | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 template.raku diff --git a/template.raku b/template.raku new file mode 100644 index 0000000000..85d9896ff3 --- /dev/null +++ b/template.raku @@ -0,0 +1,51 @@ +#! /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 + +=SUBTITLE +=head2 Submitted by + +=CHALLENGE +=head2 + +=head3 Example 1: + +=head3 Example 2: + +=head3 Example 3: + +=head3 Example 4: + +=head3 Example 5: + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub my-sub () { +; +} # end of sub my-sub () + +multi MAIN (:$test! ) { + use Test; + + my @tests = [ + %{ input => (), output => Nil, text => 'Example ' }, + ]; + + for @tests { + is my-sub( . ), ., .; + } # end of for @tests +} # end of multi MAIN (:$test! ) + + -- cgit From 0ec38a2edfb17fb341a769deab142cebefcdbc0e Mon Sep 17 00:00:00 2001 From: deoac Date: Sat, 20 May 2023 23:20:32 -0400 Subject: Completed challenge 2 --- challenge-217/shimon-ben-avraham/raku/ch-1.md | 88 -------------------- challenge-217/shimon-ben-avraham/raku/ch-1.raku | 33 -------- challenge-217/shimon-ben-avraham/raku/ch-2.md | 94 ++++++++++++++++++++++ challenge-217/shimon-ben-avraham/raku/ch-2.raku | 51 ++++++++++++ .../shimon-ben-avraham/raku/max-number.raku | 35 +++++--- 5 files changed, 171 insertions(+), 130 deletions(-) delete mode 100644 challenge-217/shimon-ben-avraham/raku/ch-1.md delete mode 100644 challenge-217/shimon-ben-avraham/raku/ch-1.raku create mode 100644 challenge-217/shimon-ben-avraham/raku/ch-2.md create mode 100644 challenge-217/shimon-ben-avraham/raku/ch-2.raku diff --git a/challenge-217/shimon-ben-avraham/raku/ch-1.md b/challenge-217/shimon-ben-avraham/raku/ch-1.md deleted file mode 100644 index e52e8592a8..0000000000 --- a/challenge-217/shimon-ben-avraham/raku/ch-1.md +++ /dev/null @@ -1,88 +0,0 @@ -```raku - -``` - -TITLE -===== - - - -Task 2: Max Number ------------------- - -SUBTITLE -======== - - - -Submitted by: Mohammad S Anwar ------------------------------- - -CHALLENGE -========= - - - -You are given a list of positive integers. Write a script to concatenate the integers to form the highest possible value. -------------------------------------------------------------------------------------------------------------------------- - -### Example 1: - - Input: @list = (1, 23) - - Output: 231 - -### Example 2: - - Input: @list = (10, 3, 2) - - Output: 3210 - -### Example 3: - - Input: @list = (31, 2, 4, 10) - - Output: 431210 - -### Example 4: - - Input: @list = (5, 11, 4, 1, 2) - - Output: 542111 - -### Example 5: - - Input: @list = (1, 10) - - Output: 110 - -SOLUTION -======== - - - -```raku - 1| use v6.*; - 2| - 3| sub max-number (@list where .all ~~ UInt) { - 4| my UInt $retval = 0; - 5| - 6| return $retval - 7| } - 8| - 9| multi MAIN (:$test! ) { - 10| use Test; - 11| - 12| my @tests = [ - 13| %{ input => (1, 23), output => 231, text => 'Example 1' }, - 14| %{ input => (10, 3, 2), output => 3210, text => 'Example 2' }, - 15| %{ input => (31, 2, 4, 10), output => 431210, text => 'Example 3' }, - 16| %{ input => (5, 11, 4, 1, 2), output => 542111, text => 'Example 4' }, - 17| %{ input => (1, 10), output => 110, text => 'Example 5' }, - 18| ]; - 19| - 20| for @tests { - 21| is max-number( . ), ., .; - 22| } # end of for @tests - 23| } # end of multi MAIN (:$test! ) -``` diff --git a/challenge-217/shimon-ben-avraham/raku/ch-1.raku b/challenge-217/shimon-ben-avraham/raku/ch-1.raku deleted file mode 100644 index 4329c14604..0000000000 --- a/challenge-217/shimon-ben-avraham/raku/ch-1.raku +++ /dev/null @@ -1,33 +0,0 @@ -#! /usr/bin/env raku - -# Perl Weekly Challenge 217-2 -# © 2023 Shimon Bollinger. All rights reserved. -# Last modified: Mon 15 May 2023 09:07:42 PM EDT -# Version 0.0.1 - -# always use the latest version of Raku -use v6.*; - -sub max-number (@list where .all ~~ UInt) { - my UInt $retval = 0; - - return $retval -} # end of sub max-number (UInt @list) - -multi MAIN (:$test! ) { - use Test; - - my @tests = [ - %{ input => (1, 23), output => 231, text => 'Example 1' }, - %{ input => (10, 3, 2), output => 3210, text => 'Example 2' }, - %{ input => (31, 2, 4, 10), output => 431210, text => 'Example 3' }, - %{ input => (5, 11, 4, 1, 2), output => 542111, text => 'Example 4' }, - %{ input => (1, 10), output => 110, text => 'Example 5' }, - ]; - - for @tests { - is max-number( . ), ., .; - } # end of for @tests -} # end of multi MAIN ( ) - - diff --git a/challenge-217/shimon-ben-avraham/raku/ch-2.md b/challenge-217/shimon-ben-avraham/raku/ch-2.md new file mode 100644 index 0000000000..4e1bf3c359 --- /dev/null +++ b/challenge-217/shimon-ben-avraham/raku/ch-2.md @@ -0,0 +1,94 @@ +TITLE +===== + + + +Task 2: Max Number +------------------ + +SUBTITLE +======== + + + +Submitted by: Mohammad S Anwar +------------------------------ + +CHALLENGE +========= + + + +You are given a list of positive integers. Write a script to concatenate the integers to form the highest possible value. +------------------------------------------------------------------------------------------------------------------------- + +### Example 1: + + Input: @list = (1, 23) + + Output: 231 + +### Example 2: + + Input: @list = (10, 3, 2) + + Output: 3210 + +### Example 3: + + Input: @list = (31, 2, 4, 10) + + Output: 431210 + +### Example 4: + + Input: @list = (5, 15, 4, 123, 2) + + Output: 542111 + +### Example 5: + + Input: @list = (1, 10) + + Output: 110 + +SOLUTION +======== + + + +```raku + 1| use v6.*; + 2| + 3| sub max-number-cmp (UInt:D $a, UInt:D $b --> Order:D) { + 4| my @a=$a.comb; + 5| my @b=$b.comb; + 6| my $l = max(+@a, +@b); + 7| for ^$l -> $i { + 8| my $a = @a[$i] // @a[$i-1]; + 9| my $b = @b[$i] // @b[$i-1]; + 10| return $b <=> $a unless $a == $b; + 11| } + 12| return Same; + 13| } + 14| + 15| sub max-number (@list where .all ~~ UInt --> UInt) { + 16| return @list.sort(&max-number-cmp).join.UInt; + 17| } + 18| + 19| multi MAIN (:$test!) { + 20| use Test; + 21| + 22| my @tests = [ + 23| %{ input => (1, 23), output => 231, text => 'Example 1' }, + 24| %{ input => (10, 3, 2), output => 3210, text => 'Example 2' }, + 25| %{ input => (31, 2, 4, 10), output => 431210, text => 'Example 3' }, + 26| %{ input => (5, 11, 4, 1, 2), output => 542111, text => 'Example 4' }, + 27| %{ input => (1, 10), output => 110, text => 'Example 5' }, + 28| ]; + 29| + 30| for @tests { + 31| is max-number( . ), ., .; + 32| } # end of for @tests + 33| } # end of multi MAIN (:$test! ) +``` diff --git a/challenge-217/shimon-ben-avraham/raku/ch-2.raku b/challenge-217/shimon-ben-avraham/raku/ch-2.raku new file mode 100644 index 0000000000..7beb6d55d3 --- /dev/null +++ b/challenge-217/shimon-ben-avraham/raku/ch-2.raku @@ -0,0 +1,51 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge 217-2 +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Sat 20 May 2023 11:16:24 PM EDT +# Version 0.0.1 + + +# always use the latest version of Raku +use v6.*; + +# We need a special comparison function. +# It will compare each digit in the two numbers. +# If one number has more digits than the other, +# the longer one always compares its remaining digits +# to the last digit of the shorter one. +sub max-number-cmp (UInt:D $a, UInt:D $b --> Order:D) { + my @a=$a.comb; + my @b=$b.comb; + my $l = max(+@a, +@b); + for ^$l -> $i { + my $a = @a[$i] // @a[$i-1]; + my $b = @b[$i] // @b[$i-1]; + return $b <=> $a unless $a == $b; + } + return Same; +} # end of my sub a (UInt:D $a, UInt:D $b --> Order:D) + +# Here's the solution to the challenge. It uses the comparison function above. +sub max-number (@list where .all ~~ UInt --> UInt) { + return @list.sort(&max-number-cmp).join.UInt; +} # end of sub max-number (@list where .all ~~ UInt --> UInt) + +# run `raku max-number.raku --test` to verify that all the examples work. +multi MAIN (:$test!) { + use Test; + + my @tests = [ + %{ input => (1, 23), output => 231, text => 'Example 1' }, + %{ input => (10, 3, 2), output => 3210, text => 'Example 2' }, + %{ input => (31, 2, 4, 10), output => 431210, text => 'Example 3' }, + %{ input => (5, 11, 4, 1, 2), output => 542111, text => 'Example 4' }, + %{ input => (1, 10), output => 110, text => 'Example 5' }, + ]; + + for @tests { + is max-number( . ), ., .; + } # end of for @tests +} # end of multi MAIN (:$test! ) + + diff --git a/challenge-217/shimon-ben-avraham/raku/max-number.raku b/challenge-217/shimon-ben-avraham/raku/max-number.raku index c0f67173f6..8d1c3c3bce 100755 --- a/challenge-217/shimon-ben-avraham/raku/max-number.raku +++ b/challenge-217/shimon-ben-avraham/raku/max-number.raku @@ -2,7 +2,7 @@ # Perl Weekly Challenge 217-2 # © 2023 Shimon Bollinger. All rights reserved. -# Last modified: Mon 15 May 2023 09:13:54 PM EDT +# Last modified: Sat 20 May 2023 11:16:24 PM EDT # Version 0.0.1 =begin pod @@ -37,7 +37,7 @@ Write a script to concatenate the integers to form the highest possible value. =head3 Example 4: - Input: @list = (5, 11, 4, 1, 2) + Input: @list = (5, 15, 4, 123, 2) Output: 542111 @@ -54,13 +54,30 @@ Write a script to concatenate the integers to form the highest possible value. # always use the latest version of Raku use v6.*; -sub max-number (@list where .all ~~ UInt) { - my UInt $retval = 0; - - return $retval -} # end of sub max-number (UInt @list) - -multi MAIN (:$test! ) { +# We need a special comparison function. +# It will compare each digit in the two numbers. +# If one number has more digits than the other, +# the longer one always compares its remaining digits +# to the last digit of the shorter one. +sub max-number-cmp (UInt:D $a, UInt:D $b --> Order:D) { + my @a=$a.comb; + my @b=$b.comb; + my $l = max(+@a, +@b); + for ^$l -> $i { + my $a = @a[$i] // @a[$i-1]; + my $b = @b[$i] // @b[$i-1]; + return $b <=> $a unless $a == $b; + } + return Same; +} # end of my sub a (UInt:D $a, UInt:D $b --> Order:D) + +# Here's the solution to the challenge. It uses the comparison function above. +sub max-number (@list where .all ~~ UInt --> UInt) { + return @list.sort(&max-number-cmp).join.UInt; +} # end of sub max-number (@list where .all ~~ UInt --> UInt) + +# run `raku max-number.raku --test` to verify that all the examples work. +multi MAIN (:$test!) { use Test; my @tests = [ -- cgit From 760b6c66901fcd6f378076579375d01c232309f8 Mon Sep 17 00:00:00 2001 From: Avery Adams Date: Sun, 21 May 2023 16:16:38 +1200 Subject: Solution for challenge 1 for Avery Adams for week 217 --- challenge-217/avery-adams/blogs.txt | 2 ++ challenge-217/avery-adams/perl/ch-1.pl | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 challenge-217/avery-adams/blogs.txt create mode 100644 challenge-217/avery-adams/perl/ch-1.pl diff --git a/challenge-217/avery-adams/blogs.txt b/challenge-217/avery-adams/blogs.txt new file mode 100644 index 0000000000..de43b255fe --- /dev/null +++ b/challenge-217/avery-adams/blogs.txt @@ -0,0 +1,2 @@ +https://blogs.perl.org/users/oldtechaa/2023/05/perl-weekly-challenge-217---flattening-the-matrix.html +https://dev.to/oldtechaa/perl-weekly-challenge-217-flattening-the-matrix-4mk3 diff --git a/challenge-217/avery-adams/perl/ch-1.pl b/challenge-217/avery-adams/perl/ch-1.pl new file mode 100644 index 0000000000..2bb48fd55f --- /dev/null +++ b/challenge-217/avery-adams/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl + +use strict; +use v5.24; + +my @matrix1 = ([3, 1, 2], [5, 2, 4], [0, 1, 3]); +my @list1; + +foreach (@matrix1) {map {push @list1, $_} @{$_}} +@list1 = sort @list1; +say $list1[2]; + +my @matrix2 = ([2, 1], [4, 5]); +my @list2; + +foreach (@matrix2) {map {push @list2, $_} @{$_}} +@list2 = sort @list2; +say $list2[2]; + +my @matrix3 = ([1, 0, 3], [0, 0, 0], [1, 2, 1]); +my @list3; + +foreach (@matrix3) {map {push @list3, $_} @{$_}} +@list3 = sort @list3; +say $list3[2]; -- cgit From 0b993c6051fdc996c88df66264653351958af75c Mon Sep 17 00:00:00 2001 From: simon-dueck <126712673+simon-dueck@users.noreply.github.com> Date: Sun, 21 May 2023 00:58:17 -0500 Subject: Added solution for challenge #1 in F# --- challenge-217/simon-dueck/README | 1 + challenge-217/simon-dueck/fsharp/ch-1.fsx | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 challenge-217/simon-dueck/fsharp/ch-1.fsx diff --git a/challenge-217/simon-dueck/README b/challenge-217/simon-dueck/README index e69de29bb2..1356b2f16f 100644 --- a/challenge-217/simon-dueck/README +++ b/challenge-217/simon-dueck/README @@ -0,0 +1 @@ +Solution by Simon Dueck \ No newline at end of file diff --git a/challenge-217/simon-dueck/fsharp/ch-1.fsx b/challenge-217/simon-dueck/fsharp/ch-1.fsx new file mode 100644 index 0000000000..2b64595b10 --- /dev/null +++ b/challenge-217/simon-dueck/fsharp/ch-1.fsx @@ -0,0 +1,14 @@ +(* + You are given a n x n matrix where n >= 2. + Write a script to find 3rd smallest element in the sorted matrix. +*) + +let sort_matrix (matrix: int list list): int = + let rec loop matrix = + match matrix with + | x::xs -> x @ loop xs + | [] -> [] + in let sorted = loop matrix |> List.sort + sorted[2] + +printfn $"{sort_matrix [[1;2;3];[2;3;4];[6;7;8];]}" \ No newline at end of file -- cgit From 0d6822c17fa4b88bcadee5c2791929305fae7e5a Mon Sep 17 00:00:00 2001 From: simon-dueck <126712673+simon-dueck@users.noreply.github.com> Date: Sun, 21 May 2023 00:58:56 -0500 Subject: Added solution for challenge #1 in F# --- challenge-216/simon-dueck/README | 1 + challenge-216/simon-dueck/fsharp/ch-1.fsx | 36 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 challenge-216/simon-dueck/fsharp/ch-1.fsx diff --git a/challenge-216/simon-dueck/README b/challenge-216/simon-dueck/README index e69de29bb2..1356b2f16f 100644 --- a/challenge-216/simon-dueck/README +++ b/challenge-216/simon-dueck/README @@ -0,0 +1 @@ +Solution by Simon Dueck \ No newline at end of file diff --git a/challenge-216/simon-dueck/fsharp/ch-1.fsx b/challenge-216/simon-dueck/fsharp/ch-1.fsx new file mode 100644 index 0000000000..6a777fa28d --- /dev/null +++ b/challenge-216/simon-dueck/fsharp/ch-1.fsx @@ -0,0 +1,36 @@ +(* + You are given a list of words and a random registration number. + Write a script to find all the words in the given list that has every letter in the given registration number. +*) + +let word_contains_letter (word: string) (l: char): bool = + if l >= 'a' && l <= 'z' then + List.contains (char ((int l) - 32)) (Seq.toList word) // to uppercase + elif l >= 'A' && l <= 'Z' then + List.contains l (Seq.toList word) + else true // l is not a letter + +let rec word_contains_all_letters (word: string) (l: char list): bool = + match l with + | x::xs when word_contains_letter word x -> word_contains_all_letters word xs + | [] -> true + | _ -> false + +let contains_all (words: string list) (plate: string): string list = + let rec loop (words:string list) (plate: char list) = + match words with + | word::xs when word_contains_all_letters (word.ToUpper()) plate -> word :: (loop xs plate) + | _::xs -> loop xs plate + | [] -> [] + loop words (plate |> Seq.toList) + +let words_a = ["abc"; "abcd"; "bcd"] +let plate_a = "AB1 2CD" +let words_b = ["job"; "james"; "bjorg"] +let plate_b = "007 JB" +let words_c = ["crack"; "road"; "rac"] +let plate_c = "C7 RA2" + +printfn "%A %s -> %A" words_a plate_a (contains_all words_a plate_a) +printfn "%A %s -> %A" words_b plate_b (contains_all words_b plate_b) +printfn "%A %s -> %A" words_c plate_c (contains_all words_c plate_c) -- cgit From cedfb3790fc11f7b41651249df99e83e709d471f Mon Sep 17 00:00:00 2001 From: simon-dueck <126712673+simon-dueck@users.noreply.github.com> Date: Sun, 21 May 2023 00:59:39 -0500 Subject: Added solutions in F# --- challenge-215/Week 215/simon-dueck/README | 1 + challenge-215/Week 215/simon-dueck/fsharp/ch-1.fsx | 27 ++++++++++++++++++++++ challenge-215/Week 215/simon-dueck/fsharp/ch-2.fsx | 26 +++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 challenge-215/Week 215/simon-dueck/README create mode 100644 challenge-215/Week 215/simon-dueck/fsharp/ch-1.fsx create mode 100644 challenge-215/Week 215/simon-dueck/fsharp/ch-2.fsx diff --git a/challenge-215/Week 215/simon-dueck/README b/challenge-215/Week 215/simon-dueck/README new file mode 100644 index 0000000000..1356b2f16f --- /dev/null +++ b/challenge-215/Week 215/simon-dueck/README @@ -0,0 +1 @@ +Solution by Simon Dueck \ No newline at end of file diff --git a/challenge-215/Week 215/simon-dueck/fsharp/ch-1.fsx b/challenge-215/Week 215/simon-dueck/fsharp/ch-1.fsx new file mode 100644 index 0000000000..1367346b2f --- /dev/null +++ b/challenge-215/Week 215/simon-dueck/fsharp/ch-1.fsx @@ -0,0 +1,27 @@ +(* + You are given a list of words (alphabetic characters only) of same size. + + Write a script to remove all words not sorted alphabetically and print + the number of words in the list that are not alphabetically sorted. +*) + +let word_is_not_sorted (word: string): bool = + let rec loop (letters: char list): bool = + match letters with + | x::y::xs when x < y -> loop (y::xs) + | [x] -> false + | _ -> true + in loop (Seq.toList word) + +let count_unsorted (words: string list): int = + List.filter word_is_not_sorted words |> List.length + + +let words_a = ["abc"; "xyz"; "tsu"] +printfn $"{count_unsorted words_a}" + +let words_b = ["rat"; "cab"; "dad"] +printfn $"{count_unsorted words_b}" + +let words_c = ["x"; "y"; "z"] +printfn $"{count_unsorted words_c}" diff --git a/challenge-215/Week 215/simon-dueck/fsharp/ch-2.fsx b/challenge-215/Week 215/simon-dueck/fsharp/ch-2.fsx new file mode 100644 index 0000000000..4c3ca66711 --- /dev/null +++ b/challenge-215/Week 215/simon-dueck/fsharp/ch-2.fsx @@ -0,0 +1,26 @@ +(* + You are given a list of numbers having just 0 and 1. You are also given placement count (>=1). + + Write a script to find out if it is possible to replace 0 with 1 in the given list. + The only condition is that you can only replace when there is no 1 on either side. + Print 1 if it is possible otherwise 0. +*) + +let rec replace_zero (arr: int list) (count: int): int = + match arr with + | _ when count = 0 -> 1 // all are placed + | 0::0::xs -> replace_zero xs (count - 1) // can replace; skip next index as it will not be replaceable + | [0] -> replace_zero [] (count - 1) // end of the list is zero and replacable + | 1::0::xs -> replace_zero xs count // current and next index are not replaceable + | _::xs -> replace_zero xs count // current index is not replaceable but next potentially is or is a one + | _ -> 0 // end of list with count not reaching zero + +let numbers_a = [1; 0; 0; 0; 1] +let numbers_b = [1; 0; 0; 0; 1] +let numbers_c = [1; 0; 0; 0; 0; 0; 0; 0; 1] +let numbers_d = [1; 0; 0; 0; 1; 0; 0; 1] + +printfn $"{replace_zero numbers_a 1}" +printfn $"{replace_zero numbers_b 2}" +printfn $"{replace_zero numbers_c 3}" +printfn $"{replace_zero numbers_d 2}" \ No newline at end of file -- cgit From 5afb4e5aa13acc773018fdbb7a694a36a6d24fb9 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Sat, 20 May 2023 23:28:49 -0700 Subject: Ch217: implement Tasks 1 & 2 in Clojure --- challenge-217/tyler-wardhaugh/clojure/README.md | 16 ++++++++-------- challenge-217/tyler-wardhaugh/clojure/bb.edn | 2 +- challenge-217/tyler-wardhaugh/clojure/build.clj | 19 +++++++++++++++++++ challenge-217/tyler-wardhaugh/clojure/deps.edn | 4 ++-- challenge-217/tyler-wardhaugh/clojure/src/c217/t1.clj | 17 +++++++++++++++++ challenge-217/tyler-wardhaugh/clojure/src/c217/t2.clj | 18 ++++++++++++++++++ .../tyler-wardhaugh/clojure/test/c217/t1_test.clj | 9 +++++++++ .../tyler-wardhaugh/clojure/test/c217/t2_test.clj | 11 +++++++++++ 8 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 challenge-217/tyler-wardhaugh/clojure/build.clj create mode 100644 challenge-217/tyler-wardhaugh/clojure/src/c217/t1.clj create mode 100644 challenge-217/tyler-wardhaugh/clojure/src/c217/t2.clj create mode 100644 challenge-217/tyler-wardhaugh/clojure/test/c217/t1_test.clj create mode 100644 challenge-217/tyler-wardhaugh/clojure/test/c217/t2_test.clj diff --git a/challenge-217/tyler-wardhaugh/clojure/README.md b/challenge-217/tyler-wardhaugh/clojure/README.md index 530c7479c2..edcc7f57c7 100644 --- a/challenge-217/tyler-wardhaugh/clojure/README.md +++ b/challenge-217/tyler-wardhaugh/clojure/README.md @@ -1,6 +1,6 @@ -# c216 +# c217 -The Weekly Challenge — #216 — Tyler Wardhaugh +The Weekly Challenge — #217 — Tyler Wardhaugh ## Usage @@ -8,21 +8,21 @@ Clojure ([installation instructions](https://clojure.org/guides/getting_started# Run Task #1: - $ clojure -M:t1 REG COLL + $ clojure -M:t1 COLL # ... or ... - $ bb run task-1 REG COLL + $ bb run task-1 COLL # Alternatively, to run it via Babashka: - $ bb run task-1-bb REG COLL + $ bb run task-1-bb COLL Run Task #2: - $ clojure -M:t2 WORD COLL + $ clojure -M:t2 COLL # ... or ... - $ bb run task-2 WORD COLL + $ bb run task-2 COLL # Alternatively, to run it via Babashka: - $ bb run task-2-bb WORD COLL + $ bb run task-2-bb COLL Run the project's tests (which are samples from the task descriptions): diff --git a/challenge-217/tyler-wardhaugh/clojure/bb.edn b/challenge-217/tyler-wardhaugh/clojure/bb.edn index f62b373e9f..e9f441cb63 100644 --- a/challenge-217/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-217/tyler-wardhaugh/clojure/bb.edn @@ -1,6 +1,6 @@ { :paths ["src" "resources"] - :deps {c216/c216 {:local/root "."}} + :deps {c217/c217 {:local/root "."}} :tasks { diff --git a/challenge-217/tyler-wardhaugh/clojure/build.clj b/challenge-217/tyler-wardhaugh/clojure/build.clj new file mode 100644 index 0000000000..18baef6c0c --- /dev/null +++ b/challenge-217/tyler-wardhaugh/clojure/build.clj @@ -0,0 +1,19 @@ +(ns build + (:refer-clojure :exclude [test]) + (:require [org.corfield.build :as bb])) + +(def lib 'net.clojars.c217/c217) +(def version "0.1.0-SNAPSHOT") +(def main 'c217.c217) + +(defn test "Run the tests." [opts] + (bb/run-tests opts)) + +(def clean bb/clean) + +(defn ci "Run the CI pipeline of tests (and build the uberjar)." [opts] + (-> opts + (assoc :lib lib :version version :main main) + (bb/run-tests) + (bb/clean) + (bb/uber))) diff --git a/challenge-217/tyler-wardhaugh/clojure/deps.edn b/challenge-217/tyler-wardhaugh/clojure/deps.edn index 0459e1f12d..9c7a50a024 100644 --- a/challenge-217/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-217/tyler-wardhaugh/clojure/deps.edn @@ -1,8 +1,8 @@ {:paths ["src" "resources"] :deps {org.clojure/clojure {:mvn/version "1.11.1"}} :aliases - {:t1 {:main-opts ["-m" "c216.t1"]} - :t2 {:main-opts ["-m" "c216.t2"]} + {:t1 {:main-opts ["-m" "c217.t1"]} + :t2 {:main-opts ["-m" "c217.t2"]} :build {:deps {io.github.seancorfield/build-clj {:git/tag "v0.8.3" :git/sha "7ac1f8d" ;; since we're building an app uberjar, we do not diff --git a/challenge-217/tyler-wardhaugh/clojure/src/c217/t1.clj b/challenge-217/tyler-wardhaugh/clojure/src/c217/t1.clj new file mode 100644 index 0000000000..6eb9e7e85d --- /dev/null +++ b/challenge-217/tyler-wardhaugh/clojure/src/c217/t1.clj @@ -0,0 +1,17 @@ +(ns c217.t1 + (:require + [clojure.edn :as edn])) + +(def DEFAULT-INPUT [[[3 1 2] [5 2 4] [0 1 3]]]) + +(defn third-smallest-of-sorted-matrix + [coll] + (let [sorted (->> coll (mapcat identity) sort)] + (nth sorted 2))) + +(defn -main + "Run Task 1 with a given input COLL, defaulting to the first example from the + task description." + [& args] + (let [[coll] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (println (third-smallest-of-sorted-matrix coll)))) diff --git a/challenge-217/tyler-wardhaugh/clojure/src/c217/t2.clj b/challenge-217/tyler-wardhaugh/clojure/src/c217/t2.clj new file mode 100644 index 0000000000..ed72f558b2 --- /dev/null +++ b/challenge-217/tyler-wardhaugh/clojure/src/c217/t2.clj @@ -0,0 +1,18 @@ +(ns c217.t2 + (:require + [clojure.edn :as edn] + [clojure.string :as str])) + +(def DEFAULT-INPUT [[1 23]]) + +(defn max-number + [coll] + (let [cmp (fn [a b] (compare (str b a) (str a b)))] + (->> coll (sort cmp) str/join parse-long))) + +(defn -main + "Run Task 2 with a given input COLL, defaulting to the first example from the + task description." + [& args] + (let [[coll] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (println (max-number coll)))) diff --git a/challenge-217/tyler-wardhaugh/clojure/test/c217/t1_test.clj b/challenge-217/tyler-wardhaugh/clojure/test/c217/t1_test.clj new file mode 100644 index 0000000000..e95fe74789 --- /dev/null +++ b/challenge-217/tyler-wardhaugh/clojure/test/c217/t1_test.clj @@ -0,0 +1,9 @@ +(ns c217.t1-test + (:require [clojure.test :refer [deftest is testing]] + [c217.t1 :refer [third-smallest-of-sorted-matrix]])) + +(deftest task-1 + (testing "Task 1 produces the correct results from examples in the description" + (is (= 1 (third-smallest-of-sorted-matrix [[3 1 2] [5 2 4] [0 1 3]]))) + (is (= 4 (third-smallest-of-sorted-matrix [[2 1] [4 5]]))) + (is (= 0 (third-smallest-of-sorted-matrix [[1 0 3] [0 0 0] [1 2 1]]))))) diff --git a/challenge-217/tyler-wardhaugh/clojure/test/c217/t2_test.clj b/challenge-217/tyler-wardhaugh/clojure/test/c217/t2_test.clj new file mode 100644 index 0000000000..705d631493 --- /dev/null +++ b/challenge-217/tyler-wardhaugh/clojure/test/c217/t2_test.clj @@ -0,0 +1,11 @@ +(ns c217.t2-test + (:require [clojure.test :refer [deftest is testing]] + [c217.t2 :refer [max-number]])) + +(deftest task-2 + (testing "Task 2 produces the correct results from examples in the description" + (is (= 231 (max-number [1 23]))) + (is (= 3210 (max-number [10 3 2]))) + (is (= 431210 (max-number [31 2 4 10]))) + (is (= 542111 (max-number [5 11 4 1 2]))) + (is (= 110 (max-number [1 10]))))) -- cgit From adb843bbec5505e90c578ef65f4d6ff18b96609c Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Sun, 21 May 2023 10:14:00 +0100 Subject: RogerBW blog post for challenge no. 217 --- challenge-217/roger-bell-west/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-217/roger-bell-west/blog.txt diff --git a/challenge-217/roger-bell-west/blog.txt b/challenge-217/roger-bell-west/blog.txt new file mode 100644 index 0000000000..fd064890f3 --- /dev/null +++ b/challenge-217/roger-bell-west/blog.txt @@ -0,0 +1 @@ +https://blog.firedrake.org/archive/2023/05/The_Weekly_Challenge_217__Sorted_Max.html -- cgit From 5180e8021a174f065f31630be9160c6b6453b2dd Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Sun, 21 May 2023 12:38:05 +0200 Subject: Solution to task 1 --- challenge-217/jo-37/perl/ch-1.pl | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 challenge-217/jo-37/perl/ch-1.pl diff --git a/challenge-217/jo-37/perl/ch-1.pl b/challenge-217/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..4c43a4b8ca --- /dev/null +++ b/challenge-217/jo-37/perl/ch-1.pl @@ -0,0 +1,65 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; +use PDL::NiceSlice; +use experimental 'signatures'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <flat, my $min = zeroes indx, 3; + $p->flat->($min(-1))->sclr; +} + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is third_smallest(pdl([3, 1, 2], [5, 2, 4], [0, 1, 3])), 1, + 'example 1'; + + is third_smallest(pdl([2, 1], [4, 5])), 4, + 'example 2'; + + is third_smallest(pdl([1, 0, 3], [0, 0, 0], [1, 2, 1])), 0, + 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} -- cgit From 4eebc07786114e281c00a6002eb0183daa35e0f4 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sun, 21 May 2023 13:26:30 +0200 Subject: feat(challenge-071/lubos-kolouch/perl,python/ch-1,-ch-2): Challenge 071 LK Perl Python --- challenge-071/lubos-kolouch/perl/ch-1.pl | 23 +++++++++ challenge-071/lubos-kolouch/perl/ch-2.pl | 77 ++++++++++++++++++++++++++++++ challenge-071/lubos-kolouch/python/ch-1.py | 26 ++++++++++ challenge-071/lubos-kolouch/python/ch-2.py | 52 ++++++++++++++++++++ 4 files changed, 178 insertions(+) create mode 100644 challenge-071/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-071/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-071/lubos-kolouch/python/ch-1.py create mode 100644 challenge-071/lubos-kolouch/python/ch-2.py diff --git a/challenge-071/lubos-kolouch/perl/ch-1.pl b/challenge-071/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..5499ab959b --- /dev/null +++ b/challenge-071/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub find_peaks { + my @arr = @_; + my $n = scalar @arr; + + my @peaks; + push @peaks, $arr[0] if $arr[0] > $arr[1]; + + for my $i ( 1 .. $n - 2 ) { + push @peaks, $arr[$i] if $arr[$i] > $arr[ $i - 1 ] and $arr[$i] > $arr[ $i + 1 ]; + } + + push @peaks, $arr[-1] if $arr[-1] > $arr[-2]; + + return @peaks; +} + +# Tests +print join( ", ", find_peaks( 18, 45, 38, 25, 10, 7, 21, 6, 28, 48 ) ), "\n"; # 48, 45, 21 +print join( ", ", find_peaks( 47, 11, 32, 8, 1, 9, 39, 14, 36, 23 ) ), "\n"; # 47, 32, 39, 36 diff --git a/challenge-071/lubos-kolouch/perl/ch-2.pl b/challenge-071/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..1f8970ad3b --- /dev/null +++ b/challenge-071/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,77 @@ +#!/usr/bin/perl +use strict; +use warnings; + +package Node; + +sub new { + my ( $class, $data ) = @_; + my $self = { + data => $data, + next => undef + }; + return bless $self, $class; +} + +package LinkedList; + +sub new { + my ($class) = @_; + my $self = { head => undef }; + return bless $self, $class; +} + +sub append { + my ( $self, $data ) = @_; + my $new_node = Node->new($data); + if ( !$self->{head} ) { + $self->{head} = $new_node; + } + else { + my $cur = $self->{head}; + $cur = $cur->{next} while $cur->{next}; + $cur->{next} = $new_node; + } +} + +sub remove_from_end { + my ( $self, $n ) = @_; + my $size = 0; + my $cur = $self->{head}; + while ($cur) { + $size++; + $cur = $cur->{next}; + } + if ( $n >= $size ) { + $self->{head} = $self->{head}->{next}; + } + else { + $cur = $self->{head}; + for ( 1 .. $size - $n - 1 ) { + $cur = $cur->{next}; + } + $cur->{next} = $cur->{next}->{next} if $cur->{next}; + } +} + +sub print { + my ($self) = @_; + my @values; + my $cur = $self->{head}; + while ($cur) { + push @values, $cur->{data}; + $cur = $cur->{next}; + } + print join( " -> ", @values ), "\n"; +} + +# Tests +my $ll = LinkedList->new(); +for my $i ( 1 .. 5 ) { + $ll->append($i); +} + +for my $i ( 1 .. 6 ) { + $ll->remove_from_end($i); + $ll->print(); +} diff --git a/challenge-071/lubos-kolouch/python/ch-1.py b/challenge-071/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..7a92d52f8b --- /dev/null +++ b/challenge-071/lubos-kolouch/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def find_peaks(arr: List[int]) -> List[int]: + n = len(arr) + # Initialize peaks list with the first element if it's bigger than the second + peaks = [arr[0]] if arr[0] > arr[1] else [] + + # Iterate over the array checking for peaks + for i in range(1, n-1): + if arr[i] > arr[i-1] and arr[i] > arr[i+1]: + peaks.append(arr[i]) + + # Add the last element if it's bigger than the penultimate + if arr[-1] > arr[-2]: + peaks.append(arr[-1]) + + return peaks + + +# Tests +print(find_peaks([18, 45, 38, 25, 10, 7, 21, 6, 28, 48])) # [48, 45, 21] +print(find_peaks([47, 11, 32, 8, 1, 9, 39, 14, 36, 23])) # [47, 32, 39, 36] diff --git a/challenge-071/lubos-kolouch/python/ch-2.py b/challenge-071/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..5662d38226 --- /dev/null +++ b/challenge-071/lubos-kolouch/python/ch-2.py @@ -0,0 +1,52 @@ +class Node: + def __init__(self, data=None): + self.data = data + self.next = None + + +class LinkedList: + def __init__(self): + self.head = None + + def append(self, data): + if not self.head: + self.head = Node(data) + else: + cur = self.head + while cur.next: + cur = cur.next + cur.next = Node(data) + + def remove_from_end(self, n): + if self.head is None: + return + size = 0 + cur = self.head + while cur: + size += 1 + cur = cur.next + if n >= size: + self.head = self.head.next if self.head.next else None + else: + cur = self.head + for _ in range(size - n - 1): + cur = cur.next + cur.next = cur.next.next if cur.next and cur.next.next else None + + def __str__(self): + values = [] + cur = self.head + while cur: + values.append(str(cur.data)) + cur = cur.next + return ' -> '.join(values) + + +# Tests +ll = LinkedList() +for i in range(1, 6): + ll.append(i) + +for i in range(1, 7): + ll.remove_from_end(i) + print(ll) -- cgit From 4e968a0a6ea4135e3ade5dedb901f24aca3ed823 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Sun, 21 May 2023 12:38:18 +0200 Subject: Solution to task 2 --- challenge-217/jo-37/perl/ch-2.pl | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 challenge-217/jo-37/perl/ch-2.pl diff --git a/challenge-217/jo-37/perl/ch-2.pl b/challenge-217/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..f6145ffd7b --- /dev/null +++ b/challenge-217/jo-37/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die < $a . $b} @_; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is maximum_number(1, 23), 231, 'example 1'; + is maximum_number(10, 3, 2), 3210, 'example 2'; + is maximum_number(31, 2, 4, 10), 431210, 'example 3'; + is maximum_number(5, 11, 4, 1, 2), 542111, 'example 4'; + is maximum_number(1, 10), 110, 'example 5'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} -- cgit From 697ccec10af0587d98a25523f958504027135ead Mon Sep 17 00:00:00 2001 From: PerlMonk-Athanasius Date: Mon, 22 May 2023 02:34:49 +1000 Subject: Perl & Raku solutions to Task 1 for Week 217 --- challenge-217/athanasius/perl/ch-1.pl | 261 ++++++++++++++++++++++++++++++++ challenge-217/athanasius/raku/ch-1.raku | 255 +++++++++++++++++++++++++++++++ 2 files changed, 516 insertions(+) create mode 100644 challenge-217/athanasius/perl/ch-1.pl create mode 100644 challenge-217/athanasius/raku/ch-1.raku diff --git a/challenge-217/athanasius/perl/ch-1.pl b/challenge-217/athanasius/perl/ch-1.pl new file mode 100644 index 0000000000..28ba086c1b --- /dev/null +++ b/challenge-217/athanasius/perl/ch-1.pl @@ -0,0 +1,261 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 217 +========================= + +TASK #1 +------- +*Sorted Matrix* + +Submitted by: Mohammad S Anwar + +You are given a n x n matrix where n >= 2. + +Write a script to find 3rd smallest element in the sorted matrix. + +Example 1 + + Input: @matrix = ([3, 1, 2], [5, 2, 4], [0, 1, 3]) + Output: 1 + + The sorted list of the given matrix: 0, 1, 1, 2, 2, 3, 3, 4, 5. + The 3rd smallest of the sorted list is 1. + +Example 2 + + Input: @matrix = ([2, 1], [4, 5]) + Output: 4 + + The sorted list of the given matrix: 1, 2, 4, 5. + The 3rd smallest of the sorted list is 4. + +Example 3 + + Input: @matrix = ([1, 0, 3], [0, 0, 0], [1, 2, 1]) + Output: 0 + + The sorted list of the given matrix: 0, 0, 0, 0, 1, 1, 1, 2, 3. + The 3rd smallest of the sorted list is 0. + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2023 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=comment + +Interface +--------- +1. If no command-line arguments are given, the test suite is run. Otherwise: +2. If $VERBOSE is set to a true value (the default), the output is followed by + an explanation of the result. + +Assumption +---------- +Matrix elements are integers. + +=cut +#=============================================================================== + +use strict; +use warnings; +use Const::Fast; +use Regexp::Common qw( number ); +use Test::More; + +const my $VERBOSE => 1; +const my $USAGE => +"Usage: + perl $0 + perl $0 + + String representation of an n x n integer matrix where n >= 2 +"; + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + $| = 1; + print "\nChallenge 217, Task #1: Sorted Matrix (Perl)\n\n"; +} + +#=============================================================================== +MAIN: +#=============================================================================== +{ + my $args = scalar @ARGV; + + if ($args == 0) + { + run_tests(); + } + elsif ($args == 1) + { + my $matrix = parse_matrix_string( $ARGV[ 0 ] ); + + printf "Input: \@matrix = %s\n", format_matrix( $matrix ); + + my @elements; + my $third_smallest = find_third_smallest( $matrix, \@elements ); + + printf "Output: %d\n", $third_smallest; + + if ($VERBOSE) + { + printf "\nThe sorted list of the given matrix: %s\n", + join ', ', @elements; + + my $padding = length( $elements[ 0 ] ) + + length( $elements[ 1 ] ) + 4; + + printf "The third-smallest element: %s%s\n", + ' ' x $padding, '^' x length $third_smallest; + } + } + else + { + error( "Expected 0 or 1 command-line arguments, found $args" ); + } +} + +#------------------------------------------------------------------------------- +sub find_third_smallest +#------------------------------------------------------------------------------- +{ + my ($matrix, $elements_ref) = @_; + my @elements; + + push @elements, @$_ for @$matrix; + + @elements = sort { $a <=> $b } @elements; + + scalar @elements >= 3 or error( 'Matrix too small' ); + + @$elements_ref = @elements if $elements_ref; + + return $elements[ 2 ]; +} + +#------------------------------------------------------------------------------- +sub parse_matrix_string +#------------------------------------------------------------------------------- +{ + my ($matrix) = @_; + + $matrix =~ / ^ \s* \( (.*?) \s* \) \s* $ /x + or error( 'Invalid input string' ); + + my $body = $1; + my @matrix; + + while ($body =~ / \[ (.+?) \] /gx) + { + my $row = $1; + my @elems = split / , \s* /x, $row; + + for my $elem (@elems) + { + $elem =~ s/ ^ \s+ //x; + $elem =~ s/ \s+ $ //x; + $elem =~ m/ ^ $RE{num}{int} $ /x + or error( qq["$elem" is not a valid integer] ); + } + + push @matrix, [ @elems ]; + + } + + validate_matrix( \@matrix ); + + return \@matrix; +} + +#------------------------------------------------------------------------------- +sub validate_matrix +#------------------------------------------------------------------------------- +{ + my ($matrix) = @_; + + my $rows = scalar @$matrix; + my $n = scalar @{ $matrix->[ 0 ] }; + + $rows >= 2 or error( 'Too few rows in matrix' ); + $rows == $n or error( 'Matrix is not square' ); + + for my $i (1 .. $#$matrix) + { + my $m = scalar @{ $matrix->[ $i ] }; + my $j = $i + 1; + + $m == $n or error( "In matrix row $j: expected $n elements, found $m" ); + } +} + +#------------------------------------------------------------------------------- +sub format_matrix +#------------------------------------------------------------------------------- +{ + my ($matrix) = @_; + my $matrix_str = '('; + my @row_strs; + + for my $elems (@$matrix) + { + push @row_strs, '[' . join( ', ', @$elems ) . ']'; + } + + $matrix_str .= join( ', ', @row_strs ) . ')'; + + return $matrix_str; +} + +#------------------------------------------------------------------------------- +sub run_tests +#------------------------------------------------------------------------------- +{ + print "Running the test suite\n"; + + while (my $line = ) + { + chomp $line; + + my ($test_name, $matrix_str, $expected) = split / \| /x, $line; + + for ($test_name, $matrix_str, $expected) # Trim whitespace + { + s/ ^ \s+ //x; + s/ \s+ $ //x; + } + + my @matrix = parse_matrix_string( $matrix_str ); + my $third_smallest = find_third_smallest( @matrix ); + + is $third_smallest, $expected, $test_name; + } + + done_testing; +} + +#------------------------------------------------------------------------------- +sub error +#------------------------------------------------------------------------------- +{ + my ($message) = @_; + + die "ERROR: $message\n$USAGE"; +} + +################################################################################ + +__DATA__ +Example 1|([3, 1, 2], [5, 2, 4], [ 0, 1, 3])| 1 +Example 2|([2, 1], [4, 5]) | 4 +Example 3|([1, 0, 3], [0, 0, 0], [ 1, 2, 1])| 0 +Negatives|([0, -1, -2], [0, -1, -3], [-3, 1, 2])|-2 diff --git a/challenge-217/athanasius/raku/ch-1.raku b/challenge-217/athanasius/raku/ch-1.raku new file mode 100644 index 0000000000..43e9d9e547 --- /dev/null +++ b/challenge-217/athanasius/raku/ch-1.raku @@ -0,0 +1,255 @@ +use v6d; + +################################################################################ +=begin comment + +Perl Weekly Challenge 217 +========================= + +TASK #1 +------- +*Sorted Matrix* + +Submitted by: Mohammad S Anwar + +You are given a n x n matrix where n >= 2. + +Write a script to find 3rd smallest element in the sorted matrix. + +Example 1 + + Input: @matrix = ([3, 1, 2], [5, 2, 4], [0, 1, 3]) + Output: 1 + + The sorted list of the given matrix: 0, 1, 1, 2, 2, 3, 3, 4, 5. + The 3rd smallest of the sorted list is 1. + +Example 2 + + Input: @matrix = ([2, 1], [4, 5]) + Output: 4 + + The sorted list of the given matrix: 1, 2, 4, 5. + The 3rd smallest of the sorted list is 4. + +Example 3 + + Input: @matrix = ([1, 0, 3], [0, 0, 0], [1, 2, 1]) + Output: 0 + + The sorted list of the given matrix: 0, 0, 0, 0, 1, 1, 1, 2, 3. + The 3rd smallest of the sorted list is 0. + +=end comment +################################################################################ + +#--------------------------------------# +# Copyright © 2023 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=begin comment + +Interface +--------- +1. If no command-line arguments are given, the test suite is run. Otherwise: +2. If VERBOSE is set to True (the default), the output is followed by an explan- + ation of the result + +Assumption +---------- +Matrix elements are integers. + +=end comment +#=============================================================================== + +use Test; + +subset Result of List where (Int, Array[Int]); + +my Bool constant VERBOSE = True; + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + "\nChallenge 217, Task #1: Sorted Matrix (Raku)\n".put; +} + +#=============================================================================== +multi sub MAIN +( + #| String representation of an n x n integer matrix where n >= 2 + + Str:D $matrix +) +#=============================================================================== +{ + my Array[Int] @matrix = parse-matrix-string( $matrix ); + + "Input: \@matrix = %s\n".printf: format-matrix( @matrix ); + + my Result $result = find-third-smallest( @matrix ); + + "Output: %d\n".printf: $result[ 0 ]; + + if VERBOSE + { + my Int @elements = $result[ 1 ]; + + "\nThe sorted list of the given matrix: %s\n".printf: + @elements.join: ', '; + + my UInt $padding = @elements[ 0 ].chars + @elements[ 1 ].chars + 4; + + "The third-smallest element: %s%s\n".printf: + ' ' x $padding, '^' x $result[ 0 ].chars; + } +} + +#=============================================================================== +multi sub MAIN() # No input: run the test suite +#=============================================================================== +{ + run-tests(); +} + +#------------------------------------------------------------------------------- +sub find-third-smallest( List:D[List:D[Int:D]] $matrix --> Result:D ) +#------------------------------------------------------------------------------- +{ + my Int @elements; + + @elements.push: |$_ for @$matrix; + + @elements.= sort; + + @elements.elems >= 3 or error( 'Matrix too small' ); + + return @elements[ 2 ], @elements; +} + +#=============================================================================== +grammar Matrix +#=============================================================================== +{ + token TOP { ^ \s* '(' \s* [ ]+ \s* ')' \s* $ } + token row { '[' \s* [ ]+ \s* ']' } + token elem { <[-+]>? <[0..9]>+ } + token sep { \, \s* } +} + +#------------------------------------------------------------------------------- +sub parse-matrix-string( Str:D $matrix --> List:D[List:D[Int:D]] ) +#------------------------------------------------------------------------------- +{ + my Matrix $parsed = Matrix.parse( $matrix ) + or error( 'Invalid input string' ); + + my Array[Int] @matrix; + my Str @rows = $parsed< row >.map: { .Str }; + + for @rows -> Str $row + { + $row ~~ / ^ \[ \s* (.+?) \s* \] $ /; + + @matrix.push: Array[Int].new: $0.Str.split( / \, \s* / ).map: { .Int }; + } + + validate-matrix( @matrix ); + + return @matrix; +} + +#------------------------------------------------------------------------------- +sub validate-matrix( List:D[List:D[Int:D]] $matrix ) +#------------------------------------------------------------------------------- +{ + my UInt $rows = $matrix.elems; + my UInt $n = $matrix[ 0 ].elems; + + $rows >= 2 or error( 'Too few rows in matrix' ); + $rows == $n or error( 'Matrix is not square' ); + + for 1 .. $matrix.end -> UInt $i + { + my UInt $m = $matrix[ $i ].elems; + my UInt $j = $i + 1; + + $m == $n or error( "In matrix row $j: expected $n elements, found $m" ); + } +} + +#------------------------------------------------------------------------------- +sub format-matrix( List:D[List:D[Int:D]] $matrix --> Str:D ) +#------------------------------------------------------------------------------- +{ + my Str $matrix-str = '('; + my Str @row-strs; + + for @$matrix -> Int @elems + { + @row-strs.push: '[' ~ @elems.join( ', ' ) ~ ']'; + } + + $matrix-str ~= @row-strs.join( ', ' ) ~ ')'; + + return $matrix-str; +} + +#------------------------------------------------------------------------------- +sub run-tests() +#------------------------------------------------------------------------------- +{ + 'Running the test suite'.put; + + for test-data.lines -> Str $line + { + my Str ($test-name, $matrix-str, $expected) = $line.split: / \| /; + + s/ \s+ $ // for $test-name, $matrix-str; # Trim whitespace + + my Array[Int] @matrix = parse-matrix-string( $matrix-str ); + my Result $result = find-third-smallest( @matrix ); + + is $result[ 0 ], $expected.Int, $test-name; + } + + done-testing; +} + +#------------------------------------------------------------------------------- +sub error( Str:D $message ) +#------------------------------------------------------------------------------- +{ + "ERROR: $message".put; + + USAGE(); + + exit 0; +} + +#------------------------------------------------------------------------------- +sub USAGE() +#------------------------------------------------------------------------------- +{ + my Str $usage = $*USAGE; + + $usage ~~ s:g/ ($*PROGRAM-NAME) /raku $0/; + + $usage.put; +} + +#------------------------------------------------------------------------------- +sub test-data( --> Str:D ) +#------------------------------------------------------------------------------- +{ + return q:to/END/; + Example 1|([3, 1, 2], [5, 2, 4], [ 0, 1, 3])| 1 + Example 2|([2, 1], [4, 5]) | 4 + Example 3|([1, 0, 3], [0, 0, 0], [ 1, 2, 1])| 0 + Negatives|([0, -1, -2], [0, -1, -3], [-3, 1, 2])|-2 + END +} + +################################################################################ -- cgit From b262c03ac097572159e0460174c1c641696087eb Mon Sep 17 00:00:00 2001 From: Jan Krňávek Date: Sun, 21 May 2023 19:21:59 +0200 Subject: solutions week 217 --- challenge-217/wambash/raku/ch-1.raku | 17 +++++++++++++++++ challenge-217/wambash/raku/ch-2.raku | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 challenge-217/wambash/raku/ch-1.raku create mode 100644 challenge-217/wambash/raku/ch-2.raku diff --git a/challenge-217/wambash/raku/ch-1.raku b/challenge-217/wambash/raku/ch-1.raku new file mode 100644 index 0000000000..2a4d7076c7 --- /dev/null +++ b/challenge-217/wambash/raku/ch-1.raku @@ -0,0 +1,17 @@ +#!/usr/bin/env raku + +sub sorted-matrix (*@matrix) { + @matrix.sort[2] +} + +multi MAIN (Bool :test($)!) { + use Test; + is sorted-matrix([3, 1, 2], [5, 2, 4], [0, 1, 3]), 1; + is sorted-matrix([2,1],[4,5]),4; + is sorted-matrix([1, 0, 3], [0, 0, 0], [1, 2, 1]),0; + done-testing; +} + +multi MAIN (*@matrix) { + say sorted-matrix @matrix +} diff --git a/challenge-217/wambash/raku/ch-2.raku b/challenge-217/wambash/raku/ch-2.raku new file mode 100644 index 0000000000..9395791be4 --- /dev/null +++ b/challenge-217/wambash/raku/ch-2.raku @@ -0,0 +1,25 @@ +#!/usr/bin/env raku + +sub max-number (+@list) { + my $max-length = .max div .min + 1 with @list».codes.minmax; + + @list + andthen .sort: *.Str x $max-length + andthen .reverse + andthen .join +} + +multi MAIN (Bool :test($)!) { + use Test; + is max-number(1, 12, 101), '121101'; + is max-number(1, 23), '231'; + is max-number(10, 3, 2), '3210'; + is max-number(31, 2, 4, 10), '431210'; + is max-number(5, 11, 4, 1, 2), '542111'; + is max-number(1,10), '110'; + done-testing; +} + +multi MAIN (*@list) { + say max-number @list +} -- cgit From 81638724318912bcf9235c74c2a79499b78bd2a5 Mon Sep 17 00:00:00 2001 From: "Israel C. Batista" Date: Sun, 21 May 2023 15:20:58 -0300 Subject: Adding Rozcovo's solution for challenge #217 --- challenge-217/rozcovo/README | 1 + challenge-217/rozcovo/perl/ch-1.pl | 31 ++++++++++ challenge-217/rozcovo/perl/ch-2.pl | 6 ++ .../rozcovo/perl/performance_test_ch-1.pl | 71 ++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 challenge-217/rozcovo/README create mode 100644 challenge-217/rozcovo/perl/ch-1.pl create mode 100644 challenge-217/rozcovo/perl/ch-2.pl create mode 100644 challenge-217/rozcovo/perl/performance_test_ch-1.pl diff --git a/challenge-217/rozcovo/README b/challenge-217/rozcovo/README new file mode 100644 index 0000000000..7f7cc4bff7 --- /dev/null +++ b/challenge-217/rozcovo/README @@ -0,0 +1 @@ +Solution by Israel C. Batista diff --git a/challenge-217/rozcovo/perl/ch-1.pl b/challenge-217/rozcovo/perl/ch-1.pl new file mode 100644 index 0000000000..30c4da8a86 --- /dev/null +++ b/challenge-217/rozcovo/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl +use 5.36.0; +use strict; + +use subs qw/ + simple_method