diff options
49 files changed, 2864 insertions, 1728 deletions
diff --git a/challenge-096/jaldhar-h-vyas/blog.txt b/challenge-096/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..c87e41fff8 --- /dev/null +++ b/challenge-096/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2021/02/perl_weekly_challenge_week_96.html diff --git a/challenge-096/jaldhar-h-vyas/perl/ch-1.sh b/challenge-096/jaldhar-h-vyas/perl/ch-1.sh new file mode 100755 index 0000000000..5beec9b643 --- /dev/null +++ b/challenge-096/jaldhar-h-vyas/perl/ch-1.sh @@ -0,0 +1 @@ +perl -E 'say join q{ }, (reverse @ARGV);' $@
\ No newline at end of file diff --git a/challenge-096/jaldhar-h-vyas/perl/ch-2.pl b/challenge-096/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..3114d70648 --- /dev/null +++ b/challenge-096/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl +use 5.020; +use warnings; +use English qw/ -no_match_vara /; + +sub usage { + print<<"-USAGE-"; +Usage: + $PROGRAM_NAME <from> <to> + + <from> string to convert from + <to> string to convert to +-USAGE- + exit 0; +} + +sub min { + return (sort { $a <=> $b } @_)[0]; +} + +sub levenshtein { + my ($from, $to) = @_; + my $fromLength = length $from; + my $toLength = length $to; + + if ($toLength == 0) { + return $fromLength; + } + + if ($fromLength == 0) { + return $toLength; + } + + my $fromTail = substr($from, 1, $fromLength - 1); + my $toTail = substr($to, 1, $toLength - 1); + + if (substr($from, 0, 1) eq substr($to, 0, 1)) { + return levenshtein($fromTail, $toTail); + } + + return 1 + min( + levenshtein($from, $toTail), # Insert + levenshtein($fromTail, $to), # Remove + levenshtein($fromTail, $toTail) # Replace + ); +} + +if (scalar @ARGV < 2) { + usage(); +} + +say levenshtein($ARGV[0], $ARGV[1]); diff --git a/challenge-096/jaldhar-h-vyas/raku/ch-1.sh b/challenge-096/jaldhar-h-vyas/raku/ch-1.sh new file mode 100755 index 0000000000..9c33ad4bd2 --- /dev/null +++ b/challenge-096/jaldhar-h-vyas/raku/ch-1.sh @@ -0,0 +1 @@ +raku -e '@*ARGS.reverse.join(q{ }).say;' $@
\ No newline at end of file diff --git a/challenge-096/jaldhar-h-vyas/raku/ch-2.raku b/challenge-096/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..33b152b7c6 --- /dev/null +++ b/challenge-096/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,34 @@ +#!/usr/bin/raku + +sub levenshtein(Str $from, Str $to) { + my $fromLength = $from.chars; + my $toLength = $to.chars; + + if $toLength == 0 { + return $fromLength; + } + + if $fromLength == 0 { + return $toLength; + } + + my $fromTail = $from.substr(1, $fromLength - 1); + my $toTail = $to.substr(1, $toLength - 1); + + if $from.substr(0, 1) eq $to.substr(0, 1) { + return levenshtein($fromTail, $toTail); + } + + return 1 + ( + levenshtein($from, $toTail), # Insert + levenshtein($fromTail, $to), # Remove + levenshtein($fromTail, $toTail) # Replace + ).min; +} + +sub MAIN( + Str $S1, #= string to convert from + Str $S2, #= string to convert to +) { + say levenshtein($S1, $S2); +}
\ No newline at end of file diff --git a/challenge-100/dave-jacoby/blog.txt b/challenge-100/dave-jacoby/blog.txt new file mode 100644 index 0000000000..b064b34fee --- /dev/null +++ b/challenge-100/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2021/02/16/turning-over-a-new-digit-perl-weekly-challenge-100.html diff --git a/challenge-100/dave-jacoby/node/ch-2.js b/challenge-100/dave-jacoby/node/ch-2.js new file mode 100644 index 0000000000..5f023375c6 --- /dev/null +++ b/challenge-100/dave-jacoby/node/ch-2.js @@ -0,0 +1,45 @@ +"use strict"; + +var input = []; +input.push([[1], [2, 4], [6, 4, 9], [5, 1, 7, 2]]); +input.push([[3], [3, 1], [5, 2, 3], [4, 3, 1, 3]]); + +for (let i = 0; i < input.length; i++) { + triangle_sum(input[i]); +} + +function triangle_sum(input) { + let results = triangle(input, 0, 0, []).sort(function(a, b) { return a - b }); + console.log(input); + console.log(results[0]); + console.log(""); +} + +function triangle(input, x, y, path) { + if (x > 5) { + return; + } + let output = []; + + if ("undefined" === typeof input[x]) { + output.push(path.reduce((a, b) => a + b, 0)); + } else { + let v = input[x][y]; + let next_path = [...path]; + next_path.push(v); + + output.push(...triangle(input, x + 1, y, next_path)); + output.push(...triangle(input, x + 1, y + 1, next_path)); + } + return output; +} + + +// [ [ 1 ], [ 2, 4 ], [ 6, 4, 9 ], [ 5, 1, 7, 2 ] ] +// 8 +// +// [ [ 3 ], [ 3, 1 ], [ 5, 2, 3 ], [ 4, 3, 1, 3 ] ] +// 7 + + + diff --git a/challenge-100/dave-jacoby/perl/ch-1.pl b/challenge-100/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..df96c5ef42 --- /dev/null +++ b/challenge-100/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,70 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental::postderef experimental::signatures }; + +# You are given a time (12 hour / 24 hour). +# +# Write a script to convert the given time from 12 hour format to 24 hour format and vice versa. +# +# Ideally we expect a one-liner. + +my @times = + sort { $b =~ /m/ <=> $a =~ /m/ } + sort + + ( + '5:14', '05:15 pm', '05:15pm', '05:15 am', '05:15am', '17:15', + '19:15', '07:15 pm', '07:15pm', '12:00am', '12:00pm', '00:00', + '12:00', '24:00', '7:7am', '7:7pm' + ); + +for my $time (@times) { + say join "\t", '', $time, ' <=> ', switch_time($time); + say ''; +} + +# 12pm is noon +# 12am is midnight +sub switch_time ( $time ) { + my $out = ''; + + # 12-hour time + if ( $time =~ /m$/mix ) { + my ( $hr, $min, $ampm ) = $time =~ /(\d+):(\d+)\s*(am|pm|)/mix; + $out = join ':', + ( + $ampm eq 'am' + ? ( + $hr == 0 ? '00': $hr + ) + : ( + $hr == '12' + ? sprintf '%02d', + $min + : sprintf '%02d', + $hr + 12 + ) + ), + ( sprintf '%02d', $min ); + } + + # 24-hour time + else { + my ( $hr, $min ) = $time =~ /(\d+):(\d+)/mix; + $out = join '', + ( + $hr == 0 || $hr == 24 + ? 12 + : ( $hr > 12 ? $hr % 12 : $hr ) + ), + (':'), + ( sprintf '%02d', $min ), + ( $hr < 12 ? 'am' : 'pm' ); + } + return $out; +} + + diff --git a/challenge-100/dave-jacoby/perl/ch-2.pl b/challenge-100/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..15d761b17f --- /dev/null +++ b/challenge-100/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental::postderef experimental::signatures }; + +use List::Util qw{sum}; +use JSON; +my $json = JSON->new; +my $p = JSON->new->pretty->canonical; + +my @input; +push @input, [ [1], [ 2, 4 ], [ 6, 4, 9 ], [ 5, 1, 7, 2 ] ]; +push @input, [ [3], [ 3, 1 ], [ 5, 2, 3 ], [ 4, 3, 1, 3 ] ]; + +for my $input (@input) { + triangle_sum($input); +} + +sub triangle_sum ( $input ) { + my ($short) = + sort { $a->{sum} <=> $b->{sum} } triangle($input); + say qq{ sum: $short->{sum} }; + say q{ path: } . join ' ', $short->{path}->@*; + for my $i ( $input->@* ) { + say join ' ', ' ', $i->@*; + } + say ''; +} + +sub triangle ( $input, $x = 0, $y = 0, @path ) { + my @output; + + # if not a leaf, go left and right + if ( defined $input->[$x][$y] ) { + push @output, triangle( $input, $x + 1, $y, @path, $y ); + push @output, triangle( $input, $x + 1, $y + 1, @path, $y ); + } + + # if a leaf, find the sum, find the path, and return + else { + my @ind = map { $path[$_] } 0 .. $x - 1; + my $sum = sum map { $input->[$_][ $path[$_] ] } 0 .. $x - 1; + push @output, { sum => $sum, path => \@ind, }; + } + return @output; +} diff --git a/challenge-100/lance-wicks/elm/elm.json b/challenge-100/lance-wicks/elm/elm.json new file mode 100644 index 0000000000..7468f5e5cd --- /dev/null +++ b/challenge-100/lance-wicks/elm/elm.json @@ -0,0 +1,28 @@ +{ + "type": "application", + "source-directories": [ + "src" + ], + "elm-version": "0.19.1", + "dependencies": { + "direct": { + "elm/browser": "1.0.2", + "elm/core": "1.0.5", + "elm/html": "1.0.0" + }, + "indirect": { + "elm/json": "1.1.3", + "elm/time": "1.0.0", + "elm/url": "1.0.0", + "elm/virtual-dom": "1.0.2" + } + }, + "test-dependencies": { + "direct": { + "elm-explorations/test": "1.2.2" + }, + "indirect": { + "elm/random": "1.0.0" + } + } +} diff --git a/challenge-100/lance-wicks/elm/src/Fun.elm b/challenge-100/lance-wicks/elm/src/Fun.elm new file mode 100644 index 0000000000..2f9775c70b --- /dev/null +++ b/challenge-100/lance-wicks/elm/src/Fun.elm @@ -0,0 +1,69 @@ +module Fun exposing (convert) + + +convert : String -> String +convert time = + if String.contains "am" time then + String.slice 0 5 time + + else if String.contains "pm" time then + let + hourMin = + String.split ":" time + in + case List.head hourMin of + Just a -> + let + hour = + String.toInt a + in + case hour of + Just h -> + let + hx = + h + 12 + in + String.fromInt hx ++ String.slice 2 5 time + + Nothing -> + "" + + Nothing -> + "" + + else + let + hourMin = + String.split ":" time + in + case List.head hourMin of + Just a -> + let + hour = + String.toInt a + in + case hour of + Just h -> + if h > 12 then + let + hx = + h - 12 + in + let + hourStr = + String.fromInt hx + in + if String.length hourStr > 1 then + hourStr ++ String.slice 2 5 time ++ "pm" + + else + "0" ++ hourStr ++ String.slice 2 5 time ++ "pm" + + else + String.slice 0 5 time ++ "am" + + Nothing -> + "" + + Nothing -> + "" diff --git a/challenge-100/lance-wicks/elm/tests/Example.elm b/challenge-100/lance-wicks/elm/tests/Example.elm new file mode 100644 index 0000000000..64c70d1337 --- /dev/null +++ b/challenge-100/lance-wicks/elm/tests/Example.elm @@ -0,0 +1,40 @@ +module Example exposing (..) + +import Expect exposing (Expectation) +import Fun exposing (convert) +import Fuzz exposing (Fuzzer, int, list, string) +import Test exposing (..) + + +suite : Test +suite = + describe "Fun" + [ test "convert 07:15" <| + \_ -> + let + got = + convert "07:15" + in + Expect.equal got "07:15am" + , test "convert 05:15pm" <| + \_ -> + let + got = + convert "05:15 pm" + in + Expect.equal got "17:15" + , test "convert 19:15" <| + \_ -> + let + got = + convert "19:15" + in + Expect.equal got "07:15pm" + , test "convert 09:15" <| + \_ -> + let + got = + convert "09:15 am" + in + Expect.equal got "09:15" + ] diff --git a/challenge-100/lance-wicks/perl/ch-1.pl b/challenge-100/lance-wicks/perl/ch-1.pl new file mode 100644 index 0000000000..78310d45ab --- /dev/null +++ b/challenge-100/lance-wicks/perl/ch-1.pl @@ -0,0 +1,11 @@ +use strict; +use warnings; + +use lib './lib'; +use Fun; + +my $fun = Fun->new; + +print $fun->convert($ARGV[0]); +print "\n"; + diff --git a/challenge-100/lance-wicks/perl/lib/Fun.pm b/challenge-100/lance-wicks/perl/lib/Fun.pm new file mode 100644 index 0000000000..d1e1088614 --- /dev/null +++ b/challenge-100/lance-wicks/perl/lib/Fun.pm @@ -0,0 +1,35 @@ +package Fun; + +use Moo; + +sub convert { + my ( $self, $time ) = @_; + + if ( $time =~ m/([ap]m)/ ) { + my $am_pm = $1; + + $time =~ m/^(\d+):(\d+)/; + my $hours = $1; + my $minutes = $2; + + if ( $am_pm eq 'pm' ) { + $hours += 12; + } + return "$hours:$minutes"; + } + else { + $time =~ m/^(\d+):(\d+)/; + my $hours = $1; + my $minutes = $2; + + if ( $hours > 12 ) { + $hours -= 12; + return sprintf( "%02d", $hours ) . ":$minutes" . 'pm'; + } + else { + return sprintf( "%02d", $hours ) . ":$minutes" . 'am'; + } + } +} + +1; diff --git a/challenge-100/lance-wicks/perl/t/00-fun.t b/challenge-100/lance-wicks/perl/t/00-fun.t new file mode 100644 index 0000000000..8cba91420a --- /dev/null +++ b/challenge-100/lance-wicks/perl/t/00-fun.t @@ -0,0 +1,10 @@ +use Test2::V0 -target => "Fun"; + +is $CLASS->convert("05:15 pm"), '17:15', 'Example 1-1'; +is $CLASS->convert("05:15pm"), '17:15', 'Example 1-2'; + +is $CLASS->convert("19:15"), '07:15pm', 'Example 2-1'; + +is $CLASS->convert("09:15"), '09:15am', '24hr AM'; + +done_testing; diff --git a/challenge-100/mohammad-anwar/perl/ch-1.pl b/challenge-100/mohammad-anwar/perl/ch-1.pl index d05904c908..f9a3b88de9 100644 --- a/challenge-100/mohammad-anwar/perl/ch-1.pl +++ b/challenge-100/mohammad-anwar/perl/ch-1.pl @@ -8,6 +8,8 @@ use Test::More; foreach (['20:20', '08:20pm'], ['22:05', '10:05pm'], ['05:00', '05:00am'], + ['12:00', '12:00pm'], + ['00:01', '12:01am'], ['11:55', '11:55am']) { is(t($_->[0]), $_->[1]); is(t($_->[1]), $_->[0]); @@ -19,7 +21,7 @@ sub t { return sprintf("%02d:%02d%s", ($_[0] =~ /(\d+)?\:(\d+)\s?([ap]m)$/i) - ?((uc($3) eq 'PM')?($1+12,$2,''):($1,$2,'')) - :(($_[0] =~ /(\d+)?\:(\d+)/) and (($1>12)?($1-12,$2,'pm'):($1,$2,'am'))) + ?((uc($3) eq 'PM')?(($1==12)?($1,$2,''):($1+12,$2,'')):(($1==12)?($1-12,$2,''):($1,$2,''))) + :(($_[0] =~ /(\d+)?\:(\d+)/) and (($1 eq '00')?('12',$2,'am'):(($1==12)?('12',$2,'pm'):(($1>12)?($1-12,$2,'pm'):($1,$2,'am'))))) ); } diff --git a/challenge-100/mohammad-anwar/perl/ch-2.pl b/challenge-100/mohammad-anwar/perl/ch-2.pl new file mode 100644 index 0000000000..6c62dab8da --- /dev/null +++ b/challenge-100/mohammad-anwar/perl/ch-2.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +is(sum([[1], [2,4], [6,4,9], [5,1,7,2]]), 8); +is(sum([[3], [3,1], [5,2,3], [4,3,1,3]]), 7); + +done_testing; + +sub sum { + my ($t) = @_; + + my $i = 0; + my $s = (@{shift @$t})[0]; + $s += ($_->[$i] > $_->[$i+1])?($i+=2 and $_->[--$i]):($_->[$i]) for @$t; + return $s; +} diff --git a/challenge-100/paulo-custodio/forth/ch-1.fs b/challenge-100/paulo-custodio/forth/ch-1.fs new file mode 100644 index 0000000000..192b0dc2a5 --- /dev/null +++ b/challenge-100/paulo-custodio/forth/ch-1.fs @@ -0,0 +1,75 @@ +#! /usr/bin/env gforth + +\ TASK #1 > Fun Time +\ Submitted by: Mohammad S Anwar +\ You are given a time (12 hour / 24 hour). +\ +\ Write a script to convert the given time from 12 hour format to 24 hour format and vice versa. +\ +\ Ideally we expect a one-liner. +\ +\ Example 1: +\ Input: 05:15 pm or 05:15pm +\ Output: 17:15 +\ Example 2: +\ Input: 19:15 +\ Output: 07:15 pm or 07:15pm + +\ skip blanks +: skip-blanks ( str len -- str len ) + BEGIN + DUP 0= IF EXIT THEN \ string empty + OVER C@ BL <> IF EXIT THEN \ not space + 1 /STRING + AGAIN +; + +\ parse a time string, return number of minutes since midnight +\ and true if it was in AM/PM format +: parse-time { str len -- hours minutes f-am-pm } + 0 0 { hours minutes } + 0 0 str len >NUMBER TO len TO str DROP TO hours \ convert hours + str C@ ':' <> IF 1 THROW THEN + str len 1 /STRING TO len TO str \ skip ':' + 0 0 str len >NUMBER TO len TO str DROP TO minutes \ convert minutes + str len skip-blanks TO len TO str \ spaces + len 0= IF + hours minutes FALSE + ELSE str C@ DUP 'a' = SWAP 'A' = OR IF \ AM + hours 12 = IF 0 TO hours THEN + hours minutes TRUE + ELSE str C@ DUP 'p' = SWAP 'P' = OR IF \ PM + hours 12 <> IF hours 12 + TO hours THEN + hours minutes TRUE + THEN THEN THEN +; + +\ output in 24-hour format +: print24 ( hours minutes -- ) + SWAP 100 * + 0 \ convert to hhmm in decimal, double + <# # # ':' HOLD # # #> TYPE +; + +\ output in 12-hour format +: print12 { hours minutes -- } + FALSE { pm } \ assume AM + + hours DUP 12 > IF + DROP hours 12 - TO hours TRUE TO pm ELSE + DUP 12 = IF + DROP TRUE TO pm ELSE + DUP 0= IF + DROP 12 TO HOURS + ELSE DROP THEN THEN THEN + + hours minutes print24 + pm IF ." pm" ELSE ." am" THEN +; + +\ convert time +: convert-time ( str len -- ) + parse-time IF print24 ELSE print12 THEN +; + +\ main +NEXT-ARG convert-time CR BYE diff --git a/challenge-100/paulo-custodio/forth/ch-2.fs b/challenge-100/paulo-custodio/forth/ch-2.fs new file mode 100644 index 0000000000..35156151a7 --- /dev/null +++ b/challenge-100/paulo-custodio/forth/ch-2.fs @@ -0,0 +1,105 @@ +#! /usr/bin/env gforth + +\ TASK #2 > Triangle Sum +\ Submitted by: Mohammad S Anwar +\ You are given triangle array. +\ +\ Write a script to find the minimum path sum from top to bottom. +\ +\ When you are on index i on the current row then you may move to either +\ index i or index i + 1 on the next row. +\ +\ Example 1: +\ Input: Triangle = [ [1], [2,4], [6,4,9], [5,1,7,2] ] +\ Output: 8 +\ +\ Explanation: The given triangle +\ +\ 1 +\ 2 4 +\ 6 4 9 +\ 5 1 7 2 +\ +\ The minimum path sum from top to bottom: 1 + 2 + 4 + 1 = 8 +\ +\ [1] +\ [2] 4 +\ 6 [4] 9 +\ 5 [1] 7 2 +\ Example 2: +\ Input: Triangle = [ [3], [3,1], [5,2,3], [4,3,1,3] ] +\ Output: 7 +\ +\ Explanation: The given triangle +\ +\ 3 +\ 3 1 +\ 5 2 3 +\ 4 3 1 3 +\ +\ The minimum path sum from top to bottom: 3 + 1 + 2 + 1 = 7 +\ +\ [3] +\ 3 [1] +\ 5 [2] 3 +\ 4 3 [1] 3 + +\ create a square to store the triangle +20 CONSTANT max-height \ maximum height of triangle +CREATE triangle max-height DUP * CELLS ALLOT + +\ get address of value at row,col, 0-based +: triangle[] ( row col -- addr ) + SWAP max-height * + CELLS triangle + +; + +\ skip non-digits +: skip-non-digits ( str len -- str len ) + BEGIN + DUP 0= IF EXIT THEN \ string empty + OVER C@ DUP '0' >= SWAP '9' <= AND IF EXIT THEN \ digit + 1 /STRING + AGAIN +; + +\ store last filled row of triangle +-1 VALUE last-row + +\ parse a list of values from string, store at last-row +: parse-line ( str len -- ) + last-row 1+ TO last-row + last-row 1+ 0 ?DO + skip-non-digits + 0 0 2SWAP >NUMBER 2SWAP DROP \ convert one number |
