From 192dc9896e632fcf74af8b17d96238368301014f Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Wed, 5 Jun 2024 18:33:04 -0700 Subject: Added blog. --- challenge-272/robbie-hatley/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-272/robbie-hatley/blog.txt diff --git a/challenge-272/robbie-hatley/blog.txt b/challenge-272/robbie-hatley/blog.txt new file mode 100644 index 0000000000..b5658726ef --- /dev/null +++ b/challenge-272/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2024/06/robbie-hatleys-solutions-to-weekly.html \ No newline at end of file -- cgit From 56cd4be064ea930654c9b199442e7b083c0ed73a Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Thu, 6 Jun 2024 10:26:38 +0200 Subject: PWC 272 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-272/luca-ferrari/blog-1.txt | 1 + challenge-272/luca-ferrari/blog-10.txt | 1 + challenge-272/luca-ferrari/blog-2.txt | 1 + challenge-272/luca-ferrari/blog-3.txt | 1 + challenge-272/luca-ferrari/blog-4.txt | 1 + challenge-272/luca-ferrari/blog-5.txt | 1 + challenge-272/luca-ferrari/blog-6.txt | 1 + challenge-272/luca-ferrari/blog-7.txt | 1 + challenge-272/luca-ferrari/blog-8.txt | 1 + challenge-272/luca-ferrari/blog-9.txt | 1 + challenge-272/luca-ferrari/pljava/pom.xml | 6 +-- .../luca-ferrari/pljava/src/main/java/Task1.java | 56 +++++++++++++++++++ .../luca-ferrari/pljava/src/main/java/Task2.java | 63 ++++++++++++++++++++++ challenge-272/luca-ferrari/plperl/ch-1.plperl | 22 ++++++++ challenge-272/luca-ferrari/plperl/ch-2.plperl | 26 +++++++++ challenge-272/luca-ferrari/plpgsql/ch-1.sql | 16 ++++++ challenge-272/luca-ferrari/plpgsql/ch-2.sql | 33 ++++++++++++ challenge-272/luca-ferrari/python/ch-1.py | 20 +++++++ challenge-272/luca-ferrari/python/ch-2.py | 34 ++++++++++++ challenge-272/luca-ferrari/raku/ch-1.raku | 15 ++++++ challenge-272/luca-ferrari/raku/ch-2.raku | 19 +++++++ 21 files changed, 317 insertions(+), 3 deletions(-) create mode 100644 challenge-272/luca-ferrari/blog-1.txt create mode 100644 challenge-272/luca-ferrari/blog-10.txt create mode 100644 challenge-272/luca-ferrari/blog-2.txt create mode 100644 challenge-272/luca-ferrari/blog-3.txt create mode 100644 challenge-272/luca-ferrari/blog-4.txt create mode 100644 challenge-272/luca-ferrari/blog-5.txt create mode 100644 challenge-272/luca-ferrari/blog-6.txt create mode 100644 challenge-272/luca-ferrari/blog-7.txt create mode 100644 challenge-272/luca-ferrari/blog-8.txt create mode 100644 challenge-272/luca-ferrari/blog-9.txt create mode 100644 challenge-272/luca-ferrari/pljava/src/main/java/Task1.java create mode 100644 challenge-272/luca-ferrari/pljava/src/main/java/Task2.java create mode 100644 challenge-272/luca-ferrari/plperl/ch-1.plperl create mode 100644 challenge-272/luca-ferrari/plperl/ch-2.plperl create mode 100644 challenge-272/luca-ferrari/plpgsql/ch-1.sql create mode 100644 challenge-272/luca-ferrari/plpgsql/ch-2.sql create mode 100644 challenge-272/luca-ferrari/python/ch-1.py create mode 100644 challenge-272/luca-ferrari/python/ch-2.py create mode 100644 challenge-272/luca-ferrari/raku/ch-1.raku create mode 100644 challenge-272/luca-ferrari/raku/ch-2.raku diff --git a/challenge-272/luca-ferrari/blog-1.txt b/challenge-272/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..e7579f5b4b --- /dev/null +++ b/challenge-272/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task1 diff --git a/challenge-272/luca-ferrari/blog-10.txt b/challenge-272/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..eda35729fd --- /dev/null +++ b/challenge-272/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge272.html#task2pljava diff --git a/challenge-272/luca-ferrari/blog-2.txt b/challenge-272/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..fc64e99089 --- /dev/null +++ b/challenge-272/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task2 diff --git a/challenge-272/luca-ferrari/blog-3.txt b/challenge-272/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..6cb55cedef --- /dev/null +++ b/challenge-272/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task1plperl diff --git a/challenge-272/luca-ferrari/blog-4.txt b/challenge-272/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..cb1275772f --- /dev/null +++ b/challenge-272/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task2plperl diff --git a/challenge-272/luca-ferrari/blog-5.txt b/challenge-272/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..86a012d6e5 --- /dev/null +++ b/challenge-272/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task1plpgsql diff --git a/challenge-272/luca-ferrari/blog-6.txt b/challenge-272/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..403db8c903 --- /dev/null +++ b/challenge-272/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task2plpgsql diff --git a/challenge-272/luca-ferrari/blog-7.txt b/challenge-272/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..c2fb30cccc --- /dev/null +++ b/challenge-272/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task1python diff --git a/challenge-272/luca-ferrari/blog-8.txt b/challenge-272/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..63130d622d --- /dev/null +++ b/challenge-272/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task2python diff --git a/challenge-272/luca-ferrari/blog-9.txt b/challenge-272/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..ce0f7adec4 --- /dev/null +++ b/challenge-272/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task1pljava diff --git a/challenge-272/luca-ferrari/pljava/pom.xml b/challenge-272/luca-ferrari/pljava/pom.xml index bf11a0cf59..88583e06f9 100644 --- a/challenge-272/luca-ferrari/pljava/pom.xml +++ b/challenge-272/luca-ferrari/pljava/pom.xml @@ -7,14 +7,14 @@ PWC - PWC271 + PWC272 1 - Perl Weekly Challenge 271 with package PWC271 - Implementation of the tasks in PL/Java for PWC 271 + Perl Weekly Challenge 272 with package PWC272 + Implementation of the tasks in PL/Java for PWC 272 US-ASCII diff --git a/challenge-272/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-272/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..8dddbce6c0 --- /dev/null +++ b/challenge-272/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,56 @@ + + + +package PWC272; + +/** + * PL/Java implementation for PWC 272 + * 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/PWC272-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC272-1.jar', 'PWC272', true ); + select sqlj.set_classpath( 'public', 'PWC272' ); + + select pwc272.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC272-1.jar', 'PWC272', 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.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 = "pwc272", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final String task1_pljava( String ip ) throws SQLException { + logger.log( Level.INFO, "Entering pwc272.task1_pljava" ); + + return ip.replaceAll( "\\.", "[.]" ); + } +} diff --git a/challenge-272/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-272/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..4002c64b9d --- /dev/null +++ b/challenge-272/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,63 @@ + + + +package PWC272; + +/** + * PL/Java implementation for PWC 272 + * 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/PWC272-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC272-1.jar', 'PWC272', true ); + select sqlj.set_classpath( 'public', 'PWC272' ); + + select pwc272.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC272-1.jar', 'PWC272', 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.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 = "pwc272", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task2_pljava( String string ) throws SQLException { + logger.log( Level.INFO, "Entering pwc272.task2_pljava" ); + + int score = 0; + + for ( int i = 0; i < string.length() - 1; i++ ) { + int diff = string.charAt( i ) - string.charAt( i + 1 ); + score += diff > 0 ? diff : diff * -1; + } + + return score; + } +} diff --git a/challenge-272/luca-ferrari/plperl/ch-1.plperl b/challenge-272/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..a82a3f0e38 --- /dev/null +++ b/challenge-272/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,22 @@ +-- +-- Perl Weekly Challenge 272 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc272; + +CREATE OR REPLACE FUNCTION +pwc272.task1_plperl( text ) +RETURNS text +AS $CODE$ + + my ( $ip ) = @_; + + die "IP not valid" if ( $ip !~ / ^ ( \d {1,3} [.] ) {3} \d {1,3} $ /x ); + + $ip =~ s/[.]/\[\.\]/g; + return $ip; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-272/luca-ferrari/plperl/ch-2.plperl b/challenge-272/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..2085b3b3bd --- /dev/null +++ b/challenge-272/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,26 @@ +-- +-- Perl Weekly Challenge 272 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc272; + +CREATE OR REPLACE FUNCTION +pwc272.task2_plperl( text ) +RETURNS int +AS $CODE$ + + my ( $string ) = @_; + + my $score = 0; + my @chars = split //, $string; + + for my $index ( 0 .. $#chars - 1 ) { + my $diff = ord( $chars[ $index ] ) - ord( $chars[ $index + 1 ] ); + $score += $diff > 0 ? $diff : $diff * -1; + } + + return $score; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-272/luca-ferrari/plpgsql/ch-1.sql b/challenge-272/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..588bd9b572 --- /dev/null +++ b/challenge-272/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,16 @@ +-- +-- Perl Weekly Challenge 272 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc272; + +CREATE OR REPLACE FUNCTION +pwc272.task1_plpgsql( addr text ) +RETURNS text +AS $CODE$ + SELECT + regexp_replace( addr, '\.', '[.]', 'g' ); +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-272/luca-ferrari/plpgsql/ch-2.sql b/challenge-272/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..b044a9d85c --- /dev/null +++ b/challenge-272/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,33 @@ +-- +-- Perl Weekly Challenge 272 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc272; + +CREATE OR REPLACE FUNCTION +pwc272.task2_plpgsql( s text ) +RETURNS int +AS $CODE$ +DECLARE + score int; + previous int; + v int; +BEGIN + + previous := NULL; + score := 0; + FOR v IN SELECT ascii( x )::int FROM regexp_split_to_table( s, '' ) x LOOP + IF previous IS NOT NULL THEN + score := score + abs( previous - v::int ); + END IF; + + previous := v::int; + + END LOOP; + + RETURN score; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-272/luca-ferrari/python/ch-1.py b/challenge-272/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..02c5837ce1 --- /dev/null +++ b/challenge-272/luca-ferrari/python/ch-1.py @@ -0,0 +1,20 @@ +#!python + +# +# Perl Weekly Challenge 272 +# Task 1 +# +# See +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + return args[ 0 ].replace( '.', '[.]' ) + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-272/luca-ferrari/python/ch-2.py b/challenge-272/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..0a8a0001f4 --- /dev/null +++ b/challenge-272/luca-ferrari/python/ch-2.py @@ -0,0 +1,34 @@ +#!python + +# +# Perl Weekly Challenge 272 +# Task 2 +# +# See +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + score = 0 + string = args[ 0 ] + previous = None + + for x in string: + if previous is not None: + diff = ord( x ) - ord( previous ) + if diff < 0: + diff *= -1 + + score += diff + + previous = x + + return score + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-272/luca-ferrari/raku/ch-1.raku b/challenge-272/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..4966334941 --- /dev/null +++ b/challenge-272/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,15 @@ +#!raku + +# +# Perl Weekly Challenge 272 +# Task 1 +# +# See +# + +sub MAIN( Str $ip is copy + where { $ip ~~ /^ ( \d ** 1..3 '.' ) ** 3 \d ** 1..3 / } ) { + + $ip .= subst( '.', '[.]', :g ); + $ip.say; +} diff --git a/challenge-272/luca-ferrari/raku/ch-2.raku b/challenge-272/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..75cd2e7b8e --- /dev/null +++ b/challenge-272/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,19 @@ +#!raku + +# +# Perl Weekly Challenge 272 +# Task 2 +# +# See +# + +sub MAIN( Str $string where { $string.elems > 0 } ) { + my $score = 0; + my @letters = $string.comb; + + for 0 ..^ @letters.elems - 1 -> $index { + $score += abs( @letters[ $index ].Str.ord - @letters[ $index + 1 ].Str.ord ); + } + + $score.say; +} -- cgit From 84e131a68198e85b294c2cc4aaf1faabd2d41e8f Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Fri, 7 Jun 2024 11:29:52 +0100 Subject: RogerBW solutions for challenge no. 272 --- challenge-272/roger-bell-west/crystal/ch-1.cr | 15 +++++ challenge-272/roger-bell-west/crystal/ch-2.cr | 22 +++++++ challenge-272/roger-bell-west/javascript/ch-1.js | 20 +++++++ challenge-272/roger-bell-west/javascript/ch-2.js | 39 ++++++++++++ challenge-272/roger-bell-west/kotlin/ch-1.kt | 20 +++++++ challenge-272/roger-bell-west/kotlin/ch-2.kt | 30 ++++++++++ challenge-272/roger-bell-west/lua/ch-1.lua | 20 +++++++ challenge-272/roger-bell-west/lua/ch-2.lua | 43 ++++++++++++++ challenge-272/roger-bell-west/perl/ch-1.pl | 15 +++++ challenge-272/roger-bell-west/perl/ch-2.pl | 20 +++++++ challenge-272/roger-bell-west/postscript/ch-1.ps | 70 ++++++++++++++++++++++ challenge-272/roger-bell-west/postscript/ch-2.ps | 75 ++++++++++++++++++++++++ challenge-272/roger-bell-west/python/ch-1.py | 16 +++++ challenge-272/roger-bell-west/python/ch-2.py | 36 ++++++++++++ challenge-272/roger-bell-west/raku/ch-1.p6 | 13 ++++ challenge-272/roger-bell-west/raku/ch-2.p6 | 18 ++++++ challenge-272/roger-bell-west/ruby/ch-1.rb | 19 ++++++ challenge-272/roger-bell-west/ruby/ch-2.rb | 27 +++++++++ challenge-272/roger-bell-west/rust/ch-1.rs | 24 ++++++++ challenge-272/roger-bell-west/rust/ch-2.rs | 25 ++++++++ challenge-272/roger-bell-west/scala/ch-1.scala | 20 +++++++ challenge-272/roger-bell-west/scala/ch-2.scala | 32 ++++++++++ challenge-272/roger-bell-west/tests.json | 29 +++++++++ 23 files changed, 648 insertions(+) create mode 100755 challenge-272/roger-bell-west/crystal/ch-1.cr create mode 100755 challenge-272/roger-bell-west/crystal/ch-2.cr create mode 100755 challenge-272/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-272/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-272/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-272/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-272/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-272/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-272/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-272/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-272/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-272/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-272/roger-bell-west/python/ch-1.py create mode 100755 challenge-272/roger-bell-west/python/ch-2.py create mode 100755 challenge-272/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-272/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-272/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-272/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-272/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-272/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-272/roger-bell-west/scala/ch-1.scala create mode 100644 challenge-272/roger-bell-west/scala/ch-2.scala create mode 100644 challenge-272/roger-bell-west/tests.json diff --git a/challenge-272/roger-bell-west/crystal/ch-1.cr b/challenge-272/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..032e4213b7 --- /dev/null +++ b/challenge-272/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,15 @@ +#! /usr/bin/crystal + +def defrangip(a) + return a.gsub(".", "[.]") +end + +require "spec" +describe "defrangip" do + it "test_ex1" do + defrangip("1.1.1.1").should eq "1[.]1[.]1[.]1" + end + it "test_ex2" do + defrangip("255.101.1.0").should eq "255[.]101[.]1[.]0" + end +end diff --git a/challenge-272/roger-bell-west/crystal/ch-2.cr b/challenge-272/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..bcea3f7fe1 --- /dev/null +++ b/challenge-272/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,22 @@ +#! /usr/bin/crystal + +def stringscore(a) + out = 0 + a.chars.each_cons(2) do |c| + out += (c[0].ord() - c[1].ord()).abs + end + out +end + +require "spec" +describe "stringscore" do + it "test_ex1" do + stringscore("hello").should eq 13 + end + it "test_ex2" do + stringscore("perl").should eq 30 + end + it "test_ex3" do + stringscore("raku").should eq 37 + end +end diff --git a/challenge-272/roger-bell-west/javascript/ch-1.js b/challenge-272/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..27d2fa1a36 --- /dev/null +++ b/challenge-272/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,20 @@ +#! /usr/bin/node + +"use strict" + +function defrangip(a) { + return a.replace(/\./g, "[.]"); +} + +if (defrangip('1.1.1.1') == '1[.]1[.]1[.]1') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (defrangip('255.101.1.0') == '255[.]101[.]1[.]0') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-272/roger-bell-west/javascript/ch-2.js b/challenge-272/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..96364dc7c8 --- /dev/null +++ b/challenge-272/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,39 @@ +#! /usr/bin/node + +"use strict" + +// by VLAZ +// https://stackoverflow.com/a/59322890 +function toWindows(inputArray, size) { + return Array.from( + {length: inputArray.length - (size - 1)}, //get the appropriate length + (_, index) => inputArray.slice(index, index+size) //create the windows + ) +} + +function stringscore(a) { + let out = 0; + for (let t = 0; t < a.length - 1; t++) { + out += Math.abs(a.charCodeAt(t) - a.charCodeAt(t + 1)); + } + return out; +} + +if (stringscore('hello') == 13) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (stringscore('perl') == 30) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (stringscore('raku') == 37) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-272/roger-bell-west/kotlin/ch-1.kt b/challenge-272/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..495b14c77b --- /dev/null +++ b/challenge-272/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,20 @@ +fun defrangip(a: String): String { + return a.replace(".", "[.]") +} + +fun main() { + + if (defrangip("1.1.1.1") == "1[.]1[.]1[.]1") { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (defrangip("255.101.1.0") == "255[.]101[.]1[.]0") { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-272/roger-bell-west/kotlin/ch-2.kt b/challenge-272/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..ee575717b9 --- /dev/null +++ b/challenge-272/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,30 @@ +fun stringscore(a: String): Int { + var out = 0 + for (i in a.toList().windowed(size = 2)) { + out += Math.abs(i[0].code - i[1].code) + } + return out +} + +fun main() { + + if (stringscore("hello") == 13) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (stringscore("perl") == 30) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (stringscore("raku") == 37) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-272/roger-bell-west/lua/ch-1.lua b/challenge-272/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..f8b4f261b7 --- /dev/null +++ b/challenge-272/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,20 @@ +#! /usr/bin/lua + +function defrangip(a) + return string.gsub(a, "[.]", "[.]") + end + +if defrangip("1.1.1.1") == "1[.]1[.]1[.]1" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if defrangip("255.101.1.0") == "255[.]101[.]1[.]0" then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-272/roger-bell-west/lua/ch-2.lua b/challenge-272/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..7bf1100382 --- /dev/null +++ b/challenge-272/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,43 @@ +#! /usr/bin/lua + +function split(t) + local cl = {} + string.gsub(t, + "(.)", + function(c) + table.insert(cl, c) + end + ) + return cl +end + +function stringscore(a) + local out = 0 + local c = split(a) + for n = 1, #c - 1 do + out = out + math.abs(string.byte(c[n]) - string.byte(c[n + 1])) + end + return out +end + +if stringscore("hello") == 13 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if stringscore("perl") == 30 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if stringscore("raku") == 37 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-272/roger-bell-west/perl/ch-1.pl b/challenge-272/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..bd91e55ec6 --- /dev/null +++ b/challenge-272/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,15 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(defrangip('1.1.1.1'), '1[.]1[.]1[.]1', 'example 1'); +is(defrangip('255.101.1.0'), '255[.]101[.]1[.]0', 'example 2'); + +sub defrangip($a) { + (my $b = $a) =~ s/\./[.]/g; + return $b; +} diff --git a/challenge-272/roger-bell-west/perl/ch-2.pl b/challenge-272/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..f14041e844 --- /dev/null +++ b/challenge-272/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,20 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(stringscore('hello'), 13, 'example 1'); +is(stringscore('perl'), 30, 'example 2'); +is(stringscore('raku'), 37, 'example 3'); + +sub stringscore($a) { + my $out = 0; + my @c = split '',$a; + foreach my $i (0 .. $#c - 1) { + $out += abs(ord($c[$i]) - ord($c[$i+1])); + } + return $out; +} diff --git a/challenge-272/roger-bell-west/postscript/ch-1.ps b/challenge-272/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..71a4deae7c --- /dev/null +++ b/challenge-272/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,70 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/s2a { + [ exch { } forall ] +} bind def + +/a2s { + 2 dict begin + /i exch def + i length dup string /o exch def + 1 sub 0 exch 1 exch { + dup i 3 -1 roll get o 3 1 roll put + } for + o + end +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + + +% end included library code + +/defrangip { + [ exch + s2a + { + dup 46 eq { + 91 exch + 93 + } if + } forall + ] + a2s +} bind def + +(defrangip) test.start +(1.1.1.1) defrangip (1[.]1[.]1[.]1) eq test +(255.101.1.0) defrangip (255[.]101[.]1[.]0) eq test +test.end diff --git a/challenge-272/roger-bell-west/postscript/ch-2.ps b/challenge-272/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..c1de213b51 --- /dev/null +++ b/challenge-272/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,75 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/rotor { + 5 dict begin + /delta exch def + /size exch def + dup length /len exch def + /ar exch def + /ix 0 def + [ + { + ix size add len gt { + exit + } if + ar ix size getinterval + /ix ix size delta add add def + } loop + ] + end +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/s2a { + [ exch { } forall ] +} bind def + + +% end included library code + +/stringscore { + 0 exch + s2a + 2 -1 rotor + { + aload pop sub abs add + } forall +} bind def + +(stringscore) test.start +(hello) stringscore 13 eq test +(perl) stringscore 30 eq test +(raku) stringscore 37 eq test +test.end diff --git a/challenge-272/roger-bell-west/python/ch-1.py b/challenge-272/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..2fbe357488 --- /dev/null +++ b/challenge-272/roger-bell-west/python/ch-1.py @@ -0,0 +1,16 @@ +#! /usr/bin/python3 + +def defrangip(a): + return a.replace(".", "[.]") + +import unittest + +class TestDefrangip(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(defrangip("1.1.1.1"), "1[.]1[.]1[.]1", 'example 1') + + def test_ex2(self): + self.assertEqual(defrangip("255.101.1.0"), "255[.]101[.]1[.]0", 'example 2') + +unittest.main() diff --git a/challenge-272/roger-bell-west/python/ch-2.py b/challenge-272/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..4332977373 --- /dev/null +++ b/challenge-272/roger-bell-west/python/ch-2.py @@ -0,0 +1,36 @@ +#! /usr/bin/python3 + +import collections +from itertools import islice + +# https://docs.python.org/3/library/itertools.html +def sliding_window(iterable, n): + # sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG + it = iter(iterable) + window = collections.deque(islice(it, n), maxlen=n) + if len(window) == n: + yield tuple(window) + for x in it: + window.append(x) + yield tuple(window) + +def stringscore(a): + out = 0 + for i in sliding_window(a, 2): + out += abs(ord(i[0]) - ord(i[1])) + return out + +import unittest + +class TestStringscore(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(stringscore("hello"), 13, 'example 1') + + def test_ex2(self): + self.assertEqual(stringscore("perl"), 30, 'example 2') + + def test_ex3(self): + self.assertEqual(stringscore("raku"), 37, 'example 3') + +unittest.main() diff --git a/challenge-272/roger-bell-west/raku/ch-1.p6 b/challenge-272/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..fe923d38d0 --- /dev/null +++ b/challenge-272/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,13 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(defrangip('1.1.1.1'), '1[.]1[.]1[.]1', 'example 1'); +is(defrangip('255.101.1.0'), '255[.]101[.]1[.]0', 'example 2'); + +sub defrangip($a) { + (my $b = $a) ~~ s:g/\./[.]/; + return $b; +} diff --git a/challenge-272/roger-bell-west/raku/ch-2.p6 b/challenge-272/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..7f67141e39 --- /dev/null +++ b/challenge-272/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,18 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(stringscore('hello'), 13, 'example 1'); +is(stringscore('perl'), 30, 'example 2'); +is(stringscore('raku'), 37, 'example 3'); + +sub stringscore($a) { + my $out = 0; + my @c = $a.comb; + for @c.rotor(2 => -1) -> @i { + $out += abs(ord(@i[0]) - ord(@i[1])); + } + return $out; +} diff --git a/challenge-272/roger-bell-west/ruby/ch-1.rb b/challenge-272/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..82152f9fd3 --- /dev/null +++ b/challenge-272/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,19 @@ +#! /usr/bin/ruby + +def defrangip(a) + return a.gsub(".", "[.]") +end + +require 'test/unit' + +class TestDefrangip < Test::Unit::TestCase + + def test_ex1 + assert_equal('1[.]1[.]1[.]1', defrangip('1.1.1.1')) + end + + def test_ex2 + assert_equal('255[.]101[.]1[.]0', defrangip('255.101.1.0')) + end + +end diff --git a/challenge-272/roger-bell-west/ruby/ch-2.rb b/challenge-272/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..f198380624 --- /dev/null +++ b/challenge-272/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,27 @@ +#! /usr/bin/ruby + +def stringscore(a) + out = 0 + a.chars.each_cons(2) do |c| + out += (c[0].ord() - c[1].ord()).abs + end + out +end + +require 'test/unit' + +class TestStringscore < Test::Unit::TestCase + + def test_ex1 + assert_equal(13, stringscore('hello')) + end + + def test_ex2 + assert_equal(30, stringscore('perl')) + end + + def test_ex3 + assert_equal(37, stringscore('raku')) + end + +end diff --git a/challenge-272/roger-bell-west/rust/ch-1.rs b/challenge-272/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..ef13992b11 --- /dev/null +++ b/challenge-272/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,24 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(defrangip("1.1.1.1"), "1[.]1[.]1[.]1"); +} + +#[test] +fn test_ex2() { + assert_eq!(defrangip("255.101.1.0"), "255[.]101[.]1[.]0"); +} + +fn defrangip(a: &str) -> String { + let mut out = String::new(); + for c in a.chars() { + if c == '.' { + out.push_str("[.]"); + } else { + out.push(c); + } + } + out +} diff --git a/challenge-272/roger-bell-west/rust/ch-2.rs b/challenge-272/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..c95b015b88 --- /dev/null +++ b/challenge-272/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,25 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(stringscore("hello"), 13); +} + +#[test] +fn test_ex2() { + assert_eq!(stringscore("perl"), 30); +} + +#[test] +fn test_ex3() { + assert_eq!(stringscore("raku"), 37); +} + +fn stringscore(a: &str) -> u32 { + let mut out = 0; + for i in a.chars().collect::>().windows(2) { + out += ((i[0] as i32) - (i[1] as i32)).abs(); + } + out as u32 +} diff --git a/challenge-272/roger-bell-west/scala/ch-1.scala b/challenge-272/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..9cfa5dcc61 --- /dev/null +++ b/challenge-272/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,20 @@ +object Defrangip { + def defrangip(a: String): String = { + a.replaceAll("\\.", "[.]") + } + def main(args: Array[String]) { + if (defrangip("1.1.1.1") == "1[.]1[.]1[.]1") { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (defrangip("255.101.1.0") == "255[.]101[.]1[.]0") { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-272/roger-bell-west/scala/ch-2.scala b/challenge-272/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..d7bebf840d --- /dev/null +++ b/challenge-272/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,32 @@ + +object Stringscore { + def stringscore(a: String): Int = { + var out = 0 + val c = a.toList + for (i <- c.sliding(2)) { + out += (i(0).toInt - i(1).toInt).abs + } + out + } + def main(args: Array[String]) { + if (stringscore("hello") == 13) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (stringscore("perl") == 30) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (stringscore("raku") == 37) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-272/roger-bell-west/tests.json b/challenge-272/roger-bell-west/tests.json new file mode 100644 index 0000000000..2eb8bad37e --- /dev/null +++ b/challenge-272/roger-bell-west/tests.json @@ -0,0 +1,29 @@ +{ + "ch-1" : [ + { + "function" : "defrangip", + "arguments" : "1.1.1.1", + "result" : "1[.]1[.]1[.]1" + }, + { + "function" : "defrangip", + "arguments" : "255.101.1.0", + "result" : "255[.]101[.]1[.]0" + } + ], + "ch-2" : [ + { + "function" : "stringscore", + "arguments" : "hello", + "result" : 13 + }, + { + "arguments" : "perl", + "result" : 30 + }, + { + "arguments" : "raku", + "result" : 37 + } + ] +} -- cgit From 5600a5beaa5bdd44917276e1a7527ac1675e597f Mon Sep 17 00:00:00 2001 From: Jan Krňávek Date: Fri, 7 Jun 2024 14:18:00 +0200 Subject: solutions week 272 --- challenge-272/wambash/raku/ch-1.raku | 16 ++++++++++++++++ challenge-272/wambash/raku/ch-2.raku | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 challenge-272/wambash/raku/ch-1.raku create mode 100644 challenge-272/wambash/raku/ch-2.raku diff --git a/challenge-272/wambash/raku/ch-1.raku b/challenge-272/wambash/raku/ch-1.raku new file mode 100644 index 0000000000..d64d540e06 --- /dev/null +++ b/challenge-272/wambash/raku/ch-1.raku @@ -0,0 +1,16 @@ +#!/usr/bin/env raku + +sub defang-IP-address ($ip) { + $ip.trans: ['.'] => ['[.]'] +} + +multi MAIN (Bool :test($)!) { + use Test; + is defang-IP-address('1.1.1.1'),'1[.]1[.]1[.]1'; + is defang-IP-address('255.101.1.0'),'255[.]101[.]1[.]0'; + done-testing; +} + +multi MAIN ($ip) { + say defang-IP-address $ip +} diff --git a/challenge-272/wambash/raku/ch-2.raku b/challenge-272/wambash/raku/ch-2.raku new file mode 100644 index 0000000000..6178b94265 --- /dev/null +++ b/challenge-272/wambash/raku/ch-2.raku @@ -0,0 +1,21 @@ +#!/usr/bin/env raku + +sub string-score ($str) { + $str + andthen .ords + andthen .rotor: 2 => -1 + andthen .map: -> ($x, $y) {abs $x - $y}\ + andthen .sum +} + +multi MAIN (Bool :test($)!) { + use Test; + is string-score('hello'),13; + is string-score('perl'), 30; + is string-score('raku'), 37; + done-testing; +} + +multi MAIN ($str) { + say string-score $str +} -- cgit From b1a98b4180e62798cef122039206b379451b56ef Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Fri, 7 Jun 2024 03:44:08 +1000 Subject: pwc272 solution in python --- challenge-272/pokgopun/python/ch-1.py | 42 +++++++++++++++++ challenge-272/pokgopun/python/ch-2.py | 87 +++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 challenge-272/pokgopun/python/ch-1.py create mode 100644 challenge-272/pokgopun/python/ch-2.py diff --git a/challenge-272/pokgopun/python/ch-1.py b/challenge-272/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..2b328144e9 --- /dev/null +++ b/challenge-272/pokgopun/python/ch-1.py @@ -0,0 +1,42 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-272/ +""" + +Task 1: Defang IP Address + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a valid IPv4 address. + + Write a script to return the defanged version of the given IP address. + + A defanged IP address replaces every period “.” with “[.]". + +Example 1 + +Input: $ip = "1.1.1.1" +Output: "1[.]1[.]1[.]1" + +Example 2 + +Input: $ip = "255.101.1.0" +Output: "255[.]101[.]1[.]0" + +Task 2: String Score +""" +### solution by pokgopun@gmail.com + +def dfip(string: str): + return string.replace(".","[.]") + +import unittest + +class TestDfip (unittest.TestCase): + def test(self): + for inpt, otpt in { + "1.1.1.1": "1[.]1[.]1[.]1", + "255.101.1.0": "255[.]101[.]1[.]0", + }.items(): + self.assertEqual(dfip(inpt),otpt) + +unittest.main() diff --git a/challenge-272/pokgopun/python/ch-2.py b/challenge-272/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..065e0b1609 --- /dev/null +++ b/challenge-272/pokgopun/python/ch-2.py @@ -0,0 +1,87 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-272/ +""" + +Task 2: String Score + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str. + + Write a script to return the score of the given string. + + The score of a string is defined as the sum of the absolute + difference between the ASCII values of adjacent characters. + +Example 1 + +Input: $str = "hello" +Output: 13 + +ASCII values of characters: +h = 104 +e = 101 +l = 108 +l = 108 +o = 111 + +Score => |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| + => 3 + 7 + 0 + 3 + => 13 + +Example 2 + +Input: "perl" +Output: 30 + +ASCII values of characters: +p = 112 +e = 101 +r = 114 +l = 108 + +Score => |112 - 101| + |101 - 114| + |114 - 108| + => 11 + 13 + 6 + => 30 + +Example 3 + +Input: "raku" +Output: 37 + +ASCII values of characters: +r = 114 +a = 97 +k = 107 +u = 117 + +Score => |114 - 97| + |97 - 107| + |107 - 117| + => 17 + 10 + 10 + => 37 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 9th June 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def ss(string: str): + return sum( + abs(ord(p[0])-ord(p[1])) for p in + (string[i:i+2] for i in range(len(string)-1)) + ) + +import unittest + +class TestSs(unittest.TestCase): + def test(self): + for inpt, otpt in { + "hello": 13, + "perl": 30, + "raku": 37, + }.items(): + self.assertEqual(ss(inpt),otpt) + +unittest.main() -- cgit From 3e4dc5350b29cea68cb7f32562e97463722ba1c0 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Wed, 5 Jun 2024 19:08:55 +0200 Subject: Solution to task 1 --- challenge-272/jo-37/perl/ch-1.pl | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 challenge-272/jo-37/perl/ch-1.pl diff --git a/challenge-272/jo-37/perl/ch-1.pl b/challenge-272/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..9ec3849c1b --- /dev/null +++ b/challenge-272/jo-37/perl/ch-1.pl @@ -0,0 +1,65 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use Regexp::Common 'net'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die < Date: Wed, 5 Jun 2024 19:09:08 +0200 Subject: Solution to task 2 --- challenge-272/jo-37/perl/ch-2.pl | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 challenge-272/jo-37/perl/ch-2.pl diff --git a/challenge-272/jo-37/perl/ch-2.pl b/challenge-272/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..7f1ee3903f --- /dev/null +++ b/challenge-272/jo-37/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; +use PDL::NiceSlice; +use PDL::Char; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <new(shift); + sum abs long($s(0:-2)) - long($s(1:-1)); +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is score("hello"), 13, "example 1"; + is score("perl"), 30, "example 2"; + is score("raku"), 37, "example 3"; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} -- cgit From 54ac1478930ac85970011600db98d89714bc0d9f Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Wed, 5 Jun 2024 19:09:22 +0200 Subject: Blog for challenge 272 --- challenge-272/jo-37/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-272/jo-37/blog.txt diff --git a/challenge-272/jo-37/blog.txt b/challenge-272/jo-37/blog.txt new file mode 100644 index 0000000000..39fed0c9b1 --- /dev/null +++ b/challenge-272/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2024/06/07/ch-272.html -- cgit From 7f74b9b11f864730d4d2ccd5e6b7ac5d2c8ab2b0 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Sat, 8 Jun 2024 05:14:55 +1000 Subject: pwc272 solution in go --- challenge-272/pokgopun/go/ch-1.go | 59 +++++++++++++++++++++++ challenge-272/pokgopun/go/ch-2.go | 98 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 challenge-272/pokgopun/go/ch-1.go create mode 100644 challenge-272/pokgopun/go/ch-2.go diff --git a/challenge-272/pokgopun/go/ch-1.go b/challenge-272/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..10a9c05be0 --- /dev/null +++ b/challenge-272/pokgopun/go/ch-1.go @@ -0,0 +1,59 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-272/ +/*# + +Task 1: Defang IP Address + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a valid IPv4 address. + + Write a script to return the defanged version of the given IP address. + + A defanged IP address replaces every period “.” with “[.]". + +Example 1 + +Input: $ip = "1.1.1.1" +Output: "1[.]1[.]1[.]1" + +Example 2 + +Input: $ip = "255.101.1.0" +Output: "255[.]101[.]1[.]0" + +Task 2: String Score +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func dfip(str string) string { + var rs []rune + for _, v := range str { + if v == '.' { + rs = append(rs, '[', '.', ']') + } else { + rs = append(rs, v) + } + } + return string(rs) +} + +func main() { + for _, data := range []struct { + input, output string + }{ + {"1.1.1.1", "1[.]1[.]1[.]1"}, + {"255.101.1.0", "255[.]101[.]1[.]0"}, + } { + io.WriteString(os.Stdout, cmp.Diff(dfip(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-272/pokgopun/go/ch-2.go b/challenge-272/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..a66313e197 --- /dev/null +++ b/challenge-272/pokgopun/go/ch-2.go @@ -0,0 +1,98 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-272/ +/*# + +Task 2: String Score + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str. + + Write a script to return the score of the given string. + + The score of a string is defined as the sum of the absolute + difference between the ASCII values of adjacent characters. + +Example 1 + +Input: $str = "hello" +Output: 13 + +ASCII values of characters: +h = 104 +e = 101 +l = 108 +l = 108 +o = 111 + +Score => |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| + => 3 + 7 + 0 + 3 + => 13 + +Example 2 + +Input: "perl" +Output: 30 + +ASCII values of characters: +p = 112 +e = 101 +r = 114 +l = 108 + +Score => |112 - 101| + |101 - 114| + |114 - 108| + => 11 + 13 + 6 + => 30 + +Example 3 + +Input: "raku" +Output: 37 + +ASCII values of characters: +r = 114 +a = 97 +k = 107 +u = 117 + +Score => |114 - 97| + |97 - 107| + |107 - 117| + => 17 + 10 + 10 + => 37 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 9th June 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func ss(str string) int { + var c int + for i := range len(str) - 1 { + c += int(max(str[i], str[i+1]) - min(str[i], str[i+1])) + } + return c +} + +func main() { + for _, data := range []struct { + input string + output int + }{ + {"hello", 13}, + {"perl", 30}, + {"raku", 37}, + } { + io.WriteString(os.Stdout, cmp.Diff(ss(data.input), data.output)) // blank if ok, otherwise show the difference + } +} -- cgit From 788b186829df344234ac64d33625b8d5f8861bf7 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Fri, 7 Jun 2024 23:21:59 -0400 Subject: deleted files --- challenge-259/adam-russell/.gitignore | 3 --- challenge-259/adam-russell/twc.pdf | Bin 67950 -> 0 bytes 2 files changed, 3 deletions(-) delete mode 100644 challenge-259/adam-russell/.gitignore delete mode 100644 challenge-259/adam-russell/twc.pdf diff --git a/challenge-259/adam-russell/.gitignore b/challenge-259/adam-russell/.gitignore deleted file mode 100644 index d4e9a94d5e..0000000000 --- a/challenge-259/adam-russell/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -*.bbprojectd -.RData -.Rhistory diff --git a/challenge-259/adam-russell/twc.pdf b/challenge-259/adam-russell/twc.pdf deleted file mode 100644 index 181cc3d3fa..0000000000 Binary files a/challenge-259/adam-russell/twc.pdf and /dev/null differ -- cgit From a85bcb5a66c271414049c25be6e5254bac28d6f6 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Sat, 8 Jun 2024 00:16:32 -0400 Subject: initial commit --- challenge-272/adam-russell/blog.txt | 1 + challenge-272/adam-russell/perl/ch-1.pl | 25 +++++++++++++++++++++++++ challenge-272/adam-russell/perl/ch-2.pl | 26 ++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 challenge-272/adam-russell/blog.txt create mode 100644 challenge-272/adam-russell/perl/ch-1.pl create mode 100644 challenge-272/adam-russell/perl/ch-2.pl diff --git a/challenge-272/adam-russell/blog.txt b/challenge-272/adam-russell/blog.txt new file mode 100644 index 0000000000..a791044ed5 --- /dev/null +++ b/challenge-272/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2024/06/08 diff --git a/challenge-272/adam-russell/perl/ch-1.pl b/challenge-272/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..3564f846ea --- /dev/null +++ b/challenge-272/adam-russell/perl/ch-1.pl @@ -0,0 +1,25 @@ + + +use v5.38; + + +sub defang{ + my($c, $defanged) = @_; + $defanged = [] if !$defanged; + return $defanged if @{$c} == 0; + my $x = shift @{$c}; + if($x eq q/./){ + push @{$defanged}, q/[.]/; + } + else{ + push @{$defanged}, $x; + } + defang($c, $defanged); +} + + +MAIN:{ + say join(q//, @{defang([split //, q/1.1.1.1/])}); + say join(q//, @{defang([split //, q/255.101.1.0/])}); +} + diff --git a/challenge-272/adam-russell/perl/ch-2.pl b/challenge-272/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..bbdb2ea352 --- /dev/null +++ b/challenge-272/adam-russell/perl/ch-2.pl @@ -0,0 +1,26 @@ + + +use v5.38; + + +sub string_score{ + my($s) = shift; + my $score = 0; + my @s = map {ord $_} split //, $s; + { + my $x = shift @s; + my $y = shift @s; + $score += abs($x - $y) if $x && $y; + unshift @s, $y; + redo if @s > 1; + } + return $score; +} + + +MAIN:{ + say string_score q/hello/; + say string_score q/perl/; + say string_score q/raku/; +} + -- cgit From 14e4db4d669770384019246cd819e0842cc67a4e Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sat, 8 Jun 2024 13:56:41 +0100 Subject: - Added solutions by Robbie Hatley. - Added solutions by Luca Ferrari. - Added solutions by PokGoPun. - Added solutions by Roger Bell_West. - Added solutions by Jan Krnavek. - Added solutions by Jorg Sommrey. - Added solutions by Adam Russell. - Added solutions by Reinier Maliepaard. --- challenge-272/reinier-maliepaard/blog.txt | 1 + challenge-272/reinier-maliepaard/perl/ch-1.pl | 53 + challenge-272/reinier-maliepaard/perl/ch-2.pl | 60 + challenge-272/reinier-maliepaard/perl/ch-2a.pl | 16 + stats/pwc-current.json | 326 ++-- stats/pwc-language-breakdown-summary.json | 72 +- stats/pwc-language-breakdown.json | 1910 ++++++++++++------------ stats/pwc-leaders.json | 496 +++--- stats/pwc-summary-1-30.json | 44 +- stats/pwc-summary-121-150.json | 112 +- stats/pwc-summary-151-180.json | 106 +- stats/pwc-summary-181-210.json | 48 +- stats/pwc-summary-211-240.json | 38 +- stats/pwc-summary-241-270.json | 48 +- stats/pwc-summary-271-300.json | 46 +- stats/pwc-summary-301-330.json | 44 +- stats/pwc-summary-31-60.json | 42 +- stats/pwc-summary-61-90.json | 36 +- stats/pwc-summary-91-120.json | 46 +- stats/pwc-summary.json | 76 +- 20 files changed, 1932 insertions(+), 1688 deletions(-) create mode 100644 challenge-272/reinier-maliepaard/blog.txt create mode 100644 challenge-272/reinier-maliepaard/perl/ch-1.pl create mode 100644 challenge-272/reinier-maliepaard/perl/ch-2.pl create mode 100644 challenge-272/reinier-maliepaard/perl/ch-2a.pl diff --git a/challenge-272/reinier-maliepaard/blog.txt b/challenge-272/reinier-maliepaard/blog.txt new file mode 100644 index 0000000000..7d54963077 --- /dev/null +++ b/challenge-272/reinier-maliepaard/blog.txt @@ -0,0 +1 @@ +https://reiniermaliepaard.nl/perl/pwc/index.php?id=pwc272 diff --git a/challenge-272/reinier-maliepaard/perl/ch-1.pl b/challenge-272/reinier-maliepaard/perl/ch-1.pl new file mode 100644 index 0000000000..1c6542cc84 --- /dev/null +++ b/challenge-272/reinier-maliepaard/perl/ch-1.pl @@ -0,0 +1,53 @@ +#!/usr/bin/perl +use strict; +use warnings; + +=begin + Here my solution TASK #1 along with alternatives from Niels van Dijke and James Curtis-Smith. + I did some benchmark (https://metacpan.org/pod/Benchmark) with 'timethese': + James' join+split is the fastest solution, even when the validation in Niels' and my + solution is removed +=cut + +use Data::Validate::IP qw(is_ipv4); + +sub defangIP { + + if (is_ipv4($_[0])) { + $_[0] =~ s/\./[.]/g; + } + else { + print("No valid IP address!"); + } + +# Inspired by Niels van Dijke's oneliner which uses the necessary /r switch +# (Niels uses for validation another module Net::IP) +# (is_ipv4($_[0])) ? $_[0] =~ s/\./[.]/gr : "No valid IP address!"; + +# James Curtis-Smith solution: +# join '[.]', split /\./, $_[0]; +} + +# TESTS +my $ip; + +# Example 1 +$ip = "1.1.1.1"; +print(defangIP($ip), "\n"); # Output: 1[.]1[.]1[.]1 + +# Example 2 +$ip = "255.101.1.0"; +print(defangIP($ip), "\n"); # Output: 255[.]101[.]1[.]0 + +# Example 3 +$ip = "123.234.345.001"; +print(defangIP($ip), "\n"); # Output: "No valid IP address! + +# Example 4 +# From: https://metacpan.org/pod/Data::Validate::IP +# There are security implications to this around certain oddly formed addresses. +# Notably, an address like "010.0.0.1" is technically valid, but the operating +# system will treat '010' as an octal number: '010.0.0.1' is interpreted as '8.0.0.1' +# James' solution and Niels' original do not take that into account. +$ip = "010.0.0.1"; +print(defangIP($ip), "\n"); # Output: "No valid IP address! diff --git a/challenge-272/reinier-maliepaard/perl/ch-2.pl b/challenge-272/reinier-maliepaard/perl/ch-2.pl new file mode 100644 index 0000000000..e67813f50c --- /dev/null +++ b/challenge-272/reinier-maliepaard/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Benchmark qw(:all); + +=begin + Here is my solution for TASK #2, along with alternatives from + Niels van Dijke and James Curtis-Smith. I shared the benchmark results, + which show some variability when run multiple times. Nonetheless, James's + solution is the fastest. +=cut + +# Reinier Maliepaard: function to calculate total sum using split and for +sub ascii_differences_sum_split_1 { + my @chars = split(//, $_[0]); + my $total_sum = 0; + + for my $i (0 .. $#chars - 1) { + $total_sum += abs(ord($chars[$i]) - ord($chars[$i + 1])); + } + + return($total_sum); +} + +# James Curtis-Smith: function to calculate total sum using split and for +sub ascii_differences_sum_split_2 { + my ($t, @l) = (0, split //, $_[0]); + + my $f = ord shift @l; + + ($t += abs($f - ord)), $f=ord for @l; + + $t; +} + +# Niels van Dijke: function to calculate total sum using sum0 and slide +use List::Util qw(sum0); +use List::MoreUtils qw(slide); + +sub ascii_differences_sum_slide { + sum0 slide {abs(ord($b) - ord($a))} split '',$_[0] +} + +# A fanciful creation by fans, combining various Elvish elements from Tolkien's work. +my $input_string = "Aearenuialinorosinaiantirnoitisirsiensinoit"; + +# Benchmark all three methods +timethese(1000000, { + 'Using split_1' => sub { ascii_differences_sum_split_1($input_string) }, + 'Using split_2' => sub { ascii_differences_sum_split_2($input_string) }, + 'Using slide' => sub { ascii_differences_sum_slide($input_string) }, +}); + +=begin +Results: + Benchmark: timing 1000000 iterations of Using slide, Using split_1, Using split_2... + Using slide: 7 wallclock secs ( 6.55 usr + 0.00 sys = 6.55 CPU) @ 152671.76/s (n=1000000) + Using split_1: 7 wallclock secs ( 6.87 usr + 0.00 sys = 6.87 CPU) @ 145560.41/s (n=1000000) + Using split_2: 6 wallclock secs ( 5.76 usr + 0.00 sys = 5.76 CPU) @ 173611.11/s (n=1000000) +=cut diff --git a/challenge-272/reinier-maliepaard/perl/ch-2a.pl b/challenge-272/reinier-maliepaard/perl/ch-2a.pl new file mode 100644 index 0000000000..5c2b34ddce --- /dev/null +++ b/challenge-272/reinier-maliepaard/perl/ch-2a.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub ascii_differences_sum_split_2_alt +{ + my ( $t, @list ) = ( 0, split(//, $_[0]) ); + + my $f = ord( shift(@list) ); + + for (@list) { + $t += abs( $f - ord($_) ); + $f = ord($_); + } + return($t); +} diff --git a/stats/pwc-current.json b/stats/pwc-current.json index 689963ed7b..ac531cbb78 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,4 +1,23 @@ { + "xAxis" : { + "type" : "category" + }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "title" : { + "text" : "The Weekly Challenge - 272" + }, + "subtitle" : { + "text" : "[Champions: 24] Last updated at 2024-06-08 12:52:21 GMT" + }, + "tooltip" : { + "followPointer" : 1, + "pointFormat" : "{point.name}: {point.y:f}
", + "headerFormat" : "{series.name}
" + }, "drilldown" : { "series" : [ { @@ -7,31 +26,35 @@ "Perl", 2 ], - [ - "Raku", - 2 - ], [ "Blog", 1 ] ], - "name" : "Ali Moradi", - "id" : "Ali Moradi" + "name" : "Adam Russell", + "id" : "Adam Russell" }, { + "name" : "Ali Moradi", + "id" : "Ali Moradi", "data" : [ [ "Perl", 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 ] - ], - "id" : "Dave Jacoby", - "name" : "Dave Jacoby" + ] }, { - "name" : "David Ferrone", - "id" : "David Ferrone", + "id" : "Dave Jacoby", + "name" : "Dave Jacoby", "data" : [ [ "Perl", @@ -46,8 +69,18 @@ 2 ] ], + "name" : "David Ferrone", + "id" : "David Ferrone" + }, + { "name" : "E. Choroba", - "id" : "E. Choroba" + "id" : "E. Choroba", + "data" : [ + [ + "Perl", + 2 + ] + ] }, { "data" : [ @@ -56,8 +89,8 @@ 2 ] ], - "id" : "Feng Chang", - "name" : "Feng Chang" + "name" : "Feng Chang", + "id" : "Feng Chang" }, { "data" : [ @@ -74,8 +107,32 @@ 1 ] ], - "name" : "Jaldhar H. Vyas", - "id" : "Jaldhar H. Vyas" + "id" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas" + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Jan Krnavek", + "name" : "Jan Krnavek" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Jorg Sommrey", + "name" : "Jorg Sommrey" }, { "data" : [ @@ -92,40 +149,56 @@ 1 ] ], - "name" : "Laurent Rosenfeld", - "id" : "Laurent Rosenfeld" + "id" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld" }, { - "name" : "Mark Anderson", - "id" : "Mark Anderson", + "id" : "Luca Ferrari", + "name" : "Luca Ferrari", "data" : [ [ "Raku", 2 + ], + [ + "Blog", + 9 ] ] }, { - "name" : "Matthew Neleigh", - "id" : "Matthew Neleigh", + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Mark Anderson", + "name" : "Mark Anderson" + }, + { "data" : [ [ "Perl", 2