diff options
| author | Shimon Bollinger <deoac.bollinger@gmail.com> | 2023-10-01 00:01:15 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-01 00:01:15 -0400 |
| commit | 859530783c2761fd736a0370c8cecb066a33ef3f (patch) | |
| tree | fa294377b1ab81d391e47a6662dea7ca293722ed | |
| parent | 50380dd0fb4f1fd53a25e499544f6c74fd7f6f6c (diff) | |
| parent | 3da619757777db47e5c798f19f47ded9c77f65fe (diff) | |
| download | perlweeklychallenge-club-859530783c2761fd736a0370c8cecb066a33ef3f.tar.gz perlweeklychallenge-club-859530783c2761fd736a0370c8cecb066a33ef3f.tar.bz2 perlweeklychallenge-club-859530783c2761fd736a0370c8cecb066a33ef3f.zip | |
Merge pull request #1 from manwar/master
Added Task 1
30 files changed, 1962 insertions, 1488 deletions
diff --git a/challenge-236/avery-adams/blog.txt b/challenge-236/avery-adams/blog.txt new file mode 100644 index 0000000000..f41e0adcb2 --- /dev/null +++ b/challenge-236/avery-adams/blog.txt @@ -0,0 +1 @@ +https://dev.to/oldtechaa/perl-weekly-challenge-236-lemonade-stand-50p5 diff --git a/challenge-236/avery-adams/blog1.txt b/challenge-236/avery-adams/blog1.txt new file mode 100644 index 0000000000..0af9ec5cbe --- /dev/null +++ b/challenge-236/avery-adams/blog1.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/oldtechaa/2023/09/perl-weekly-challenge-236---lemonade-stand.html diff --git a/challenge-236/avery-adams/perl/ch-1.pl b/challenge-236/avery-adams/perl/ch-1.pl new file mode 100644 index 0000000000..55ce240fe5 --- /dev/null +++ b/challenge-236/avery-adams/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl +use v5.36; +use List::Util 'any'; + +my %till; +my $failure; +foreach my $bill (@ARGV) { + if(!any {$bill == $_} (5, 10, 20)) { + say('At least one bill provided is not $5, $10, or $20.') and exit; + } + $till{$bill}++; + if($bill == 20) { + if($till{10} and $till{5}) { + $till{10}--; + $till{5}--; + } elsif($till{5} >= 3) { + $till{5} -= 3; + } else { + $failure = 'false'; + last; + } + } elsif($bill == 10) { + if($till{5}) { + $till{5}--; + } else { + $failure = 'false'; + last; + } + } +} +say(defined($failure) ? $failure : 'true'); diff --git a/challenge-236/bob-lied/blog.txt b/challenge-236/bob-lied/blog.txt index 9a5af4b168..2c28048d35 100644 --- a/challenge-236/bob-lied/blog.txt +++ b/challenge-236/bob-lied/blog.txt @@ -1,2 +1 @@ https://dev.to/boblied/pwc-236-task-1-a-change-would-do-you-good-3ia4 -https://dev.to/boblied/pwc-236-task-2-the-circle-is-small-242g diff --git a/challenge-236/bob-lied/blog1.txt b/challenge-236/bob-lied/blog1.txt new file mode 100644 index 0000000000..6611eb3324 --- /dev/null +++ b/challenge-236/bob-lied/blog1.txt @@ -0,0 +1 @@ +https://dev.to/boblied/pwc-236-task-2-the-circle-is-small-242g diff --git a/challenge-236/deadmarshal/java/Ch1.java b/challenge-236/deadmarshal/java/Ch1.java new file mode 100644 index 0000000000..013b6ac91c --- /dev/null +++ b/challenge-236/deadmarshal/java/Ch1.java @@ -0,0 +1,39 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class Ch1 { + public static void main(String[] args) { + ArrayList<Integer> arr1 = new ArrayList<>(List.of(5,5,5,10,20)); + ArrayList<Integer> arr2 = new ArrayList<>(List.of(5,5,10,10,20)); + ArrayList<Integer> arr3 = new ArrayList<>(List.of(5,5,5,20)); + System.out.println(exact_change(arr1)); + System.out.println(exact_change(arr2)); + System.out.println(exact_change(arr3)); + } + + private static boolean exact_change(List<Integer> list) { + HashMap<Integer,Integer> m = new HashMap<Integer,Integer>(); + for(var e : list) + { + if(e == 10) + { + m.replace(5, m.get(5) - 1); + if(m.get(5) == 0) return false; + } + else if(e == 20) + { + if(m.getOrDefault(5,0) != 0 && + m.getOrDefault(10,0) != 0) + { + m.replace(5,m.get(5)-1); + m.replace(10,m.get(10)-1); + } + else if(m.get(5) > 2) m.replace(5,m.get(5)-3); + else return false; + } + m.put(e,m.getOrDefault(e,0)+1); + } + return true; + } +} diff --git a/challenge-236/deadmarshal/java/Ch2.java b/challenge-236/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..a37ae7edc4 --- /dev/null +++ b/challenge-236/deadmarshal/java/Ch2.java @@ -0,0 +1,36 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class Ch2 { + public static void main(String[] args) { + ArrayList<Integer> arr1 = new ArrayList<>( + List.of(4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10)); + ArrayList<Integer> arr2 = new ArrayList<>( + List.of(0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19)); + ArrayList<Integer> arr3 = new ArrayList<>( + List.of(9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17)); + System.out.println(array_loops(arr1)); + System.out.println(array_loops(arr2)); + System.out.println(array_loops(arr3)); + } + + private static int array_loops(List<Integer> list) { + int count = 0, i = 0; + List<Integer> indices = + new ArrayList<Integer>(Collections.nCopies(list.size(), -1)); + for(; i < list.size(); ++i) + { + if(indices.get(i) == -1) + { + count++; + while(indices.get(i) == -1) + { + indices.set(i,1); + i = list.get(i); + } + } + } + return count; + } +} diff --git a/challenge-236/deadmarshal/python/ch1.py b/challenge-236/deadmarshal/python/ch1.py new file mode 100644 index 0000000000..a5464e8855 --- /dev/null +++ b/challenge-236/deadmarshal/python/ch1.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python + +def exact_change(arr): + h = {} + for i in range(0,len(arr)): + if arr[i] == 10: + h[5] = h.get(5,0) - 1 + if h[5] == 0: return False + elif arr[i] == 20: + if h.get(5,0) != 0 and h.get(10,0) != 0: + h[5] -= 1 + h[10] -= 1 + elif h.get(5,0) > 2: + h[5] -= 3 + else: + return False + h[arr[i]] = h.get(arr[i],0) + 1 + return True + +print(exact_change([5,5,5,10,20])) +print(exact_change([5,5,10,10,20])) +print(exact_change([5,5,5,20])) + diff --git a/challenge-236/deadmarshal/python/ch2.py b/challenge-236/deadmarshal/python/ch2.py new file mode 100644 index 0000000000..f79c2ea21e --- /dev/null +++ b/challenge-236/deadmarshal/python/ch2.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python + +def array_loops(arr): + i,count,indices = 0,0,[-1] * len(arr) + while i < len(arr): + if indices[i] == -1: + count += 1 + while indices[i] == -1: + indices[i] = 1 + i = arr[i] + i += 1 + return count + +print(array_loops([4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10])) +print(array_loops([0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19])) +print(array_loops([9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17])) + diff --git a/challenge-236/deadmarshal/raku/ch-1.raku b/challenge-236/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..ebad8d1999 --- /dev/null +++ b/challenge-236/deadmarshal/raku/ch-1.raku @@ -0,0 +1,22 @@ +#!/usr/bin/env raku + +sub exact-change(@arr) +{ + my %hash; + for @arr { + if $_ == 10 { + return False unless %hash{5}--; + } + elsif $_ == 20 { + %hash{5} && %hash{10} ?? do {--%hash{$_} for 5,10} !! + %hash{5} > 2 ?? (%hash{5} -= 3) !! return False; + } + %hash{$_}++; + } + True +} + +say exact-change([5,5,5,10,20]); +say exact-change([5,5,10,10,20]); +say exact-change([5,5,5,20]); + diff --git a/challenge-236/deadmarshal/raku/ch-2.raku b/challenge-236/deadmarshal/raku/ch-2.raku new file mode 100644 index 0000000000..126d6df530 --- /dev/null +++ b/challenge-236/deadmarshal/raku/ch-2.raku @@ -0,0 +1,23 @@ +#!/usr/bin/env raku + +sub array-loops(@arr) +{ + my ($i,$count) = (0) xx 2; + my @indices = False xx @arr.elems; + while $i < @arr.elems { + if @indices[$i] == False { + $count++; + until @indices[$i] { + @indices[$i] = True; + $i = @arr[$i]; + } + } + $i++; + } + $count +} + +say array-loops([4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10]); +say array-loops([0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19]); +say array-loops([9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17]); + diff --git a/challenge-236/jo-37/perl/ch-1.pl b/challenge-236/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..36ae37769e --- /dev/null +++ b/challenge-236/jo-37/perl/ch-1.pl @@ -0,0 +1,82 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use List::Util qw(min); + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [B...] + +-examples + run the examples from the challenge + +-tests + run some tests + +B... + list $5, $10 and $20 bills +EOS + + +### Input and Output + +say sell_juice(@ARGV) ? 'true' : 'false'; + + +### Implementation + +sub sell_juice { + # At the beginning, the trays for $5, $10 and $20 bills in our cash + # register are empty. Counting $20 bills, but never return them. + (\my %register)->@{5, 10, 20} = (0, 0, 0); + state $price = 5; + + # Try to sell a juice for every given bill. + for my $bill (@_) { + # The amount to be returned. + my $return = $bill - $price; + # There is only one case, where the action is not uniquely + # defined: If we are to return $15, have three or more $5 bills + # and one or more $10 bills. In this situation we give one $10 + # bill and one $5 bill. + for my $tray (10, 5) { + my $cnt = min $register{$tray}, int $return / $tray; + # Take the bill(s) from the tray and adjust the to be + # returned amount. + $register{$tray} -= $cnt; + $return -= $cnt * $tray; + } + # Put the received bill into its tray. + $register{$bill}++; + # Fail if we cannot return the exact change. + return if $return; + } + # Here we succeeded. + 1; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + ok sell_juice(5, 5, 5, 10, 20), 'example 1'; + ok !sell_juice(5, 5, 10, 10, 20), 'example 2'; + ok sell_juice(5, 5, 5, 20), 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + ok sell_juice(5, 5, 5, 5, 10, 20, 10), 'prefer 1x10 + 1x5 over 3x5'; + } + + done_testing; + exit; +} diff --git a/challenge-236/jo-37/perl/ch-2.pl b/challenge-236/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..2002c6db96 --- /dev/null +++ b/challenge-236/jo-37/perl/ch-2.pl @@ -0,0 +1,66 @@ +#!/usr/bin/perl -s + +use v5.25; +use Test2::V0; +use Math::Permutation; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [N...] + +-examples + run the examples from the challenge + +-tests + run some tests + +N... + zero-based permutation of any size in one-line notation + +EOS + + +### Input and Output + +say cycles(@ARGV); + + +### Implementation + +# As the given numbers shall be unique and at the same time be indices +# into an array of the same size, they actually represent a zero-based +# permutation in one-line notation. This task asks for the number of +# cycles. Transforming the numbers into a one-based one-line notation, +# creating a permutation thereof, converting to cycles and counting +# these with the help of CY's Math::Permutation module. + +sub cycles { + scalar Math::Permutation->wrepr([map $_ + 1, @_])->cyc->@*; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is cycles(4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10), + 3, 'example 1'; + is cycles(0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19), + 6, 'example 2'; + is cycles(9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17), + 1, 'example 3'; + + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} diff --git a/challenge-236/robert-dicicco/powershell/ch-2.psl b/challenge-236/robert-dicicco/powershell/ch-2.psl new file mode 100644 index 0000000000..5f16eee5f3 --- /dev/null +++ b/challenge-236/robert-dicicco/powershell/ch-2.psl @@ -0,0 +1,95 @@ +<# +--------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 09-30-2023 +Challenge 236 Task 02 Array Loops ( Powershell ) +--------------------------------------------- +#> + +$myints = @( (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10 ), (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,1), (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17 )) +$global:seen = @() +$global:lps = 0 +function WalkLoop { + param ( + $m, + $s + ) + $outloop = @() + $start = $m[$s] + $v = $m[$start] + if ($v -eq $start) { + $seen += $s + $outloop += $s + $ol = $outloop.Count + if ($ol -eq 1 ) { + write-host "`tLoop: [$outloop]" + $global:lps += 1 + } + return + } else { + $global:seen += $start + $outloop += $start + $global:seen += $v + $outloop += $v + } + while (1) { + $v = $m[$v] + if ($global:seen -contains $v) { + break + } + if ( $v -eq $start) { + break + } else { + $global:seen += $v + $outloop += $v + } + } + #} + $ol = $outloop.Count + if (($ol -gt 2)-or ($ol -eq 1 )) { + write-host "`tLoop: [$outloop]" + $global:lps += 1 + } +} + +foreach ($mints in $myints) { + write-host "Input: [@ints = $mints]" + $global:seen = @() + $global:lps = 0 + $cnt = 0 + while ($cnt -lt 20 ) { + WalkLoop $mints $cnt + $cnt += 1 + } + write-host "`n`tOutput: $global:lps" + write-host "----------------------------------------------------" +} + +<# +SAMPLE OUTPUT +.\ArrayLoops.ps1 +Input: [@ints = 4 6 3 8 15 0 13 18 7 16 14 19 17 5 11 1 12 2 9 10] + Loop: [4 15 1 6 13 5 0] + Loop: [3 8 7 18 9 16 12 17 2] + Loop: [14 11 19 10] + + Output: 3 +---------------------------------------------------- +Input: [@ints = 0 1 13 7 6 8 10 11 2 14 16 4 12 9 17 5 3 18 15 1] + Loop: [0] + Loop: [1] + Loop: [13 9 14 17 18 15 5 8 2] + Loop: [7 11 4 6 10 16 3] + Loop: [12] + Loop: [19] + + Output: 6 +---------------------------------------------------- +Input: [@ints = 9 8 3 11 5 7 13 19 12 4 14 10 18 2 16 1 0 15 6 17] + Loop: [9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0] + + Output: 1 +---------------------------------------------------- +#> + + diff --git a/challenge-236/shimon-ben-avraham/blog.txt b/challenge-236/shimon-ben-avraham/blog.txt index 1ad8d65f01..d673308dc2 100644 --- a/challenge-236/shimon-ben-avraham/blog.txt +++ b/challenge-236/shimon-ben-avraham/blog.txt @@ -1 +1 @@ -https://github.com/manwar/perlweeklychallenge-club/tree/master/challenge-236/shimon-ben-avraham#readme +https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-236/shimon-ben-avraham/raku/ch-2.md diff --git a/stats/pwc-current.json b/stats/pwc-current.json index 1684930010..01127b0980 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,117 +1,7 @@ { - "series" : [ - { - "name" : "The Weekly Challenge - 236", - "data" : [ - { - "name" : "Ali Moradi", - "drilldown" : "Ali Moradi", - "y" : 3 - }, - { - "drilldown" : "Athanasius", - "name" : "Athanasius", - "y" : 4 - }, - { - "y" : 3, - "drilldown" : "Bob Lied", - "name" : "Bob Lied" - }, - { - "y" : 2, - "name" : "Dave Jacoby", - "drilldown" : "Dave Jacoby" - }, - { - "drilldown" : "David Ferrone", - "name" : "David Ferrone", - "y" : 2 - }, - { - "name" : "E. Choroba", - "drilldown" : "E. Choroba", - "y" : 2 - }, - { - "name" : "Jaldhar H. Vyas", - "drilldown" : "Jaldhar H. Vyas", - "y" : 5 - }, - { - "name" : "Laurent Rosenfeld", - "drilldown" : "Laurent Rosenfeld", - "y" : 6 - }, - { - "y" : 5, - "name" : "Lubos Kolouch", - "drilldown" : "Lubos Kolouch" - }, - { - "y" : 8, - "name" : "Luca Ferrari", - "drilldown" : "Luca Ferrari" - }, - { - "y" : 2, - "drilldown" : "Mark Anderson", - "name" : "Mark Anderson" - }, - { - "y" : 2, - "name" : "Niels van Dijke", - "drilldown" : "Niels van Dijke" - }, - { - "drilldown" : "Peter Campbell Smith", - "name" : "Peter Campbell Smith", - "y" : 3 - }, - { - "y" : 2, - "name" : "rcmlz", - "drilldown" : "rcmlz" - }, - { - "drilldown" : "Robert DiCicco", - "name" : "Robert DiCicco", - "y" : 4 - }, - { - "drilldown" : "Roger Bell_West", - "name" : "Roger Bell_West", - "y" : 4 - }, - { - "y" : 2, - "name" : "Shimon Bollinger", - "drilldown" : "Shimon Bollinger" - }, - { - "y" : 4, - "name" : "Thomas Kohler", - "drilldown" : "Thomas Kohler" - }, - { - "y" : 3, - "name" : "W. Luis Mochan", - "drilldown" : "W. Luis Mochan" - }, - { - "name" : "Wanderdoc", - "drilldown" : "Wanderdoc", - "y" : 2 - }, - { - "name" : "Yves Orton", - "drilldown" : "Yves Orton", - "y" : 2 - } - ], - "colorByPoint" : 1 - } - ], + "title" : { + "text" : "The Weekly Challenge - 236" + }, "plotOptions" : { "series" : { "borderWidth" : 0, @@ -121,8 +11,13 @@ } } }, - "xAxis" : { - "type" : "category" + "chart" : { + "type" : "column" + }, + "tooltip" : { + "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", + "followPointer" : 1, + "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>" }, "drilldown" : { "series" : [ @@ -134,6 +29,10 @@ 2 ], [ + "Raku", + 2 + ], + [ "Blog", 1 ] @@ -155,7 +54,20 @@ "id" : "Athanasius" }, { - "id" : "Bob Lied", + "id" : "Avery Adams", + "name" : "Avery Adams", + "data" : [ + [ + "Perl", + 1 + ], + [ + "Blog", + 2 + ] + ] + }, + { "data" : [ [ "Perl", @@ -163,30 +75,31 @@ ], [ "Blog", - 1 + 2 ] ], - "name" : "Bob Lied" + "name" : "Bob Lied", + "id" : "Bob Lied" }, { - "id" : "Dave Jacoby", "name" : "Dave Jacoby", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Dave Jacoby" }, { + "id" : "David Ferrone", "name" : "David Ferrone", "data" : [ [ "Perl", 2 ] - ], - "id" : "David Ferrone" + ] }, { "name" : "E. Choroba", @@ -217,7 +130,18 @@ "id" : "Jaldhar H. Vyas" }, { + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Jorg Sommrey", + "id" : "Jorg Sommrey" + }, + { "id" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld", "data" : [ [ "Perl", @@ -231,11 +155,10 @@ "Blog", 2 ] - ], - "name" : "Laurent Rosenfeld" + ] }, { - "id" : "Lubos Kolouch", + "name" : "Lubos Kolouch", "data" : [ [ "Perl", @@ -250,7 +173,7 @@ 1 ] ], - "name" : "Lubos Kolouch" + "id" : "Lubos Kolouch" }, { "id" : "Luca Ferrari", @@ -268,26 +191,27 @@ }, { "id" : "Mark Anderson", - "name" : "Mark Anderson", "data" : [ [ "Raku", 2 ] - ] + ], + "name" : "Mark Anderson" }, { - "name" : "Niels van Dijke", + "id" : "Niels van Dijke", "data" : [ [ "Perl", 2 ] ], - "id" : "Niels van Dijke" + "name" : "Niels van Dijke" }, { "id" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith", "data" : [ [ "Perl", @@ -297,20 +221,21 @@ "Blog", 1 ] - ], - "name" : "Peter Campbell Smith" + ] }, { - "id" : "rcmlz", + "name" : "rcmlz", "data" : [ [ "Raku", 2 ] ], - "name" : "rcmlz" + "id" : "rcmlz" }, { + "id" : "Robert DiCicco", + "name" : "Robert DiCicco", "data" : [ [ "Perl", @@ -320,11 +245,10 @@ "Raku", 2 ] - ], - "name" : "Robert DiCicco", - "id" : "Robert DiCicco" + ] }, { + "id" : "Roger Bell_West", "data" : [ [ "Perl", @@ -335,12 +259,10 @@ 2 ] ], - "name" : "Roger Bell_West", - "id" : "Roger Bell_West" + "name" : "Roger Bell_West" }, { "id" : "Shimon Bollinger", - "name" : "Shimon Bollinger", "data" : [ [ "Raku", @@ -350,11 +272,10 @@ "Blog", 1 ] - ] + ], + "name" : "Shimon Bollinger" }, { - "id" : "Thomas Kohler", - "name" : "Thomas Kohler", "data" : [ [ "Perl", @@ -364,10 +285,11 @@ "Blog", 2 ] - ] + ], + "name" : "Thomas Kohler", + "id" : "Thomas Kohler" }, |
