From 57437bd4e563c53c107ca9c1ba91284053b8d08e Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Mon, 10 Jun 2024 09:02:42 -0500 Subject: Week 273 solutions Alternate solution for ch-1.pl typo --- challenge-273/bob-lied/README | 6 ++-- challenge-273/bob-lied/perl/ch-1.pl | 61 +++++++++++++++++++++++++++++++++++++ challenge-273/bob-lied/perl/ch-2.pl | 44 ++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 challenge-273/bob-lied/perl/ch-1.pl create mode 100644 challenge-273/bob-lied/perl/ch-2.pl (limited to 'challenge-273') 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..15f0409cda --- /dev/null +++ b/challenge-273/bob-lied/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/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 $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +say pctOfChar(@ARGV); + +sub pctOfChar($str, $char) +{ + # Solutioh 1: delete everything that isn't char, use remaining length + # my $occur = 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. + my $occur = @{[ $str =~ m/$char/g ]}; + return int( 100*($occur / length($str)) + 0.5 ); +} + +sub runTest +{ + use Test2::V0; + + is( pctOfChar("perl", "e"), 25, "Example 1 perl e"); + is( pctOfChar("java", "a"), 50, "Example 2 java a"); + is( pctOfChar("python", "m"), 0, "Example 3 python m"); + is( pctOfChar("ada", "a"), 67, "Example 4 ada a"); + is( pctOfChar("ballerina", "l"), 22, "Example 5 ballerina l"); + is( pctOfChar("analitik", "k"), 13, "Example 6 analitik k"); + + is( pctOfChar("rrrr", "r"), 100, "100%"); + + done_testing; +} 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..817a23bef8 --- /dev/null +++ b/challenge-273/bob-lied/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/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 $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +say ( bAfterA($_) ? "true" : "false" ) for @ARGV; + +sub bAfterA($str) +{ + my $w = index($str, "b"); + return $w >= 0 && index($str, "a", $w) < 0; +} + +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"); + + done_testing; +} -- cgit From 53dad097e142b9e98929709397867ab1cbbfcd08 Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Wed, 12 Jun 2024 19:20:56 -0500 Subject: Benchmark different ways to count characters Benchmark different ways to count characters --- challenge-273/bob-lied/perl/ch-1.pl | 85 +++++++++++++++++++++++++++++-------- 1 file changed, 67 insertions(+), 18 deletions(-) (limited to 'challenge-273') diff --git a/challenge-273/bob-lied/perl/ch-1.pl b/challenge-273/bob-lied/perl/ch-1.pl index 15f0409cda..1ed2a5ca47 100644 --- a/challenge-273/bob-lied/perl/ch-1.pl +++ b/challenge-273/bob-lied/perl/ch-1.pl @@ -25,22 +25,51 @@ use v5.40; use Getopt::Long; -my $Verbose = 0; -my $DoTest = 0; +my $DoTest = false; +my $Benchmark = 0; +my $Counter = 'saturn'; -GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +# 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}); -say pctOfChar(@ARGV); -sub pctOfChar($str, $char) +sub pctOfChar($str, $char, $counter) { - # Solutioh 1: delete everything that isn't char, use remaining length - # my $occur = 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. - my $occur = @{[ $str =~ m/$char/g ]}; + my $occur = $counter->($str, $char); return int( 100*($occur / length($str)) + 0.5 ); } @@ -48,14 +77,34 @@ sub runTest { use Test2::V0; - is( pctOfChar("perl", "e"), 25, "Example 1 perl e"); - is( pctOfChar("java", "a"), 50, "Example 2 java a"); - is( pctOfChar("python", "m"), 0, "Example 3 python m"); - is( pctOfChar("ada", "a"), 67, "Example 4 ada a"); - is( pctOfChar("ballerina", "l"), 22, "Example 5 ballerina l"); - is( pctOfChar("analitik", "k"), 13, "Example 6 analitik k"); + 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"), 100, "100%"); + 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}) }, + }); + +} -- cgit From e3d7d9d87f2799af27557a4f0e2aa36e84057ae1 Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Wed, 12 Jun 2024 19:38:18 -0500 Subject: Benchmark index vs RE in task 2 --- challenge-273/bob-lied/perl/ch-2.pl | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'challenge-273') diff --git a/challenge-273/bob-lied/perl/ch-2.pl b/challenge-273/bob-lied/perl/ch-2.pl index 817a23bef8..7132fa848a 100644 --- a/challenge-273/bob-lied/perl/ch-2.pl +++ b/challenge-273/bob-lied/perl/ch-2.pl @@ -17,11 +17,12 @@ use v5.40; use Getopt::Long; -my $Verbose = 0; -my $DoTest = 0; +my $DoTest = false; +my $Benchmark = 0; -GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +GetOptions("test" => \$DoTest, "benchmark:i" => \$Benchmark); exit(!runTest()) if $DoTest; +exit( runBenchmark($Benchmark) ) if $Benchmark; say ( bAfterA($_) ? "true" : "false" ) for @ARGV; @@ -31,6 +32,11 @@ sub bAfterA($str) return $w >= 0 && index($str, "a", $w) < 0; } +sub bAfterA_RE($str) +{ + $str =~ m/^[^b]*b[^a]*$/ +} + sub runTest { use Test2::V0; @@ -40,5 +46,24 @@ sub runTest 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"), + }, + }); +} -- cgit From c7797f212a2db97cae7ac808542c20df8402eea5 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Thu, 13 Jun 2024 12:18:46 +0200 Subject: Add ch-1 and ch-2 in Elixir --- challenge-273/spadacciniweb/elixir/ch-1.exs | 65 +++++++++++++++++++++++++++++ challenge-273/spadacciniweb/elixir/ch-2.exs | 60 ++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 challenge-273/spadacciniweb/elixir/ch-1.exs create mode 100644 challenge-273/spadacciniweb/elixir/ch-2.exs (limited to 'challenge-273') diff --git a/challenge-273/spadacciniweb/elixir/ch-1.exs b/challenge-273/spadacciniweb/elixir/ch-1.exs new file mode 100644 index 0000000000..3fe074e791 --- /dev/null +++ b/challenge-273/spadacciniweb/elixir/ch-1.exs @@ -0,0 +1,65 @@ +# 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 + +defmodule Percentage do + + def freq(str, char) do + String.split(str, "", trim: true) + |> Enum.frequencies + |> Map.get(char) + end + + def out(str, char) do + IO.write( str <> " " <> char <> " -> ") + case freq(str, char) do + nil -> IO.puts "0" + x -> IO.puts( x / String.length(str)*100 |> round ) + end + end + +end + +str = "perl"; char = "e"; +Percentage.out(str, char) + +str = "java"; char = "a"; +Percentage.out(str, char) + +str = "python"; char = "m"; +Percentage.out(str, char) + +str = "ada"; char = "a"; +Percentage.out(str, char) + +str = "ballerina"; char = "l"; +Percentage.out(str, char) + +str = "analitik"; char = "k"; +Percentage.out(str, char) diff --git a/challenge-273/spadacciniweb/elixir/ch-2.exs b/challenge-273/spadacciniweb/elixir/ch-2.exs new file mode 100644 index 0000000000..ca07864984 --- /dev/null +++ b/challenge-273/spadacciniweb/elixir/ch-2.exs @@ -0,0 +1,60 @@ +# Task 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 + +defmodule Position do + + def first_b(str) do + case Regex.run(~r/b/, str, return: :index) do + nil -> nil + out -> out |> Enum.fetch!(0) |> elem(0) + end + end + + def last_a(str, pos) do + case Regex.run(~r/a/, str, offset: pos, return: :index) do + nil -> IO.puts( "true" ) + _ -> IO.puts( "false" ) + end + end + + def out(str) do + IO.write( str <> " -> " ) + #IO.inspect( first_b(str) ) + case first_b(str) do + nil -> IO.puts( "false" ) + out -> last_a(str, out) + end + end + +end + +str = "aabb" +Position.out(str) + +str = "abab" +Position.out(str) + +str = "aaa" +Position.out(str) + +str = "bbb" +Position.out(str) -- cgit From 9c95a292ea1d562a61a569f752954220ba6cbbc0 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Fri, 14 Jun 2024 09:13:08 +0200 Subject: Add ch-1 and ch-2 in Python --- challenge-273/spadacciniweb/python/ch-1.py | 61 ++++++++++++++++++++++++++++++ challenge-273/spadacciniweb/python/ch-2.py | 46 ++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 challenge-273/spadacciniweb/python/ch-1.py create mode 100644 challenge-273/spadacciniweb/python/ch-2.py (limited to 'challenge-273') diff --git a/challenge-273/spadacciniweb/python/ch-1.py b/challenge-273/spadacciniweb/python/ch-1.py new file mode 100644 index 0000000000..bfbc5cbc57 --- /dev/null +++ b/challenge-273/spadacciniweb/python/ch-1.py @@ -0,0 +1,61 @@ +# 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 + +def percentage(str, char): + print("%s -> %d" % + ( str, + round( str.count(char)*100/len(str) ) + ) + ) + +if __name__ == "__main__": + str = "perl" + char = "e" + percentage(str, char) + + str = "java" + char = "a" + percentage(str, char) + + str = "python" + char = "m" + percentage(str, char) + + str = "ada" + char = "a" + percentage(str, char) + + str = "ballerina" + char = "l" + percentage(str, char) + + str = "analitik" + char = "k" + percentage(str, char) diff --git a/challenge-273/spadacciniweb/python/ch-2.py b/challenge-273/spadacciniweb/python/ch-2.py new file mode 100644 index 0000000000..dce0bca454 --- /dev/null +++ b/challenge-273/spadacciniweb/python/ch-2.py @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +# Task 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 + +def out(str): + offset = str.find('b') + if offset >= 0 and str.find('a', offset) == -1: + res = "true" + else: + res = "false" + print("%s -> %s" % + ( str, res ) + ) + +if __name__ == "__main__": + str = "aabb" + out(str) + + str = "abab" + out(str) + + str = "aaa" + out(str) + + str = "bbb" + out(str) -- cgit From 63876f502b91d23fc368baf387b52d14c2f781ca Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Fri, 14 Jun 2024 09:14:27 +0200 Subject: Remake ch-2 in Perl --- challenge-273/spadacciniweb/perl/ch-2.pl | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'challenge-273') diff --git a/challenge-273/spadacciniweb/perl/ch-2.pl b/challenge-273/spadacciniweb/perl/ch-2.pl index a7c968b0fd..9f7cc330ca 100644 --- a/challenge-273/spadacciniweb/perl/ch-2.pl +++ b/challenge-273/spadacciniweb/perl/ch-2.pl @@ -42,14 +42,10 @@ exit 0; sub position { my $str = shift; - my $first_b = index($str, 'b'); - my $last_a = index((scalar reverse $str), 'a'); - $last_a = length($str) - $last_a - 1 - if $last_a >= 0; - + my $offset = index($str, 'b'); printf "%s -> %s\n", $str, - ($last_a > $first_b) - ? 'false' - : 'true'; + ($offset >= 0 and index($str, 'a', $offset) == -1) + ? 'true' + : 'false'; } -- cgit From 6ccb7df2da52549ac03b8d1c079d0b754cf73081 Mon Sep 17 00:00:00 2001 From: deadmarshal Date: Fri, 14 Jun 2024 03:37:26 -0400 Subject: TWC273 --- challenge-273/deadmarshal/go/ch1.go | 24 +++++++++++++++ challenge-273/deadmarshal/go/ch2.go | 18 +++++++++++ challenge-273/deadmarshal/java/Ch1.java | 17 +++++++++++ challenge-273/deadmarshal/java/Ch2.java | 14 +++++++++ challenge-273/deadmarshal/modula-3/ch1/src/Ch1.m3 | 23 ++++++++++++++ .../deadmarshal/modula-3/ch1/src/m3makefile | 5 ++++ challenge-273/deadmarshal/modula-3/ch2/src/Ch2.m3 | 35 ++++++++++++++++++++++ .../deadmarshal/modula-3/ch2/src/m3makefile | 5 ++++ challenge-273/deadmarshal/perl/ch-1.pl | 18 +++++++++++ challenge-273/deadmarshal/perl/ch-2.pl | 13 ++++++++ challenge-273/deadmarshal/raku/ch-1.raku | 15 ++++++++++ challenge-273/deadmarshal/raku/ch-2.raku | 12 ++++++++ 12 files changed, 199 insertions(+) create mode 100644 challenge-273/deadmarshal/go/ch1.go create mode 100644 challenge-273/deadmarshal/go/ch2.go create mode 100644 challenge-273/deadmarshal/java/Ch1.java create mode 100644 challenge-273/deadmarshal/java/Ch2.java create mode 100644 challenge-273/deadmarshal/modula-3/ch1/src/Ch1.m3 create mode 100644 challenge-273/deadmarshal/modula-3/ch1/src/m3makefile create mode 100644 challenge-273/deadmarshal/modula-3/ch2/src/Ch2.m3 create mode 100644 challenge-273/deadmarshal/modula-3/ch2/src/m3makefile create mode 100644 challenge-273/deadmarshal/perl/ch-1.pl create mode 100644 challenge-273/deadmarshal/perl/ch-2.pl create mode 100644 challenge-273/deadmarshal/raku/ch-1.raku create mode 100644 challenge-273/deadmarshal/raku/ch-2.raku (limited to 'challenge-273') 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) +} + +say percentage-of-character('perl','e'); +say percentage-of-character('java','a'); +say percentage-of-character('python','m'); +say percentage-of-character('ada','a'); +say percentage-of-character('ballerina','l'); +say percentage-of-character('analitik','k'); + diff --git a/challenge-273/deadmarshal/raku/ch-2.raku b/challenge-273/deadmarshal/raku/ch-2.raku new file mode 100644 index 0000000000..bd2bb29664 --- /dev/null +++ b/challenge-273/deadmarshal/raku/ch-2.raku @@ -0,0 +1,12 @@ +#!/usr/bin/env raku + +sub b-after-a($str) +{ + ?($str ~~ /bb/) +} + +say b-after-a('aabb'); +say b-after-a('abab'); +say b-after-a('aaa'); +say b-after-a('bbb'); + -- cgit From f38f02987804feb1a1803099f8c832ec75dfd79a Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Fri, 14 Jun 2024 09:24:11 +0100 Subject: - Added solutions by Mariano Spadaccini. - Added solutions by Reinier Maliepaard. - Added solutions by Ali Moradi. --- challenge-273/reinier-maliepaard/blog.txt | 1 + challenge-273/reinier-maliepaard/perl/ch-1.pl | 47 ++++++++++++++ challenge-273/reinier-maliepaard/perl/ch-2.pl | 88 +++++++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 challenge-273/reinier-maliepaard/blog.txt create mode 100644 challenge-273/reinier-maliepaard/perl/ch-1.pl create mode 100644 challenge-273/reinier-maliepaard/perl/ch-2.pl (limited to 'challenge-273') diff --git a/challenge-273/reinier-maliepaard/blog.txt b/challenge-273/reinier-maliepaard/blog.txt new file mode 100644 index 0000000000..7b99b8f2e0 --- /dev/null +++ b/challenge-273/reinier-maliepaard/blog.txt @@ -0,0 +1 @@ +https://reiniermaliepaard.nl/perl/pwc/index.php?id=pwc273 diff --git a/challenge-273/reinier-maliepaard/perl/ch-1.pl b/challenge-273/reinier-maliepaard/perl/ch-1.pl new file mode 100644 index 0000000000..31ce4bff5b --- /dev/null +++ b/challenge-273/reinier-maliepaard/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub percentage_of_character { + + my ($str, $char) = (@_); + + # create a new Statistics::Frequency object. + my $f = Statistics::Frequency->new( split(//, $str) ); + return( int( ($f->proportional_frequency($char) * 100) + 0.5) ); + +} + +#TESTS + +my ($str, $char); + +# Example 1 +$str = "perl"; +$char = "e"; +print(percentage_of_character($str, $char), "\n"); # Output: 25 + +# Example 2 +$str = "java"; +$char = "a"; +print(percentage_of_character($str, $char), "\n"); # Output: 50 + +# Example 3 +$str = "python"; +$char = "m"; +print(percentage_of_character($str, $char), "\n"); # Output: 0 + +# Example 4 +$str = "ada"; +$char = "a"; +print(percentage_of_character($str, $char), "\n"); # Output: 67 + +# Example 5 +$str = "ballerina"; +$char = "l"; +print(percentage_of_character($str, $char), "\n"); # Output: 22 + +# Example 6 +$str = "analitik"; +$char = "k"; +print(percentage_of_character($str, $char), "\n"); # Output: 13 diff --git a/challenge-273/reinier-maliepaard/perl/ch-2.pl b/challenge-273/reinier-maliepaard/perl/ch-2.pl new file mode 100644 index 0000000000..d8c125399c --- /dev/null +++ b/challenge-273/reinier-maliepaard/perl/ch-2.pl @@ -0,0 +1,88 @@ +#!/usr/bin/perl +use strict; +use warnings; + +=begin + I prefer not to replicate the interesting regex solutions of others. + Therefore, I chose a different approach using the 'index' method. + + Postulate 1: The string must contain at least one 'b'. Strings like + 'b', 'bb', 'bbb', etc., are also valid. + Postulate 2: If a string contains the characters 'a' and 'b', then + 'a' is not allowed to follow the first 'b'. There are + two cases to consider: + 1. If 'ba' appears at the beginning of a string, it + should be evaluated as 'false'. + 2. If 'ba' appears elsewhere in the string, it must be + preceded by another 'b' to be evaluated as 'true'. + For instance, 'aaba' is 'false' but 'aabba' is 'true'. + + Performance: benchmarking my solution against several regex solutions I + found on GitHub showed that it performs adequately. It is neither the + fastest nor the slowest. +=cut + +sub BafterA { + + # 'b' in $_[0]? + return(0) if(index($_[0], "b") == -1); + # 'ba' at the beginning of $_[0] + return(0) if(index($_[0], "ba") == 0); + # 'bba'? 'aba' not allowed + return(0) if ((index($_[0], "ba") > 0) && (substr($_[0], (index($_[0], "ba")-1), 1) ne "b")); + 1; +} + +# TESTS + +my $str; + +# Example 1 +$str = "aabb"; +print(BafterA($str), "\n"); # Output: 1 + +# Example 2 +$str = "abab"; +print(BafterA($str), "\n"); # Output: 0 + +# Example 3 +$str = "aaa"; +print(BafterA($str), "\n"); # Output: 0 + +# Example 4 +$str = "bbb"; +print(BafterA($str), "\n"); # Output: 1 + +# Own tests + +# Example 5 +$str = "b"; +print(BafterA($str), "\n"); # Output: 1 + +# Example 6 +$str = "a"; +print(BafterA($str), "\n"); # Output: 0 + +# Example 7 +$str = "ba"; +print(BafterA($str), "\n"); # Output: 0 + +# Example 8 +$str = "ab"; +print(BafterA($str), "\n"); # Output: 1 + +# Example 9 +$str = "bba"; +print(BafterA($str), "\n"); # Output: 1 + +# Example 10 +$str = "aba"; +print(BafterA($str), "\n"); # Output: 0 + +# Example 11 +$str = "abba"; +print(BafterA($str), "\n"); # Output: 1 + +# Example 12 +$str = "babba"; +print(BafterA($str), "\n"); # Output: 0 -- cgit From de6b31ef501a17a31aa33060d6f1641c20ae1538 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Fri, 14 Jun 2024 11:17:02 +0200 Subject: Add ch-1 and ch-2 in Go --- challenge-273/spadacciniweb/go/ch-1.go | 74 ++++++++++++++++++++++++++++++++++ challenge-273/spadacciniweb/go/ch-2.go | 60 +++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 challenge-273/spadacciniweb/go/ch-1.go create mode 100644 challenge-273/spadacciniweb/go/ch-2.go (limited to 'challenge-273') diff --git a/challenge-273/spadacciniweb/go/ch-1.go b/challenge-273/spadacciniweb/go/ch-1.go new file mode 100644 index 0000000000..c98cffe354 --- /dev/null +++ b/challenge-273/spadacciniweb/go/ch-1.go @@ -0,0 +1,74 @@ +/* +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 +*/ + +package main + +import ( + "fmt" +) + +func percentage(str string, char string) { + frequency := make(map[rune]int) + + for _, val := range str { + frequency[val]++ //assign frequencies + } + + fmt.Printf("%s %s -> %.0f\n", str, char, float64(frequency[ rune(char[0]) ]*100)/float64( len(str) ) ) +} + +func main() { + str := "perl" + char := "e" + percentage(str, char) + + str = "java" + char = "a" + percentage(str, char) + + str = "python" + char = "m" + percentage(str, char) + + str = "ada" + char = "a" + percentage(str, char); + + str = "ballerina" + char = "l" + percentage(str, char); + + str = "analitik" + char = "k"; + percentage(str, char); +} + diff --git a/challenge-273/spadacciniweb/go/ch-2.go b/challenge-273/spadacciniweb/go/ch-2.go new file mode 100644 index 0000000000..509565840e --- /dev/null +++ b/challenge-273/spadacciniweb/go/ch-2.go @@ -0,0 +1,60 @@ +/* +Task 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 +*/ + +package main + +import ( + "fmt" + s "strings" +) + +func position (str string) { + offset := s.Index(str, "b") + res := 0 + + if (offset >=0) { + res = s.Index( str[offset+1:], "a"); + } + + if (res == -1) { + fmt.Printf("%s -> %s\n", str, "true") + } else { + fmt.Printf("%s -> %s\n", str, "false") + } +} + +func main() { + str := "aabb" + position(str) + + str = "abab"; + position(str) + + str = "aaa" + position(str) + + str = "bbb" + position(str) +} + -- cgit From 9fd3080dfcd5d671ba6b068c7ed09e854745243d Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Thu, 13 Jun 2024 21:20:29 +0200 Subject: Solution to task 1 --- challenge-273/jo-37/perl/ch-1.pl | 68 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100755 challenge-273/jo-37/perl/ch-1.pl (limited to 'challenge-273') diff --git a/challenge-273/jo-37/perl/ch-1.pl b/challenge-273/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..b832bb2989 --- /dev/null +++ b/challenge-273/jo-37/perl/ch-1.pl @@ -0,0 +1,68 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use experimental 'signatures'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die < Date: Thu, 13 Jun 2024 21:20:43 +0200 Subject: Solution to task 2 --- challenge-273/jo-37/perl/ch-2.pl | 62 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 challenge-273/jo-37/perl/ch-2.pl (limited to 'challenge-273') diff --git a/challenge-273/jo-37/perl/ch-2.pl b/challenge-273/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..0079a2fa55 --- /dev/null +++ b/challenge-273/jo-37/perl/ch-2.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die < Date: Thu, 13 Jun 2024 21:20:59 +0200 Subject: Blog for challenge 273 --- challenge-273/jo-37/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-273/jo-37/blog.txt (limited to 'challenge-273') diff --git a/challenge-273/jo-37/blog.txt b/challenge-273/jo-37/blog.txt new file mode 100644 index 0000000000..a09857df8d --- /dev/null +++ b/challenge-273/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2024/06/14/ch-273.html -- cgit From 1c938200c9e497031bfc4dd3dcad0775412638c1 Mon Sep 17 00:00:00 2001 From: rir Date: Fri, 14 Jun 2024 23:29:42 -0400 Subject: 273 --- challenge-273/0rir/raku/ch-1.raku | 55 +++++++++++++++++++++++++++++++++++++++ challenge-273/0rir/raku/ch-2.raku | 53 +++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 challenge-273/0rir/raku/ch-1.raku create mode 100644 challenge-273/0rir/raku/ch-2.raku (limited to 'challenge-273') diff --git a/challenge-273/0rir/raku/ch-1.raku b/challenge-273/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..ae6e95a181 --- /dev/null +++ b/challenge-273/0rir/raku/ch-1.raku @@ -0,0 +1,55 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴ +use v6.d; +INIT $*RAT-OVERFLOW = FatRat; +use lib $?FILE.IO.cleanup.parent(2).add("lib"); +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, +; +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; + + -- cgit From b2da84baa5ded4451f460c4e28fd81d4105389c5 Mon Sep 17 00:00:00 2001 From: rir Date: Fri, 14 Jun 2024 23:45:55 -0400 Subject: de-cruft --- challenge-273/0rir/raku/ch-1.raku | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'challenge-273') diff --git a/challenge-273/0rir/raku/ch-1.raku b/challenge-273/0rir/raku/ch-1.raku index ae6e95a181..0465e7f56c 100644 --- a/challenge-273/0rir/raku/ch-1.raku +++ b/challenge-273/0rir/raku/ch-1.raku @@ -1,8 +1,6 @@ #!/usr/bin/env raku # :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴ use v6.d; -INIT $*RAT-OVERFLOW = FatRat; -use lib $?FILE.IO.cleanup.parent(2).add("lib"); use Test; =begin comment @@ -33,13 +31,19 @@ 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, + "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; -- cgit From 3529b3dea653fe9a88affbbcb7ba423992535dea Mon Sep 17 00:00:00 2001 From: arnesom Date: Sat, 15 Jun 2024 20:02:35 +0200 Subject: Arne Sommer --- challenge-273/arne-sommer/blog.txt | 1 + challenge-273/arne-sommer/raku/b-after-a | 16 ++++++++++++++++ challenge-273/arne-sommer/raku/ch-1.raku | 5 +++++ challenge-273/arne-sommer/raku/ch-2.raku | 16 ++++++++++++++++ challenge-273/arne-sommer/raku/percentage-of-character | 5 +++++ 5 files changed, 43 insertions(+) create mode 100644 challenge-273/arne-sommer/blog.txt create mode 100755 challenge-273/arne-sommer/raku/b-after-a create mode 100755 challenge-273/arne-sommer/raku/ch-1.raku create mode 100755 challenge-273/arne-sommer/raku/ch-2.raku create mode 100755 challenge-273/arne-sommer/raku/percentage-of-character (limited to 'challenge-273') 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; -- cgit From 9377c989402c0a0184e67adc359b89eb86bf33ff Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sat, 15 Jun 2024 13:29:02 -0600 Subject: Joelle Maslak's Week 273 Submissions --- challenge-273/joelle-maslak/perl5/ch-1.pl | 31 ++++++++++++++++++++++++++++++ challenge-273/joelle-maslak/perl5/ch-2.pl | 29 ++++++++++++++++++++++++++++ challenge-273/joelle-maslak/raku/ch-1.raku | 13 +++++++++++++ challenge-273/joelle-maslak/raku/ch-2.raku | 11 +++++++++++ 4 files changed, 84 insertions(+) create mode 100644 challenge-273/joelle-maslak/perl5/ch-1.pl create mode 100644 challenge-273/joelle-maslak/perl5/ch-2.pl create mode 100644 challenge-273/joelle-maslak/raku/ch-1.raku create mode 100644 challenge-273/joelle-maslak/raku/ch-2.raku (limited to 'challenge-273') diff --git a/challenge-273/joelle-maslak/perl5/ch-1.pl b/challenge-273/joelle-maslak/perl5/ch-1.pl new file mode 100644 index 0000000000..1d26f44a42 --- /dev/null +++ b/challenge-273/joelle-maslak/perl5/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl + +# +# Copyright (C) 2024 Joelle Maslak +# + +use JTM::Boilerplate 'script'; + +MAIN: { + if ((scalar(@ARGV) != 2) or (length($ARGV[1]) != 1) or (length($ARGV[0]) < 1)) { + help(); + exit(1); + } + + calculate($ARGV[0], $ARGV[1]); +} + +sub calculate($str, $char) { + # Calculate the percentage that a character appears + my $strlen = length($str); + my $count = scalar(grep { $_ eq $char } split(//, $str)); + + printf("Percentage of times character appears in string: %.0f%%\n", 100.0*$count/$strlen); +} + +sub help() { + print STDERR "Calculate the percent of a string that contains a character\n"; + print STDERR "You must provide two parameters:\n"; + print STDERR "\n"; + print STDERR " ch-1.pl \n"; +} \ No newline at end of file diff --git a/challenge-273/joelle-maslak/perl5/ch-2.pl b/challenge-273/joelle-maslak/perl5/ch-2.pl new file mode 100644 index 0000000000..003ebb6372 --- /dev/null +++ b/challenge-273/joelle-maslak/perl5/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +# +# Copyright (C) 2024 Joelle Maslak +# + +use JTM::Boilerplate; + +MAIN: { + if ((scalar(@ARGV) != 1)) { + help(); + exit(1); + } + + calculate($ARGV[0]); +} + +sub calculate($str) { + # Calculate the percentage that a character appears + my $check = $str =~ m/^[^b]*b(?!a)/; + say("Output: " . ($check ? "true" : "false")); +} + +sub help() { + print STDERR "Calculate whether there is a 'b' in a string where the first 'b' isn't followed by an 'a'\n"; + print STDERR "You must provide one parameter:\n"; + print STDERR "\n"; + print STDERR " ch-2.pl \n"; +} diff --git a/challenge-273/joelle-maslak/raku/ch-1.raku b/challenge-273/joelle-maslak/raku/ch-1.raku new file mode 100644 index 0000000000..10a8eb6926 --- /dev/null +++ b/challenge-273/joelle-maslak/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku +use v6.d; + +# +# Copyright © 2024 Joelle Maslak +# + +sub MAIN(Str:D $str where $str.chars > 0, Str:D $char where $char.chars == 1) { + my $charcount = $str.comb.grep($char); + printf("Percentage of times character apears in string %.0f%%\n", 100.0*$charcount/$str.chars); +} + + diff --git a/challenge-273/joelle-maslak/raku/ch-2.raku b/challenge-273/joelle-maslak/raku/ch-2.raku new file mode 100644 index 0000000000..9e61fcf34b --- /dev/null +++ b/challenge-273/joelle-maslak/raku/ch-2.raku @@ -0,0 +1,11 @@ +#!/usr/bin/env raku +use v6.d; + +# +# Copyright © 2024 Joelle Maslak +# + +sub MAIN(Str:D $str) { + my $match = $str ~~ m/^ <-[ b ]>* 'b' /; + say "Output: " ~ ($match ?? "true" !! " false"); +} \ No newline at end of file -- cgit From e7c6e4694cbc20e39268624c74ec9d5134bd66b7 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sat, 15 Jun 2024 21:15:59 +0100 Subject: - Added solutions by Arne Sommer. - Added solutions by Joelle Maslak. --- challenge-273/joelle-maslak/perl/ch-1.pl | 31 +++++++++++++++++++++++++++++++ challenge-273/joelle-maslak/perl/ch-2.pl | 29 +++++++++++++++++++++++++++++ challenge-273/joelle-maslak/perl5/ch-1.pl | 31 ------------------------------- challenge-273/joelle-maslak/perl5/ch-2.pl | 29 ----------------------------- 4 files changed, 60 insertions(+), 60 deletions(-) create mode 100644 challenge-273/joelle-maslak/perl/ch-1.pl create mode 100644 challenge-273/joelle-maslak/perl/ch-2.pl delete mode 100644 challenge-273/joelle-maslak/perl5/ch-1.pl delete mode 100644 challenge-273/joelle-maslak/perl5/ch-2.pl (limited to 'challenge-273') diff --git a/challenge-273/joelle-maslak/perl/ch-1.pl b/challenge-273/joelle-maslak/perl/ch-1.pl new file mode 100644 index 0000000000..1d26f44a42 --- /dev/null +++ b/challenge-273/joelle-maslak/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl + +# +# Copyright (C) 2024 Joelle Maslak +# + +use JTM::Boilerplate 'script'; + +MAIN: { + if ((scalar(@ARGV) != 2) or (length($ARGV[1]) != 1) or (length($ARGV[0]) < 1)) { + help(); + exit(1); + } + + calculate($ARGV[0], $ARGV[1]); +} + +sub calculate($str, $char) { + # Calculate the percentage that a character appears + my $strlen = length($str); + my $count = scalar(grep { $_ eq $char } split(//, $str)); + + printf("Percentage of times character appears in string: %.0f%%\n", 100.0*$count/$strlen); +} + +sub help() { + print STDERR "Calculate the percent of a string that contains a character\n"; + print STDERR "You must provide two parameters:\n"; + print STDERR "\n"; + print STDERR " ch-1.pl \n"; +} \ No newline at end of file diff --git a/challenge-273/joelle-maslak/perl/ch-2.pl b/challenge-273/joelle-maslak/perl/ch-2.pl new file mode 100644 index 0000000000..003ebb6372 --- /dev/null +++ b/challenge-273/joelle-maslak/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +# +# Copyright (C) 2024 Joelle Maslak +# + +use JTM::Boilerplate; + +MAIN: { + if ((scalar(@ARGV) != 1)) { + help(); + exit(1); + } + + calculate($ARGV[0]); +} + +sub calculate($str) { + # Calculate the percentage that a character appears + my $check = $str =~ m/^[^b]*b(?!a)/; + say("Output: " . ($check ? "true" : "false")); +} + +sub help() { + print STDERR "Calculate whether there is a 'b' in a string where the first 'b' isn't followed by an 'a'\n"; + print STDERR "You must provide one parameter:\n"; + print STDERR "\n"; + print STDERR " ch-2.pl \n"; +} diff --git a/challenge-273/joelle-maslak/perl5/ch-1.pl b/challenge-273/joelle-maslak/perl5/ch-1.pl deleted file mode 100644 index 1d26f44a42..0000000000 --- a/challenge-273/joelle-maslak/perl5/ch-1.pl +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/perl - -# -# Copyright (C) 2024 Joelle Maslak -# - -use JTM::Boilerplate 'script'; - -MAIN: { - if ((scalar(@ARGV) != 2) or (length($ARGV[1]) != 1) or (length($ARGV[0]) < 1)) { - help(); - exit(1); - } - - calculate($ARGV[0], $ARGV[1]); -} - -sub calculate($str, $char) { - # Calculate the percentage that a character appears - my $strlen = length($str); - my $count = scalar(grep { $_ eq $char } split(//, $str)); - - printf("Percentage of times character appears in string: %.0f%%\n", 100.0*$count/$strlen); -} - -sub help() { - print STDERR "Calculate the percent of a string that contains a character\n"; - print STDERR "You must provide two parameters:\n"; - print STDERR "\n"; - print STDERR " ch-1.pl \n"; -} \ No newline at end of file diff --git a/challenge-273/joelle-maslak/perl5/ch-2.pl b/challenge-273/joelle-maslak/perl5/ch-2.pl deleted file mode 100644 index 003ebb6372..0000000000 --- a/challenge-273/joelle-maslak/perl5/ch-2.pl +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/perl - -# -# Copyright (C) 2024 Joelle Maslak -# - -use JTM::Boilerplate; - -MAIN: { - if ((scalar(@ARGV) != 1)) { - help(); - exit(1); - } - - calculate($ARGV[0]); -} - -sub calculate($str) { - # Calculate the percentage that a character appears - my $check = $str =~ m/^[^b]*b(?!a)/; - say("Output: " . ($check ? "true" : "false")); -} - -sub help() { - print STDERR "Calculate whether there is a 'b' in a string where the first 'b' isn't followed by an 'a'\n"; - print STDERR "You must provide one parameter:\n"; - print STDERR "\n"; - print STDERR " ch-2.pl \n"; -} -- cgit