From de8820341c75c44bfa1b66dcaccbd9116681067e Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Tue, 17 Jan 2023 10:04:44 +0800 Subject: challenge 200, raku solutions --- challenge-200/feng-chang/raku/ch-1.raku | 8 ++++++++ challenge-200/feng-chang/raku/ch-2.raku | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100755 challenge-200/feng-chang/raku/ch-1.raku create mode 100755 challenge-200/feng-chang/raku/ch-2.raku diff --git a/challenge-200/feng-chang/raku/ch-1.raku b/challenge-200/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..e3decf7bec --- /dev/null +++ b/challenge-200/feng-chang/raku/ch-1.raku @@ -0,0 +1,8 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +.put for (^+@N X ^+@N) + .grep({ .[1] - .[0] > 1 }) + .grep({ [==] (.[0] ..^ .[1]).map({ @N[$_+1] - @N[$_] }) }) + .map({ "({ @N[.[0] .. .[1]].join(', ') })" }); diff --git a/challenge-200/feng-chang/raku/ch-2.raku b/challenge-200/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..e50151bf9a --- /dev/null +++ b/challenge-200/feng-chang/raku/ch-2.raku @@ -0,0 +1,32 @@ +#!/bin/env raku + +use v6.d; +unit sub MAIN(UInt:D $n); + +my @N = $n.comb; +my @truth is Array = ; +my @display = [' ' xx (9 * @N.elems - 2)] xx 7; +my $offset = 0; + +for @N -> $d { + for @truth[$d].comb -> $seg { + given $seg { + when 'a' { @display[0;$offset..$offset+6] X= '-'; } + when 'b' { @display[1..2;$offset+6] X= '|'; } + when 'c' { @display[4..5;$offset+6] X= '|'; } + when 'd' { @display[6;$offset..$offset+6] X= '-'; } + when 'e' { @display[4..5;$offset] X= '|'; } + when 'f' { @display[1..2;$offset] X= '|'; } + when 'g' { @display[3;$offset..$offset+6] X= '-'; } + default { die "wrong segment «$seg»"; } + } + + #| must do something here + #my $question = 'but why?!'; + LAST { } + } + + $offset += 9; +} + +.join.put for @display; -- cgit From 9d964b9bb2fc6df2fcaa6018fa6369f582996dab Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Mon, 6 Feb 2023 19:11:39 +0800 Subject: challeng 203, raku solutions --- challenge-203/feng-chang/raku/a/b/c/1/1.txt | 0 challenge-203/feng-chang/raku/a/b/c/2/2.txt | 0 challenge-203/feng-chang/raku/a/b/c/3/3.txt | 0 challenge-203/feng-chang/raku/a/b/c/5/5.txt | 0 challenge-203/feng-chang/raku/ch-1.raku | 5 +++++ challenge-203/feng-chang/raku/ch-2.raku | 20 ++++++++++++++++++++ 6 files changed, 25 insertions(+) create mode 100644 challenge-203/feng-chang/raku/a/b/c/1/1.txt create mode 100644 challenge-203/feng-chang/raku/a/b/c/2/2.txt create mode 100644 challenge-203/feng-chang/raku/a/b/c/3/3.txt create mode 100644 challenge-203/feng-chang/raku/a/b/c/5/5.txt create mode 100755 challenge-203/feng-chang/raku/ch-1.raku create mode 100755 challenge-203/feng-chang/raku/ch-2.raku diff --git a/challenge-203/feng-chang/raku/a/b/c/1/1.txt b/challenge-203/feng-chang/raku/a/b/c/1/1.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-203/feng-chang/raku/a/b/c/2/2.txt b/challenge-203/feng-chang/raku/a/b/c/2/2.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-203/feng-chang/raku/a/b/c/3/3.txt b/challenge-203/feng-chang/raku/a/b/c/3/3.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-203/feng-chang/raku/a/b/c/5/5.txt b/challenge-203/feng-chang/raku/a/b/c/5/5.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-203/feng-chang/raku/ch-1.raku b/challenge-203/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..8d609a3296 --- /dev/null +++ b/challenge-203/feng-chang/raku/ch-1.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +put @N.sort.combinations(4).grep({ .[0] + .[1] + .[2] == .[3] }).elems; diff --git a/challenge-203/feng-chang/raku/ch-2.raku b/challenge-203/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..5d6ec35da9 --- /dev/null +++ b/challenge-203/feng-chang/raku/ch-2.raku @@ -0,0 +1,20 @@ +#!/bin/env raku + +unit sub MAIN( + Str:D $source where *.IO.d = 'a/b/c', + Str:D $target where *.IO.d = 'x/y', +); + +my $offset; + +sub copy-dirs($source, $target) { + for $source.IO.dir -> $file { + next unless $file.d; + + mkdir "$target/{ $file.relative.substr($offset) }"; + copy-dirs($file, $target); + } +} + +$offset = $source.chars + 1; +copy-dirs($source, $target); -- cgit From fdd8f80b356d44a39a8b38648b1ae6f132985b1c Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Mon, 13 Feb 2023 18:10:26 +0800 Subject: challenge 204, raku solutions --- challenge-204/feng-chang/raku/ch-1.raku | 5 +++++ challenge-204/feng-chang/raku/ch-2.raku | 10 ++++++++++ 2 files changed, 15 insertions(+) create mode 100755 challenge-204/feng-chang/raku/ch-1.raku create mode 100755 challenge-204/feng-chang/raku/ch-2.raku diff --git a/challenge-204/feng-chang/raku/ch-1.raku b/challenge-204/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..d20f0b7537 --- /dev/null +++ b/challenge-204/feng-chang/raku/ch-1.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +put +([<=] @N or [>=] @N); diff --git a/challenge-204/feng-chang/raku/ch-2.raku b/challenge-204/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..b03c2adeef --- /dev/null +++ b/challenge-204/feng-chang/raku/ch-2.raku @@ -0,0 +1,10 @@ +#!/bin/env raku + +unit sub MAIN(); + +my @N = $*IN.words; +my ($r, $c) = @N.splice(*-2); + +put $r * $c == +@N ?? + (^$r).map({ "[ { @N[$c*$_ ..^ $c*($_+1)].join(' ') } ]" }).join("\n") !! + 0; -- cgit From 06c67bd969fd43c8e427d9d761a8087d93529d49 Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Tue, 21 Feb 2023 16:50:41 +0800 Subject: challenge 205, raku solutions --- challenge-205/feng-chang/raku/ch-1.raku | 6 ++++++ challenge-205/feng-chang/raku/ch-2.raku | 5 +++++ 2 files changed, 11 insertions(+) create mode 100755 challenge-205/feng-chang/raku/ch-1.raku create mode 100755 challenge-205/feng-chang/raku/ch-2.raku diff --git a/challenge-205/feng-chang/raku/ch-1.raku b/challenge-205/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..283d25297d --- /dev/null +++ b/challenge-205/feng-chang/raku/ch-1.raku @@ -0,0 +1,6 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +@N = @N.sort.unique; +put @N[*-3] // @N.tail; diff --git a/challenge-205/feng-chang/raku/ch-2.raku b/challenge-205/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..ba75153722 --- /dev/null +++ b/challenge-205/feng-chang/raku/ch-2.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +put @N.combinations(2).map({ .[0] +^ .[1] }).max; -- cgit From d2cca4f3533f095d018affa34893ac385b6b02af Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Mon, 27 Feb 2023 18:39:20 +0800 Subject: challenge 206, raku solutions --- challenge-206/feng-chang/raku/ch-1.raku | 12 ++++++++++++ challenge-206/feng-chang/raku/ch-2.raku | 5 +++++ 2 files changed, 17 insertions(+) create mode 100755 challenge-206/feng-chang/raku/ch-1.raku create mode 100755 challenge-206/feng-chang/raku/ch-2.raku diff --git a/challenge-206/feng-chang/raku/ch-1.raku b/challenge-206/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..d1679698b7 --- /dev/null +++ b/challenge-206/feng-chang/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!/bin/env raku + +unit sub MAIN(*@T); + +my $today = now.Date; +my $tomorrow = $today.later(days => 1); + +my @instants = @T.map({ "{$today.yyyy-mm-dd}T{$_}:00".DateTime }); +@instants.push($_) for @T.map({ "{$tomorrow.yyyy-mm-dd}T{$_}:00".DateTime }); +@instants .= sort; + +put (^(+@instants-1)).map({ (@instants[$_+1] - @instants[$_]) / 60 }).min; diff --git a/challenge-206/feng-chang/raku/ch-2.raku b/challenge-206/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..08056bb905 --- /dev/null +++ b/challenge-206/feng-chang/raku/ch-2.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(*@N where +* %% 2); + +put @N.sort[0, 2 ... (+@N-2)].sum; -- cgit From a9fb614002e21969691cc0883239db1a383904b6 Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Tue, 7 Mar 2023 10:35:41 +0800 Subject: challenge 207, raku solutions --- challenge-207/feng-chang/raku/ch-1.raku | 7 +++++++ challenge-207/feng-chang/raku/ch-2.raku | 5 +++++ 2 files changed, 12 insertions(+) create mode 100755 challenge-207/feng-chang/raku/ch-1.raku create mode 100755 challenge-207/feng-chang/raku/ch-2.raku diff --git a/challenge-207/feng-chang/raku/ch-1.raku b/challenge-207/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..cd6e9ecc55 --- /dev/null +++ b/challenge-207/feng-chang/raku/ch-1.raku @@ -0,0 +1,7 @@ +#!/bin/env raku + +unit sub MAIN(*@words); + +my %AtRow = .map({ .comb.all => $++ }); + +put @words.grep({ [==] %AtRow{ .lc.comb } }); diff --git a/challenge-207/feng-chang/raku/ch-2.raku b/challenge-207/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..47315c561d --- /dev/null +++ b/challenge-207/feng-chang/raku/ch-2.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +put (+@N...1).first(-> \m { @N.grep(* ≥ m) ≥ m }); -- cgit From 4acaa7af727c18449670d3167b386f057b9c55bd Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 13 Mar 2023 23:46:12 +0000 Subject: Challenge 208 Solutions (Raku) --- challenge-208/mark-anderson/raku/ch-1.raku | 52 ++++++++++++++++++++++++++++++ challenge-208/mark-anderson/raku/ch-2.raku | 31 ++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 challenge-208/mark-anderson/raku/ch-1.raku create mode 100644 challenge-208/mark-anderson/raku/ch-2.raku diff --git a/challenge-208/mark-anderson/raku/ch-1.raku b/challenge-208/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..d28af6fc98 --- /dev/null +++ b/challenge-208/mark-anderson/raku/ch-1.raku @@ -0,0 +1,52 @@ +#!/usr/bin/env raku +use Test; + +is-deeply(min-index-sum(, ), ("Perl", "Raku"), "Example 1"); + +is-deeply(min-index-sum(, ), (), "Example 2"); + +is-deeply(min-index-sum(, ), ("A",), "Example 3"); + +is-deeply(min-index-sum(, + , + , + , + , +

, + , + , + , + ), ("V",), "10 X 25"); + +is-deeply(min-index-sum(, + , + , + , + , + , + , + , + , +

, + , + , + , + , + , + , + , + , + , + , + , + , + , + , + ), ("D", "E", "W"), "25 X 25"); + +sub min-index-sum(+$a) +{ + my $m := $a>>.antipairs>>.Map; + my $k := ([(&)] $m).keys; + ($k Z=> [Z+] $m>>{$k}).Map.minpairs.Map.keys.sort +} diff --git a/challenge-208/mark-anderson/raku/ch-2.raku b/challenge-208/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..b7cf810067 --- /dev/null +++ b/challenge-208/mark-anderson/raku/ch-2.raku @@ -0,0 +1,31 @@ +#!/usr/bin/env raku +use Algorithm::Diff; +use Test; + +is-deeply duplicate-and-missing(1,2,2,4), (2,3); +is-deeply duplicate-and-missing(1,2,3,4), -1; +is-deeply duplicate-and-missing(1,2,3,3), (3,4); +is-deeply duplicate-and-missing(4,5,6,7,7,8), (7,9); +is-deeply duplicate-and-missing(4,5,6,7,7,9), (7,8); + +# Algorithm::Diff +is-deeply diff-module(1,2,2,4), ("2", "3"); +is-deeply diff-module(1,2,3,4), (Nil, Nil); +is-deeply diff-module(1,2,3,3), ("3", "4"); +is-deeply diff-module(4,5,6,7,7,8), ("7", "9"); +is-deeply diff-module(4,5,6,7,7,9), ("7", "8"); + +sub duplicate-and-missing(*@nums) +{ + given @nums + { + my @range = .head .. .head + .end; + flat .repeated, keys @range (-) $_ or -1 + } +} + +sub diff-module(*@nums) +{ + my @range = .head .. .head + .end given @nums; + diff(@nums, @range).comb(/\d+/)[1,3] +} -- cgit From d0992efd24bfc17a029362cecdc4800dbecaec3c Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 13 Mar 2023 23:59:52 +0000 Subject: Challenge 208 Solutions (Raku) --- challenge-208/mark-anderson/raku/ch-1.raku | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/challenge-208/mark-anderson/raku/ch-1.raku b/challenge-208/mark-anderson/raku/ch-1.raku index d28af6fc98..f3f080ea2e 100644 --- a/challenge-208/mark-anderson/raku/ch-1.raku +++ b/challenge-208/mark-anderson/raku/ch-1.raku @@ -1,13 +1,13 @@ #!/usr/bin/env raku use Test; -is-deeply(min-index-sum(, ), ("Perl", "Raku"), "Example 1"); +is-deeply min-index-sum(, ), ("Perl", "Raku"), "Example 1"; -is-deeply(min-index-sum(, ), (), "Example 2"); +is-deeply min-index-sum(, ), (), "Example 2"; -is-deeply(min-index-sum(, ), ("A",), "Example 3"); +is-deeply min-index-sum(, ), ("A",), "Example 3"; -is-deeply(min-index-sum(, +is-deeply min-index-sum(, , , , @@ -16,9 +16,9 @@ is-deeply(min-index-sum(, , , , - ), ("V",), "10 X 25"); + ), ("V",), "10 X 25"; -is-deeply(min-index-sum(, +is-deeply min-index-sum(, , , , @@ -42,7 +42,7 @@ is-deeply(min-index-sum(, , , , - ), ("D", "E", "W"), "25 X 25"); + ), ("D", "E", "W"), "25 X 25"; sub min-index-sum(+$a) { -- cgit From ae9116a3dd63815d8427f239ceb28506973a20f0 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 13 Mar 2023 20:04:57 -0600 Subject: Solve PWC208 --- challenge-208/wlmb/blog.txt | 2 ++ challenge-208/wlmb/perl/ch-1.pl | 30 ++++++++++++++++++++++++++++++ challenge-208/wlmb/perl/ch-2.pl | 24 ++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 challenge-208/wlmb/blog.txt create mode 100755 challenge-208/wlmb/perl/ch-1.pl create mode 100755 challenge-208/wlmb/perl/ch-2.pl diff --git a/challenge-208/wlmb/blog.txt b/challenge-208/wlmb/blog.txt new file mode 100644 index 0000000000..f9c74067a0 --- /dev/null +++ b/challenge-208/wlmb/blog.txt @@ -0,0 +1,2 @@ +https://wlmb.github.io/2023/03/13/PWC208/ + diff --git a/challenge-208/wlmb/perl/ch-1.pl b/challenge-208/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..5ad20f583b --- /dev/null +++ b/challenge-208/wlmb/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl +# Perl weekly challenge 208 +# Task 1: Minimum Index Sum +# +# See https://wlmb.github.io/2023/03/13/PWC208/#task-1-minimum-index-sum +use v5.36; +use List::UtilsBy qw(nsort_by); +use feature qw(refaliasing declared_refs); +no warnings qw(experimental::refaliasing experimental::declared_refs); +die <<~"FIN" unless @ARGV==2; + Usage: $0 S1 S2 + to find the common space separated substrings of S1 and S2 with the minimum index sum + FIN +my (\@list1, \@list2)=map {[split " "]} @ARGV; # Save a couple of $'s +my (%list1, %list2); +@list1{@list1} = (0..@list1-1); +@list2{@list2} = (0..@list2-1); +my $smallest; +my @results = map {$_->[0]} # extract string part + grep { + $smallest//=$_->[1]; # initialize with lowest index sum + $smallest==$_->[1] # compare index sum with lowest + } + nsort_by {$_->[1]} # sort by index sums + map { + [$_, $list1{$_}+$list2{$_}] # [string, index sum] + } + grep {defined $list2{$_}} # common strings go through + keys %list1; +say "(@list1), (@list2) -> (@results)"; # print result diff --git a/challenge-208/wlmb/perl/ch-2.pl b/challenge-208/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..b8fccdfa89 --- /dev/null +++ b/challenge-208/wlmb/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl +# Perl weekly challenge 208 +# Task 2: Duplicate and Missing +# +# See https://wlmb.github.io/2023/03/13/PWC208/#task-2-duplicate-and-missing +use v5.36; +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to find missing numbers and duplicates in the list N1 N2... + FIN +my @list=sort {$a <=> $b} @ARGV; +my $previous=shift @list; +my @duplicates; +my @missing; +for(@list){ + push @duplicates, $_ if $_==$previous; + push @missing, $previous+1..$_-1; + $previous=$_ +} +push @missing, $previous+1 unless @missing; # missing after last for default +die "More than one duplicate\n" if @duplicates>1; +die "More than one missing\n" if @missing>1; +my $result=@duplicates?"(@duplicates @missing)":-1; +say "@ARGV -> $result"; -- cgit From f0932f64660f5f3ea94798d22731f6710104fd5a Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Tue, 14 Mar 2023 17:54:37 +0800 Subject: Challenge 208, Raku solutions --- challenge-208/feng-chang/raku/ch-1.raku | 24 ++++++++++++++++++++++++ challenge-208/feng-chang/raku/ch-2.raku | 6 ++++++ 2 files changed, 30 insertions(+) create mode 100755 challenge-208/feng-chang/raku/ch-1.raku create mode 100755 challenge-208/feng-chang/raku/ch-2.raku diff --git a/challenge-208/feng-chang/raku/ch-1.raku b/challenge-208/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..5d36026336 --- /dev/null +++ b/challenge-208/feng-chang/raku/ch-1.raku @@ -0,0 +1,24 @@ +#!/bin/env raku + +sub min-index-sum(@list1, @list2) { + my %hash1 = @list1 Z=> ^+@list1; + my %hash2 = @list2 Z=> ^+@list2; + my @common = (%hash1.keys (&) %hash2.keys).keys; + my $min-index = @common.map({ %hash1{$_} + %hash2{$_} }).min; + + @common.grep({ %hash1{$_} + %hash2{$_} == $min-index }).sort +} + +multi MAIN('test') { + use Test; + + is-deeply min-index-sum(, ), , 'example 1 matches'; + is-deeply min-index-sum(, ), < >, 'example 2 matches'; + is-deeply min-index-sum(, ), ('A',), 'example 3 matches'; + + done-testing; +} + +multi MAIN() { + put min-index-sum(|$*IN.lines[0, 1].map({ .words.Array })); +} diff --git a/challenge-208/feng-chang/raku/ch-2.raku b/challenge-208/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..5d85033739 --- /dev/null +++ b/challenge-208/feng-chang/raku/ch-2.raku @@ -0,0 +1,6 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +put "Duplicate: { @N.Bag.grep({ .value > 1 }).Hash.keys || 'null' } and ", + "Missing: { ((@N[0] .. +@N) (-) @N».Int) || 'null' }"; -- cgit From 87fc99e47cc0c15093aaf397f59cc66ca6013f4d Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Tue, 14 Mar 2023 11:48:16 +0100 Subject: Solve 208: Minimum Index Sum & Duplicate and Missing by E. Choroba --- challenge-208/e-choroba/perl/ch-1.pl | 33 +++++++++++++++++++++++++++++++ challenge-208/e-choroba/perl/ch-2.pl | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100755 challenge-208/e-choroba/perl/ch-1.pl create mode 100755 challenge-208/e-choroba/perl/ch-2.pl diff --git a/challenge-208/e-choroba/perl/ch-1.pl b/challenge-208/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..411d501fc2 --- /dev/null +++ b/challenge-208/e-choroba/perl/ch-1.pl @@ -0,0 +1,33 @@ +#! /usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub minimum_index_sum($list1, $list2) { + my %index; + $index{ $list2->[$_] } //= $_ for 0 .. $#$list2; + + my $minimum = ($#$list2 + $#$list1 + 1); + my @indices; + for my $i (0 .. $#$list1) { + if (exists $index{ $list1->[$i] }) { + my $sum = $index{ $list1->[$i] } + $i; + if ($sum <= $minimum) { + @indices = () if $sum < $minimum; + $minimum = $sum; + push @indices, $i; + } + } + } + return [@$list1[@indices]] +} + +use Test2::V0; +plan 3; + +is minimum_index_sum([qw[ Perl Raku Love]], [qw[ Raku Perl Hate ]]), + [qw[ Perl Raku ]], 'Example 1'; + +is minimum_index_sum([qw[ A B C ]], [qw[ D E F ]]), [], 'Example 2'; + +is minimum_index_sum([qw[ A B C ]], [qw[ C A B ]]), ['A'], 'Example 3'; diff --git a/challenge-208/e-choroba/perl/ch-2.pl b/challenge-208/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..fb60b95c76 --- /dev/null +++ b/challenge-208/e-choroba/perl/ch-2.pl @@ -0,0 +1,38 @@ +#! /usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub duplicate_and_missing(@nums) { + my %seen; + my $duplicate; + my $missing; + for my $n (@nums) { + if (exists $seen{$n}) { + return [-1] if 1 < $seen{$n}; # Triplicate. + + $duplicate = $n; + } + ++$seen{$n}; + } + return [-1] unless defined $duplicate; + + return [-1] + if $seen{ + $missing = $duplicate + (exists $seen{ $duplicate + 1 } ? -1 : 1) + }; + + return [$duplicate, $missing] +} + +use Test2::V0; +plan 3 + 4; + +is duplicate_and_missing(1, 2, 2, 4), [2, 3], 'Example 1'; +is duplicate_and_missing(1, 2, 3, 4), [-1], 'Example 2'; +is duplicate_and_missing(1, 2, 3, 3), [3, 4], 'Example 3'; + +is duplicate_and_missing(1, 2, 2, 3), [-1], 'Duplicate but not missing'; +is duplicate_and_missing(1, 2, 4, 5), [-1], 'Missing but not duplicate'; +is duplicate_and_missing(2, 2, 3, 4), [2, 1], 'Missing at the beginning'; +is duplicate_and_missing(2, 3, 3, 3), [-1], 'Triplicate'; -- cgit From bf995619b6b34ca5b440bb1d7013e87ac6e14e16 Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Tue, 14 Mar 2023 19:16:27 +0800 Subject: Challenge 201, Raku solutions --- challenge-201/feng-chang/raku/ch-1.raku | 5 ++++ challenge-201/feng-chang/raku/ch-2.raku | 46 +++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100755 challenge-201/feng-chang/raku/ch-1.raku create mode 100755 challenge-201/feng-chang/raku/ch-2.raku diff --git a/challenge-201/feng-chang/raku/ch-1.raku b/challenge-201/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..03195cb99d --- /dev/null +++ b/challenge-201/feng-chang/raku/ch-1.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(*@N where @N.all ~~ UInt); + +put ((0..+@N) (-) @N».UInt).keys.sort; diff --git a/challenge-201/feng-chang/raku/ch-2.raku b/challenge-201/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..daf9522db9 --- /dev/null +++ b/challenge-201/feng-chang/raku/ch-2.raku @@ -0,0 +1,46 @@ +#!/bin/env raku + +proto penny-piles(UInt:D $n, Int:D $lower-bound where $lower-bound > 0 = 1) { * } + +multi penny-piles($n) { penny-piles($n, 1) } + +multi penny-piles($n, $m where $m > $n) { [] } +multi penny-piles($n, $m where $m == $n) { [[$n],] } + +multi penny-piles($n, $lower-bound) { + my @a; + + for $lower-bound .. $n-1 -> $i { + @a.push(|penny-piles($n - $i, $i).map({ [ $i, |$_ ] })); + } + + @a.push([$n]); + + @a +} + +multi MAIN('test') { + use Test; + + is-deeply penny-piles(1, 1), [[1],], 'penny-piles(1, 1) => [[1],]'; + is-deeply penny-piles(2, 2), [[2],], 'penny-piles(2, 2) => [[2],]'; + is-deeply penny-piles(5, 5), [[5],], 'penny-piles(5, 5) => [[5],]'; + + is-deeply penny-piles(1), [[1],], 'penny-piles(1) => [[1],]'; + + is-deeply penny-piles(2, 1), [[1, 1], [2]], 'penny-piles(2, 1) => [[1, 1], [2]]'; + is-deeply penny-piles(2), [[1, 1], [2]], 'penny-piles(2) => [[1, 1], [2]]'; + + is-deeply penny-piles(3, 1), [[1, 1, 1], [1, 2], [3]], 'penny-piles(3, 1) => [[1, 1, 1], [1, 2], [3]]'; + is-deeply penny-piles(3), [[1, 1, 1], [1, 2], [3]], 'penny-piles(3) => [[1, 1, 1], [1, 2], [3]]'; + + is-deeply penny-piles(4), [[1, 1, 1, 1], [1, 1, 2], [1, 3], [2, 2], [4]], 'penny-piles(4) => [[1, 1, 1, 1], [1, 1, 2], [1, 3], [2, 2], [4]]'; + is-deeply penny-piles(5), [[1, 1, 1, 1, 1], [1, 1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 4], [2, 3], [5]], + 'penny-piles(5) => [[1, 1, 1, 1, 1], [1, 1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 4], [2, 3], [5]]'; + + done-testing; +} + +multi MAIN(UInt:D $n) { + put penny-piles($n).elems; +} -- cgit From 4895c63c67294452dcf9fa4e8f3f47e4a3f1c39f Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 14 Mar 2023 16:22:28 +0000 Subject: Add Perl solution --- challenge-208/paulo-custodio/Makefile | 2 + challenge-208/paulo-custodio/README | 1 + challenge-208/paulo-custodio/perl/ch-1.pl | 74 ++++++++++++++++++++++++++++++ challenge-208/paulo-custodio/perl/ch-2.pl | 57 +++++++++++++++++++++++ challenge-208/paulo-custodio/t/test-1.yaml | 15 ++++++ challenge-208/paulo-custodio/t/test-2.yaml | 15 ++++++ 6 files changed, 164 insertions(+) create mode 100644 challenge-208/paulo-custodio/Makefile create mode 100644 challenge-208/paulo-custodio/README create mode 100644 challenge-208/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-208/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-208/paulo-custodio/t/test-1.yaml create mode 100644 challenge-208/paulo-custodio/t/test-2.yaml diff --git a/challenge-208/paulo-custodio/Makefile b/challenge-208/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-208/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-208/paulo-custodio/README b/challenge-208/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-208/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-208/paulo-custodio/perl/ch-1.pl b/challenge-208/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..c11b91e5f4 --- /dev/null +++ b/challenge-208/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,74 @@ +#!/usr/bin/perl + +# Challenge 208 +# +# Task 1: Minimum Index Sum +# Submitted by: Mohammad S Anwar +# +# You are given two arrays of strings. +# +# Write a script to find out all common strings in the given two arrays with +# minimum index sum. If no common strings found returns an empty list. +# Example 1 +# +# Input: @list1 = ("Perl", "Raku", "Love") +# @list2 = ("Raku", "Perl", "Hate") +# +# Output: ("Perl", "Raku") +# +# There are two common strings "Perl" and "Raku". +# Index sum of "Perl": 0 + 1 = 1 +# Index sum of "Raku": 1 + 0 = 1 +# +# Example 2 +# +# Input: @list1 = ("A", "B", "C") +# @list2 = ("D", "E", "F") +# +# Output: () +# +# No common string found, so no result. +# +# Example 3 +# +# Input: @list1 = ("A", "B", "C") +# @list2 = ("C", "A", "B") +# +# Output: ("A") +# +# There are three common strings "A", "B" and "C". +# Index sum of "A": 0 + 1 = 1 +# Index sum of "B": 1 + 2 = 3 +# Index sum of "C": 2 + 0 = 2 + +use Modern::Perl; + +sub common_strings { + my($list1, $list2) = @_; + my @list1 = @$list1; + my @list2 = @$list2; + my $min_index = @list1 + @list2; + my @common; + + for my $i (0 .. $#list1) { + for my $j (0 .. $#list2) { + if ($list1[$i] eq $list2[$j]) { + if ($i+$j < $min_index) { + $min_index = $i+$j; + @common = $list1[$i]; + } + elsif ($i+$j == $min_index) { + push @common, $list1[$i]; + } + } + } + } + + return @common; +} + +my @list1 = @ARGV[0..@ARGV/2-1]; +my @list2 = @ARGV[@ARGV/2..$#ARGV]; + +my @common = common_strings(\@list1, \@list2); +say @common ? "@common" : "()"; diff --git a/challenge-208/paulo-custodio/perl/ch-2.pl b/challenge-208/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..527c589197 --- /dev/null +++ b/challenge-208/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl + +# Challenge 208 +# +# Task 2: Duplicate and Missing +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers in sequence with one missing and one duplicate. +# +# Write a script to find the duplicate and missing integer in the given array. +# Return -1 if none found. +# +# For the sake of this task, let us assume the array contains no more than one +# duplicate and missing. +# +# Example 1: +# +# Input: @nums = (1,2,2,4) +# Output: (2,3) +# +# Duplicate is 2 and Missing is 3. +# +# Example 2: +# +# Input: @nums = (1,2,3,4) +# Output: -1 +# +# No duplicate and missing found. +# +# Example 3: +# +# Input: @nums = (1,2,3,3) +# Output: (3,4) +# +# Duplicate is 3 and Missing is 4. + +use Modern::Perl; + +sub dup_and_missing { + my(@in) = @_; + my @dup; + my @missing; + my %in; $in{$_}++ for @in; + for (1 .. @in) { + push @dup, $_ if exists $in{$_} && $in{$_}>1; + push @missing, $_ if !exists $in{$_}; + } + if (@dup+@missing == 0) { + return -1; + } + else { + return (@dup, @missing); + } +} + +my @in = @ARGV; +say join(" ", dup_and_missing(@in)); diff --git a/challenge-208/paulo-custodio/t/test-1.yaml b/challenge-208/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..4295422433 --- /dev/null +++ b/challenge-208/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: Perl Raku Love Raku Perl Hate + input: + output: Perl Raku +- setup: + cleanup: + args: A B C D E F + input: + output: () +- setup: + cleanup: + args: A B C C A B + input: + output: A diff --git a/challenge-208/paulo-custodio/t/test-2.yaml b/challenge-208/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..e6d696824c --- /dev/null +++ b/challenge-208/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 2 2 4 + input: + output: 2 3 +- setup: + cleanup: + args: 1 2 3 4 + input: + output: -1 +- setup: + cleanup: + args: 1 2 3 3 + input: + output: 3 4 -- cgit From f8fccce87c49ef9d9e5da19d23e4c8549ff024c9 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Tue, 14 Mar 2023 18:52:38 +0100 Subject: Challenge 208 LK Perl Python Bash --- challenge-208/lubos-kolouch/bash/ch-2.sh | 30 +++++++++++++++ challenge-208/lubos-kolouch/perl/ch-1.pl | 60 ++++++++++++++++++++++++++++++ challenge-208/lubos-kolouch/perl/ch-2.pl | 41 ++++++++++++++++++++ challenge-208/lubos-kolouch/python/ch-1.py | 58 +++++++++++++++++++++++++++++ challenge-208/lubos-kolouch/python/ch-2.py | 52 ++++++++++++++++++++++++++ 5 files changed, 241 insertions(+) create mode 100644 challenge-208/lubos-kolouch/bash/ch-2.sh create mode 100644 challenge-208/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-208/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-208/lubos-kolouch/python/ch-1.py create mode 100644 challenge-208/lubos-kolouch/python/ch-2.py diff --git a/challenge-208/lubos-kolouch/bash/ch-2.sh b/challenge-208/lubos-kolouch/bash/ch-2.sh new file mode 100644 index 0000000000..dcf3b2fe89 --- /dev/null +++ b/challenge-208/lubos-kolouch/bash/ch-2.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# Define the input array +nums=(1 2 2 4) + +# Find the duplicate and missing integers +n=${#nums[@]} +missing=0 +duplicate=0 +for (( i=0; i 0 )); then + nums[index-1]=-${nums[index-1]} + else + duplicate=$index + fi +done +for (( i=0; i 0 )); then + missing=$((i+1)) + fi +done + +# Print the results +if (( missing != 0 && duplicate != 0 )); then + echo "Duplicate is $duplicate and missing is $missing." +else + echo "-1" +fi + diff --git a/challenge-208/lubos-kolouch/perl/ch-1.pl b/challenge-208/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..be8ede0ef2 --- /dev/null +++ b/challenge-208/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More; + +sub find_common_strings { + my ($list1_ref, $list2_ref) = @_; + + my @list1 = @$list1_ref; + my @list2 = @$list2_ref; + + my %common; + my $min_index_sum = scalar(@list1) + scalar(@list2); + + for (my $i = 0; $i < scalar(@list1); $i++) { + for (my $j = 0; $j < scalar(@list2); $j++) { + if ($list1[$i] eq $list2[$j]) { + my $index_sum = $i + $j; + if ($index_sum <= $min_index_sum) { + $min_index_sum = $index_sum; + $common{$list1[$i]} = $index_sum; + } + } + } + } + + if (scalar(keys %common) == 0) { + return (); + } else { + my @result; + foreach my $key (keys %common) { + if ($common{$key} == $min_index_sum) { + push(@result, $key); + } + } + return @result; + } +} + +# Example 1 +my @list1_1 = ("Perl", "Raku", "Love"); +my @list2_1 = ("Raku", "Perl", "Hate"); +my @common_1 = sort find_common_strings(\@list1_1, \@list2_1); +is_deeply(\@common_1, ["Raku"], "Example 1"); + +# Example 2 +my @list1_2 = ("A", "B", "C"); +my @list2_2 = ("D", "E", "F"); +my @common_2 = find_common_strings(\@list1_2, \@list2_2); +is_deeply(\@common_2, [], "Example 2"); + +# Example 3 +my @list1_3 = ("A", "B", "C"); +my @list2_3 = ("C", "A", "B"); +my @common_3 = find_common_strings(\@list1_3, \@list2_3); +is_deeply(\@common_3, ["A"], "Example 3"); + +done_testing(); + diff --git a/challenge-208/lubos-kolouch/perl/ch-2.pl b/challenge-208/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..d88f83245d --- /dev/null +++ b/challenge-208/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,41 @@ +use strict; +use warnings; +use Test::More; + +sub find_missing_and_duplicate { + my @nums = @_; + my %count; + my $missing = 0; + my $duplicate = 0; + foreach my $num (@nums) { + $count{$num}++; + if ($count{$num} > 1) { + $duplicate = $num; + } + } + for (my $i = 1; $i <= scalar(@nums) + 1; $i++) { + if (!$count{$i}) { + $missing = $i; + last; + } + } + if ($missing && $duplicate) { + return [$duplicate, $missing]; + } else { + return -1; + } +} + +# Run the tests + +# Test 1: find_missing_and_duplicate returns the correct output for input (1,2,2,4) +is_deeply(find_missing_and_duplicate(1,2,2,4), [2,3], "Test 1 passed"); + +# Test 2: find_missing_and_duplicate returns the correct output for input (1,2,3,4) +is(find_missing_and_duplicate(1,2,3,4), -1, "Test 2 passed"); + +# Test 3: find_missing_and_duplicate returns the correct output for input (1,2,3,3) +is_deeply(find_missing_and_duplicate(1,2,3,3), [3,4], "Test 3 passed"); + +done_testing(); + diff --git a/challenge-208/lubos-kolouch/python/ch-1.py b/challenge-208/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..f29f86e8a7 --- /dev/null +++ b/challenge-208/lubos-kolouch/python/ch-1.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def find_common_strings(list1: List[str], list2: List[str]) -> List[str]: + """ + Finds all common strings in the given two arrays with minimum index sum. + If no common strings are found, returns an empty list. + Args: + list1: The first list of strings + list2: The second list of strings + Returns: + A list of common strings with minimum index sum, or an empty list if no common strings are found. + """ + common = {} + min_index_sum = len(list1) + len(list2) + for i, elem1 in enumerate(list1): + for j, elem2 in enumerate(list2): + if elem1 == elem2: + index_sum = i + j + if index_sum <= min_index_sum: + min_index_sum = index_sum + common[list1[i]] = index_sum + + result = [] + for key in common.keys(): + if common[key] == min_index_sum: + result.append(key) + + return result + + +# Tests + + +def test_find_common_strings(): + list1_1 = ["Perl", "Raku", "Love"] + list2_1 = ["Raku", "Perl", "Hate"] + common_1 = find_common_strings(list1_1, list2_1) + assert common_1 == ["Perl", "Raku"] + + list1_2 = ["A", "B", "C"] + list2_2 = ["D", "E", "F"] + common_2 = find_common_strings(list1_2, list2_2) + assert common_2 == [] + + list1_3 = ["A", "B", "C"] + list2_3 = ["C", "A", "B"] + common_3 = find_common_strings(list1_3, list2_3) + assert common_3 == ["A"] + + print("All tests pass") + + +if __name__ == "__main__": + test_find_common_strings() diff --git a/challenge-208/lubos-kolouch/python/ch-2.py b/challenge-208/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..38f78d4ba9 --- /dev/null +++ b/challenge-208/lubos-kolouch/python/ch-2.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List, Tuple, Union + + +def find_missing_and_duplicate(nums: List[int]) -> Union[Tuple[int, int], int]: + """ + Finds the duplicate and missing integer in a given sequence of integers. + + Args: + nums (List[int]): A list of integers with one missing and one duplicate. + + Returns: + Union[Tuple[int, int], int]: If both a missing and duplicate integer are found, returns a tuple + containing the duplicate integer followed by the missing integer. If none are found, returns -1. + + Example: + >>> find_missing_and_duplicate([1, 2, 2, 4]) + (2, 3) + >>> find_missing_and_duplicate([1, 2, 3, 4]) + -1 + >>> find_missing_and_duplicate([1, 2, 3, 3]) + (3, 4) + """ + count = {} + missing = 0 + duplicate = 0 + for num in nums: + count[num] = count.get(num, 0) + 1 + if count[num] > 1: + duplicate = num + for i in range(1, len(nums) + 2): + if i not in count: + missing = i + break + if missing and duplicate: + return (duplicate, missing) + else: + return -1 + + +# Run the tests + + +def test_find_missing_and_duplicate(): + assert find_missing_and_duplicate([1, 2, 2, 4]) == (2, 3) + assert find_missing_and_duplicate([1, 2, 3, 4]) == -1 + assert find_missing_and_duplicate([1, 2, 3, 3]) == (3, 4) + + +test_find_missing_and_duplicate() -- cgit From 3d81eb4fc51a3963c8e032602f0434fbd00d5408 Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Tue, 14 Mar 2023 19:58:25 +0100 Subject: Add solution for week 208 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-208/jeanluc2020/blog-1.txt | 1 + challenge-208/jeanluc2020/blog-2.txt | 1 + challenge-208/jeanluc2020/perl/ch-1.pl | 107 +++++++++++++++++++++++++++++++++ challenge-208/jeanluc2020/perl/ch-2.pl | 70 +++++++++++++++++++++ 4 files changed, 179 insertions(+) create mode 100644 challenge-208/jeanluc2020/blog-1.txt create mode 100644 challenge-208/jeanluc2020/blog-2.txt create mode 100755 challenge-208/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-208/jeanluc2020/perl/ch-2.pl diff --git a/challenge-208/jeanluc2020/blog-1.txt b/challenge-208/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..fb31fe351f --- /dev/null +++ b/challenge-208/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-208-1.html diff --git a/challenge-208/jeanluc2020/blog-2.txt b/challenge-208/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..020e78afd7 --- /dev/null +++ b/challenge-208/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-208-2.html diff --git a/challenge-208/jeanluc2020/perl/ch-1.pl b/challenge-208/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..6328e21a4b --- /dev/null +++ b/challenge-208/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,107 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-208/#TASK1 +# +# Task 1: Minimum Index Sum +# ========================= +# +# You are given two arrays of strings. +# +# Write a script to find out all common strings in the given two arrays with minimum index sum. If no common strings found returns an empty list. +# +## Example 1 +## +## Input: @list1 = ("Perl", "Raku", "Love") +## @list2 = ("Raku", "Perl", "Hate") +## +## Output: ("Perl", "Raku") +## +## There are two common strings "Perl" and "Raku". +## Index sum of "Perl": 0 + 1 = 1 +## Index sum of "Raku": 1 + 0 = 1 +# +## Example 2 +## +## Input: @list1 = ("A", "B", "C") +## @list2 = ("D", "E", "F") +## +## Output: () +## +## No common string found, so no result. +# +## Example 3 +## +## Input: @list1 = ("A", "B", "C") +## @list2 = ("C", "A", "B") +## +## Output: ("A") +## +## There are three common strings "A", "B" and "C". +## Index sum of "A": 0 + 1 = 1 +## Index sum of "B": 1 + 2 = 3 +## Index sum of "C": 2 + 0 = 2 +# +############################################################ +## +## discussion +## +############################################################ +# +# We have to find the index for every string in both arrays. +# Then we find the index sum for each string. +# Then we find the minimum value for the sums. +# Then we output every string that has this minimum value as its sum. + +use strict; +use warnings; +use List::Util qw(min); + +index_sum( ["Perl", "Raku", "Love"], ["Raku", "Perl", "Hate"] ); +index_sum( ["A", "B", "C"], ["D", "E", "F"] ); +index_sum( ["A", "B", "C"], ["C", "A", "B"] ); + +sub index_sum { + my ($l1, $l2) = @_; + my @list1 = @$l1; + my @list2 = @$l2; + my @result = (); + my $index_data = {}; + print "Input: (" . join(", ", @list1) . "); (" . join(", ", @list2) . ")\n"; + # find the index for every string in list1 + foreach my $i (0..$#list1) { + my $value = $list1[$i]; + $index_data->{$value}->{"list1_index"} = $i; + } + # find the index for every string in list2 + foreach my $j (0..$#list2) { + my $value = $list2[$j]; + $index_data->{$value}->{"list2_index"} = $j; + } + # for each found string, if it exists in both lists, calculate the sum + my @sums = (); + foreach my $v (keys %$index_data) { + if(defined($index_data->{$v}->{"list1_index"}) + && defined($index_data->{$v}->{"list2_index"})) { + $index_data->{$v}->{"sum"} = + $index_data->{$v}->{"list1_index"} + + $index_data->{$v}->{"list2_index"}; + push @sums, $index_data->{$v}->{"sum"}; + } + } + # calculate the minimum sum + my $minimum = min(@sums); + # find all values that share the minimum index sum + foreach my $v (keys %$index_data) { + if(defined($index_data->{$v}->{"sum"})) { + if($index_data->{$v}->{"sum"} == $minimum) { + push @result, $v; + } + } + } + # in order to avoid a random sort order of the results we sort by + # their index in list1 + print "Output: (" . join(", ", + sort { + $index_data->{ $a }->{"list1_index"} <=> + $index_data->{ $b }->{"list1_index"} } + @result) . ")\n"; +} diff --git a/challenge-208/jeanluc2020/perl/ch-2.pl b/challenge-208/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..00eda8c66f --- /dev/null +++ b/challenge-208/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,70 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-208/#TASK2 +# +# Task 2: Duplicate and Missing +# ============================= +# +# You are given an array of integers in sequence with one missing and one duplicate. +# +# Write a script to find the duplicate and missing integer in the given array. Return -1 if none found. +# +# For the sake of this task, let us assume the array contains no more than one duplicate and missing. +# +## Example 1: +## +## Input: @nums = (1,2,2,4) +## Output: (2,3) +## +## Duplicate is 2 and Missing is 3. +# +## Example 2: +## +## Input: @nums = (1,2,3,4) +## Output: -1 +## +## No duplicate and missing found. +# +## Example 3: +## +## Input: @nums = (1,2,3,3) +## Output: (3,4) +## +## Duplicate is 3 and Missing is 4. +# +############################################################ +## +## discussion +## +############################################################ +# +# Walk the array, keep track of the previous element, and +# check where we're at with the current one. + +use strict; +use warnings; + +duplicate_and_missing(1,2,2,4); +duplicate_and_missing(1,2,3,4); +duplicate_and_missing(1,2,3,3); + +sub duplicate_and_missing { + my @nums = @_; + my $duplicate; + my $missing; + print "Input: (" . join(",", @nums) . ")\n"; + my $last_element = shift @nums; + foreach my $element (@nums) { + if($element == $last_element) { + $duplicate = $element; + $missing = $element+1; + } elsif ($element > $last_element+1) { + $missing = $last_element+1; + } + $last_element = $element; + } + if(defined($duplicate) && defined($missing)) { + print "Output: ($duplicate, $missing)\n"; + } else { + print "Output: -1\n"; + } +} -- cgit From 4eb4fceee746cca3ce78e163eb61961b42805adc Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 14 Mar 2023 20:34:06 +0000 Subject: Challenge 208 Solutions (Raku) --- challenge-208/mark-anderson/raku/ch-2.raku | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/challenge-208/mark-anderson/raku/ch-2.raku b/challenge-208/mark-anderson/raku/ch-2.raku index b7cf810067..36f56f72e2 100644 --- a/challenge-208/mark-anderson/raku/ch-2.raku +++ b/challenge-208/mark-anderson/raku/ch-2.raku @@ -1,5 +1,4 @@ #!/usr/bin/env raku -use Algorithm::Diff; use Test; is-deeply duplicate-and-missing(1,2,2,4), (2,3); @@ -7,13 +6,17 @@ is-deeply duplicate-and-missing(1,2,3,4), -1; is-deeply duplicate-and-missing(1,2,3,3), (3,4); is-deeply duplicate-and-missing(4,5,6,7,7,8), (7,9); is-deeply duplicate-and-missing(4,5,6,7,7,9), (7,8); +is-deeply duplicate-and-missing(4,5,6,6,7,9), (6,8); +is-deeply duplicate-and-missing(4,6,6,7,8,9), (6,5); -# Algorithm::Diff -is-deeply diff-module(1,2,2,4), ("2", "3"); -is-deeply diff-module(1,2,3,4), (Nil, Nil); -is-deeply diff-module(1,2,3,3), ("3", "4"); -is-deeply diff-module(4,5,6,7,7,8), ("7", "9"); -is-deeply diff-module(4,5,6,7,7,9), ("7", "8"); +use Algorithm::Diff; +is-deeply diff-module(1,2,2,4), (2,3); +is-deeply diff-module(1,2,3,4), -1; +is-deeply diff-module(1,2,3,3), (3,4); +is-deeply diff-module(4,5,6,7,7,8), (7,9); +is-deeply diff-module(4,5,6,7,7,9), (7,8); +is-deeply diff-module(4,5,6,6,7,9), (6,8); +is-deeply diff-module(4,6,6,7,8,9), (6,5); sub duplicate-and-missing(*@nums) { @@ -27,5 +30,6 @@ sub duplicate-and-missing(*@nums) sub diff-module(*@nums) { my @range = .head .. .head + .end given @nums; - diff(@nums, @range).comb(/\d+/)[1,3] + my $r = diff(@nums, @range).comb(/\d+/)[1,3]; + $r.all.defined ?? $r>>.Int !! -1 } -- cgit From b58a2bd4fcf64591ab9e8db0d1901228e561c441 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Tue, 14 Mar 2023 18:42:14 -0400 Subject: Week 208 --- challenge-208/zapwai/perl/ch-1.pl | 29 +++++++++++++++++++++++++++++ challenge-208/zapwai/perl/ch-2.pl | 21 +++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 challenge-208/zapwai/perl/ch-1.pl create mode 100644 challenge-208/zapwai/perl/ch-2.pl diff --git a/challenge-208/zapwai/perl/ch-1.pl b/challenge-208/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..1285580b25 --- /dev/null +++ b/challenge-208/zapwai/perl/ch-1.pl @@ -0,0 +1,29 @@ +use v5.30.0; +my @list1 = ("Perl", "Raku", "Love"); +my @list2 = ("Raku", "Perl", "Hate"); + +#my @list1 = ("A", "B", "C"); +#my @list2 = ("C", "A", "B"); + +#my @list1 = ("A", "B", "C"); +#my @list2 = ("D", "e", "f"); + +my (@match, @ind1, @ind2); +for my $i (0 .. $#list1) { + for my $j (0 .. $#list2) { + if ($list1[$i] eq $list2[$j]) { + push @match, $list1[$i]; + push @ind1, $i; + push @ind2, $j; + } + } +} + +say "Input: \@list1 = (".join(", ",@list1).")"; +say "\t\@list2 = (".join(", ",@list2).")"; +say "Output: (".join(", ",@match).")"; + +say "There are ".($#match + 1)." common strings: @match"; +for my $i (0 .. $#match) { + say "Index sum of " . $match[$i] . ": $ind1[$i] + $ind2[$i] = ". ($ind1[$i] + $ind2[$i]); +} diff --git a/challenge-208/zapwai/perl/ch-2.pl b/challenge-208/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..e418495b78 --- /dev/null +++ b/challenge-208/zapwai/perl/ch-2.pl @@ -0,0 +1,21 @@ +use v5.30.0; +my @nums = (1, 2, 2, 4); +#my @nums = (1,2,3,3); +#my @nums = (1,2,3,4); +say "Input: \@nums = (".join(",",@nums).")"; +print "Output: "; +my $catch; # flag for printing -1 +for (1 .. $#nums) { + my $i = $nums[$_ - 1]; + my $j = $nums[$_]; + if ($j - $i == 2) { + say "(".($i+1).",".($i+2).")"; + $catch = 1; + last; + } elsif ($j - $i == 0) { + say "($i,".($i+1).")"; + $catch = 1; + last; + } +} +say "-1" unless ($catch); -- cgit From 8e5c206909f60833316f94fd95e600e73850e983 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 14 Mar 2023 23:13:18 +0000 Subject: Challenge 208 Solutions (Raku) --- challenge-208/mark-anderson/raku/ch-2.raku | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/challenge-208/mark-anderson/raku/ch-2.raku b/challenge-208/mark-anderson/raku/ch-2.raku index 36f56f72e2..fa193e2cd0 100644 --- a/challenge-208/mark-anderson/raku/ch-2.raku +++ b/challenge-208/mark-anderson/raku/ch-2.raku @@ -8,15 +8,7 @@ is-deeply duplicate-and-missing(4,5,6,7,7,8), (7,9); is-deeply duplicate-and-missing(4,5,6,7,7,9), (7,8); is-deeply duplicate-and-missing(4,5,6,6,7,9), (6,8); is-deeply duplicate-and-missing(4,6,6,7,8,9), (6,5); - -use Algorithm::Diff; -is-deeply diff-module(1,2,2,4), (2,3); -is-deeply diff-module(1,2,3,4), -1; -is-deeply diff-module(1,2,3,3), (3,4); -is-deeply diff-module(4,5,6,7,7,8), (7,9); -is-deeply diff-module(4,5,6,7,7,9), (7,8); -is-deeply diff-module(4,5,6,6,7,9), (6,8); -is-deeply diff-module(4,6,6,7,8,9), (6,5); +is-deeply duplicate-and-missing(3,6,5,6,7,8), (6,4); sub duplicate-and-missing(*@nums) { @@ -26,10 +18,3 @@ sub duplicate-and-missing(*@nums) flat .repeated, keys @range (-) $_ or -1 } } - -sub diff-module(*@nums) -{ - my @range = .head .. .head + .end given @nums; - my $r = diff(@nums, @range).comb(/\d+/)[1,3]; - $r.all.defined ?? $r>>.Int !! -1 -} -- cgit From f7ec28790793e89b81f22c0355e96e41403195ab Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 14 Mar 2023 23:36:11 +0000 Subject: Challenge 208 Solutions (Raku) --- challenge-208/mark-anderson/raku/ch-2.raku | 1 + 1 file changed, 1 insertion(+) diff --git a/challenge-208/mark-anderson/raku/ch-2.raku b/challenge-208/mark-anderson/raku/ch-2.raku index fa193e2cd0..c2c8ff76ec 100644 --- a/challenge-208/mark-anderson/raku/ch-2.raku +++ b/challenge-208/mark-anderson/raku/ch-2.raku @@ -9,6 +9,7 @@ is-deeply duplicate-and-missing(4,5,6,7,7,9), (7,8); is-deeply duplicate-and-missing(4,5,6,6,7,9), (6,8); is-deeply duplicate-and-missing(4,6,6,7,8,9), (6,5); is-deeply duplicate-and-missing(3,6,5,6,7,8), (6,4); +is-deeply duplicate-and-missing(3,4,5,6,7,3), (3,8); sub duplicate-and-missing(*@nums) { -- cgit From be86ff7f425ddc7f2c569a8ca3ec1d0e276e39a8 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Wed, 15 Mar 2023 11:08:02 +0000 Subject: Challenge 208 Solutions (Raku) --- challenge-208/mark-anderson/raku/ch-1.raku | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/challenge-208/mark-anderson/raku/ch-1.raku b/challenge-208/mark-anderson/raku/ch-1.raku index f3f080ea2e..d1318f5ac3 100644 --- a/challenge-208/mark-anderson/raku/ch-1.raku +++ b/challenge-208/mark-anderson/raku/ch-1.raku @@ -7,6 +7,23 @@ is-deeply min-index-sum(, ), (), "Example 2"; is-deeply min-index-sum(, ), ("A",), "Example 3"; +is-deeply min-index-sum(, + , + , + , + , + , + ), ("B", "D"), "7 X 5"; + +is-deeply min-index-sum(, + , + , + , + , + , + , + ), ("H",), "Uneven"; + is-deeply min-index-sum(, , , @@ -16,7 +33,8 @@ is-deeply min-index-sum(, , , , - ), ("V",), "10 X 25"; + ), ("P",), "10 X 25"; + is-deeply min-index-sum(, , @@ -42,11 +60,11 @@ is-deeply min-index-sum(, , , , - ), ("D", "E", "W"), "25 X 25"; + ), ("P",), "25 X 25"; sub min-index-sum(+$a) { my $m := $a>>.antipairs>>.Map; - my $k := ([(&)] $m).keys; + my $k := ([(&)] $m>>.keys).keys; ($k Z=> [Z+] $m>>{$k}).Map.minpairs.Map.keys.sort } -- cgit From 8694ea6ad1d60bf3cf7b149ec1256a303eb3db12 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Wed, 15 Mar 2023 11:13:11 +0000 Subject: Challenge 208 Solutions (Raku) --- challenge-208/mark-anderson/raku/ch-1.raku | 1 - 1 file changed, 1 deletion(-) diff --git a/challenge-208/mark-anderson/raku/ch-1.raku b/challenge-208/mark-anderson/raku/ch-1.raku index d1318f5ac3..3cfea57dc3 100644 --- a/challenge-208/mark-anderson/raku/ch-1.raku +++ b/challenge-208/mark-anderson/raku/ch-1.raku @@ -35,7 +35,6 @@ is-deeply min-index-sum(, , ), ("P",), "10 X 25"; - is-deeply min-index-sum(, , , -- cgit From 08e970df3bcaba7829f31f9de952c4a469966554 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Wed, 15 Mar 2023 13:39:19 +0100 Subject: PWC-208: Perl, Python, Ruby, Go --- challenge-208/spadacciniweb/go/ch-1.go | 70 ++++++++++++++++++++++++++++ challenge-208/spadacciniweb/go/ch-2.go | 70 ++++++++++++++++++++++++++++ challenge-208/spadacciniweb/perl/ch-1.pl | 74 ++++++++++++++++++++++++++++++ challenge-208/spadacciniweb/perl/ch-2.pl | 53 +++++++++++++++++++++ challenge-208/spadacciniweb/python/ch-1.py | 56 ++++++++++++++++++++++ challenge-208/spadacciniweb/python/ch-2.py | 40 ++++++++++++++++ challenge-208/spadacciniweb/ruby/ch-1.rb | 61 ++++++++++++++++++++++++ challenge-208/spadacciniweb/ruby/ch-2.rb | 46 +++++++++++++++++++ 8 files changed, 470 insertions(+) create mode 100644 challenge-208/spadacciniweb/go/ch-1.go create mode 100644 challenge-208/spadacciniweb/go/ch-2.go create mode 100644 challenge-208/spadacciniweb/perl/ch-1.pl create mode 100644 challenge-208/spadacciniweb/perl/ch-2.pl create mode 100644 challenge-208/spadacciniweb/python/ch-1.py create mode 100644 challenge-208/spadacciniweb/python/ch-2.py create mode 100644 challenge-208/spadacciniweb/ruby/ch-1.rb create mode 100644 challenge-208/spadacciniweb/ruby/ch-2.rb diff --git a/challenge-208/spadacciniweb/go/ch-1.go b/challenge-208/spadacciniweb/go/ch-1.go new file mode 100644 index 0000000000..65fc88e734 --- /dev/null +++ b/challenge-208/spadacciniweb/go/ch-1.go @@ -0,0 +1,70 @@ +/* +# Task 1: Minimum Index Sum +# Submitted by: Mohammad S Anwar +# +# You are given two arrays of strings. +# Write a script to find out all common strings in the given two arrays with minimum index sum. If no common strings found returns an empty list. +# +# Example 1 +# Input: @list1 = ("Perl", "Raku", "Love") +# @list2 = ("Raku", "Perl", "Hate") +# Output: ("Perl", "Raku") +# +# There are two common strings "Perl" and "Raku". +# Index sum of "Perl": 0 + 1 = 1 +# Index sum of "Raku": 1 + 0 = 1 +# +# Example 2 +# Input: @list1 = ("A", "B", "C") +# @list2 = ("D", "E", "F") +# Output: () +# +# No common string found, so no result. +# +# Example 3 +# Input: @list1 = ("A", "B", "C") +# @list2 = ("C", "A", "B") +# Output: ("A") +# +# There are three common strings "A", "B" and "C". +# Index sum of "A": 0 + 1 = 1 +# Index sum of "B": 1 + 2 = 3 +# Index sum of "C": 2 + 0 = 2 +*/ + +package main + +import ( + "fmt" +) + +func get_minimum_index_sum(list1 []string, list2 []string) []string { + words := make([]string, 0) + var value int = -1 + + for i := 0; i <= len(list1)-1; i++ { + for j := 0; j <= len(list2)-1; j++ { + if value != -1 && value < i + j { + break + } + if list1[i] == list2[j] { + if value == -1 || value > i + j { + words = nil + value = i + j + } + words = append(words, list1[i]) + } + } + } + return words +} + +func main() { + fmt.Println(get_minimum_index_sum([]string {"Perl", "Raku", "Love"}, + []string {"Raku", "Perl", "Hate"} ) ) + fmt.Println(get_minimum_index_sum([]string {"A", "B", "C"}, + []string {"D", "E", "F"} ) ) + fmt.Println(get_minimum_index_sum([]string {"A", "B", "C"}, + []string {"C", "A", "B"} ) ) +} + diff --git a/challenge-208/spadacciniweb/go/ch-2.go b/challenge-208/spadacciniweb/go/ch-2.go new file mode 100644 index 0000000000..c393bff392 --- /dev/null +++ b/challenge-208/spadacciniweb/go/ch-2.go @@ -0,0 +1,70 @@ +/* +# Task 2: Duplicate and Missing +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers in sequence with one missing and one duplicate. +# Write a script to find the duplicate and missing integer in the given array. Return -1 if none found. +# For the sake of this task, let us assume the array contains no more than one duplicate and missing. +# +# Example 1: +# Input: @nums = (1,2,2,4) +# Output: (2,3) +# Duplicate is 2 and Missing is 3. +# +# Example 2: +# Input: @nums = (1,2,3,4) +# Output: -1 +# No duplicate and missing found. +# +# Example 3: +# Input: @nums = (1,2,3,3) +# Output: (3,4) +# Duplicate is 3 and Missing is 4. +*/ + +package main + +import ( + "fmt" + "sort" +) + +func skip_1_value(list1 []int, index int) []int { + skip_1_value := make([]int, 0) + for i, _ := range list1 { + if i == index { + continue + } + skip_1_value = append(skip_1_value, list1[i]) + } + return skip_1_value +} + +func duplicate_and_missing(list1 []int) []int { + dupl_and_miss := make([]int, 0) + + for i, val := range list1 { + numbers := skip_1_value(list1, i) + pos := sort.SearchInts(numbers, val) + if pos < len(numbers) && numbers[pos] == val { // found + dupl_and_miss = append(dupl_and_miss, val) + break + } + } + for i := 0; i <= len(list1)-1; i++ { + val := list1[0] + i + pos := sort.SearchInts(list1, val) + if !(pos < len(list1) && list1[pos] == val) { // not found + dupl_and_miss = append(dupl_and_miss, val) + } + } + return dupl_and_miss +} + +func main() { + fmt.Println(duplicate_and_missing([]int {1,2,2,4} ) ) + fmt.Println(duplicate_and_missing([]int {1,2,3,4} ) ) + fmt.Println(duplicate_and_missing([]int {1,2,