diff options
| author | Matthew Neleigh <87619159+mattneleigh@users.noreply.github.com> | 2024-06-05 13:04:21 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-05 13:04:21 -0400 |
| commit | b7ddc0df59586129822cd79732dee50ddabbf18c (patch) | |
| tree | e78636a66f5b729b049459837b524e701dc22082 /challenge-272 | |
| parent | 74e6d81b2e785bd2f7dd4398ea5e2733618ac4b3 (diff) | |
| parent | 7aeb2014a04d815bc006a3c337b73426a10ea591 (diff) | |
| download | perlweeklychallenge-club-b7ddc0df59586129822cd79732dee50ddabbf18c.tar.gz perlweeklychallenge-club-b7ddc0df59586129822cd79732dee50ddabbf18c.tar.bz2 perlweeklychallenge-club-b7ddc0df59586129822cd79732dee50ddabbf18c.zip | |
Merge branch 'manwar:master' into pwc272
Diffstat (limited to 'challenge-272')
71 files changed, 1186 insertions, 0 deletions
diff --git a/challenge-272/chazzka/README b/challenge-272/chazzka/README new file mode 100644 index 0000000000..fd45943659 --- /dev/null +++ b/challenge-272/chazzka/README @@ -0,0 +1 @@ +Solution by chazzka diff --git a/challenge-272/chazzka/coconut/ch-2.coco b/challenge-272/chazzka/coconut/ch-2.coco new file mode 100644 index 0000000000..f66b849fdb --- /dev/null +++ b/challenge-272/chazzka/coconut/ch-2.coco @@ -0,0 +1,6 @@ +def string_score(s, accum = 0): + match s: + case [_]: + return accum + case [head, sec] + tail: + return string_score([sec] + tail, accum + abs(ord(sec) - ord(head)))
\ No newline at end of file diff --git a/challenge-272/chazzka/coconut/test/matching_test.coco b/challenge-272/chazzka/coconut/test/matching_test.coco new file mode 100644 index 0000000000..de3748ff84 --- /dev/null +++ b/challenge-272/chazzka/coconut/test/matching_test.coco @@ -0,0 +1,14 @@ +import unittest +import importlib +solution = importlib.import_module("ch-2") + + +class TestStringMethods(unittest.TestCase): + def test_matcher(self): + solution.string_score("hello") |> self.assertEqual$(13) + solution.string_score("perl") |> self.assertEqual$(30) + solution.string_score("raku") |> self.assertEqual$(37) + solution.string_score("coconut") |> self.assertEqual$(45) + +if __name__ == '__main__': + unittest.main()
\ No newline at end of file diff --git a/challenge-272/dave-jacoby/perl/ch-1.pl b/challenge-272/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..abaa466aae --- /dev/null +++ b/challenge-272/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ bitwise fc postderef say signatures state }; + +use List::Util qw{max}; + +my @examples = ( + + "1.1.1.1", "255.101.1.0" +); +for my $example (@examples) { + my $output = defang_ipv4($example); + say <<"END"; + Input: \$ip = "$example" + Output: "$output" +END +} + +sub defang_ipv4 ( $address ) { + $address =~ s/\./[.]/gmx; + return $address; +} diff --git a/challenge-272/dave-jacoby/perl/ch-2.pl b/challenge-272/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..79650b52d9 --- /dev/null +++ b/challenge-272/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +use List::Util qw{ sum0 }; + +my @examples = (qw{ hello perl raku weekly linux Perl PERL }); + +for my $example (@examples) { + my $output = string_score($example); + + say <<"END"; + Input: \$str = "$example" + Output: $output +END +} + +sub string_score ($str) { + my @str = split //, $str; + return sum0 + map { abs ord( $str[$_] ) - ord( $str[ 1 + $_ ] ) } + 0 .. -2 + scalar @str; +} diff --git a/challenge-272/deadmarshal/blog.txt b/challenge-272/deadmarshal/blog.txt new file mode 100644 index 0000000000..dbb200ed12 --- /dev/null +++ b/challenge-272/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2024/06/twc272.html diff --git a/challenge-272/deadmarshal/java/Ch1.java b/challenge-272/deadmarshal/java/Ch1.java new file mode 100644 index 0000000000..6f589b0407 --- /dev/null +++ b/challenge-272/deadmarshal/java/Ch1.java @@ -0,0 +1,7 @@ +public class Ch1 { + public static void main(String[] args) { + System.out.println("1.1.1.1".replaceAll("\\.", "[.]")); + System.out.println("255.101.1.0".replaceAll("\\.", "[.]")); + } +} + diff --git a/challenge-272/deadmarshal/java/Ch2.java b/challenge-272/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..91614ab462 --- /dev/null +++ b/challenge-272/deadmarshal/java/Ch2.java @@ -0,0 +1,16 @@ +public class Ch2 { + public static void main(String[] args) { + System.out.println(string_score("hello")); + System.out.println(string_score("perl")); + System.out.println(string_score("raku")); + } + + private static int string_score(String str) { + int sum = 0; + for (int i = 0; i < str.length() - 1; ++i) { + sum += Math.abs(str.charAt(i + 1) - str.charAt(i)); + } + return sum; + } +} + diff --git a/challenge-272/deadmarshal/lua/ch-1.lua b/challenge-272/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..0269798b42 --- /dev/null +++ b/challenge-272/deadmarshal/lua/ch-1.lua @@ -0,0 +1,10 @@ +#!/usr/bin/env lua + +local function defrag_ip_address(str) + assert(type(str) == 'string','str must be a string!') + return (str:gsub("%.","[.]")) +end + +print(defrag_ip_address('1.1.1.1')) +print(defrag_ip_address('255.101.1.0')) + diff --git a/challenge-272/deadmarshal/lua/ch-2.lua b/challenge-272/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..059197dbfd --- /dev/null +++ b/challenge-272/deadmarshal/lua/ch-2.lua @@ -0,0 +1,16 @@ +#!/usr/bin/env lua + +local function string_score(str) + assert(type(str) == 'string','str must be a string!') + local sum = 0 + for i=1,#str-1 do + sum = sum + math.abs(string.byte(str:sub(i+1,i+1)) + - string.byte(str:sub(i,i))) + end + return sum +end + +print(string_score('hello')) +print(string_score('perl')) +print(string_score('raku')) + diff --git a/challenge-272/deadmarshal/modula-3/ch1/src/Ch1.m3 b/challenge-272/deadmarshal/modula-3/ch1/src/Ch1.m3 new file mode 100644 index 0000000000..57909c2830 --- /dev/null +++ b/challenge-272/deadmarshal/modula-3/ch1/src/Ch1.m3 @@ -0,0 +1,30 @@ +MODULE Ch1 EXPORTS Main; + +IMPORT SIO,Text,CharSeq; + +PROCEDURE DefragIpAddress(READONLY t:TEXT):TEXT = + VAR + Seq:CharSeq.T := NEW(CharSeq.T).init(Text.Length(t)); + A:REF ARRAY OF CHAR; + C:CHAR; + BEGIN + FOR I := 0 TO Text.Length(t)-1 DO + C := Text.GetChar(t,I); + IF C = '.' THEN + Seq.addhi('['); + Seq.addhi(C); + Seq.addhi(']') + ELSE + Seq.addhi(C) + END; + END; + A := NEW(REF ARRAY OF CHAR,Seq.size()); + FOR I := 0 TO Seq.size()-1 DO A[I] := Seq.get(I) END; + RETURN Text.FromChars(A^) + END DefragIpAddress; + +BEGIN + SIO.PutText(DefragIpAddress("1.1.1.1") & "\n"); + SIO.PutText(DefragIpAddress("255.101.1.0") & "\n"); +END Ch1. + diff --git a/challenge-272/deadmarshal/modula-3/ch1/src/m3makefile b/challenge-272/deadmarshal/modula-3/ch1/src/m3makefile new file mode 100644 index 0000000000..9f66e4a51f --- /dev/null +++ b/challenge-272/deadmarshal/modula-3/ch1/src/m3makefile @@ -0,0 +1,5 @@ +import("libm3") +import("libsio") +implementation("Ch1") +program("ch1") + diff --git a/challenge-272/deadmarshal/modula-3/ch2/src/Ch2.m3 b/challenge-272/deadmarshal/modula-3/ch2/src/Ch2.m3 new file mode 100644 index 0000000000..d59307e562 --- /dev/null +++ b/challenge-272/deadmarshal/modula-3/ch2/src/Ch2.m3 @@ -0,0 +1,19 @@ +MODULE Ch2 EXPORTS Main; + +IMPORT SIO,Text; + +PROCEDURE StringScore(READONLY t:TEXT):INTEGER = + VAR Sum:INTEGER := 0; + BEGIN + FOR I := 0 TO Text.Length(t)-2 DO + INC(Sum,ABS(ORD(Text.GetChar(t,I+1)) - ORD(Text.GetChar(t,I)))) + END; + RETURN Sum + END StringScore; + +BEGIN + SIO.PutInt(StringScore("hello")); SIO.Nl(); + SIO.PutInt(StringScore("perl")); SIO.Nl(); + SIO.PutInt(StringScore("raku")); SIO.Nl() +END Ch2. + diff --git a/challenge-272/deadmarshal/modula-3/ch2/src/m3makefile b/challenge-272/deadmarshal/modula-3/ch2/src/m3makefile new file mode 100644 index 0000000000..798c627ef3 --- /dev/null +++ b/challenge-272/deadmarshal/modula-3/ch2/src/m3makefile @@ -0,0 +1,5 @@ +import("libm3") +import("libsio") +implementation("Ch2") +program("ch2") + diff --git a/challenge-272/deadmarshal/perl/ch-1.pl b/challenge-272/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..cd1f537594 --- /dev/null +++ b/challenge-272/deadmarshal/perl/ch-1.pl @@ -0,0 +1,11 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub defrag_ip_address{ + $_[0] =~ s/\./\[\.\]/gr +} + +printf "%s\n",defrag_ip_address('1.1.1.1'); +printf "%s\n",defrag_ip_address('255.101.1.0'); + diff --git a/challenge-272/deadmarshal/perl/ch-2.pl b/challenge-272/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..bc0e36b06c --- /dev/null +++ b/challenge-272/deadmarshal/perl/ch-2.pl @@ -0,0 +1,14 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw(sum0); +use List::MoreUtils qw(slide); + +sub string_score{ + sum0 slide {abs(ord($b) - ord($a))} split '',$_[0] +} + +printf "%d\n",string_score('hello'); +printf "%d\n",string_score('perl'); +printf "%d\n",string_score('raku'); + diff --git a/challenge-272/deadmarshal/raku/ch-1.raku b/challenge-272/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..1017df67ae --- /dev/null +++ b/challenge-272/deadmarshal/raku/ch-1.raku @@ -0,0 +1,10 @@ +#!/usr/bin/env raku + +sub defrag-ip-address($str) +{ + $str.subst('.','[.]',:g) +} + +say defrag-ip-address('1.1.1.1'); +say defrag-ip-address('255.101.1.0'); + diff --git a/challenge-272/deadmarshal/raku/ch-2.raku b/challenge-272/deadmarshal/raku/ch-2.raku new file mode 100644 index 0000000000..1760ab24a7 --- /dev/null +++ b/challenge-272/deadmarshal/raku/ch-2.raku @@ -0,0 +1,16 @@ +#!/usr/bin/env raku + +sub string-score($str) +{ + my @arr = $str.ords; + my $sum = 0; + for 0..^@arr.end -> $i { + $sum += abs(@arr[$i+1] - @arr[$i]) + } + $sum +} + +say string-score('hello'); +say string-score('perl'); +say string-score('raku'); + diff --git a/challenge-272/e-choroba/perl/ch-1.pl b/challenge-272/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..e38b5da952 --- /dev/null +++ b/challenge-272/e-choroba/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub defang_ip_address($address) { + $address =~ s/[.]/[.]/gr +} + +use Test::More tests => 2; + +is defang_ip_address('1.1.1.1'), '1[.]1[.]1[.]1', 'Example 1'; +is defang_ip_address('255.101.1.0'), '255[.]101[.]1[.]0', 'Example 2'; diff --git a/challenge-272/e-choroba/perl/ch-2.pl b/challenge-272/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..f0f4116061 --- /dev/null +++ b/challenge-272/e-choroba/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub string_score($str) { + my $score = 0; + + while ($str =~ /(.)(?=(.))/g) { + $score += abs(ord($1) - ord($2)); + } + return $score +} + +use Test::More tests => 3 + 2; + +is string_score('hello'), 13, 'Example 1'; +is string_score('perl'), 30, 'Example 2'; +is string_score('raku'), 37, 'Example 3'; + +is string_score(""), 0, 'Empty'; +is string_score('a'), 0, 'Single character'; diff --git a/challenge-272/eric-cheung/python/ch-1.py b/challenge-272/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..6ac6998ad4 --- /dev/null +++ b/challenge-272/eric-cheung/python/ch-1.py @@ -0,0 +1,7 @@ +
+## strIP = "1.1.1.1" ## Example 1
+strIP = "255.101.1.0" ## Example 2
+
+strOutput = strIP.replace(".", "[.]")
+
+print (strOutput)
diff --git a/challenge-272/eric-cheung/python/ch-2.py b/challenge-272/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..52f8329ed9 --- /dev/null +++ b/challenge-272/eric-cheung/python/ch-2.py @@ -0,0 +1,9 @@ +
+## strInput = "hello" ## Example 1
+## strInput = "perl" ## Example 2
+strInput = "raku" ## Example 3
+
+arrCode = [ord(charLoop) for charLoop in strInput]
+arrCodeAbsDiff = [abs(arrCode[nIndx] - arrCode[nIndx + 1]) for nIndx in range(len(arrCode) - 1)]
+
+print (sum(arrCodeAbsDiff))
diff --git a/challenge-272/feng-chang/raku/ch-1.raku b/challenge-272/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..cfad2243be --- /dev/null +++ b/challenge-272/feng-chang/raku/ch-1.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(Str:D $s); + +put $s.subst('.', '[.]', :g); diff --git a/challenge-272/feng-chang/raku/ch-2.raku b/challenge-272/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..a8829e7dda --- /dev/null +++ b/challenge-272/feng-chang/raku/ch-2.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(Str:D $s); + +put $s.comb».ord.rotor(2 => -1).map({ [-] $_ })».abs.sum; diff --git a/challenge-272/feng-chang/raku/test.raku b/challenge-272/feng-chang/raku/test.raku new file mode 100755 index 0000000000..89720db130 --- /dev/null +++ b/challenge-272/feng-chang/raku/test.raku @@ -0,0 +1,23 @@ +#!/bin/env raku + +# The Weekly Challenge 272 +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, Defrang IP Address +pwc-test './ch-1.raku', '1.1.1.1', '1[.]1[.]1[.]1', 'Defrang IP Address: 1.1.1.1 => 1[.]1[.]1[.]1'; +pwc-test './ch-1.raku', '255.101.1.0', '255[.]101[.]1[.]0', 'Defrang IP Address: 255.101.1.0 => 255[.]101[.]1[.]0'; + +# Task 2, String Score +pwc-test './ch-2.raku', 'hello', 13, 'String Score: hello => 13'; |
