diff options
55 files changed, 3111 insertions, 1740 deletions
diff --git a/challenge-273/0rir/raku/ch-1.raku b/challenge-273/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..0465e7f56c --- /dev/null +++ b/challenge-273/0rir/raku/ch-1.raku @@ -0,0 +1,59 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +Task 1: Percentage of Character +Submitted by: Mohammad Sajid Anwar +You are given a string, $str and a character $char. + +Write a script to return the percentage, nearest whole, of given character in the given string. + +Example 1 +Input: $str = "perl", $char = "e" +Output: 25 +Example 2 +Input: $str = "java", $char = "a" +Output: 50 +Example 3 +Input: $str = "python", $char = "m" +Output: 0 +Example 4 +Input: $str = "ada", $char = "a" +Output: 67 +Example 5 +Input: $str = "ballerina", $char = "l" +Output: 22 +Example 6 +Input: $str = "analitik", $char = "k" +Output: 13 +=end comment + +my @Test = + "perl", "e", 25, + "java", "a", 50, + "python", "m", 0, + "ada", "a", 67, + "ballerina", "l", 22, + "analitik", "k", 13, + 'a', "a", 100, + 'a' x 100 ~ 'b', 'a', 99, + 'a' x 100 ~ 'b', 'b', 1, + 'a' x 200 ~ 'b', 'a', 100, + 'a' x 200 ~ 'b', 'b', 0, + 'a' x 199 ~ 'b', 'a', 100, + 'a' x 199 ~ 'b', 'b', 1, +; +plan @Test ÷ 3; + +sub task( $word, $letter) { + my @w = $word.comb; + (100 × @w.grep( $letter) ÷ @w).round; +} + +for @Test -> $in, $letter, $exp { + is task($in, $letter), $exp, "$exp <- ($letter) $in"; +} + +done-testing; diff --git a/challenge-273/0rir/raku/ch-2.raku b/challenge-273/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..267e402c7f --- /dev/null +++ b/challenge-273/0rir/raku/ch-2.raku @@ -0,0 +1,53 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +273-2: B After A Submitted by: Mohammad Sajid Anwar +You are given a string, $str. + +Write a script to return true if there is at least one b, and no a appears after the first b. + +Example 1 +Input: $str = "aabb" +Output: true +Example 2 +Input: $str = "abab" +Output: false +Example 3 +Input: $str = "aaa" +Output: false +Example 4 +Input: $str = "bbb" +Output: true + +=end comment + +my @Test = + "aabb", True, + "abab", False, + "aaa", False, + "bbb", True, + "ccc", False, + "cbc", True, + "cac", False, + "cab", True, +; +plan @Test ÷ 2; + +sub task( $word) { + my @w = $word.comb; + without my $b-k = @w.first( 'b', :k) { return False } + with + @w[++$b-k..^@w].first( 'a') { return False } + True; +} + +for @Test -> $in, $exp { + is task($in), $exp, "$exp\t<- $in"; +} + +done-testing; + + diff --git a/challenge-273/arne-sommer/blog.txt b/challenge-273/arne-sommer/blog.txt new file mode 100644 index 0000000000..0b9e6b9601 --- /dev/null +++ b/challenge-273/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/b-of-a.html diff --git a/challenge-273/arne-sommer/raku/b-after-a b/challenge-273/arne-sommer/raku/b-after-a new file mode 100755 index 0000000000..1bdc6b234e --- /dev/null +++ b/challenge-273/arne-sommer/raku/b-after-a @@ -0,0 +1,16 @@ +#! /usr/bin/env raku + +unit sub MAIN ($str where $str.chars > 0); + +if $str ~~ /b/ +{ + my $after = $str.split(/b/, 2)[1]; + + if $after !~~ /a/ + { + say 'true'; + exit; + } +} + +say 'false'; diff --git a/challenge-273/arne-sommer/raku/ch-1.raku b/challenge-273/arne-sommer/raku/ch-1.raku new file mode 100755 index 0000000000..0b835c61e9 --- /dev/null +++ b/challenge-273/arne-sommer/raku/ch-1.raku @@ -0,0 +1,5 @@ +#! /usr/bin/env raku + +unit sub MAIN ($str where $str.chars > 0, $char where $char.chars == 1); + +say ( (100 * $str.comb.Bag{$char} / $str.chars) + 0.5).int; diff --git a/challenge-273/arne-sommer/raku/ch-2.raku b/challenge-273/arne-sommer/raku/ch-2.raku new file mode 100755 index 0000000000..1bdc6b234e --- /dev/null +++ b/challenge-273/arne-sommer/raku/ch-2.raku @@ -0,0 +1,16 @@ +#! /usr/bin/env raku + +unit sub MAIN ($str where $str.chars > 0); + +if $str ~~ /b/ +{ + my $after = $str.split(/b/, 2)[1]; + + if $after !~~ /a/ + { + say 'true'; + exit; + } +} + +say 'false'; diff --git a/challenge-273/arne-sommer/raku/percentage-of-character b/challenge-273/arne-sommer/raku/percentage-of-character new file mode 100755 index 0000000000..0b835c61e9 --- /dev/null +++ b/challenge-273/arne-sommer/raku/percentage-of-character @@ -0,0 +1,5 @@ +#! /usr/bin/env raku + +unit sub MAIN ($str where $str.chars > 0, $char where $char.chars == 1); + +say ( (100 * $str.comb.Bag{$char} / $str.chars) + 0.5).int; diff --git a/challenge-273/bob-lied/README b/challenge-273/bob-lied/README index ebf6fe8695..fbef60f528 100644 --- a/challenge-273/bob-lied/README +++ b/challenge-273/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 272 by Bob Lied +Solutions to weekly challenge 273 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-272/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-272/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-273/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-273/bob-lied diff --git a/challenge-273/bob-lied/perl/ch-1.pl b/challenge-273/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..1ed2a5ca47 --- /dev/null +++ b/challenge-273/bob-lied/perl/ch-1.pl @@ -0,0 +1,110 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-1.pl Perl Weekly Challenge 273 Task 1 Percentage of Character +#============================================================================= +# You are given a string, $str and a character $char. +# Write a script to return the percentage, nearest whole, of given +# character in the given string. +# Example 1 Input: $str = "perl", $char = "e" +# Output: 25 +# Example 2 Input: $str = "java", $char = "a" +# Output: 50 +# Example 3 Input: $str = "python", $char = "m" +# Output: 0 +# Example 4 Input: $str = "ada", $char = "a" +# Output: 67 +# Example 5 Input: $str = "ballerina", $char = "l" +# Output: 22 +# Example 6 Input: $str = "analitik", $char = "k" +# Output: 13 +#============================================================================= + +use v5.40; + +use Getopt::Long; +my $DoTest = false; +my $Benchmark = 0; +my $Counter = 'saturn'; + +# Different ways to count the occurrence of a character in a string. +# Sample benchmark run on my system: +# Rate splitgrep delete grepcmp treval match saturn +# splitgrep 44683/s -- -26% -39% -84% -94% -96% +# delete 60386/s 35% -- -18% -78% -91% -94% +# grepcmp 73314/s 64% 21% -- -73% -89% -93% +# treval 274725/s 515% 355% 275% -- -60% -73% +# match 694444/s 1454% 1050% 847% 153% -- -31% +# saturn 1000000/s 2138% 1556% 1264% 264% 44% -- + +my %CountChar = ( + # Solution 1: delete everything that isn't char, use remaining length + delete => sub($str, $char) { length( $str =~ s/[^$char]//gr ) }, + + # Solution 2: Global match in list context yields an array of + # matching characters. Assigning to scalar yields length of the list. + match => sub($str, $char) { scalar( @{[ $str =~ m/$char/g ]} ) }, + + # Solution 3: Same array/scalar idea, but use =()= to get context + saturn => sub($str, $char) { my $occur =()= ( $str =~ m/$char/g ) }, + + # Solution 4: turn string into array and count with grep + splitgrep => sub($str, $char) { scalar( grep /$char/, split(//, $str) ) }, + + # Solution 4a: use string compare instead of RE in grep + grepcmp => sub($str, $char) { scalar( grep {$_ eq $char} split(//, $str) ) }, + + # Solution 5: count with tr///, needs eval to interpolate + treval => sub($str, $char) { eval "\$str =~ tr/$char//d" }, +); + +GetOptions("test" => \$DoTest, "benchmark:i" => \$Benchmark, "counter:s" => \$Counter); +exit(!runTest()) if $DoTest; +exit( runBenchmark($Benchmark) ) if $Benchmark; + +say pctOfChar(@ARGV, $CountChar{$Counter}); + + +sub pctOfChar($str, $char, $counter) +{ + my $occur = $counter->($str, $char); + return int( 100*($occur / length($str)) + 0.5 ); +} + +sub runTest +{ + use Test2::V0; + + for my $countFunc ( sort keys %CountChar ) + { + is( pctOfChar("perl", "e", $CountChar{$countFunc}), 25, "Example 1 perl e $countFunc"); + is( pctOfChar("java", "a", $CountChar{$countFunc}), 50, "Example 2 java a $countFunc"); + is( pctOfChar("python", "m", $CountChar{$countFunc}), 0, "Example 3 python m $countFunc"); + is( pctOfChar("ada", "a", $CountChar{$countFunc}), 67, "Example 4 ada a $countFunc"); + is( pctOfChar("ballerina", "l", $CountChar{$countFunc}), 22, "Example 5 ballerina l $countFunc"); + is( pctOfChar("analitik", "k", $CountChar{$countFunc}), 13, "Example 6 analitik k $countFunc"); + + is( pctOfChar("rrrr", "r", $CountChar{$countFunc}), 100, "100% $countFunc"); + } + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + my $str = 'abcdefghijklmnopqrstuvwxy' x 10; + + cmpthese($repeat, { + "delete" => sub { pctOfChar($str, 'n', $CountChar{delete}) }, + "match" => sub { pctOfChar($str, 'n', $CountChar{match}) }, + "saturn" => sub { pctOfChar($str, 'n', $CountChar{saturn}) }, + "splitgrep" => sub { pctOfChar($str, 'n', $CountChar{splitgrep}) }, + "grepcmp" => sub { pctOfChar($str, 'n', $CountChar{grepcmp}) }, + "treval" => sub { pctOfChar($str, 'n', $CountChar{treval}) }, + }); + +} diff --git a/challenge-273/bob-lied/perl/ch-2.pl b/challenge-273/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..7132fa848a --- /dev/null +++ b/challenge-273/bob-lied/perl/ch-2.pl @@ -0,0 +1,69 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-2.pl Perl Weekly Challenge 273 Task 2 B after A +#============================================================================= +# You are given a string, $str. +# Write a script to return true if there is at least one b, and no a +# appears after the first b. +# Example 1 Input: $str = "aabb" Output: true +# Example 2 Input: $str = "abab" Output: false +# Example 3 Input: $str = "aaa" Output: false +# Example 4 Input: $str = "bbb" Output: true +#============================================================================= + +use v5.40; + +use Getopt::Long; +my $DoTest = false; +my $Benchmark = 0; + +GetOptions("test" => \$DoTest, "benchmark:i" => \$Benchmark); +exit(!runTest()) if $DoTest; +exit( runBenchmark($Benchmark) ) if $Benchmark; + +say ( bAfterA($_) ? "true" : "false" ) for @ARGV; + +sub bAfterA($str) +{ + my $w = index($str, "b"); + return $w >= 0 && index($str, "a", $w) < 0; +} + +sub bAfterA_RE($str) +{ + $str =~ m/^[^b]*b[^a]*$/ +} + +sub runTest +{ + use Test2::V0; + + is (bAfterA("aabb"), true, "Example 1"); + is (bAfterA("abab"), false, "Example 2"); + is (bAfterA("aaa" ), false, "Example 3"); + is (bAfterA("bbb" ), true, "Example 4"); + + is (bAfterA_RE("aabb"), true, "Example 1 RE"); + is (bAfterA_RE("abab"), false, "Example 2 RE"); + is (bAfterA_RE("aaa" ), false, "Example 3 RE"); + is (bAfterA_RE("bbb" ), true, "Example 4 RE"); + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + cmpthese( $repeat, { + index => sub{ + bAfterA("aabb"), bAfterA("abab"), bAfterA("aaaa"), bAfterA("bbbb"), + }, + regex => sub{ + bAfterA_RE("aabb"), bAfterA_RE("abab"), bAfterA_RE("aaaa"), bAfterA_RE("bbbb"), + }, + }); +} diff --git a/challenge-273/deadmarshal/go/ch1.go b/challenge-273/deadmarshal/go/ch1.go new file mode 100644 index 0000000000..72e876ca87 --- /dev/null +++ b/challenge-273/deadmarshal/go/ch1.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" +) + +func percentageOfCharacter(str string, c rune) int { + count := 0 + for _,v := range str { + if v == c { + count++ + } + } + return int(float32(count) / float32(len(str)) * 100.0 + float32(0.5)) +} + +func main() { + fmt.Println(percentageOfCharacter("perl", 'e')) + fmt.Println(percentageOfCharacter("java", 'a')) + fmt.Println(percentageOfCharacter("python", 'm')) + fmt.Println(percentageOfCharacter("ada", 'a')) + fmt.Println(percentageOfCharacter("ballerina", 'l')) + fmt.Println(percentageOfCharacter("analitik", 'k')) +} diff --git a/challenge-273/deadmarshal/go/ch2.go b/challenge-273/deadmarshal/go/ch2.go new file mode 100644 index 0000000000..e58655dc17 --- /dev/null +++ b/challenge-273/deadmarshal/go/ch2.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "strings" +) + +func bAfterA(str string) bool { + i := strings.Index(str,"b") + return i != -1 && strings.LastIndex(str,"a") <= i +} + +func main(){ + fmt.Println(bAfterA("aabb")) + fmt.Println(bAfterA("abab")) + fmt.Println(bAfterA("aaa")) + fmt.Println(bAfterA("bbb")) +} diff --git a/challenge-273/deadmarshal/java/Ch1.java b/challenge-273/deadmarshal/java/Ch1.java new file mode 100644 index 0000000000..267c218829 --- /dev/null +++ b/challenge-273/deadmarshal/java/Ch1.java @@ -0,0 +1,17 @@ +public class Ch1 { + public static void main(String[] args) { + System.out.println(percentage_of_character("perl",'e')); + System.out.println(percentage_of_character("java",'a')); + System.out.println(percentage_of_character("python",'m')); + System.out.println(percentage_of_character("ada",'a')); + System.out.println(percentage_of_character("ballerina",'l')); + System.out.println(percentage_of_character("analitik",'k')); + } + + private static int percentage_of_character(String str,char c) { + int count = 0; + for(int i = 0; i < str.length(); ++i) if(str.charAt(i) == c) count++; + return Math.round(((float)count/str.length()) * 100); + } +} + diff --git a/challenge-273/deadmarshal/java/Ch2.java b/challenge-273/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..a606950072 --- /dev/null +++ b/challenge-273/deadmarshal/java/Ch2.java @@ -0,0 +1,14 @@ +public class Ch2 { + public static void main(String[] args) { + System.out.println(b_after_a("aabb")); + System.out.println(b_after_a("abab")); + System.out.println(b_after_a("aaa")); + System.out.println(b_after_a("bbb")); + } + + private static boolean b_after_a(String str) { + int i = str.indexOf('b'); + return i != -1 && i >= str.lastIndexOf('a'); + } +} + diff --git a/challenge-273/deadmarshal/modula-3/ch1/src/Ch1.m3 b/challenge-273/deadmarshal/modula-3/ch1/src/Ch1.m3 new file mode 100644 index 0000000000..b78a09364d --- /dev/null +++ b/challenge-273/deadmarshal/modula-3/ch1/src/Ch1.m3 @@ -0,0 +1,23 @@ +MODULE Ch1 EXPORTS Main; + +IMPORT SIO,Text; + +PROCEDURE PercentageOfCharacter(READONLY Str:TEXT; + READONLY C:CHAR):CARDINAL = + VAR Count:CARDINAL := 0; + BEGIN + FOR I := 0 TO Text.Length(Str)-1 DO + IF Text.GetChar(Str,I) = C THEN INC(Count) END + END; + RETURN FLOOR((FLOAT(Count) / FLOAT(Text.Length(Str))) * 100.0 + 0.5) + END PercentageOfCharacter; + +BEGIN + SIO.PutInt(PercentageOfCharacter("perl",'e')); SIO.Nl(); + SIO.PutInt(PercentageOfCharacter("java",'a')); SIO.Nl(); + SIO.PutInt(PercentageOfCharacter("python",'m')); SIO.Nl(); + SIO.PutInt(PercentageOfCharacter("ada",'a')); SIO.Nl(); + SIO.PutInt(PercentageOfCharacter("ballerina",'l')); SIO.Nl(); + SIO.PutInt(PercentageOfCharacter("analitik",'k')); SIO.Nl(); +END Ch1. + diff --git a/challenge-273/deadmarshal/modula-3/ch1/src/m3makefile b/challenge-273/deadmarshal/modula-3/ch1/src/m3makefile new file mode 100644 index 0000000000..9f66e4a51f --- /dev/null +++ b/challenge-273/deadmarshal/modula-3/ch1/src/m3makefile @@ -0,0 +1,5 @@ +import("libm3") +import("libsio") +implementation("Ch1") +program("ch1") + diff --git a/challenge-273/deadmarshal/modula-3/ch2/src/Ch2.m3 b/challenge-273/deadmarshal/modula-3/ch2/src/Ch2.m3 new file mode 100644 index 0000000000..421b09e8e0 --- /dev/null +++ b/challenge-273/deadmarshal/modula-3/ch2/src/Ch2.m3 @@ -0,0 +1,35 @@ +MODULE Ch2 EXPORTS Main; + +IMPORT SIO,Text; + +PROCEDURE FindChar(READONLY Str:TEXT; + READONLY C:CHAR; + READONLY Backwards:BOOLEAN := FALSE):INTEGER = + BEGIN + IF Backwards THEN + FOR I := Text.Length(Str)-1 TO 0 BY -1 DO + IF Text.GetChar(Str,I) = C THEN RETURN I END + END + ELSE + FOR I := 0 TO Text.Length(Str)-1 DO + IF Text.GetChar(Str,I) = C THEN RETURN I END + END + END; + RETURN -1 + END FindChar; + +PROCEDURE BAfterA(READONLY Str:TEXT):BOOLEAN = + VAR + IB:INTEGER := FindChar(Str,'b'); + IA:INTEGER := FindChar(Str,'a',TRUE); + BEGIN + RETURN IB # -1 AND IA <= IB + END BAfterA; + +BEGIN + SIO.PutBool(BAfterA("aabb")); SIO.Nl(); + SIO.PutBool(BAfterA("abab")); SIO.Nl(); + SIO.PutBool(BAfterA("aaa")); SIO.Nl(); + SIO.PutBool(BAfterA("bbb")); SIO.Nl(); +END Ch2. + diff --git a/challenge-273/deadmarshal/modula-3/ch2/src/m3makefile b/challenge-273/deadmarshal/modula-3/ch2/src/m3makefile new file mode 100644 index 0000000000..798c627ef3 --- /dev/null +++ b/challenge-273/deadmarshal/modula-3/ch2/src/m3makefile @@ -0,0 +1,5 @@ +import("libm3") +import("libsio") +implementation("Ch2") +program("ch2") + diff --git a/challenge-273/deadmarshal/perl/ch-1.pl b/challenge-273/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..044da5f05e --- /dev/null +++ b/challenge-273/deadmarshal/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use POSIX qw(round); + +sub percentage_of_character{ + my ($str,$char) = @_; + my $count =()= $str =~ /\Q$char/g; + round(100 * $count / length $str) +} + +printf "%d\n",percentage_of_character('perl','e'); +printf "%d\n",percentage_of_character('java','a'); +printf "%d\n",percentage_of_character('python','m'); +printf "%d\n",percentage_of_character('ada','a'); +printf "%d\n",percentage_of_character('ballerina','l'); +printf "%d\n",percentage_of_character('analitik','k'); + diff --git a/challenge-273/deadmarshal/perl/ch-2.pl b/challenge-273/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..5a9f32d508 --- /dev/null +++ b/challenge-273/deadmarshal/perl/ch-2.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub b_after_a{ + (-1 != index($_[0],'b')) >= rindex($_[0],'a') +} + +printf "%d\n",b_after_a('aabb'); +printf "%d\n",b_after_a('abab'); +printf "%d\n",b_after_a('aaa'); +printf "%d\n",b_after_a('bbb'); + diff --git a/challenge-273/deadmarshal/raku/ch-1.raku b/challenge-273/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..8b2ecd76e0 --- /dev/null +++ b/challenge-273/deadmarshal/raku/ch-1.raku @@ -0,0 +1,15 @@ +#!/usr/bin/env raku + +sub percentage-of-character($str,$c) +{ + my $count = $str.comb($c).elems; + round(100 * $count / $str.chars) +} + |
