From a39b92bf93df3522d4d0f726974515679050edaf Mon Sep 17 00:00:00 2001 From: rir Date: Sun, 23 Jun 2024 17:55:46 -0400 Subject: 247 --- challenge-274/0rir/raku/ch-1.raku | 68 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 challenge-274/0rir/raku/ch-1.raku diff --git a/challenge-274/0rir/raku/ch-1.raku b/challenge-274/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..8cfeecccd3 --- /dev/null +++ b/challenge-274/0rir/raku/ch-1.raku @@ -0,0 +1,68 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴ +use v6.d; +use Test; + +=begin comment +274-1: Goat Latin +Submitted by: Mohammad Sajid Anwar +You are given a sentence, $sentance. + +Write a script to convert the given sentence to Goat Latin, a made up language similar to Pig Latin. + +Rules for Goat Latin: + +1) If a word begins with a vowel ("a", "e", "i", "o", "u"), append + "ma" to the end of the word. +2) If a word begins with consonant i.e. not a vowel, remove first + letter and append it to the end then add "ma". +3) Add letter "a" to the end of first word in the sentence, "aa" to + the second word, etc etc. +Example 1 +Input: $sentence = "I love Perl" +Output: "Imaa ovelmaaa erlPmaaaa" +Example 2 +Input: $sentence = "Perl and Raku are friends" +Output: "erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa" +Example 3 +Input: $sentence = "The Weekly Challenge" +Output: "heTmaa eeklyWmaaa hallengeCmaaaa" +=end comment + +my @Test = + "I love Perl", + "Imaa ovelmaaa erlPmaaaa", + "Perl and Raku are friends", + "erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa", + "The Weekly Challenge", + "heTmaa eeklyWmaaa hallengeCmaaaa", +; +plan @Test ÷ 2; + +sub shift-push( Any(Str) $a --> Str) { + $a.substr(1) ~ $a.substr(0,1); +} + +sub task( $sentence --> Str) { + my Str $ret; + for $sentence.words -> $a is copy { + state $suffix-length; + ++$suffix-length; + if $a !~~ / ^ <[ a e i o u A E I O U ]> / { + $a = shift-push( $a); + } + $ret ~= $a ~= 'ma' ~ ('a' x $suffix-length) ~ ' '; + LAST $ret.=chop: 1; + } + $ret; +} + +for @Test -> $in, $exp { + is task($in), $exp, "$exp <- $in"; +} + +done-testing; + +my $sentence = "built for two"; +say qq{\nInput: \$sentence = "$sentence"\nOutput: }, task($sentence), qq{"}; + -- cgit From 53ad7a4fc2868f0f42d5aa0e0f81c93378733b3f Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sun, 23 Jun 2024 21:15:08 -0600 Subject: Joelle Maslak's solutions for week 275 --- challenge-275/joelle-maslak/perl/ch-1.pl | 14 ++++++++++++++ challenge-275/joelle-maslak/perl/ch-2.pl | 18 ++++++++++++++++++ challenge-275/joelle-maslak/raku/ch-1.raku | 6 ++++++ challenge-275/joelle-maslak/raku/ch-2.raku | 15 +++++++++++++++ challenge-275/joelle-maslak/test-1.sh | 28 ++++++++++++++++++++++++++++ challenge-275/joelle-maslak/test-2.sh | 28 ++++++++++++++++++++++++++++ 6 files changed, 109 insertions(+) create mode 100755 challenge-275/joelle-maslak/perl/ch-1.pl create mode 100755 challenge-275/joelle-maslak/perl/ch-2.pl create mode 100755 challenge-275/joelle-maslak/raku/ch-1.raku create mode 100755 challenge-275/joelle-maslak/raku/ch-2.raku create mode 100755 challenge-275/joelle-maslak/test-1.sh create mode 100755 challenge-275/joelle-maslak/test-2.sh diff --git a/challenge-275/joelle-maslak/perl/ch-1.pl b/challenge-275/joelle-maslak/perl/ch-1.pl new file mode 100755 index 0000000000..390a5febfd --- /dev/null +++ b/challenge-275/joelle-maslak/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl + +use v5.34; +use strict; +use warnings; + +MAIN: { + my $sentence = $ARGV[0]; + my $badkeys = $ARGV[1]; + + my $count = scalar grep {! /[$badkeys]/i} split /\s+/, $sentence; + + say $count; +} diff --git a/challenge-275/joelle-maslak/perl/ch-2.pl b/challenge-275/joelle-maslak/perl/ch-2.pl new file mode 100755 index 0000000000..05eca7c7a5 --- /dev/null +++ b/challenge-275/joelle-maslak/perl/ch-2.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +use JTM::Boilerplate 'script'; + +MAIN: { + my $input = $ARGV[0]; + + my $letter = "a"; + for my $current (split //, $input) { + if ($current =~ /^\d$/) { + print chr(ord($letter) + $current); + } else { + print $current; + $letter = $current; + } + } + say ""; +} diff --git a/challenge-275/joelle-maslak/raku/ch-1.raku b/challenge-275/joelle-maslak/raku/ch-1.raku new file mode 100755 index 0000000000..cfae04b31f --- /dev/null +++ b/challenge-275/joelle-maslak/raku/ch-1.raku @@ -0,0 +1,6 @@ +#!/usr/bin/env raku +use v6.d; + +sub MAIN($sentence, $badkeys) { + say $sentence.fc.words.grep({!(.comb ∩ $badkeys.fc.comb)}).elems; +} diff --git a/challenge-275/joelle-maslak/raku/ch-2.raku b/challenge-275/joelle-maslak/raku/ch-2.raku new file mode 100755 index 0000000000..7b4cd75af2 --- /dev/null +++ b/challenge-275/joelle-maslak/raku/ch-2.raku @@ -0,0 +1,15 @@ +#!/usr/bin/env raku +use v6.d; + +sub MAIN($input) { + my $letter = "a"; + for $input.comb() -> $current { + if ($current ~~ /^\d$/) { + print chr(ord($letter) + $current); + } else { + print $current; + $letter = $current; + } + } + say ""; +} \ No newline at end of file diff --git a/challenge-275/joelle-maslak/test-1.sh b/challenge-275/joelle-maslak/test-1.sh new file mode 100755 index 0000000000..9db4fc0ccc --- /dev/null +++ b/challenge-275/joelle-maslak/test-1.sh @@ -0,0 +1,28 @@ +#!/bin/bash +set -e + +test_it() { + echo -n $1 "'$2'" $3 ... + OUT=$($1 "$2" "$3") + if [ "$OUT" == "$4" ] ; then + echo "ok" + else + echo "INCORRECT ($OUT)" + fi +} + +test_combo() { + test_it "$1" "Perl Weekly Challenge" la 0 + test_it "$1" "Perl and Raku" a 1 + test_it "$1" "Well done Team PWC" lo 2 + test_it "$1" "The joys of polyglottism" T 2 +} + +do_it() { + test_combo "perl perl/ch-1.pl" + test_combo "raku raku/ch-1.raku" +} + +do_it "$@" + + diff --git a/challenge-275/joelle-maslak/test-2.sh b/challenge-275/joelle-maslak/test-2.sh new file mode 100755 index 0000000000..8d2d17e201 --- /dev/null +++ b/challenge-275/joelle-maslak/test-2.sh @@ -0,0 +1,28 @@ +#!/bin/bash +set -e + +test_it() { + echo -n $1 $2 ... + OUT=$($1 $2) + if [ "$OUT" == "$3" ] ; then + echo "ok" + else + echo "INCORRECT ($OUT)" + fi +} + +test_combo() { + test_it "$1" "a1c1e1" "abcdef" + test_it "$1" "a1b2c3d4" "abbdcfdh" + test_it "$1" "b2b" "bdb" + test_it "$1" "a16z" "abgz" +} + +do_it() { + test_combo "perl perl/ch-2.pl" + test_combo "raku raku/ch-2.raku" +} + +do_it "$@" + + -- cgit From ef8987dbde7ed84e63318b1ca43a895b4588dc0c Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Mon, 24 Jun 2024 08:48:45 +0200 Subject: Add solution 275 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-275/jeanluc2020/blog-1.txt | 1 + challenge-275/jeanluc2020/blog-2.txt | 1 + challenge-275/jeanluc2020/perl/ch-1.pl | 72 ++++++++++++++++++++++++++++++++ challenge-275/jeanluc2020/perl/ch-2.pl | 76 ++++++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 challenge-275/jeanluc2020/blog-1.txt create mode 100644 challenge-275/jeanluc2020/blog-2.txt create mode 100755 challenge-275/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-275/jeanluc2020/perl/ch-2.pl diff --git a/challenge-275/jeanluc2020/blog-1.txt b/challenge-275/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..d3164fc98c --- /dev/null +++ b/challenge-275/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-275-1.html diff --git a/challenge-275/jeanluc2020/blog-2.txt b/challenge-275/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..77a6c93758 --- /dev/null +++ b/challenge-275/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-275-2.html diff --git a/challenge-275/jeanluc2020/perl/ch-1.pl b/challenge-275/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..1e78eea602 --- /dev/null +++ b/challenge-275/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-275/#TASK1 +# +# Task 1: Broken Keys +# =================== +# +# You are given a sentence, $sentence and list of broken keys @keys. +# +# Write a script to find out how many words can be typed fully. +# +## Example 1 +## +## Input: $sentence = "Perl Weekly Challenge", @keys = ('l', 'a') +## Output: 0 +# +## Example 2 +## +## Input: $sentence = "Perl and Raku", @keys = ('a') +## Output: 1 +## +## Only Perl since the other word two words contain 'a' and can't be typed fully. +# +## Example 3 +## +## Input: $sentence = "Well done Team PWC", @keys = ('l', 'o') +## Output: 2 +# +## Example 4 +## +## Input: $sentence = "The joys of polyglottism", @keys = ('T') +## Output: 2 +# +############################################################ +## +## discussion +## +############################################################ +# +# We split the sentence into its words. Then we create an empty +# temporary list, and walk through the list of broken keys. If a +# word doesn't match the broken key, it will go into the temporary +# list, and at the end of each loop over the broken keys, the new +# list will be the list of words from the sentence that didn't +# match any broken keys so far, while the temporary list will be +# emptied again. This way, at the end of the loop over the broken +# keys, we only have the words that don't match any of the broken +# keys in the final list, of which we return the number of elements. + +use strict; +use warnings; + +broken_keys( "Perl Weekly Challenge", "l", "a" ); +broken_keys( "Perl and Raku", "a" ); +broken_keys( "Well done Team PWC", "l", "o" ); +broken_keys( "The joys of polyglottism", "T" ); + +sub broken_keys { + my ($sentence, @bk ) = @_; + print "Input: \$sentence = '$sentence', ('", join("', '", @bk), "')\n"; + my @words = split /\s+/, lc($sentence); + my @tmp = (); + foreach my $broken (@bk) { + $broken = lc($broken); + foreach my $word (@words) { + push @tmp, $word unless $word =~ m/$broken/; + } + @words = @tmp; + @tmp = (); + } + print "Output: ", scalar(@words), "\n"; +} + diff --git a/challenge-275/jeanluc2020/perl/ch-2.pl b/challenge-275/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..9b9d384d05 --- /dev/null +++ b/challenge-275/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,76 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-275/#TASK2 +# +# Task 2: Replace Digits +# ====================== +# +# You are given an alphanumeric string, $str, where each character is either a +# letter or a digit. +# +# Write a script to replace each digit in the given string with the value of +# the previous letter plus (digit) places. +# +## Example 1 +## +## Input: $str = 'a1c1e1' +## Ouput: 'abcdef' +## +## shift('a', 1) => 'b' +## shift('c', 1) => 'd' +## shift('e', 1) => 'f' +# +## Example 2 +## +## Input: $str = 'a1b2c3d4' +## Output: 'abbdcfdh' +## +## shift('a', 1) => 'b' +## shift('b', 2) => 'd' +## shift('c', 3) => 'f' +## shift('d', 4) => 'h' +# +## Example 3 +## +## Input: $str = 'b2b' +## Output: 'bdb' +# +## Example 4 +## +## Input: $str = 'a16z' +## Output: 'abgz' +# +############################################################ +## +## discussion +## +############################################################ +# +# Walk the $str character by character. If it is a digits, +# calculate the corresponding new character for the result by +# using the previous character and the digit, otherwise just +# append the current character and take note of the character +# for the next round. + +use strict; +use warnings; + +replace_digits('a1c1e1'); +replace_digits('a1b2c3d4'); +replace_digits('b2b'); +replace_digits('a16z'); + +sub replace_digits { + my $str = shift; + print "Input: '$str'\n"; + my $previous_char = "a"; + my $result = ""; + foreach my $char (split //, $str) { + if($char =~ m/\d/) { + $result .= chr(ord($previous_char) + $char); + } else { + $result .= $char; + $previous_char = $char; + } + } + print "Output: '$result'\n"; +} -- cgit From bfd1832bbbda9d5b99459af6bf5a28e180316a99 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 24 Jun 2024 07:55:57 +0000 Subject: w275 - Task 1 & 2 --- challenge-275/perlboy1967/perl/ch1.pl | 35 ++++++++++++++++++++++++++++++ challenge-275/perlboy1967/perl/ch2.pl | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100755 challenge-275/perlboy1967/perl/ch1.pl create mode 100755 challenge-275/perlboy1967/perl/ch2.pl diff --git a/challenge-275/perlboy1967/perl/ch1.pl b/challenge-275/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..624b8fb931 --- /dev/null +++ b/challenge-275/perlboy1967/perl/ch1.pl @@ -0,0 +1,35 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 275 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-275 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Broken Keys +Submitted by: Mohammad Sajid Anwar + +You are given a sentence, $sentence and list of broken keys @keys. + +Write a script to find out how many words can be typed fully. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0 qw(-no_srand); + +sub brokenKeys ($sentence,@keys) { + my $re = '['.join('',@keys).']'; + scalar grep !/$re/i, split /\s+/, $sentence; +} + +is(brokenKeys('Perl Weekly Challenge','l','a'),0); +is(brokenKeys('Perl and Raku','a'),1); +is(brokenKeys('Well done Team PWC','l','o'),2); +is(brokenKeys('The joys of polyglottism','T'),2); + +done_testing; diff --git a/challenge-275/perlboy1967/perl/ch2.pl b/challenge-275/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..ccd347c497 --- /dev/null +++ b/challenge-275/perlboy1967/perl/ch2.pl @@ -0,0 +1,41 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 275 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-275 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Replace Digits +Submitted by: Mohammad Sajid Anwar + +You are given an alphanumeric string, $str, where each character +is either a letter or a digit. + +Write a script to replace each digit in the given string with the +value of the previous letter plus (digit) places. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0 qw(-no_srand); + +sub replaceDigits ($str) { + my $r; + while ($str =~ s#^(?[a-z])(?\d+)##i) { + $r .= $+{c} . join('', map { chr(ord($+{c})+$_) } split(//,$+{d})); + } + return $r.$str; +} + +is(replaceDigits('a1c1e1'),'abcdef','Example 1'); +is(replaceDigits('a1b2c3d4'),'abbdcfdh','Example 2'); +is(replaceDigits('b2b'),'bdb','Example 3'); +is(replaceDigits('a16z'),'abgz','Example 4'); +is(replaceDigits('Z5'),'Z_','Own test'); + +done_testing; -- cgit From 8a4ca153cebf203e1849bddd21beef4711474d70 Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Mon, 24 Jun 2024 17:35:50 +0800 Subject: challenge 275, raku solutions --- challenge-275/feng-chang/raku/ch-1.raku | 6 ++++++ challenge-275/feng-chang/raku/ch-2.raku | 10 ++++++++++ challenge-275/feng-chang/raku/test.raku | 26 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100755 challenge-275/feng-chang/raku/ch-1.raku create mode 100755 challenge-275/feng-chang/raku/ch-2.raku create mode 100755 challenge-275/feng-chang/raku/test.raku diff --git a/challenge-275/feng-chang/raku/ch-1.raku b/challenge-275/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..9dd4df3d57 --- /dev/null +++ b/challenge-275/feng-chang/raku/ch-1.raku @@ -0,0 +1,6 @@ +#!/bin/env raku + +unit sub MAIN(*@words); + +my @broken = @words.tail.lc.comb; +put +@words.grep(*.lc.comb.any ne @broken.any); diff --git a/challenge-275/feng-chang/raku/ch-2.raku b/challenge-275/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..ef35e4272f --- /dev/null +++ b/challenge-275/feng-chang/raku/ch-2.raku @@ -0,0 +1,10 @@ +#!/bin/env raku + +unit sub MAIN(Str:D $s); + +my $c; +for $s.comb { + when 0..9 { print ($c.ord + $_).chr } + default { $c = $_; .print } +} +put ''; diff --git a/challenge-275/feng-chang/raku/test.raku b/challenge-275/feng-chang/raku/test.raku new file mode 100755 index 0000000000..8d88f59ff9 --- /dev/null +++ b/challenge-275/feng-chang/raku/test.raku @@ -0,0 +1,26 @@ +#!/bin/env raku + +# The Weekly Challenge 275 +use Test; + +sub pwc-test(Str:D $script, Bool :$deeply? = False, *@input) { + my ($expect, $assertion) = @input.splice(*-2, 2); + my $p = run $script, |@input, :out; + if $deeply { + is-deeply $p.out.slurp(:close).chomp.words.Bag, $expect, $assertion; + } else { + is $p.out.slurp(:close).chomp, $expect, $assertion; + } +} + +# Task 1, Broken Keys +pwc-test './ch-1.raku', , 'la', 0, 'Broken Keys: "Perl Weekly Challenge", => 0'; +pwc-test './ch-1.raku', , 'a', 1, 'Broken Keys: "Perl and Raku", => 1'; +pwc-test './ch-1.raku', , 'lo', 2, 'Broken Keys: "Well done Team PWC", => 2'; +pwc-test './ch-1.raku', , 'T', 2, 'Broken Keys: "The joys of polyglottism", => 2'; + +# Task 2, Replace Digits +pwc-test './ch-2.raku', 'a1c1e1', 'abcdef', 'Replace Digits: "a1c1e1" => "abcdef"'; +pwc-test './ch-2.raku', 'a1b2c3d4', 'abbdcfdh', 'Replace Digits: "a1b2c3d4" => "abbdcfdh"'; +pwc-test './ch-2.raku', 'b2b', 'bdb', 'Replace Digits: "b2b" => "bdb"'; +pwc-test './ch-2.raku', 'a16z', 'abgz', 'Replace Digits: "a16z" => "abgz"'; -- cgit From e3413e5215e89c8512218669be7701def126c9cd Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 24 Jun 2024 09:55:54 +0200 Subject: PWC 275 Task 1 Raku done Task 2 Raku done Task 1 PL/Perl done Task 2 PL/Perl done Task 1 PL/PgSQL done Task 2 PL/PgSQL done Task 1 PL/Java done Task 2 PL/Java done Task 1 Python done Task 2 Python done --- challenge-275/luca-ferrari/blog-1.txt | 1 + challenge-275/luca-ferrari/blog-10.txt | 1 + challenge-275/luca-ferrari/blog-2.txt | 1 + challenge-275/luca-ferrari/blog-3.txt | 1 + challenge-275/luca-ferrari/blog-4.txt | 1 + challenge-275/luca-ferrari/blog-5.txt | 1 + challenge-275/luca-ferrari/blog-6.txt | 1 + challenge-275/luca-ferrari/blog-7.txt | 1 + challenge-275/luca-ferrari/blog-8.txt | 1 + challenge-275/luca-ferrari/blog-9.txt | 1 + challenge-275/luca-ferrari/pljava/pom.xml | 72 +++++++++++++++++++++ .../luca-ferrari/pljava/src/main/java/Task1.java | 69 ++++++++++++++++++++ .../luca-ferrari/pljava/src/main/java/Task2.java | 74 ++++++++++++++++++++++ challenge-275/luca-ferrari/plperl/ch-1.plperl | 30 +++++++++ challenge-275/luca-ferrari/plperl/ch-2.plperl | 34 ++++++++++ challenge-275/luca-ferrari/plpgsql/ch-1.sql | 37 +++++++++++ challenge-275/luca-ferrari/plpgsql/ch-2.sql | 32 ++++++++++ challenge-275/luca-ferrari/python/ch-1.py | 34 ++++++++++ challenge-275/luca-ferrari/python/ch-2.py | 32 ++++++++++ challenge-275/luca-ferrari/raku/ch-1.raku | 25 ++++++++ challenge-275/luca-ferrari/raku/ch-2.raku | 35 ++++++++++ 21 files changed, 484 insertions(+) create mode 100644 challenge-275/luca-ferrari/blog-1.txt create mode 100644 challenge-275/luca-ferrari/blog-10.txt create mode 100644 challenge-275/luca-ferrari/blog-2.txt create mode 100644 challenge-275/luca-ferrari/blog-3.txt create mode 100644 challenge-275/luca-ferrari/blog-4.txt create mode 100644 challenge-275/luca-ferrari/blog-5.txt create mode 100644 challenge-275/luca-ferrari/blog-6.txt create mode 100644 challenge-275/luca-ferrari/blog-7.txt create mode 100644 challenge-275/luca-ferrari/blog-8.txt create mode 100644 challenge-275/luca-ferrari/blog-9.txt create mode 100644 challenge-275/luca-ferrari/pljava/pom.xml create mode 100644 challenge-275/luca-ferrari/pljava/src/main/java/Task1.java create mode 100644 challenge-275/luca-ferrari/pljava/src/main/java/Task2.java create mode 100644 challenge-275/luca-ferrari/plperl/ch-1.plperl create mode 100644 challenge-275/luca-ferrari/plperl/ch-2.plperl create mode 100644 challenge-275/luca-ferrari/plpgsql/ch-1.sql create mode 100644 challenge-275/luca-ferrari/plpgsql/ch-2.sql create mode 100644 challenge-275/luca-ferrari/python/ch-1.py create mode 100644 challenge-275/luca-ferrari/python/ch-2.py create mode 100644 challenge-275/luca-ferrari/raku/ch-1.raku create mode 100644 challenge-275/luca-ferrari/raku/ch-2.raku diff --git a/challenge-275/luca-ferrari/blog-1.txt b/challenge-275/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..db1be1abf8 --- /dev/null +++ b/challenge-275/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task1 diff --git a/challenge-275/luca-ferrari/blog-10.txt b/challenge-275/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..6bd6046323 --- /dev/null +++ b/challenge-275/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge275.html#task2pljava diff --git a/challenge-275/luca-ferrari/blog-2.txt b/challenge-275/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..34fc986234 --- /dev/null +++ b/challenge-275/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task2 diff --git a/challenge-275/luca-ferrari/blog-3.txt b/challenge-275/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..0ba72be87f --- /dev/null +++ b/challenge-275/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task1plperl diff --git a/challenge-275/luca-ferrari/blog-4.txt b/challenge-275/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..bc30ee141c --- /dev/null +++ b/challenge-275/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task2plperl diff --git a/challenge-275/luca-ferrari/blog-5.txt b/challenge-275/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..3a96070b9e --- /dev/null +++ b/challenge-275/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task1plpgsql diff --git a/challenge-275/luca-ferrari/blog-6.txt b/challenge-275/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..da55d40190 --- /dev/null +++ b/challenge-275/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task2plpgsql diff --git a/challenge-275/luca-ferrari/blog-7.txt b/challenge-275/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..637169aa7a --- /dev/null +++ b/challenge-275/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task1python diff --git a/challenge-275/luca-ferrari/blog-8.txt b/challenge-275/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..5070b3d968 --- /dev/null +++ b/challenge-275/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task2python diff --git a/challenge-275/luca-ferrari/blog-9.txt b/challenge-275/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..6d122588c0 --- /dev/null +++ b/challenge-275/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task1pljava diff --git a/challenge-275/luca-ferrari/pljava/pom.xml b/challenge-275/luca-ferrari/pljava/pom.xml new file mode 100644 index 0000000000..ea10099725 --- /dev/null +++ b/challenge-275/luca-ferrari/pljava/pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + + PWC + + PWC275 + + + 1 + + + Perl Weekly Challenge 275 with package PWC275 + Implementation of the tasks in PL/Java for PWC 275 + + + US-ASCII + + + + + org.postgresql + pljava-api + 1.6.6 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 9 + + + + org.apache.maven.plugins + maven-jar-plugin + 2.6 + + + + + + true + + + + + + + pljava.ddr + + + true + + + + + + + + + + diff --git a/challenge-275/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-275/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..2e27bd455b --- /dev/null +++ b/challenge-275/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,69 @@ + + + +package PWC275; + +/** + * PL/Java implementation for PWC 275 + * Task 1 + * See + * + * + * To compile on the local machine: + + $ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ # if not already set + $ mvn clean build + $ scp target/PWC275-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC275-1.jar', 'PWC275', true ); + select sqlj.set_classpath( 'public', 'PWC275' ); + + select pwc275.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC275-1.jar', 'PWC275', true ); + +*/ + +import org.postgresql.pljava.*; +import org.postgresql.pljava.annotation.Function; +import static org.postgresql.pljava.annotation.Function.Effects.IMMUTABLE; +import static org.postgresql.pljava.annotation.Function.OnNullInput.RETURNS_NULL; + +import java.util.*; +import java.util.regex.*; +import java.util.stream.*; +import java.sql.SQLException; +import java.util.logging.*; +import java.sql.ResultSet; +import java.sql.Date; + +public class Task1 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc275", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task1_pljava( String sentence, String[] keys ) throws SQLException { + logger.log( Level.INFO, "Entering pwc275.task1_pljava" ); + + List patterns = Stream.of( keys ) + .map( letter -> { return Pattern.compile( String.format( "%s", letter ), Pattern.CASE_INSENSITIVE ); } ) + .collect( Collectors.toList() ); + + return (int) Stream.of( sentence.split( "\\s+" ) ) + .filter( word -> { + for ( Pattern p : patterns ) + if ( p.matcher( word ).find() ) + return false; + + return true; + } ).count(); + + } +} diff --git a/challenge-275/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-275/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..9778016704 --- /dev/null +++ b/challenge-275/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,74 @@ + + + +package PWC275; + +/** + * PL/Java implementation for PWC 275 + * Task 2 + * See + * + * + * To compile on the local machine: + + $ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ # if not already set + $ mvn clean build + $ scp target/PWC275-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC275-1.jar', 'PWC275', true ); + select sqlj.set_classpath( 'public', 'PWC275' ); + + select pwc275.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC275-1.jar', 'PWC275', true ); + +*/ + +import org.postgresql.pljava.*; +import org.postgresql.pljava.annotation.Function; +import static org.postgresql.pljava.annotation.Function.Effects.IMMUTABLE; +import static org.postgresql.pljava.annotation.Function.OnNullInput.RETURNS_NULL; + +import java.util.*; +import java.util.regex.*; +import java.util.stream.*; +import java.sql.SQLException; +import java.util.logging.*; +import java.sql.ResultSet; +import java.sql.Date; + +public class Task2 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc275", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final String task2_pljava( String text ) throws SQLException { + logger.log( Level.INFO, "Entering pwc275.task2_pljava" ); + + Pattern digit = Pattern.compile( "\\d", Pattern.CASE_INSENSITIVE ); + final String[] previous = new String[ 1 ]; + + return Stream.of( text.split( "" ) ) + .map( current -> { + if ( digit.matcher( current ).find() ) { + // a digit + + current = String.format( "%c", ( (int) previous[ 0 ].charAt( 0 ) + Integer.parseInt( current ) ) ); + + } + else { + previous[ 0 ] = current; + } + + return current; + } ) + .collect( Collectors.joining("") ); + } +} diff --git a/challenge-275/luca-ferrari/plperl/ch-1.plperl b/challenge-275/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..6fb40b9988 --- /dev/null +++ b/challenge-275/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,30 @@ +-- +-- Perl Weekly Challenge 275 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc275; + +CREATE OR REPLACE FUNCTION +pwc275.task1_plperl( text, text[] ) +RETURNS int +AS $CODE$ + + my ( $string, $keys ) = @_; + my @ok_words; + + for my $word ( split /\s+/, lc $string ) { + my $ok = 1; + for my $wrong_key ( $keys->@* ) { + $ok = 0 if ( $word =~ /$wrong_key/ ); + last if ( ! $ok ); + } + + push @ok_words, $word if ( $ok ); + } + + return scalar( @ok_words ); + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-275/luca-ferrari/plperl/ch-2.plperl b/challenge-275/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..1325119d11 --- /dev/null +++ b/challenge-275/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,34 @@ +-- +-- Perl Weekly Challenge 275 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc275; + +CREATE OR REPLACE FUNCTION +pwc275.task2_plperl( text ) +RETURNS text +AS $CODE$ + + my ( $string ) = @_; + my @alphabet = 'a' .. 'z'; + $string = lc $string; + my @result; + my $previous; + + for my $letter ( split //, $string ) { + if ( $letter =~ /[a-z]/ ) { + $previous = $letter; + } + else { + $letter = chr( ord( 'a' ) + int( $letter ) ); + } + + push @result, $letter; + } + + return join( '', @result ); + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-275/luca-ferrari/plpgsql/ch-1.sql b/challenge-275/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..4be873b80d --- /dev/null +++ b/challenge-275/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,37 @@ +-- +-- Perl Weekly Challenge 275 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc275; + +CREATE OR REPLACE FUNCTION +pwc275.task1_plpgsql( s text, k text[] ) +RETURNS int +AS $CODE$ +DECLARE + ok boolean; + current_k text; + current text; + word_counter int := 0; +BEGIN + + FOR current IN SELECT word FROM regexp_split_to_table( s, '\s+' ) word LOOP + ok := true; + + FOREACH current_k IN ARRAY k LOOP + IF current ~ current_k THEN + ok := false; + END IF; + END LOOP; + + IF ok THEN + word_counter := word_counter + 1; + END IF; + END LOOP; + + RETURN word_counter; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-275/luca-ferrari/plpgsql/ch-2.sql b/challenge-275/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..3d5642972d --- /dev/null +++ b/challenge-275/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,32 @@ +-- +-- Perl Weekly Challenge 275 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc275; + +CREATE OR REPLACE FUNCTION +pwc275.task2_plpgsql( s text ) +RETURNS text +AS $CODE$ +DECLARE + output text := ''; + previous text; + c text; +BEGIN + + FOR c IN SELECT v FROM regexp_split_to_table( s, '' ) v LOOP + IF c ~ '[a-z]' THEN + previous := c; + ELSE + c := chr( c::int + ascii( 'a' ) ); + END IF; + + output := output || c; + END LOOP; + + return output; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-275/luca-ferrari/python/ch-1.py b/challenge-275/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..f131b1e602 --- /dev/null +++ b/challenge-275/luca-ferrari/python/ch-1.py @@ -0,0 +1,34 @@ +#!python + +# +# Perl Weekly Challenge 275 +# Task 1 +# +# See +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + sentence = args[ 0 ] + keys = args[ 1: ] + words_ok = 0 + + for word in sentence.lower().split(): + bad = 0 + for k in keys: + k = k.lower() + if k in word: + bad += 1 + break + + if bad == 0: + words_ok += 1 + + return words_ok + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-275/luca-ferrari/python/ch-2.py b/challenge-275/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..ebe16641b0 --- /dev/null +++ b/challenge-275/luca-ferrari/python/ch-2.py @@ -0,0 +1,32 @@ +#!python + +# +# Perl Weekly Challenge 275 +# Task 2 +# +# See +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + string = args[ 0 ] + previous = None + result = "" + + for c in string: + if c.isdigit(): + c = chr( int( c ) + 97 ) + else: + previous = c + + result += c + + return result + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-275/luca-ferrari/raku/ch-1.raku b/challenge-275/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..6d8a2faac3 --- /dev/null +++ b/challenge-275/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,25 @@ +#!raku + +# +# Perl Weekly Challenge 275 +# Task 1 +# +# See +# + +sub MAIN( Str $sentence, *@keys ) { + my @ok-words; + + for $sentence.lc.split( / \s+ / ) -> $word { + my $ok = True; + + for @keys -> $needle { + $ok = False if ( $word ~~ / $needle / ); + last if ! $ok; + } + + @ok-words.push: $word if $ok; + } + + @ok-words.elems.say; +} diff --git a/challenge-275/luca-ferrari/raku/ch-2.raku b/challenge-275/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..a3f40351f0 --- /dev/null +++ b/challenge-275/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,35 @@ +#!raku + +# +# Perl Weekly Challenge 275 +# Task 2 +# +# See +# + +sub MAIN( Str $string where { $string ~~ / ^ <[a..zA..Z]> <[a..zA..Z0..9]>+ $ / } ) { + my $previous; + my @result; + + my $index = 0; + my %alphabet; + %alphabet{ $_ } = $index++ for 'a' .. 'z'; + + + + $string.comb.map( -> $current is copy { + if ( $current.lc ~~ / <[a..z]> / ) { + # it is a letter + $previous = $current; + } + else { + # it is a number + $current = %alphabet.pairs.grep( { $_.value == ( %alphabet{ $previous } + $current.Int ) } )[ 0 ].key; + } + + @result.push: $current; + } ); + + + @result.join.say; +} -- cgit From 5129e0b9516902c4b1ed68e1061888d2d799dfc6 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 24 Jun 2024 18:34:30 +1000 Subject: pwc275 solution in python --- challenge-275/pokgopun/python/ch-1.py | 59 +++++++++++++++++++++++ challenge-275/pokgopun/python/ch-2.py | 91 +++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 challenge-275/pokgopun/python/ch-1.py create mode 100644 challenge-275/pokgopun/python/ch-2.py diff --git a/challenge-275/pokgopun/python/ch-1.py b/challenge-275/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..181a07ee0d --- /dev/null +++ b/challenge-275/pokgopun/python/ch-1.py @@ -0,0 +1,59 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-275/ +""" + +Task 1: Broken Keys + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a sentence, $sentence and list of broken keys @keys. + + Write a script to find out how many words can be typed fully. + +Example 1 + +Input: $sentence = "Perl Weekly Challenge", @keys = ('l', 'a') +Output: 0 + +Example 2 + +Input: $sentence = "Perl and Raku", @keys = ('a') +Output: 1 + +Only Perl since the other word two words contain 'a' and can't be typed fully. + +Example 3 + +Input: $sentence = "Well done Team PWC", @keys = ('l', 'o') +Output: 2 + +Example 4 + +Input: $sentence = "The joys of polyglottism", @keys = ('T') +Output: 2 + +Task 2: Replace Digits +""" +### solution by pokgopun@gmail.com + +def bk(sentence: str, keys: tuple): + keys = set(e.lower() for e in keys) + return sum( + 1 for e in sentence.split() if set(e.lower()).intersection(keys) == set() + ) + +import unittest + +class TestBk(unittest.TestCase): + def test(self): + for (sentence, keys), otpt in { + ("Perl Weekly Challenge", ('l', 'a')): 0, + ("Perl and Raku", ('a',)): 1, + ("Well done Team PWC", ('l', 'o')): 2, + ("The joys of polyglottism", ('T',)): 2, + }.items(): + self.assertEqual(bk(sentence,keys),otpt) + +unittest.main() + + diff --git a/challenge-275/pokgopun/python/ch-2.py b/challenge-275/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..c842eca8fc --- /dev/null +++ b/challenge-275/pokgopun/python/ch-2.py @@ -0,0 +1,91 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-275/ +""" + +Task 2: Replace Digits + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an alphanumeric string, $str, where each character is + either a letter or a digit. + + Write a script to replace each digit in the given string with the value + of the previous letter plus (digit) places. + +Example 1 + +Input: $str = 'a1c1e1' +Ouput: 'abcdef' + +shift('a', 1) => 'b' +shift('c', 1) => 'd' +shift('e', 1) => 'f' + +Example 2 + +Input: $str = 'a1b2c3d4' +Output: 'abbdcfdh' + +shift('a', 1) => 'b' +shift('b', 2) => 'd' +shift('c', 3) => 'f' +shift('d', 4) => 'h' + +Example 3 + +Input: $str = 'b2b' +Output: 'bdb' + +Example 4 + +Input: $str = 'a16z' +Output: 'abgz' + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 30th June 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def replaceDigits1(string: str): + r = list(string) + for i in range(len(r)): + if r[i].isalpha(): + prv = r[i] + else: + r[i] = chr(ord(prv)+int(r[i])) + return "".join(r) + +def replaceDigits2(string: str): + r = "" + for c in string: + if c.isalpha(): + prv = c + r += c + else: + r += chr(ord(prv)+int(c)) + return r + +import unittest + +class TestReplaceDigits(unittest.TestCase): + def test1(self): + for inpt,otpt in { + 'a1c1e1': 'abcdef', + 'a1b2c3d4': 'abbdcfdh', + 'b2b': 'bdb', + 'a16z': 'abgz', + }.items(): + self.assertEqual(replaceDigits1(inpt),otpt) + def test2(self): + for inpt,otpt in { + 'a1c1e1': 'abcdef', + 'a1b2c3d4': 'abbdcfdh', + 'b2b': 'bdb', + 'a16z': 'abgz', + }.items(): + self.assertEqual(replaceDigits2(inpt),otpt) + +unittest.main() -- cgit From e47302d99328b696544a2fcf1457f86aa5b3c5ab Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 24 Jun 2024 21:10:19 +1000 Subject: pwc275 solution in go --- challenge-275/pokgopun/go/ch-1.go | 84 +++++++++++++++++++++++++++++++++++++ challenge-275/pokgopun/go/ch-2.go | 88 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 challenge-275/pokgopun/go/ch-1.go create mode 100644 challenge-275/pokgopun/go/ch-2.go diff --git a/challenge-275/pokgopun/go/ch-1.go b/challenge-275/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..c9524faa19 --- /dev/null +++ b/challenge-275/pokgopun/go/ch-1.go @@ -0,0 +1,84 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-275/ +/*# + +Task 1: Broken Keys + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a sentence, $sentence and list of broken keys @keys. + + Write a script to find out how many words can be typed fully. + +Example 1 + +Input: $sentence = "Perl Weekly Challenge", @keys = ('l', 'a') +Output: 0 + +Example 2 + +Input: $sentence = "Perl and Raku", @keys = ('a') +Output: 1 + +Only Perl since the other word two words contain 'a' and can't be typed fully. + +Example 3 + +Input: $sentence = "Well done Team PWC", @keys = ('l', 'o') +Output: 2 + +Example 4 + +Input: $sentence = "The joys of polyglottism", @keys = ('T') +Output: 2 + +Task 2: Replace Digits +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "strings" + + "github.com/google/go-cmp/cmp" +) + +type chars []rune + +func bk(st string, cs chars) int { + words := strings.Split(st, " ") + l := len(words) + n := l + var c rune + for l > 0 { + l-- + for _, c = range cs { + if c >= 'A' && c <= 'Z' { + c += 32 + } + if strings.ContainsRune(strings.ToLower(words[l]), c) { + n-- + break + } + } + } + return n +} + +func main() { + for _, data := range []struct { + sentence string + chars chars + count int + }{ + {"Perl Weekly Challenge", chars{'l', 'a'}, 0}, + {"Perl and Raku", chars{'a'}, 1}, + {"Well done Team PWC", chars{'l', 'o'}, 2}, + {"The joys of polyglottism", chars{'T'}, 2}, + } { + io.WriteString(os.Stdout, cmp.Diff(bk(data.sentence, data.chars), data.count)) // blank if ok, otherwise show the differences + } +} diff --git a/challenge-275/pokgopun/go/ch-2.go b/challenge-275/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..c8b76fbf44 --- /dev/null +++ b/challenge-275/pokgopun/go/ch-2.go @@ -0,0 +1,88 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-275/ +/*# + +Task 2: Replace Digits + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an alphanumeric string, $str, where each character is + either a letter or a digit. + + Write a script to replace each digit in the given string with the value + of the previous letter plus (digit) places. + +Example 1 + +Input: $str = 'a1c1e1' +Ouput: 'abcdef' + +shift('a', 1) => 'b' +shift('c', 1) => 'd' +shift('e', 1) => 'f' + +Example 2 + +Input: $str = 'a1b2c3d4' +Output: 'abbdcfdh' + +shift('a', 1) => 'b' +shift('b', 2) => 'd' +shift('c', 3) => 'f' +shift('d', 4) => 'h' + +Example 3 + +Input: $str = 'b2b' +Output: 'bdb' + +Example 4 + +Input: $str = 'a16z' +Output: 'abgz' + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 30th June 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func replaceDigits(str string) string { + bs := []byte(str) + var ( + b byte + i int + ) + for i = range len(bs) { + if bs[i] >= 48 && bs[i] <= 57 { + bs[i] = b + bs[i] - 48 + } else { + b = bs[i] + } + } + return string(bs) +} + +func main() { + for _, data := range []struct { + input, output string + }{ + {"a1c1e1", "abcdef"}, + {"a1b2c3d4", "abbdcfdh"}, + {"b2b", "bdb"}, + {"a16z", "abgz"}, + } { + io.WriteString(os.Stdout, cmp.Diff(replaceDigits(data.input), data.output)) // blank if ok, otherwise show the differences + } +} -- cgit From 87a17ee467b49dd156cec7571f67dfb4120159b0 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 24 Jun 2024 12:54:34 +0000 Subject: Challenge 275 Solutions (Raku) --- challenge-275/mark-anderson/raku/ch-1.raku | 15 +++++++++++++++ challenge-275/mark-anderson/raku/ch-2.raku | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 challenge-275/mark-anderson/raku/ch-1.raku create mode 100644 challenge-275/mark-anderson/raku/ch-2.raku diff --git a/challenge-275/mark-anderson/raku/ch-1.raku b/challenge-275/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..2fd24dc19a --- /dev/null +++ b/challenge-275/mark-anderson/raku/ch-1.raku @@ -0,0 +1,15 @@ +#!/usr/bin/env raku +use Test; + +is broken-keys("Perl Weekly Challenger", ), 0; +is broken-keys("Perl and Raku", ), 1; +is broken-keys("Well done team PWC", ), 2; +is broken-keys("The joys of polyglottism", ), 2; + +sub broken-keys +{ + my $s = $^a.fc; + my $k = $^b>>.fc; + + + $s.words.grep({ .comb.any !~~ $k.any }) +} diff --git a/challenge-275/mark-anderson/raku/ch-2.raku b/challenge-275/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..412bfea82f --- /dev/null +++ b/challenge-275/mark-anderson/raku/ch-2.raku @@ -0,0 +1,21 @@ +#!/usr/bin/env raku +use Test; + +is replace-digits('a1c1e1'), 'abcdef'; +is replace-digits('a1b2c3d4'), 'abbdcfdh'; +is replace-digits('b2b'), 'bdb'; +is replace-digits('a16z'), 'abgz'; +is replace-digits('a16bzd7'), 'abgdk'; + +sub replace-digits($s) +{ + my ($head, $tail) = $s.split(/ <.alpha>+ $ /, :v); + + my @result = do for $head.split(/\d+/, :v:skip-empty).batch(2) + { + my $char = .head ~~ / <.alpha> $ /; + $char ~ [~] .tail.comb.map({ chr($char.ord + $_) }) + } + + ([~] @result) ~ ($tail or Empty) +} -- cgit From 0bf1a8f78a7032c192a341803615ce331e30306a Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 24 Jun 2024 13:07:46 +0000 Subject: Challenge 275 Solutions (Raku) --- challenge-275/mark-anderson/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-275/mark-anderson/raku/ch-2.raku b/challenge-275/mark-anderson/raku/ch-2.raku index 412bfea82f..e88360e34e 100644 --- a/challenge-275/mark-anderson/raku/ch-2.raku +++ b/challenge-275/mark-anderson/raku/ch-2.raku @@ -11,7 +11,7 @@ sub replace-digits($s) { my ($head, $tail) = $s.split(/ <.alpha>+ $ /, :v); - my @result = do for $head.split(/\d+/, :v:skip-empty).batch(2) + my @result = do for $head.split(/ <.digit>+ /, :v:skip-empty).batch(2) { my $char = .head ~~ / <.alpha> $ /; $char ~ [~] .tail.comb.map({ chr($char.ord + $_) }) -- cgit From d557fb075504f51ce68bd5972a29fc8f56932c38 Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 24 Jun 2024 15:01:02 +0100 Subject: add solutions week 275 in python --- challenge-275/steven-wilson/python/ch-1.py | 22 ++++++++++++++++++ challenge-275/steven-wilson/python/ch-2.py | 37 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 challenge-275/steven-wilson/python/ch-1.py create mode 100644 challenge-275/steven-wilson/python/ch-2.py diff --git a/challenge-275/steven-wilson/python/ch-1.py b/challenge-275/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..1d4026cdc2 --- /dev/null +++ b/challenge-275/steven-wilson/python/ch-1.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + + +def typed_fully(sentence, *keys): + ''' Given a sentence and list of broken keys, return number of words can be + typed fully. + >>> typed_fully("Perl Weekly Challenge", 'l', 'a') + 0 + >>> typed_fully("Perl and Raku", 'a') + 1 + >>> typed_fully("Well done Team PWC", 'l', 'o') + 2 + >>> typed_fully("The joys of polyglottism", 'T') + 2 + ''' + return sum(1 for word in sentence.split() if not any(key.casefold() in word.casefold() for key in keys)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) diff --git a/challenge-275/steven-wilson/python/ch-2.py b/challenge-275/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..d215aadbe9 --- /dev/null +++ b/challenge-275/steven-wilson/python/ch-2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + + +def replace_digits(string): + ''' Given an alphanumeric string, where each character is either a letter + or a digit, replace each digit in the given string with the value of the + previous letter plus (digit) places. + >>> replace_digits('a1c1e1') + 'abcdef' + >>> replace_digits('a1b2c3d4') + 'abbdcfdh' + >>> replace_digits('b2b') + 'bdb' + >>> replace_digits('a16z') + 'abgz' + ''' + if not string[0].isalpha(): + raise ValueError('First character of string should be letter.') + + if not string.isalnum(): + raise ValueError('String should be alphanumeric') + + previous_letter = None + result = [] + for c in string: + if c.isdigit(): + result.append(chr(ord(previous_letter) + int(c))) + else: + result.append(c) + previous_letter = c + return "".join(result) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) -- cgit From 2a181d86de5581d752d7542c76ae6091d7d760c7 Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 24 Jun 2024 17:03:15 +0100 Subject: Week 275 - Broken digits --- challenge-275/peter-campbell-smith/blog.txt | 1 + challenge-275/peter-campbell-smith/perl/ch-1.pl | 36 ++++++++++++++++++++++++ challenge-275/peter-campbell-smith/perl/ch-2.pl | 37 +++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 challenge-275/peter-campbell-smith/blog.txt create mode 100755 challenge-275/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-275/peter-campbell-smith/perl/ch-2.pl diff --git a/challenge-275/peter-campbell-smith/blog.txt b/challenge-275/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..7a7f960cc1 --- /dev/null +++ b/challenge-275/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/275 diff --git a/challenge-275/peter-campbell-smith/perl/ch-1.pl b/challenge-275/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..464bc4ee42 --- /dev/null +++ b/challenge-275/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-06-24 +use utf8; # Week 275 - task 1 - Broken keys +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +# You are given a sentence !!$sentence!! and list of broken keys !!@keys!!. +# Write a script to find out how many words can be typed fully. + +my ($sentence, @keys); + +broken_keys('Perl Weekly Challenge', 'l', 'a'); +broken_keys('The joys of polyglottism', 'T'); +broken_keys('Write a script to find out how many words can be typed fully', 'i', 'o'); +broken_keys('All cows eat grass', 'b', 'd', 'f', 'h', 'i'); +broken_keys('Vitamins keep you healthy', 'v', 'k', 'u', 'y'); + +sub broken_keys { + + my ($sentence, @keys, $count); + + printf(qq[\nInput: \$sentence = ('%s'), \@keys = ('%s')\n], $_[0], join(q[', '], @_[1 .. @_ - 1])); + $sentence = lc(shift @_); + push @keys, lc($_) for @_; + + # change any occurrences of keys to '#' + $sentence =~ s|$_|#|g for @keys; + + # count the words which don't contain '#' + $count += ($_ !~ m|#| ? 1 : 0) for split(/ /, $sentence); + + printf(qq[Output: %s\n], defined $count ? $count : 0); +} diff --git a/challenge-275/peter-campbell-smith/perl/ch-2.pl b/challenge-275/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..2fa54e9f38 --- /dev/null +++ b/challenge-275/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-06-24 +use utf8; # Week 275 - task 2 - Replace digits +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +replace_digits('a9c9e9'); +replace_digits('a1b2c3d4'); +replace_digits('a2c6d1n6'); + +sub replace_digits { + + my ($j, $char, $str, $result, $prev); + + $str = shift; + + # loop over characters in $str + for $j (0 .. length($str)) { + $char = substr($str, $j, 1); + + # if it's a digit do the magic + if ($j > 0 and $char =~ m|([0-9])|) { + $result .= chr(ord($prev) + $char); + + # else don't + } else { + $result .= $char; + } + $prev = $char; + } + + printf(qq[\nInput: \@str = '%s'\n], $str); + printf(qq[Output: '%s'\n], $result); +} -- cgit From 514ed2553f66f2a534eb9f98a3b26b7b7a2d91c1 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 24 Jun 2024 16:22:56 +0000 Subject: Challenge 275 Solutions (Raku) --- .../mark-anderson/raku/ch-2-benchmark.raku | 55 ++++++++++++++++++++++ challenge-275/mark-anderson/raku/ch-2.raku | 4 +- 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 challenge-275/mark-anderson/raku/ch-2-benchmark.raku diff --git a/challenge-275/mark-anderson/raku/ch-2-benchmark.raku b/challenge-275/mark-anderson/raku/ch-2-benchmark.raku new file mode 100644 index 0000000000..7fb8999327 --- /dev/null +++ b/challenge-275/mark-anderson/raku/ch-2-benchmark.raku @@ -0,0 +1,55 @@ +#!/usr/bin/env raku +use Benchy; + +my $string = [~] gather for ^100 +{ + my $n = (1..100).pick; + my @chars = ('a'..'z').Array.roll($n); + take [~] @chars; + + $n = (1..100).pick; + my @digits = (^10).Array.roll($n); + take [~] @digits +} + +# 😭 😭 😭 😭 😭 + +b 10, { replace-digits-old($string) }, + { replace-digits-new($string) } + +# Bare: 0.000041032s +# Old: 1.344218988s +# New: 5.990887955s +# OLD version is 4.46x faster + +sub replace-digits-old($s) +{ + my $last-c; + + [~] gather for $s.comb -> $c + { + if $c ~~ 0..9 + { + take chr($last-c.ord + $c) + } + + else + { + $last-c = $c; + take $c + } + } +} + +sub replace-digits-new($s) +{ + my ($head, $tail) = $s.split(/ <.alpha>+ $ /, :v); + + my @result = do for $head.split(/ <.digit>+ /, :v:skip-empty).batch(2) + { + my $char = .head ~~ / <.alpha> $ /; + .head ~ [~] .tail.comb.map({ chr($char.ord + $_) }) + } + + ([~] @result) ~ ($tail or Empty) +} diff --git a/challenge-275/mark-anderson/raku/ch-2.raku b/challenge-275/mark-anderson/raku/ch-2.raku index e88360e34e..2ef1b3b45d 100644 --- a/challenge-275/mark-anderson/raku/ch-2.raku +++ b/challenge-275/mark-anderson/raku/ch-2.raku @@ -5,7 +5,7 @@ is replace-digits('a1c1e1'), 'abcdef'; is replace-digits('a1b2c3d4'), 'abbdcfdh'; is replace-digits('b2b'), 'bdb'; is replace-digits('a16z'), 'abgz'; -is replace-digits('a16bzd7'), 'abgdk'; +is replace-digits('a16bzd7'), 'abgbzdk'; sub replace-digits($s) { @@ -14,7 +14,7 @@ sub replace-digits($s) my @result = do for $head.split(/ <.digit>+ /, :v:skip-empty).batch(2) { my $char = .head ~~ / <.alpha> $ /; - $char ~ [~] .tail.comb.map({ chr($char.ord + $_) }) + .head ~ [~] .tail.comb.map({ chr($char.ord + $_) }) } ([~] @result) ~ ($tail or Empty) -- cgit From 991ec4276b44b5e3724c12484ff396e9363e5f7b Mon Sep 17 00:00:00 2001 From: pme Date: Mon, 24 Jun 2024 18:25:26 +0200 Subject: challenge-275 --- challenge-275/peter-meszaros/perl/ch-1.pl | 69 +++++++++++++++++++++++++++ challenge-275/peter-meszaros/perl/ch-2.pl | 78 +++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100755 challenge-275/peter-meszaros/perl/ch-1.pl create mode 100755 challenge-275/peter-meszaros/perl/ch-2.pl diff --git a/challenge-275/peter-meszaros/perl/ch-1.pl b/challenge-275/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..69149d8055 --- /dev/null +++ b/challenge-275/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,69 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Broken Keys + +You are given a sentence, $sentence and list of broken keys @keys. + +Write a script to find out how many words can be typed fully. + +=head2 Example 1 + + Input: $sentence = "Perl Weekly Challenge", @keys = ('l', 'a') + Output: 0 + +=head2 Example 2 + + Input: $sentence = "Perl and Raku", @keys = ('a') + Output: 1 + +Only Perl since the other word two words contain 'a' and can't be typed fully. + +=head2 Example 3 + + Input: $sentence = "Well done Team PWC", @keys = ('l', 'o') + Output: 2 + +=head2 Example 4 + + Input: $sentence = "The joys of polyglottism", @keys = ('T') + Output: 2 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [["Perl Weekly Challenge", ['l', 'a']], 0], + [["Perl and Raku", ['a']], 1], + [["Well done Team PWC", ['l', 'o']], 2], + [["The joys of polyglottism", ['T']], 2], +]; + +sub broken_keys +{ + my $s = $_[0]->[0]; + my $keys = $_[0]->[1]; + + my $cnt = 0; + my %h; + + WORD: for my $w (split / /, $s) { + for my $k (@$keys) { + next WORD unless index(lc($w), lc($k)) < 0; + } + ++$cnt; + } + + return $cnt; + +} + +for (@$cases) { + is(broken_keys($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-275/peter-meszaros/perl/ch-2.pl b/challenge-275/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..b25938d8f9 --- /dev/null +++ b/challenge-275/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,78 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Replace Digits + +You are given an alphanumeric string, $str, where each character is either a +letter or a digit. + +Write a script to replace each digit in the given string with the value of the +previous letter plus (digit) places. + +=head2 Example 1 + + Input: $str = 'a1c1e1' + Ouput: 'abcdef' + + shift('a', 1) => 'b' + shift('c', 1) => 'd' + shift('e', 1) => 'f' + +=head2 Example 2 + + Input: $str = 'a1b2c3d4' + Output: 'abbdcfdh' + + shift('a', 1) => 'b' + shift('b', 2) => 'd' + shift('c', 3) => 'f' + shift('d', 4) => 'h' + +=head2 Example 3 + + Input: $str = 'b2b' + Output: 'bdb' + +=head2 Example 4 + + Input: $str = 'a16z' + Output: 'abgz' + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + ['a1c1e1', 'abcdef'], + ['a1b2c3d4', 'abbdcfdh'], + ['b2b', 'bdb'], + ['a16z', 'abgz'], + ['816z', undef], +]; + +sub replace_digits +{ + my $str = shift; + + my @str = split //, $str; + return undef if $str[0] =~ /\d/; + my $c = ord($str[0]); + for my $i (1..$#str) { + if ($str[$i] =~ /\d/) { + $str[$i] = chr($c+$str[$i]); + } else { + $c = ord($str[$i]); + } + } + return join '', @str; +} + +for (@$cases) { + is(replace_digits($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; + -- cgit From 6574ce41758f8ecb16b59e411603930c1c670b8d Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 24 Jun 2024 16:47:09 +0000 Subject: Challenge 275 Solutions (Raku) --- challenge-275/mark-anderson/raku/ch-2-benchmark.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-275/mark-anderson/raku/ch-2-benchmark.raku b/challenge-275/mark-anderson/raku/ch-2-benchmark.raku index 7fb8999327..bb44f0f059 100644 --- a/challenge-275/mark-anderson/raku/ch-2-benchmark.raku +++ b/challenge-275/mark-anderson/raku/ch-2-benchmark.raku @@ -15,7 +15,7 @@ my $string = [~] gather for ^100 # 😭 😭 😭 😭 😭 b 10, { replace-digits-old($string) }, - { replace-digits-new($string) } + { replace-digits-new($string) } # Bare: 0.000041032s # Old: 1.344218988s -- cgit From 740859cfa0649d28ec498e2d695ee33ac38e39a5 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 24 Jun 2024 12:43:41 -0700 Subject: Add solutions to 275: Broken Keys & Replace Digits by E. Choroba --- challenge-275/e-choroba/perl/ch-1.pl | 18 ++++++++++++++++++ challenge-275/e-choroba/perl/ch-2.pl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100755 challenge-275/e-choroba/perl/ch-1.pl create mode 100755 challenge-275/e-choroba/perl/ch-2.pl diff --git a/challenge-275/e-choroba/perl/ch-1.pl b/challenge-275/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..c4cc55dee4 --- /dev/null +++ b/challenge-275/e-choroba/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub broken_keys($sentence, @keys) { + my @words = split ' ', $sentence; + my $broken = join "", @keys; + $broken = "[$broken]"; + return grep ! /$broken/i, @words +} + +use Test::More tests => 4; + +is broken_keys('Perl Weekly Challenge','l', 'a'), 0, 'Example 1'; +is broken_keys('Perl and Raku','a'), 1, 'Example 2'; +is broken_keys('Well done Team PWC','l', 'o'), 2, 'Example 3'; +is broken_keys('The joys of polyglottism','T'), 2, 'Example 4'; diff --git a/challenge-275/e-choroba/perl/ch-2.pl b/challenge-275/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..f134b98889 --- /dev/null +++ b/challenge-275/e-choroba/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub replace_digits($str) { + my $result = ""; + my $letter; + for my $char (split //, $str) { + if ($char =~ /[0-9]/) { + return "" unless $letter; + + my $ord = $char + ord $letter; + return "" if $ord > ord 'z'; + + $result .= chr $ord; + } else { + $result .= $letter = $char; + } + } + return $result +} + +use Test::More tests => 4 + 3; + +is replace_digits('a1c1e1'), 'abcdef', 'Example 1'; +is replace_digits('a1b2c3d4'), 'abbdcfdh', 'Example 2'; +is replace_digits('b2b'), 'bdb', 'Example 3'; +is replace_digits('a16z'), 'abgz', 'Example 4'; + +is replace_digits('a00b'), 'aaab', 'Zero'; +is replace_digits('1a'), "", 'Start with a digit'; +is replace_digits('z01'), "",