From df7b8cd1780e7e184f0196f5de80fae5f4438f3d Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Tue, 27 Jun 2023 08:01:03 -0500 Subject: Count Primes with sieve --- challenge-223/bob-lied/perl/ch-1.pl | 65 +++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 challenge-223/bob-lied/perl/ch-1.pl diff --git a/challenge-223/bob-lied/perl/ch-1.pl b/challenge-223/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..de9a6d4625 --- /dev/null +++ b/challenge-223/bob-lied/perl/ch-1.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl Perl Weekly Challenge 223 Task1 Count Primes +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given a positive integer, $n. +# Write a script to find the total count of primes less than or equal to the +# given integer. +# Example 1 Input: $n = 10 Output: 4 +# Since there are 4 primes (2,3,5,7) less than or equal to 10. +# Example 2 Input: $n = 1 Output: 0 +# Example 3 Input: $n = 20 Output: 8 +# Since there are 4 primes (2,3,5,7,11,13,17,19) less than or equal to 20. +#============================================================================= + +use v5.36; + +use builtin qw(true false); no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub sieve($n) +{ + my @prime = (false, false, true, (true,false) x (($n-1)/2) ); + + for (my $p = 3; $p*$p <= $n; $p++ ) + { + if ( $prime[$p] ) + { + for (my $i = $p*$p ; $i <= $n ; $i += $p ) + { + $prime[$i] = false; + } + } + } + my $count = grep { $prime[$_] } 2 .. $n+1; +} + +sub countPrimes($n) +{ + return ( $n < 2 ? 0 : sieve($n) ); +} + +sub runTest +{ + use Test2::V0; + + is( countPrimes( 10), 4, "Example 1"); + is( countPrimes( 1), 0, "Example 2"); + is( countPrimes( 20), 8, "Example 3"); + is( countPrimes( 2), 1, "p 2"); + is( countPrimes( 3), 2, "p 3"); + is( countPrimes( 11), 5, "p 11"); + is( countPrimes( 100), 25, "p 100"); + is( countPrimes(1000), 168, "p 1000"); + + done_testing; +} -- cgit From 28f16fa30c7b628bab77e1da7cac90dea5e23c9e Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Mon, 3 Jul 2023 11:03:50 +0200 Subject: Add solution 224 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-224/jeanluc2020/blog-1.txt | 1 + challenge-224/jeanluc2020/blog-2.txt | 1 + challenge-224/jeanluc2020/perl/ch-1.pl | 75 ++++++++++++++++++++++++++ challenge-224/jeanluc2020/perl/ch-2.pl | 99 ++++++++++++++++++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 challenge-224/jeanluc2020/blog-1.txt create mode 100644 challenge-224/jeanluc2020/blog-2.txt create mode 100755 challenge-224/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-224/jeanluc2020/perl/ch-2.pl diff --git a/challenge-224/jeanluc2020/blog-1.txt b/challenge-224/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..72c2ea34a9 --- /dev/null +++ b/challenge-224/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-224-1.html diff --git a/challenge-224/jeanluc2020/blog-2.txt b/challenge-224/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..88840c69d3 --- /dev/null +++ b/challenge-224/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-224-2.html diff --git a/challenge-224/jeanluc2020/perl/ch-1.pl b/challenge-224/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..fd3d82624b --- /dev/null +++ b/challenge-224/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,75 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-224/#TASK1 +# +# Task 1: Special Notes +# ===================== +# +# You are given two strings, $source and $target. +# +# Write a script to find out if using the characters (only once) from source, a target string can be created. +# +## Example 1 +## +## Input: $source = "abc" +## $target = "xyz" +## Output: false +# +## Example 2 +## +## Input: $source = "scriptinglanguage" +## $target = "perl" +## Output: true +# +## Example 3 +## +## Input: $source = "aabbcc" +## $target = "abc" +## Output: true +# +############################################################ +## +## discussion +## +############################################################ +# +# We re-use the solution from the weekly challenge 221 task 1: +# https://theweeklychallenge.org/blog/perl-weekly-challenge-221/#TASK1 +# Solution from +# http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-221-1.html +# This has a function that returns the length of a good string in words - in +# our case, "words" is our first input, and the potential good string consists +# of our second input string. +# The actual solution is therefore trivial. + +use strict; +use warnings; +use Data::Dumper; + +special_notes("abc", "xyz"); +special_notes("scriptinglanguage", "perl"); +special_notes("aabbcc", "abc"); + +sub special_notes { + my ($source, $target) = @_; + print "Input: source: $source, target: $target\n"; + if(good_string_result($target, $source)) { + print "Output: true\n"; + return "true"; + } + print "Output: false\n"; + return "false"; +} + +sub good_string_result { + my ($word, $chars) = @_; + my ($wordmap, $charmap); + my $result = 0; + map { $wordmap->{$_}++ } split //, $word; + map { $charmap->{$_}++ } split //, $chars; + foreach my $c (keys %$wordmap) { + return 0 unless $charmap->{$c}; + return 0 unless $charmap->{$c} >= $wordmap->{$c}; + $result += $wordmap->{$c}; + } + return $result; +} diff --git a/challenge-224/jeanluc2020/perl/ch-2.pl b/challenge-224/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..bf03889361 --- /dev/null +++ b/challenge-224/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,99 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-224/#TASK2 +# +# Task 2: Additive Number +# ======================= +# +# You are given a string containing digits 0-9 only. +# +# Write a script to find out if the given string is additive number. An +# additive number is a string whose digits can form an additive sequence. +# +### A valid additive sequence should contain at least 3 numbers. Except the +### first 2 numbers, each subsequent number in the sequence must be the sum of +### the preceding two. +# +## Input: $string = "112358" +## Output: true +## +## The additive sequence can be created using the given string digits: 1,1,2,3,5,8 +## 1 + 1 => 2 +## 1 + 2 => 3 +## 2 + 3 => 5 +## 3 + 5 => 8 +# +## Example 2: +## +## Input: $string = "12345" +## Output: false +## +## No additive sequence can be created using the given string digits. +# +## Example 3: +## +## Input: $string = "199100199" +## Output: true +## +## The additive sequence can be created using the given string digits: 1,99,100,199 +## 1 + 99 => 100 +## 99 + 100 => 199 +# +############################################################ +## +## discussion +## +############################################################ +# +# We start by selecting a substring of length l as the first number, with +# 1 <= l <= 1/3*length(string). Then we select another substring starting after +# the first one with the same length restriction. Then, the rest remains as our +# reminder of the string. We now add up the first two strings and compare the +# result to the beginning of the rest with the same length. If that matches, +# our current second string becomes our new first one, the subtring we just cut +# out of the rest is our new second string, and the remainder of the rest after +# cutting out this new second string will be our new rest. If it has zero +# length, we have found an additive number. We recurse down with this function +# until the first two numbers don't match the beginning of the rest or the +# length of the rest is no longer long enough to match the sum, in which cases +# we return 0 and therfore short-curcuit the execution, or we have reached the +# end of the rest with all sums in between matching, so we can return 1. + +use strict; +use warnings; + +additive_number("112358"); +additive_number("12345"); +additive_number("199100199"); +additive_number("991100101"); + +sub additive_number { + my $string = shift; + my $max_len = length($string) / 3; + print "Input: $string\n"; + my ($first, $second, $rest); + foreach my $len1 (1..$max_len) { + $first = substr($string,0,$len1); + foreach my $len2 (1..$max_len) { + $second = substr($string,$len1,$len2); + $rest = substr($string,$len1+$len2); + if(is_additive($first, $second, $rest)) { + print "Output: true\n"; + return; + } + } + } + print "Output: false\n"; +} + +sub is_additive { + my ($first, $second, $rest) = @_; + my $sum = $first+$second; + return 0 unless length($rest) >= length($sum); + my $tmp = substr($rest,0,length($sum)); + my $tmp_rest = substr($rest,length($sum)); + if($tmp != $sum) { + return 0; + } + return 1 if length($tmp_rest) == 0; + return is_additive($second, $tmp, $tmp_rest); +} -- cgit From 8bacf11057d3ae65d0a6d248c1e507d4ecddb1ca Mon Sep 17 00:00:00 2001 From: Scimon Date: Mon, 3 Jul 2023 10:09:21 +0100 Subject: Challenge 1 --- challenge-224/simon-proctor/raku/ch-1.raku | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 challenge-224/simon-proctor/raku/ch-1.raku diff --git a/challenge-224/simon-proctor/raku/ch-1.raku b/challenge-224/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..ff036f5e4f --- /dev/null +++ b/challenge-224/simon-proctor/raku/ch-1.raku @@ -0,0 +1,6 @@ +#!/usr/bin/env raku + +#| Print whether the target string can be made from the characters in the source string +sub MAIN( Str $source, Str $target ) { + ($target.comb.Bag (<=) $source.comb.Bag).lc.say; +} -- cgit From 2a916df1caaf4c35eb6939a9977b6506302cfee2 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 3 Jul 2023 15:47:11 +0000 Subject: Initial 224 (Raku) --- challenge-224/mark-anderson/raku/ch-1.raku | 10 ++++ challenge-224/mark-anderson/raku/ch-2.raku | 75 ++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 challenge-224/mark-anderson/raku/ch-1.raku create mode 100644 challenge-224/mark-anderson/raku/ch-2.raku diff --git a/challenge-224/mark-anderson/raku/ch-1.raku b/challenge-224/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..3d94f6d2c1 --- /dev/null +++ b/challenge-224/mark-anderson/raku/ch-1.raku @@ -0,0 +1,10 @@ +#!/usr/bin/env raku + +say special-notes('abc', 'xyz'); +say special-notes('scriptinglanguage', 'perl'); +say special-notes('aabbcc', 'abc'); + +sub special-notes($s, $t) +{ + $t.comb.Bag (<=) $s.comb.Bag +} diff --git a/challenge-224/mark-anderson/raku/ch-2.raku b/challenge-224/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..3abf9fdf0d --- /dev/null +++ b/challenge-224/mark-anderson/raku/ch-2.raku @@ -0,0 +1,75 @@ +#!/usr/bin/raku +use Test; + +ok additive-number("112358"); +nok additive-number("12345"); +ok additive-number("199100199"); +ok additive-number("1458832198458832199"); +ok additive-number("1321517324981130211"); + +# A few notes: + +# I wish I knew how to write grammars. + +# For some reason I can't get interpolation to work in my regexes +# so I'm stuck with this hardcoded mess. + +# This only works for up to 10 numbers. + +sub additive-number($n) +{ + return True if $n ~~ + / ^ (\d+)(\d+)(\d+) $ /; + + return True if $n ~~ + / ^ (\d+)(\d+)(\d+)(\d+) $ /; + + return True if $n ~~ + / ^ (\d+)(\d+)(\d+)(\d+)(\d+) $ /; + + return True if $n ~~ + / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /; + + return True if $n ~~ + / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /; + + return True if $n ~~ + / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /; + + return True if $n ~~ + / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /; + + return True if $n ~~ + / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ + /; + + return False +} -- cgit From d8eb4300c7f91c86ed3c4db9b5926b3514548cbf Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 3 Jul 2023 17:05:44 +0000 Subject: Initial 224 (Raku) --- challenge-224/mark-anderson/raku/ch-1.raku | 7 ++- challenge-224/mark-anderson/raku/ch-2.raku | 92 ++++++++++++++---------------- 2 files changed, 46 insertions(+), 53 deletions(-) diff --git a/challenge-224/mark-anderson/raku/ch-1.raku b/challenge-224/mark-anderson/raku/ch-1.raku index 3d94f6d2c1..9f1754fc29 100644 --- a/challenge-224/mark-anderson/raku/ch-1.raku +++ b/challenge-224/mark-anderson/raku/ch-1.raku @@ -1,8 +1,9 @@ #!/usr/bin/env raku +use Test; -say special-notes('abc', 'xyz'); -say special-notes('scriptinglanguage', 'perl'); -say special-notes('aabbcc', 'abc'); +nok special-notes('abc', 'xyz'); +ok special-notes('scriptinglanguage', 'perl'); +ok special-notes('aabbcc', 'abc'); sub special-notes($s, $t) { diff --git a/challenge-224/mark-anderson/raku/ch-2.raku b/challenge-224/mark-anderson/raku/ch-2.raku index 3abf9fdf0d..3bb9453580 100644 --- a/challenge-224/mark-anderson/raku/ch-2.raku +++ b/challenge-224/mark-anderson/raku/ch-2.raku @@ -1,11 +1,11 @@ #!/usr/bin/raku use Test; -ok additive-number("112358"); -nok additive-number("12345"); -ok additive-number("199100199"); -ok additive-number("1458832198458832199"); -ok additive-number("1321517324981130211"); +ok additive-number("112358"), "1,2,2,3,5,8"; +nok additive-number("12345"), "No Solution"; +ok additive-number("199100199"), "1,99,100,199"; +ok additive-number("4588321981458832199"), "458832198,1,458832199"; +ok additive-number("1321517324981130211"), "13,2,15,17,32,49,81,130,211"; # A few notes: @@ -18,58 +18,50 @@ ok additive-number("1321517324981130211"); sub additive-number($n) { - return True if $n ~~ - / ^ (\d+)(\d+)(\d+) $ /; + return True if $n ~~ any + / ^ (\d+)(\d+)(\d+) $ /, - return True if $n ~~ - / ^ (\d+)(\d+)(\d+)(\d+) $ /; + / ^ (\d+)(\d+)(\d+)(\d+) $ /, - return True if $n ~~ - / ^ (\d+)(\d+)(\d+)(\d+)(\d+) $ /; + / ^ (\d+)(\d+)(\d+)(\d+)(\d+) $ /, - return True if $n ~~ - / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /; + / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /, - return True if $n ~~ - / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /; + / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /, - return True if $n ~~ - / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /; + / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /, - return True if $n ~~ - / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /; + / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /, - return True if $n ~~ - / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ - /; + / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /; return False } -- cgit From 5ac9dfb0c21aa72353f2f24a2a192d98d6411b20 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 3 Jul 2023 12:00:25 -0600 Subject: Solve PWC224 --- challenge-224/wlmb/blog.txt | 1 + challenge-224/wlmb/perl/ch-1.pl | 16 ++++++++++++++++ challenge-224/wlmb/perl/ch-2.pl | 31 +++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 challenge-224/wlmb/blog.txt create mode 100755 challenge-224/wlmb/perl/ch-1.pl create mode 100755 challenge-224/wlmb/perl/ch-2.pl diff --git a/challenge-224/wlmb/blog.txt b/challenge-224/wlmb/blog.txt new file mode 100644 index 0000000000..4b18a54ae8 --- /dev/null +++ b/challenge-224/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2023/07/03/PWC224/ diff --git a/challenge-224/wlmb/perl/ch-1.pl b/challenge-224/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..5427d31911 --- /dev/null +++ b/challenge-224/wlmb/perl/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl +# Perl weekly challenge 224 +# Task 1: Special Notes +# +# See https://wlmb.github.io/2023/07/03/PWC224/#task-1-special-notes +use v5.36; +use List::Util qw(all); +die <<~"FIN" unless @ARGV==2; + Usage: $0 source target + to find out if target may be written with non-repeated characters from source. + FIN +my ($source, $target)=@ARGV; +my %available; +$available{$_}=1 for split "", $source; # initialize available character counts +my $output=(all {$available{$_}-- > 0} split "", $target)?"True":"False"; +say "source: $source, target: $target -> $output"; diff --git a/challenge-224/wlmb/perl/ch-2.pl b/challenge-224/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..510f090788 --- /dev/null +++ b/challenge-224/wlmb/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl +# Perl weekly challenge 224 +# Task 2: Additive Number +# +# See https://wlmb.github.io/2023/07/03/PWC224/#task-2-additive-number +use v5.36; +use List::Util qw(max min); +die <<~"FIN" unless @ARGV; + Usage: $0 S1 [S2...] + to check if the strings S1 S2... correspond to additive numbers. + FIN + INPUT: for(@ARGV){ + say("String should contain digits only: $_"), next INPUT unless /^\d+$/; + my $length=length; + FIRST: for my $length1(1..$length/2){ # no need to consider larger first numbers + my $copy=$_; # make a copy of input string + my $previous=substr $copy, 0, $length1, ""; # choose the starting number + for my $length2(1..$length/2){ + # ignore numbers that are too large: + next FIRST if 2*max($length1,$length2) > $length - min($length1, $length2); + my $copy_of_copy=$copy; + my $current=substr $copy_of_copy, 0, $length2, ""; + my $next=$previous + $current; + while($copy_of_copy=~s/^$next//){ # found next number in sequence? + say("$_ -> True"), next INPUT if length $copy_of_copy==0; # Finished? + ($current, $next)=($next, $current+$next) + } + } + } + say "$_ -> False"; +} -- cgit From bcede8685e93734da8ad4ecb37be2d98c7b04d71 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 3 Jul 2023 22:35:00 +0200 Subject: Disproved solution to task 2 --- challenge-223/jo-37/perl/ch-2.pl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/challenge-223/jo-37/perl/ch-2.pl b/challenge-223/jo-37/perl/ch-2.pl index 6edf150ce7..1e6b980920 100755 --- a/challenge-223/jo-37/perl/ch-2.pl +++ b/challenge-223/jo-37/perl/ch-2.pl @@ -49,6 +49,15 @@ say collect_max(@ARGV); # # I'm curious about the comparison between this procedure's results with # exact solutions. +# +# Update 2023/07/03: +# As expected, this procedure does not provide the correct results. Fed +# some random data to the solutions from andreas-voegele, polettix and +# sgreen that seem to be correct and compared the results with mine. +# There is at lease one error in this approach: Whenever there are some +# consecutive minima in the list, not this minimum needs to be chosen +# but the larger neighbor. E.g. from (6 5 4 4) we need to choose 5 +# instead of 4. sub collect_max { my $l = long @_; -- cgit From d1a2cc4bce61296c5ba418d92298b161196941aa Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 3 Jul 2023 16:54:08 -0400 Subject: Week 224 --- challenge-224/zapwai/perl/ch-1.pl | 28 ++++++++++++++ challenge-224/zapwai/perl/ch-2.pl | 76 +++++++++++++++++++++++++++++++++++++ challenge-224/zapwai/raku/ch-1.raku | 31 +++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 challenge-224/zapwai/perl/ch-1.pl create mode 100644 challenge-224/zapwai/perl/ch-2.pl create mode 100644 challenge-224/zapwai/raku/ch-1.raku diff --git a/challenge-224/zapwai/perl/ch-1.pl b/challenge-224/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..5ccc473578 --- /dev/null +++ b/challenge-224/zapwai/perl/ch-1.pl @@ -0,0 +1,28 @@ +use v5.30; +my $src1 = "abc"; +my $tar1 = "xyz"; +my $src2 = "scriptinglanguage"; +my $tar2 = "perl"; +my $src3 = "aabbcc"; +my $tar3 = "abc"; +my @src = ($src1, $src2, $src3); +my @tar = ($tar1, $tar2, $tar3); +for my $i (0 .. 2) { + my $src = $src[$i]; + my $tar = $tar[$i]; + my @l = split("", $src); + my %h; + $h{$_}++ for @l; + my @L = split "", $tar; + my $fail = 0; + foreach my $l (@L) { + if ($h{$l} > 0) { + $h{$l}--; + } else { + $fail = 1; + } + } + say "Input: \$source = $src\n\t\$target = $tar"; + my $out = ($fail == 1) ? "False" : "True"; + say "Output: $out\n"; +} diff --git a/challenge-224/zapwai/perl/ch-2.pl b/challenge-224/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..21732b8358 --- /dev/null +++ b/challenge-224/zapwai/perl/ch-2.pl @@ -0,0 +1,76 @@ +use v5.30; +my $str1 = "12345"; +my $str2 = "112358"; +my $str3 = "199100199"; +my $str4 = "12364884132"; +my $str5 = "28101828"; +my $str6 = "7111829"; +my $str7 = "112358132134"; +my $str8 = "12345657910351614"; +my $str9 = "102420483072"; +my $str10 = "21425"; +my $str11 = "3105108"; +my @S = ($str1, $str2, $str3, $str4, $str5, $str6, $str7, $str8, $str9, $str10, $str11); +for my $i (0 .. $#S) { + my $str = $S[$i]; + my $outstr = "Sorry, not additive."; + say "Input: \$str = $str"; + print "Output: "; + say chunk(\$outstr, $str); + say $outstr; + say "-" x 20; +} + +sub chunk { + my $r = shift; + my $str = shift; + my @pile_of_string = make_pile($str); + for my $s (@pile_of_string) { + my @nums = split " ", $s; + return 1 if (check($r, @nums) == 1); + } + return 0; +} + +sub make_pile { + my $str = shift; + my $n = length($str) - 1; + my $l = '%0' . $n . 'b'; + my @A; + my @letters = split "", $str; + my @mask; + for (0 .. 2**$n - 1) { + push @mask, sprintf $l, $_; + } + foreach my $mask (@mask) { + my @bits = split "", $mask; + my @list; + for my $i (0 .. $#bits) { + push @list, $i if ($bits[$i] == 1); + } + my $newstr; + foreach my $i (0 .. $#letters) { + $newstr .= $letters[$i]; + $newstr .= " " if ($i ~~ @list); + } + push @A, $newstr; + + } + return @A; +} + +sub check { + my $str_ref = shift; + my @nums = @_; + return 0 if (@nums == 2); + my $cnt = 0; + for my $i (0 .. $#nums - 2) { + $cnt++ if ($nums[$i] + $nums[$i + 1] == $nums[$i + 2]); + } + if ($cnt == $#nums - 1) { + $$str_ref = "\t@nums is additive!"; + return 1; + } + return 0; +} + diff --git a/challenge-224/zapwai/raku/ch-1.raku b/challenge-224/zapwai/raku/ch-1.raku new file mode 100644 index 0000000000..0858134825 --- /dev/null +++ b/challenge-224/zapwai/raku/ch-1.raku @@ -0,0 +1,31 @@ +my $src1 = "abc"; +my $tar1 = "xyz"; +my $src2 = "scriptinglanguage"; +my $tar2 = "perl"; +my $src3 = "aabbcc"; +my $tar3 = "abc"; +my @src = ($src1, $src2, $src3); +my @tar = ($tar1, $tar2, $tar3); +for 0 .. 2 -> $i { + my $src = @src[$i]; + my $tar = @tar[$i]; + my @l = split "", $src; + my %h; + %h{$_}++ for @l; + my @L = split "", $tar; + my $fail = 0; + for @L -> $l { + unless (%h{$l}) { + $fail = 1; + last; + } + if (%h{$l} > 0) { + %h{$l}--; + } else { + $fail = 1; + } + } + say "Input: \$source = $src\n\t\$target = $tar"; + my $out = ($fail == 1) ?? "False" !! "True"; + say "Output: $out\n"; +} -- cgit From 10b8c9090684239ce2dbd2cbbb53f90dde8a2612 Mon Sep 17 00:00:00 2001 From: Andreas Vögele Date: Mon, 3 Jul 2023 12:21:53 +0200 Subject: Challenge 224 by Andreas Vögele MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- challenge-224/andreas-voegele/kotlin/ch-1.kts | 24 ++++++++++++ challenge-224/andreas-voegele/kotlin/ch-2.kts | 43 ++++++++++++++++++++ challenge-224/andreas-voegele/perl/ch-1.pl | 23 +++++++++++ challenge-224/andreas-voegele/perl/ch-2.pl | 56 +++++++++++++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100755 challenge-224/andreas-voegele/kotlin/ch-1.kts create mode 100755 challenge-224/andreas-voegele/kotlin/ch-2.kts create mode 100755 challenge-224/andreas-voegele/perl/ch-1.pl create mode 100755 challenge-224/andreas-voegele/perl/ch-2.pl diff --git a/challenge-224/andreas-voegele/kotlin/ch-1.kts b/challenge-224/andreas-voegele/kotlin/ch-1.kts new file mode 100755 index 0000000000..6e7a8f6845 --- /dev/null +++ b/challenge-224/andreas-voegele/kotlin/ch-1.kts @@ -0,0 +1,24 @@ +#!/usr/bin/env kotlin + +/* + * You are given two strings, $source and $target. Write a script to find out + * if using the characters (only once) from source, a target string can be + * created. + */ + +fun countChars(string: String): Map = + string.groupBy { it }.map { (char, chars) -> char to chars.size }.toMap() + +fun isSpecialNote(source: String, target: String): Boolean { + val sourceCountFor = countChars(source) + for ((char, targetCount) in countChars(target)) { + if (sourceCountFor.getOrDefault(char, 0) < targetCount) { + return false + } + } + return true +} + +println(isSpecialNote("abc", "xyz")) +println(isSpecialNote("scriptinglanguage", "perl")) +println(isSpecialNote("aabbcc", "abc")) diff --git a/challenge-224/andreas-voegele/kotlin/ch-2.kts b/challenge-224/andreas-voegele/kotlin/ch-2.kts new file mode 100755 index 0000000000..da2245a498 --- /dev/null +++ b/challenge-224/andreas-voegele/kotlin/ch-2.kts @@ -0,0 +1,43 @@ +#!/usr/bin/env kotlin + +/* + * You are given a string containing digits 0-9 only. Write a script to find + * out if the given string is additive number. An additive number is a string + * whose digits can form an additive sequence. + * + * A valid additive sequence should contain at least 3 numbers. Except the + * first 2 numbers, each subsequent number in the sequence must be the sum of + * the preceding two. + */ + +fun combineDigits(numericString: String): List> = + (1..numericString.length).flatMap { n -> + val number = numericString.take(n).toInt() + val rest = numericString.drop(n) + if (rest.isEmpty()) { + listOf(listOf(number)) + } + else { + combineDigits(rest).map { listOf(number) + it } + } + } + +fun isAdditive(numbers: List): Boolean { + val size = numbers.size + if (size < 3) { + return false + } + val triples = sequence { + for (i in 0..size - 3) { + yield(Triple(numbers[i], numbers[i + 1], numbers[i + 2])) + } + } + return triples.all { it.first + it.second == it.third } +} + +fun isAdditiveNumber(numericString: String) = + combineDigits(numericString).any { isAdditive(it) } + +println(isAdditiveNumber("112358")); +println(isAdditiveNumber("12345")); +println(isAdditiveNumber("199100199")); diff --git a/challenge-224/andreas-voegele/perl/ch-1.pl b/challenge-224/andreas-voegele/perl/ch-1.pl new file mode 100755 index 0000000000..c833832324 --- /dev/null +++ b/challenge-224/andreas-voegele/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl + +# You are given two strings, $source and $target. Write a script to find out +# if using the characters (only once) from source, a target string can be +# created. + +use 5.036; +use utf8; + +sub is_special_note ($source, $target) { + my %count_for; + for my $char (split //, $source) { + $count_for{$char}++; + } + for my $char (split //, $target) { + return 0 if !exists $count_for{$char} || $count_for{$char}-- < 1; + } + return 1; +} + +say is_special_note('abc', 'xyz'); +say is_special_note('scriptinglanguage', 'perl'); +say is_special_note('aabbcc', 'abc'); diff --git a/challenge-224/andreas-voegele/perl/ch-2.pl b/challenge-224/andreas-voegele/perl/ch-2.pl new file mode 100755 index 0000000000..ba348e61db --- /dev/null +++ b/challenge-224/andreas-voegele/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl + +# You are given a string containing digits 0-9 only. Write a script to find +# out if the given string is additive number. An additive number is a string +# whose digits can form an additive sequence. +# +# A valid additive sequence should contain at least 3 numbers. Except the +# first 2 numbers, each subsequent number in the sequence must be the sum of +# the preceding two. + +use 5.036; +use utf8; + +sub combine_digits ($numeric_string) { + my @combinations; + for my $n (1 .. length $numeric_string) { + my $number = substr $numeric_string, 0, $n; + my $rest = substr $numeric_string, $n; + if ($rest eq q{}) { + push @combinations, [$number]; + } + else { + for my $numbers (@{combine_digits($rest)}) { + unshift @{$numbers}, $number; + push @combinations, $numbers; + } + } + } + return \@combinations; +} + +sub is_additive (@numbers) { + my $size = @numbers; + return 0 if $size < 3; + for my $i (0 .. $size - 3) { + return 0 if $numbers[$i] + $numbers[$i + 1] != $numbers[$i + 2]; + } + return 1; +} + +sub myany : prototype(&@) { + my ($code, @list) = @_; + + for my $element (@list) { + return 1 if $code->(local $_ = $element); + } + return 0; +} + +sub is_additive_number ($numeric_string) { + return myany { is_additive(@{$_}) } @{combine_digits($numeric_string)}; +} + +say is_additive_number('112358'); +say is_additive_number('12345'); +say is_additive_number('199100199'); -- cgit From dac272ef4eae45aaa8d2768f58066ae5abdfc3da Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 3 Jul 2023 23:04:04 +0200 Subject: Add solutions to 224: Special Notes & Additive Number by E. Choroba --- challenge-224/e-choroba/perl/ch-1.pl | 40 ++++++++++++++++++++++++++++++++++++ challenge-224/e-choroba/perl/ch-2.pl | 33 +++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100755 challenge-224/e-choroba/perl/ch-1.pl create mode 100755 challenge-224/e-choroba/perl/ch-2.pl diff --git a/challenge-224/e-choroba/perl/ch-1.pl b/challenge-224/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..fb36b315c1 --- /dev/null +++ b/challenge-224/e-choroba/perl/ch-1.pl @@ -0,0 +1,40 @@ +#! /usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub special_notes($source, $target) { + for my $char (split //, $target) { + return unless $source =~ s/$char//; + } + return 1 +} + +sub special_notes_optimised($source, $target) { + my %count; + ++$count{$_} for split //, $source; + $count{$_}-- || return for split //, $target; + return 1 +} + +use Test::More tests => 2 * 3 + 1; + +for my $special_notes (\&special_notes, + \&special_notes_optimised +) { + ok ! $special_notes->('abc', 'xyz'), 'Example 1'; + ok $special_notes->('scriptinglanguage', 'perl'), 'Example 2'; + ok $special_notes->('aabbcc', 'abc'), 'Example 3'; +} + +use Benchmark qw{ cmpthese }; +my $source = 'pythonicrubylikescriptinglanguage'; +my $target = 'perlrubypython'; + +is special_notes($source, $target), + special_notes_optimised($source, $target), + 'same'; +cmpthese(-3, { + regex => sub { special_notes($source, $target) }, + opt => sub { special_notes_optimised($source, $target) }, +}); diff --git a/challenge-224/e-choroba/perl/ch-2.pl b/challenge-224/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..4c141dbe45 --- /dev/null +++ b/challenge-224/e-choroba/perl/ch-2.pl @@ -0,0 +1,33 @@ +#! /usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub additive_number($string) { + for my $l1 (1 .. length($string) - 1) { + LENGTH: + for my $l2 (1 .. length($string) - $l1) { + my $first = substr $string, 0, $l1; + my $second = substr $string, $l1, $l2; + my $pos = 0; + while (1) { + my $plus = $first + $second; + + next LENGTH unless $pos == index $string, "$first$second$plus"; + + return 1 if $pos + length "$first$second$plus" == length $string; + + $pos += length $first; + $first = $second; + $second = $plus; + } + } + } + return +} + +use Test::More tests => 3; + +ok additive_number('112358'), 'Example 1'; +ok ! additive_number('12345'), 'Example 2'; +ok additive_number('199100199'), 'Example 3'; -- cgit From e98c57174ceb94927ab0e27b809d110313a52954 Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 3 Jul 2023 22:30:10 +0100 Subject: Here's week 224 ... --- challenge-224/peter-campbell-smith/blog.txt | 1 + challenge-224/peter-campbell-smith/perl/ch-1.pl | 40 +++++++++++++ challenge-224/peter-campbell-smith/perl/ch-2.pl | 77 +++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 challenge-224/peter-campbell-smith/blog.txt create mode 100755 challenge-224/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-224/peter-campbell-smith/perl/ch-2.pl diff --git a/challenge-224/peter-campbell-smith/blog.txt b/challenge-224/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..9fa043f470 --- /dev/null +++ b/challenge-224/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/224 diff --git a/challenge-224/peter-campbell-smith/perl/ch-1.pl b/challenge-224/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..ecf9d30db5 --- /dev/null +++ b/challenge-224/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2023-07-03 +use utf8; # Week 224 task 1 - Special notes +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +special_notes('abc', 'xyz'); +special_notes('scriptinglanguage', 'perl'); +special_notes('aabbcc', 'abc'); +special_notes('gorge', 'george'); +special_notes('fornowisthetimeforallgoodpeopletocometotheaidofthebaby', 'hyperbolicparaboloid'); + +sub special_notes { + + my ($source, $target, $letter, %letters, $good); + + ($source, $target) = @_; + + # split $source into individual letters + for $letter (split('', $source)) { + $letters{$letter} ++; + } + + # check if $target has any letters not in $source + $good = 'true'; + for $letter (split('', $target)) { + if ($letters{$letter}) { + $letters{$letter} --; + } else { + $good = 'false '; + last; + } + } + + # show answer + say qq[\nInput: \$source = '$source'\n \$target = '$target']; + say qq[Output: $good]; + +} diff --git a/challenge-224/peter-campbell-smith/perl/ch-2.pl b/challenge-224/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..513ecf9796 --- /dev/null +++ b/challenge-224/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,77 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2023-07-03 +use utf8; # Week 224 task 2 - Additive number +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +my ($solution, @string, $j); + +additive_number('12354782'); +additive_number('199100199299498'); +additive_number('0030047'); +additive_number('314159'); + +# create a long one +@string = (28,74);; +for $j (0 .. 19) { + push @string, $string[-1] + $string[-2]; +} +additive_number(join('', @string)); + +sub additive_number { + + my $string; + + # initialise + $solution = ''; + $string = shift @_; + + # start recursion + test([], $string); + + # results + say qq[\nInput: \$string = '$string']; + say qq[Output: ] . ($solution ? $solution : 'false'); +} + +sub test { + + # test 1 further number (maybe several digits) along $string + + my (@numbers, $string, $count, $length, $j, @new_numbers, $new_string, $keep_going); + + # we can stop if a solution has been found + return if $solution; + + # initialise + @numbers = @{$_[0]}; + $string = $_[1]; + $count = scalar @numbers; + + # if we have only 1 or 2 numbers they must be valid and we don't need to test them + if ($count < 3) { + $keep_going = 1; + + # we check that the last 3 numbers are additive, and if the string is now empty we have a solution + } else { + $keep_going = $numbers[-3] + $numbers[-2] == $numbers[-1]; + if ($keep_going and $string eq '') { + $solution = join(', ', @numbers); + return; + } + } + + # $keep_going is true if the sequence of @numbers is good but incomplete + return unless $keep_going; + + # try using the next 1, 2, 3 ... digits from $string + $length = length($string); + for $j (1 .. $length) { + @new_numbers = (@numbers, substr($string, 0, $j)); + $new_string = $j == $length ? '' : substr($string, $j); + + # and recurse with those + test(\@new_numbers, $new_string); + } +} -- cgit From 31f9b79ff91a5fdd1823551c4f38b161d72fc6b0 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 4 Jul 2023 02:31:18 +0000 Subject: Initial 224 (Raku) --- challenge-224/mark-anderson/raku/ch-2.raku | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-224/mark-anderson/raku/ch-2.raku b/challenge-224/mark-anderson/raku/ch-2.raku index 3bb9453580..02bcd8dd4d 100644 --- a/challenge-224/mark-anderson/raku/ch-2.raku +++ b/challenge-224/mark-anderson/raku/ch-2.raku @@ -18,7 +18,8 @@ ok additive-number("1321517324981130211"), "13,2,15,17,32,49,81,130,211"; sub additive-number($n) { - return True if $n ~~ any + $n ~~ any + / ^ (\d+)(\d+)(\d+) $ /, / ^ (\d+)(\d+)(\d+)(\d+) $ /; - return False } -- cgit From a1d9856dfee91e964e8a9cfdc8cf1650600048da Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 4 Jul 2023 08:44:36 +0000 Subject: edit... --- challenge-224/mark-anderson/raku/ch-2.raku | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-224/mark-anderson/raku/ch-2.raku b/challenge-224/mark-anderson/raku/ch-2.raku index 02bcd8dd4d..ca96165f95 100644 --- a/challenge-224/mark-anderson/raku/ch-2.raku +++ b/challenge-224/mark-anderson/raku/ch-2.raku @@ -46,7 +46,7 @@ sub additive-number($n) $3 + $4 == $5, $4 + $5 == $6, $5 + $6 == $7 }> /, - + / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /, - + / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ Date: Tue, 4 Jul 2023 14:52:50 +0100 Subject: add solution week 224 task 1 in perl --- challenge-224/steven-wilson/perl/ch-01.pl | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 challenge-224/steven-wilson/perl/ch-01.pl diff --git a/challenge-224/steven-wilson/perl/ch-01.pl b/challenge-224/steven-wilson/perl/ch-01.pl new file mode 100644 index 0000000000..ac2e83a88b --- /dev/null +++ b/challenge-224/steven-wilson/perl/ch-01.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl +# Task 1: Special Notes + +# You are given two strings, $source and $target. + +# Write a script to find out if using the characters (only once) from +# source, a target string can be created. + +use 5.12.0; +use Test::More; + +cmp_ok( special_notes( "abc", "xyz" ), "==", 0, "Example 1" ); +cmp_ok( special_notes( "scriptinglanguage", "perl" ), "==", 1, "Example 2" ); +cmp_ok( special_notes( "aabbcc", "abc" ), "==", 1, "Example 3" ); +done_testing(); + +sub special_notes { + my ( $source, $target ) = @_; + my %characters; + my $special = 1; + + map { $characters{$_}++ } ( split //, $source ); + + for ( split //, $target ) { + if ( $characters{$_} ) { + $characters{$_}--; + } + else { + $special = 0; + last; + } + } + return $special; +} -- cgit From 6156452a21eca9b55c74044cbb45880823029a61 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 4 Jul 2023 16:05:11 +0000 Subject: Initial 224 (Raku) --- challenge-224/mark-anderson/raku/ch-2.raku | 73 +++++++++++++++--------------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/challenge-224/mark-anderson/raku/ch-2.raku b/challenge-224/mark-anderson/raku/ch-2.raku index ca96165f95..56be2702df 100644 --- a/challenge-224/mark-anderson/raku/ch-2.raku +++ b/challenge-224/mark-anderson/raku/ch-2.raku @@ -20,48 +20,47 @@ sub additive-number($n) { $n ~~ any - / ^ (\d+)(\d+)(\d+) $ /, + / ^ $0 = <{ '(\d+)' x 3 }> $ /, - / ^ (\d+)(\d+)(\d+)(\d+) $ /, + / ^ $0 = <{ '(\d+)' x 4 }> $ /, - / ^ (\d+)(\d+)(\d+)(\d+)(\d+) $ /, + / ^ $0 = <{ '(\d+)' x 5 }> $ /, - / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /, + / ^ $0 = <{ '(\d+)' x 6 }> $ /, - / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /, + / ^ $0 = <{ '(\d+)' x 7 }> $ /, - / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /, + / ^ $0 = <{ '(\d+)' x 8 }> $ /, - / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /, - - / ^ (\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+)(\d+) $ /; + / ^ $0 = <{ '(\d+)' x 9 }> $ /, + / ^ $0 = <{ '(\d+)' x 10 }> $ /; } -- cgit From 3c81437e0abfc84a1c83c6faf66e2bd1c26b9bd0 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 4 Jul 2023 20:33:55 +0000 Subject: Challenge 224 Solutions (Raku) --- challenge-224/mark-anderson/raku/ch-2.raku | 59 ++++-------------------------- 1 file changed, 7 insertions(+), 52 deletions(-) diff --git a/challenge-224/mark-anderson/raku/ch-2.raku b/challenge-224/mark-anderson/raku/ch-2.raku index 56be2702df..ef409486c3 100644 --- a/challenge-224/mark-anderson/raku/ch-2.raku +++ b/challenge-224/mark-anderson/raku/ch-2.raku @@ -7,60 +7,15 @@ ok additive-number("199100199"), "1,99,100,199"; ok additive-number("4588321981458832199"), "458832198,1,458832199"; ok additive-number("1321517324981130211"), "13,2,15,17,32,49,81,130,211"; -# A few notes: - -# I wish I knew how to write grammars. - -# For some reason I can't get interpolation to work in my regexes -# so I'm stuck with this hardcoded mess. - -# This only works for up to 10 numbers. - sub additive-number($n) { - $n ~~ any - - / ^ $0 = <{ '(\d+)' x 3 }> $ /, - - / ^ $0 = <{ '(\d+)' x 4 }> $ /, - - / ^ $0 = <{ '(\d+)' x 5 }> $ /, - - / ^ $0 = <{ '(\d+)' x 6 }> $ /, - - / ^ $0 = <{ '(\d+)' x 7 }> $ /, - - / ^ $0 = <{ '(\d+)' x 8 }> $ /, + my $limit = $n.substr(($n.chars div 2), *); - / ^ $0 = <{ '(\d+)' x 9 }> $ /, + for (1..($n.chars div 2).succ).combinations(2) -> ($h, $t) + { + my $s := $n.substr(0, $h), $n.substr($h, $t-$h), { $^a + $^b } ... * >= $limit; + return True if $s.join.substr(0, $n.chars) eq $n + } - / ^ $0 = <{ '(\d+)' x 10 }> $ /; + False } -- cgit From 03b0dd280e0deda7a7543fcaeafa4e407a7aff3b Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 4 Jul 2023 21:52:41 +0000 Subject: Challenge 224 Solutions (Raku) --- challenge-224/mark-anderson/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-224/mark-anderson/raku/ch-2.raku b/challenge-224/mark-anderson/raku/ch-2.raku index ef409486c3..2b10bf2cf2 100644 --- a/challenge-224/mark-anderson/raku/ch-2.raku +++ b/challenge-224/mark-anderson/raku/ch-2.raku @@ -9,7 +9,7 @@ ok additive-number("1321517324981130211"), "13,2,15,17,32,49,81,130,211"; sub additive-number($n) { - my $limit = $n.substr(($n.chars div 2), *); + my $limit = + $n.substr(($n.chars / 2).ceiling, *); for (1..($n.chars div 2).succ).combinations(2) -> ($h, $t) { -- cgit From ae8c441ac4e5c16fb925662ddfd3718346950959 Mon Sep 17 00:00:00 2001 From: Avery Adams Date: Wed, 5 Jul 2023 11:04:18 +1200 Subject: Solution for 224 for Avery Adams --- challenge-224/avery-adams/blogs.txt | 2 ++ challenge-224/avery-adams/perl/ch-1.pl | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 challenge-224/avery-adams/blogs.txt create mode 100644 challenge-224/avery-adams/perl/ch-1.pl diff --git a/challenge-224/avery-adams/blogs.txt b/challenge-224/avery-adams/blogs.txt new file mode 100644 index 0000000000..337cd77557 --- /dev/null +++ b/challenge-224/avery-adams/blogs.txt @@ -0,0 +1,2 @@ +https://dev.to/oldtechaa/perl-weekly-challenge-224-passing-notes-3kp5 +https://blogs.perl.org/users/oldtechaa/2023/07/perl-weekly-challenge-224---passing-notes.html diff --git a/challenge-224/avery-adams/perl/ch-1.pl b/challenge-224/avery-adams/perl/ch-1.pl new file mode 100644 index 0000000000..e92f893824 --- /dev/null +++ b/challenge-224/avery-adams/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +use strict; +use v5.24; + +my ($source, $target) = @ARGV; +my %chars; + +foreach (split //, $source) {$chars{$_}++} + +foreach (split //, $target) { + if ($chars{$_}) { + $chars{$_}--; + } else { + say 'false' and exit; + } +} +say 'true'; -- cgit From c508756142b318297bf61a13c7cb6adff64ab67f Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Wed, 5 Jul 2023 03:00:30 +0000 Subject: Challenge 224 Solutions (Raku) --- challenge-224/mark-anderson/raku/ch-2.raku | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/challenge-224/mark-anderson/raku/ch-2.raku b/challenge-224/mark-anderson/raku/ch-2.raku index 2b10bf2cf2..dead5aa20c 100644 --- a/challenge-224/mark-anderson/raku/ch-2.raku +++ b/challenge-224/mark-anderson/raku/ch-2.raku @@ -1,4 +1,4 @@ -#!/usr/bin/raku +#!/usr/bin/env raku use Test; ok additive-number("112358"), "1,2,2,3,5,8"; @@ -9,13 +9,14 @@ ok additive-number("1321517324981130211"), "13,2,15,17,32,49,81,130,211"; sub additive-number($n) { - my $limit = + $n.substr(($n.chars / 2).ceiling, *); + my $a = $n.substr(0, ($n.chars div 2).succ); + my $b = $n.substr(($n.chars div 2).succ, *); - for (1..($n.chars div 2).succ).combinations(2) -> ($h, $t) + for (1..$a.chars.succ).combinations(2) -> ($h, $t) { - my $s := $n.substr(0, $h), $n.substr($h, $t-$h), { $^a + $^b } ... * >= $limit; - return True if $s.join.substr(0, $n.chars) eq $n + my $s := $n.substr(0, $h), $n.substr($h, $t-$h), { $^a + $^b } ... * >= $b; + return True if $s.join.substr(0, $n.chars) eq $n; } - False + return False } -- cgit From 2ea04e5ca28c964ced02b634d3350fcdc169d717 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Wed, 5 Jul 2023 03:48:46 +0000 Subject: Challenge 224 Solutions (Raku) --- challenge-224/mark-anderson/raku/ch-2.raku | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/challenge-224/mark-anderson/raku/ch-2.raku b/challenge-224/mark-anderson/raku/ch-2.raku index dead5aa20c..41f9080d16 100644 --- a/challenge-224/mark-anderson/raku/ch-2.raku +++ b/challenge-224/mark-anderson/raku/ch-2.raku @@ -7,10 +7,20 @@ ok additive-number("199100199"), "1,99,100,199"; ok additive-number("4588321981458832199"), "458832198,1,458832199"; ok additive-number("1321517324981130211"), "13,2,15,17,32,49,81,130,211"; +# additional tests taken from zapwai's solution +ok additive-number("12364884132"), "12,36,48,84,132"; +ok additive-number("28101828"), "2,8,10,18,28"; +ok additive-number("7111829"), "7,11,18,29"; +ok additive-number("112358132134"), "1,1,2,3,5,8,13,21,34"; +ok additive-number("12345657910351614"), "123,456,579,1035,1614"; +ok additive-number("102420483072"), "1024,2048,3072"; +ok additive-number("21425"), "21,4,25"; +ok additive-number("3105108"), "3,105,108"; + sub additive-number($n) { - my $a = $n.substr(0, ($n.chars div 2).succ); - my $b = $n.substr(($n.chars div 2).succ, *); + my $a = $n.substr(0, $_), + my $b = $n.substr($_, *) given ($n.chars div 2).succ; for (1..$a.chars.succ).combinations(2) -> ($h, $t) { -- cgit From 0bd1c82f6d0bee8cc121398ee9a7f87a4d025262 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Wed, 5 Jul 2023 12:42:23 +0200 Subject: feat(challenge-224/lubos-kolouch/perl,python,java/): Challenge 224 LK Perl Python Java --- challenge-224/lubos-kolouch/java/ch-1.java | 26 +++++++++++++++ challenge-224/lubos-kolouch/perl/ch-1.pl | 15 +++++++++ challenge-224/lubos-kolouch/perl/ch-2.pl | 53 ++++++++++++++++++++++++++++++ challenge-224/lubos-kolouch/python/ch-1.py | 15 +++++++++ challenge-224/lubos-kolouch/python/ch-2.py | 24 ++++++++++++++ 5 files changed, 133 insertions(+) create mode 100644 challenge-224/lubos-kolouch/java/ch-1.java create mode 100644 challenge-224/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-224/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-224/lubos-kolouch/python/ch-1.py create mode 100644 challenge-224/lubos-kolouch/python/ch-2.py diff --git a/challenge-224/lubos-kolouch/java/ch-1.java b/challenge-224/lubos-kolouch/java/ch-1.java new file mode 100644 index 0000000000..aeff01b10f --- /dev/null +++ b/challenge-224/lubos-kolouch/java/ch-1.java @@ -0,0 +1,26 @@ +import java.util.HashMap; +import java.util.Map; + +public class Main { + public static void main(String[] args) { + System.out.println(canFormTarget("abc", "xyz")); // Output: false + System.out.println( + canFormTarget("scriptinglanguage", "perl")); // Output: true + System.out.println(canFormTarget("aabbcc", "abc")); // Output: true + } + + public static boolean canFormTarget(String source, String target) { + Map sourceChars = new HashMap<>(); + for (char c : source.toCharArray()) { + sourceChars.put(c, s