diff options
| author | Ryan Thompson <rjt-pl@users.noreply.github.com> | 2025-10-04 02:47:51 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-04 02:47:51 -0600 |
| commit | acee7b83ca2daace4e269f78a272ce07936a0fd6 (patch) | |
| tree | 9f8a910367fe83de0b18fc63ae8ba50193bab2fc | |
| parent | 99d2e30c7556e8872a30adf78c0b175cdd8bd7ab (diff) | |
| parent | 9506417ade8b33371ed335c95719c3ad5bf83c33 (diff) | |
| download | perlweeklychallenge-club-acee7b83ca2daace4e269f78a272ce07936a0fd6.tar.gz perlweeklychallenge-club-acee7b83ca2daace4e269f78a272ce07936a0fd6.tar.bz2 perlweeklychallenge-club-acee7b83ca2daace4e269f78a272ce07936a0fd6.zip | |
Merge branch 'manwar:master' into master
37 files changed, 776 insertions, 190 deletions
diff --git a/challenge-259/jaldhar-h-vyas/perl/ch-1.pl b/challenge-259/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..1cda735ecd --- /dev/null +++ b/challenge-259/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl +use 5.038; +use warnings; +use DateTime; +use DateTime::Format::Strptime; + +my $strptime = DateTime::Format::Strptime->new( + pattern => '%F', + on_error => sub { die "Invalid date\n"; } +); + +my $startDate = $strptime->parse_datetime(shift @ARGV); +my $offset = shift @ARGV; +my @bankHolidays = map { $strptime->parse_datetime($_) } @ARGV; +my $endDate = $startDate; + +while ($offset > 0 ) { + $endDate->add(days => 1); + my $dow = $endDate->dow; + if ($dow > 0 && $dow < 6 && !grep { $_ == $endDate } @bankHolidays) { + $offset--; + } +} + +say $endDate->ymd; diff --git a/challenge-259/jaldhar-h-vyas/perl/ch-2.pl b/challenge-259/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..2c9ac89821 --- /dev/null +++ b/challenge-259/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl +use 5.038; +use warnings; +use Parse::RecDescent; + +# $::RD_TRACE = 1; + +my $grammar = <<'-EOT-'; + startrule: multi_line | single_line + + single_line: '{%' ws identifier field(s) '%}' + { + $return = "{\n" + . " name => $item{identifier},\n" + . " fields => {\n " + . join(",\n ", @{$item{'field(s)'}}) + . "\n }\n" + . "}\n"; + } + + multi_line: '{%' ws identifier field(s) '%}' text '{%' ws "endmyid" ws '%}' + { + $return = "{\n" + . " name => $item{identifier},\n" + . " fields => {\n " + . join(",\n ", @{$item{'field(s)'}}) + . "\n },\n" + . " text => $item{text}" + . "}\n"; + } + + ws: /\s*/ + + identifier: /\w+/ + { + my $id = $item[1]; + $return = $id; + } + + field: ws name ws '=' ws value + { $return = "$item{ name } => $item{ value }" } + + name: /\w+/ + + value: number | string + + number: /\d+/ + { $return = 0 + $item[1]; } + + string: /"/ content /"/ + { $return = $item{content}; } + + content: /(\\" | \\\\ | [^"])+/x + + text: /[^{]+/ +-EOT- + +my $parser = Parse::RecDescent->new($grammar); +my $text = shift; +say $parser->startrule($text); diff --git a/challenge-259/jaldhar-h-vyas/raku/ch-1.raku b/challenge-259/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..742adcac7b --- /dev/null +++ b/challenge-259/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,21 @@ +#!/usr/bin/raku + +sub MAIN( + $start, + $offset is copy, + *@holidays +) { + my $startDate = Date.new($start); + my @bankHolidays = @holidays.map({ Date.new($_) }); + my $endDate = $startDate; + + while ($offset > 0 ) { + $endDate += 1; + my $dow = $endDate.day-of-week; + if ($dow > 0 && $dow < 6 && @bankHolidays.none == $endDate) { + $offset--; + } + } + + say $endDate.yyyy-mm-dd; +}
\ No newline at end of file diff --git a/challenge-259/jaldhar-h-vyas/raku/ch-2.raku b/challenge-259/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..8f3ab62275 --- /dev/null +++ b/challenge-259/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,105 @@ +#!/usr/bin/raku + +grammar LineParser { + rule TOP { + | <single-line> + | <multi-line> + } + + token single-line { + ^ '{%' \s+ <identifier> <field>* \s* '%}' $ + } + + token multi-line { + '{%' \s+ <identifier> <field>* \s* '%}' \n + <text> + '{%' \s+ 'end' <end-id=.identifier> \s* '%}' \n? + } + + token identifier { \w+ } + + token ws { \h* } + + token field { + \s+ <name=.identifier> \s* '=' \s* <value> + } + + proto token value {*} + + token value:sym<number> { \d+ } + + token value:sym<string> { + '"' <content> '"' + } + + token content { + [ + | <-[\"\\]>+ + | '\\"' + | '\\\\' + ]* + } + + token text { + <-[{]>* + } +} + +class LineActions { + method TOP($/) { + make $<single-line> ?? $<single-line>.made !! $<multi-line>.made; + } + + method single-line($/) { + make { + name => ~$<identifier>, + fields => $<field>.map({ + ~$_<name> => $_<value>.made + }).Hash + } + } + + method multi-line($/) { + die "Closing tag 'end{$<identifier>}' does not match opening tag '{$<end-id>}'" + unless $<identifier> eq $<end-id>; + + make { + name => ~$<identifier>, + fields => $<field>.map({ + ~$_<name> => $_<value>.made + }).Hash, + text => ~$<text> + } + } + + method value:sym<number>($/) { make +$/ } + method value:sym<string>($/) { + make ~$<content> + .subst(/'\\"'/, '"', :g) + .subst(/'\\\\'/, '\\', :g) + } +} + +sub prettyprint(%data) { + my $output = "\{\n name => %data<name>,\n fields => \{\n "; + + $output ~= %data<fields> + .keys + .map({ "$_ => %data<fields>{$_}" }) + .join(",\n "); + + + $output ~= "\n \},\n"; + if %data<text> { + $output ~= " text => %data<text>"; + } + $output ~= "}\n"; + + return $output; +} + +sub MAIN($input) { + my $actions = LineActions.new; + my $match = LineParser.parse($input, :$actions); + say $match ?? prettyprint($match.made) !! 'Failed to parse line'; +}
\ No newline at end of file diff --git a/challenge-341/jeanluc2020/blog-1.txt b/challenge-341/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..a9c2ab303b --- /dev/null +++ b/challenge-341/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-341-1.html diff --git a/challenge-341/jeanluc2020/blog-2.txt b/challenge-341/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..e2a5f835ab --- /dev/null +++ b/challenge-341/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-341-2.html diff --git a/challenge-341/jeanluc2020/perl/ch-1.pl b/challenge-341/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..0e319f6c22 --- /dev/null +++ b/challenge-341/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,79 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-341/#TASK1 +# +# Task 1: Broken Keyboard +# ======================= +# +# You are given a string containing English letters only and also you are given +# broken keys. +# +# Write a script to return the total words in the given sentence can be typed +# completely. +# +## Example 1 +## +## Input: $str = 'Hello World', @keys = ('d') +## Output: 1 +## +## With broken key 'd', we can only type the word 'Hello'. +# +# +## Example 2 +## +## Input: $str = 'apple banana cherry', @keys = ('a', 'e') +## Output: 0 +# +# +## Example 3 +## +## Input: $str = 'Coding is fun', @keys = () +## Output: 3 +## +## No keys broken. +# +# +## Example 4 +## +## Input: $str = 'The Weekly Challenge', @keys = ('a','b') +## Output: 2 +# +# +## Example 5 +## +## Input: $str = 'Perl and Python', @keys = ('p') +## Output: 1 +# +############################################################ +## +## discussion +## +############################################################ +# +# Create a list of the words in $str. Then for each of the words, +# check if any of the characters in the word is a broken key, we remove +# it from the list. Count the remaining words as the result + +use v5.36; + + +broken_keyboard('Hello World', 'd'); +broken_keyboard('apple banana cherry', 'a', 'e'); +broken_keyboard('Coding is fun' ); +broken_keyboard('The Weekly Challenge', 'a','b'); +broken_keyboard('Perl and Python', 'p'); + +sub broken_keyboard($str, @keys) { + say "Input: '$str', (" . join(", ", @keys) . ")"; + my @words = split /\s+/, $str; + my $count = scalar(@words); + OUTER: + foreach my $w (@words) { + foreach my $key (@keys) { + if($w =~ m/$key/i) { + $count--; + next OUTER; + } + } + } + say "Output: $count"; +} diff --git a/challenge-341/jeanluc2020/perl/ch-2.pl b/challenge-341/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..068d80869b --- /dev/null +++ b/challenge-341/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-341/#TASK2 +# +# Task 2: Reverse Prefix +# ====================== +# +# You are given a string, $str and a character in the given string, $char. +# +# Write a script to reverse the prefix upto the first occurrence of the given +# $char in the given string $str and return the new string. +# +## Example 1 +## +## Input: $str = "programming", $char = "g" +## Output: "gorpramming" +## +## Reverse of prefix "prog" is "gorp". +# +# +## Example 2 +## +## Input: $str = "hello", $char = "h" +## Output: "hello" +# +# +## Example 3 +## +## Input: $str = "abcdefghij", $char = "h" +## Output: "hgfedcbaij" +# +# +## Example 4 +## +## Input: $str = "reverse", $char = "s" +## Output: "srevere" +# +# +## Example 5 +## +## Input: $str = "perl", $char = "r" +## Output: "repl" +# +############################################################ +## +## discussion +## +############################################################ +# +# This is a simple s/old/new/ thanks to perl's s///e feature. +# We just need a regular expression that collects everything from +# the beginning of the string up until the first appearance of $char. +# The rest is applying reverse() to it which does exactly what we +# need in scalar context. + +use v5.36; + +reverse_prefix("programming", "g"); +reverse_prefix("hello", "h"); +reverse_prefix("abcdefghij", "h"); +reverse_prefix("reverse", "s"); +reverse_prefix("perl", "r"); + +sub reverse_prefix($str, $char) { + say "Input: '$str', '$char'"; + $str =~ s/^([^$char]*$char)/reverse($1)/e; + say "Output: $str"; +} diff --git a/challenge-341/mattneleigh/ch-1.pl b/challenge-341/mattneleigh/perl/ch-1.pl index a3d3442276..a3d3442276 100755 --- a/challenge-341/mattneleigh/ch-1.pl +++ b/challenge-341/mattneleigh/perl/ch-1.pl diff --git a/challenge-341/mattneleigh/ch-2.pl b/challenge-341/mattneleigh/perl/ch-2.pl index cd00fbe876..cd00fbe876 100755 --- a/challenge-341/mattneleigh/ch-2.pl +++ b/challenge-341/mattneleigh/perl/ch-2.pl diff --git a/challenge-341/spadacciniweb/elixir/ch-2.exs b/challenge-341/spadacciniweb/elixir/ch-2.exs new file mode 100644 index 0000000000..e7a183b845 --- /dev/null +++ b/challenge-341/spadacciniweb/elixir/ch-2.exs @@ -0,0 +1,61 @@ +# Task 2: Reverse Prefix +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string, $str and a character in the given string, $char. +# Write a script to reverse the prefix upto the first occurrence of the given $char in the given string $str and return the new string. +# +# Example 1 +# Input: $str = "programming", $char = "g" +# Output: "gorpmming" +# +# Reverse of prefix "prog" is "gorp". +# +# Example 2 +# Input: $str = "hello", $char = "h" +# Output: "hello" +# +# Example 3 +# Input: $str = "abcdefghij", $char = "h" +# Output: "hgfedcbaj" +# +# Example 4 +# Input: $str = "reverse", $char = "s" +# Output: "srevere" +# +# Example 5 +# Input: $str = "perl", $char = "r" +# Output: "repl" + +defmodule ReverseString do + + def splitStr(str, char) do + s = String.split(str, char, parts: 2) + String.reverse( Enum.at(s, 0) <> char ) <> Enum.at(s, 1) + end + + def out(str, char) do + IO.write( "'" <> str <> "' '" <> char <> "' -> ") + IO.puts( splitStr(str, char) ) + end + +end + +str = "programming" +char = "g" +ReverseString.out( str, char ) + +str = "hello" +char = "h" +ReverseString.out( str, char ) + +str = "abcdefghij" +char = "h" +ReverseString.out( str, char ) + +str = "reverse" +char = "s" +ReverseString.out( str, char ) + +str = "perl" +char = "r" +ReverseString.out( str, char ) diff --git a/challenge-341/wanderdoc/perl/ch-1.pl b/challenge-341/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..f61d965de6 --- /dev/null +++ b/challenge-341/wanderdoc/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string containing English letters only and also you are given broken keys. +Write a script to return the total words in the given sentence can be typed completely. + +Example 1 + +Input: $str = 'Hello World', @keys = ('d') +Output: 1 + +With broken key 'd', we can only type the word 'Hello'. + + +Example 2 + +Input: $str = 'apple banana cherry', @keys = ('a', 'e') +Output: 0 + + +Example 3 + +Input: $str = 'Coding is fun', @keys = () +Output: 3 + +No keys broken. + + +Example 4 + +Input: $str = 'The Weekly Challenge', @keys = ('a','b') +Output: 2 + + +Example 5 + +Input: $str = 'Perl and Python', @keys = ('p') +Output: 1 + +=cut + + +use Test2::V0 -no_srand => 1; +is(can_print('Hello World', ['d']), 1, 'Example 1'); +is(can_print('apple banana cherry', ['a', 'e']), 0, 'Example 2'); +is(can_print('coding is fun', []), 3, 'Example 3'); +is(can_print('The Weekly Challenge', ['a','b']), 2, 'Example 4'); +is(can_print('Perl and Python', ['p']), 1, 'Example 5'); +done_testing(); + + + + +sub can_print +{ + my ($str, $keys_aref) = @_; + my $keys_str = join('|', @$keys_aref); + my $keys_re = length($keys_str) ? qr/$keys_str/i : qr/ /; + return scalar(grep {!/$keys_re/} split(/\s/, $str)); +} diff --git a/challenge-341/wanderdoc/perl/ch-2.pl b/challenge-341/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..245dfb5c22 --- /dev/null +++ b/challenge-341/wanderdoc/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string, $str and a character in the given string, $char. +Write a script to reverse the prefix upto the first occurrence of the given $char in the given string $str and return the new string. + +Example 1 + +Input: $str = "programming", $char = "g" +Output: "gorpramming" + +Reverse of prefix "prog" is "gorp". + +Example 2 + +Input: $str = "hello", $char = "h" +Output: "hello" + +Example 3 + +Input: $str = "abcdefghij", $char = "h" +Output: "hgfedcbaij" + +Example 4 + +Input: $str = "reverse", $char = "s" +Output: "srevere" + +Example 5 + +Input: $str = "perl", $char = "r" +Output: "repl" + +=cut + + + + +use Test2::V0 -no_srand => 1; + +is(reverse_prefix("programming", "g"), "gorpramming", "Example 1"); +is(reverse_prefix("hello", "h"), "hello", "Example 2"); +is(reverse_prefix("abcdefghij", "h"), "hgfedcbaij", "Example 3"); +is(reverse_prefix("reverse", "s"), "srevere", "Example 4"); +is(reverse_prefix("perl", "r"), "repl", "Example 5"); +done_testing(); + +sub reverse_prefix +{ + my ($str, $char) = @_; + my $idx = index($str, $char, 0); + return reverse(substr($str, 0, $idx + 1)) . substr($str, $idx + 1); +} diff --git a/stats/pwc-challenge-259.json b/stats/pwc-challenge-259.json index 3b234b3618..55055b3204 100644 --- a/stats/pwc-challenge-259.json +++ b/stats/pwc-challenge-259.json @@ -1,26 +1,10 @@ { - "title" : { - "text" : "The Weekly Challenge - 259" - }, - "plotOptions" : { - "series" : { - "dataLabels" : { - "enabled" : 1, - "format" : "{point.y}" - }, - "borderWidth" : 0 - } - }, - "subtitle" : { - "text" : "[Champions: 30] Last updated at 2024-08-26 16:13:46 GMT" - }, - "legend" : { - "enabled" : 0 + "chart" : { + "type" : "column" }, "drilldown" : { "series" : [ { - "id" : "Adam Russell", "data" : [ [ "Perl", @@ -31,6 +15,7 @@ 1 ] ], + "id" : "Adam Russell", "name" : "Adam Russell" }, { @@ -62,17 +47,16 @@ "name" : "Athanasius" }, { - "name" : "BarrOff", "data" : [ [ "Raku", 1 ] ], - "id" : "BarrOff" + "id" : "BarrOff", + "name" : "BarrOff" }, { - "id" : "Bob Lied", "data" : [ [ "Perl", @@ -83,30 +67,30 @@ 1 ] ], + "id" : "Bob Lied", "name" : "Bob Lied" }, { - "name" : "Bruce Gray", "data" : [ [ "Raku", 2 ] ], - "id" : "Bruce Gray" + "id" : "Bruce Gray", + "name" : "Bruce Gray" }, { - "name" : "Cheok-Yin Fung", "data" : [ [ "Perl", 2 ] ], - "id" : "Cheok-Yin Fung" + "id" : "Cheok-Yin Fung", + "name" : "Cheok-Yin Fung" }, { - "name" : "Dave Jacoby", "data" : [ [ "Perl", @@ -117,51 +101,64 @@ 1 ] ], - "id" : "Dave Jacoby" + "id" : "Dave Jacoby", + "name" : "Dave Jacoby" }, { - "name" : "David Ferrone", "data" : [ [ "Perl", 2 ] ], - "id" : "David Ferrone" + "id" : "David Ferrone", + "name" : "David Ferrone" }, { - "id" : "E. Choroba", "data" : [ [ "Perl", 2 ] ], + "id" : "E. Choroba", "name" : "E. Choroba" }, { - "name" : "Feng Chang", "data" : [ [ "Raku", 2 ] ], - "id" : "Feng Chang" + "id" : "Feng Chang", + "name" : "Feng Chang" }, { - "name" : "Jan Krnavek", "data" : [ [ + "Perl", + 2 + ], + [ "Raku", 2 ] ], - "id" : "Jan Krnavek" + "id" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas" + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Jan Krnavek", + "name" : "Jan Krnavek" }, { - "name" : "Jorg Sommrey", - "id" : "Jorg Sommrey", "data" : [ [ "Perl", @@ -171,7 +168,9 @@ "Blog", 1 ] - ] + ], + "id" : "Jorg Sommrey", + "name" : "Jorg Sommrey" }, { "data" : [ @@ -202,37 +201,36 @@ "name" : "Luca Ferrari" }, { - "name" : "Mariano Spadaccini", "data" : [ [ "Perl", 1 ] ], - "id" : "Mariano Spadaccini" + "id" : "Mariano Spadaccini", + "name" : "Mariano Spadaccini" }, { - "name" : "Mark Anderson", - "id" : "Mark Anderson", "data" : [ [ "Raku", 1 ] - ] + ], + "id" : "Mark Anderson", + "name" : "Mark Anderson" }, { - "name" : "Matthew Neleigh", - "id" : "Matthew Neleigh", "data" : [ [ "Perl", 1 ] - ] + ], + "id" : "Matthew Neleigh", + "name" : "Matthew Neleigh" }, { - "id" : "Matthias Muth", "data" : [ [ "Perl", @@ -243,21 +241,20 @@ 1 ] ], + "id" : "Matthias Muth", "name" : "Matthias Muth" }, { - "name" : "Nelo Tovar", - "id" : "Nelo Tovar", "data" : [ [ "Perl", 2 ] - ] + ], + |
