diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2024-03-18 13:26:14 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-18 13:26:14 -0400 |
| commit | 4e3f2749e5e6a4c4e9c53ffd5a3dde78204efb0e (patch) | |
| tree | 2e2743718c62dd4698210bb4b03617d0a8d96963 | |
| parent | 9e7e80939c8a0ae12ace548d7b9efb43b32ec4c3 (diff) | |
| parent | b91a4623cd4c2b51f779f59b53310a0bd745f03f (diff) | |
| download | perlweeklychallenge-club-4e3f2749e5e6a4c4e9c53ffd5a3dde78204efb0e.tar.gz perlweeklychallenge-club-4e3f2749e5e6a4c4e9c53ffd5a3dde78204efb0e.tar.bz2 perlweeklychallenge-club-4e3f2749e5e6a4c4e9c53ffd5a3dde78204efb0e.zip | |
Merge branch 'manwar:master' into master
63 files changed, 3754 insertions, 2620 deletions
diff --git a/challenge-236/peter-meszaros/perl/ch-1.pl b/challenge-236/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..345d5eeb55 --- /dev/null +++ b/challenge-236/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,88 @@ +#!/usr/bin/env perl +# +# You are asked to sell juice each costs $5. You are given an array of bills. +# You can only sell ONE juice to each customer but make sure you return exact +# change back. You only have $5, $10 and $20 notes. You do not have any change in +# hand at first. +# +# Write a script to find out if it is possible to sell to each customers with +# correct change. +# Example 1 +# +# Input: @bills = (5, 5, 5, 10, 20) +# Output: true +# +# From the first 3 customers, we collect three $5 bills in order. +# From the fourth customer, we collect a $10 bill and give back a $5. +# From the fifth customer, we give a $10 bill and a $5 bill. +# Since all customers got correct change, we output true. +# +# Example 2 +# +# Input: @bills = (5, 5, 10, 10, 20) +# Output: false +# +# From the first two customers in order, we collect two $5 bills. +# For the next two customers in order, we collect a $10 bill and give back a $5 bill. +# For the last customer, we can not give the change of $15 back because we only +# have two $10 bills. +# Since not every customer received the correct change, the answer is false. +# +# Example 3 +# +# Input: @bills = (5, 5, 5, 20) +# Output: true +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [5, 5, 5, 10, 20], + [5, 5, 10, 10, 20], + [5, 5, 5, 20], +]; + +sub exact_change +{ + my $bill = shift; + + my %change = ( + 5 => 0, + 10 => 0, + 20 => 0, + ); + my $ret = 1; + for my $bill (@$bill) { + ++$change{$bill}; + if ($bill == 10) { + if ($change{5}) { + --$change{5}; + } else { + $ret = 0; + last; + } + } elsif ($bill == 20) { + if ($change{5} && $change{10}) { + --$change{5}; + --$change{10}; + } elsif ($change{5} >= 3) { + $change{5} -= 3; + } else { + $ret = 0; + last; + } + } + } + + return $ret; +} + +is(exact_change($cases->[0]), 1, 'Example 1'); +is(exact_change($cases->[1]), 0, 'Example 2'); +is(exact_change($cases->[2]), 1, 'Example 3'); +done_testing(); + +exit 0; diff --git a/challenge-236/peter-meszaros/perl/ch-2.pl b/challenge-236/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..d258a37fbc --- /dev/null +++ b/challenge-236/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,85 @@ +#!/usr/bin/env perl +# +# You are given an array of unique integers. +# +# Write a script to determine how many loops are in the given array. +# +# To determine a loop: Start at an index and take the number at # +# array[index] and then proceed to that index and continue this until you end up +# at the starting index. +# +# +# Example 1 +# +# Input: @ints = (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10) +# Output: 3 +# +# To determine the 1st loop, start at index 0, the number at that index is 4, +# proceed to index 4, the number at that index is 15, proceed to index 15 and so +# on until you're back at index 0. +# +# Loops are as below: +# [4 15 1 6 13 5 0] +# [3 8 7 18 9 16 12 17 2] +# [14 11 19 10] +# +# Example 2 +# +# Input: @ints = (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19) +# Output: 6 +# +# Loops are as below: +# [0] +# [1] +# [13 9 14 17 18 15 5 8 2] +# [7 11 4 6 10 16 3] +# [12] +# [19] +# +# Example 3 +# +# Input: @ints = (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17) +# Output: 1 +# +# Loop is as below: +# [9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0] +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + #0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + [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, 19], + [9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, 1, 0, 15, 6, 17], +]; + +sub array_loops +{ + my $l = shift; + + my $nloop = 0; + my %visited; + for my $start (0..$#$l) { + next if defined $visited{$start}; + my $next = $l->[$start]; + while ($next != $start || not defined $visited{$next}) { + ++$visited{$next}; + $next = $l->[$next]; + } + ++$nloop if $next == $start; + } + + return $nloop; +} + +is(array_loops($cases->[0]), 3, 'Example 1'); +is(array_loops($cases->[1]), 6, 'Example 2'); +is(array_loops($cases->[2]), 1, 'Example 3'); +done_testing(); + +exit 0; + diff --git a/challenge-260/feng-chang/raku/ch-1.raku b/challenge-260/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..5a4a5a375b --- /dev/null +++ b/challenge-260/feng-chang/raku/ch-1.raku @@ -0,0 +1,6 @@ +#!/bin/env raku + +unit sub MAIN(*@ints); + +my @a = @ints.Bag.values; +put +(+@a.unique == +@a); diff --git a/challenge-260/feng-chang/raku/ch-2.raku b/challenge-260/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..12823d8afe --- /dev/null +++ b/challenge-260/feng-chang/raku/ch-2.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(Str:D $w); + +put $w.comb.permutationsĀ».join.unique.sort.first($w, :k) + 1; diff --git a/challenge-260/feng-chang/raku/test.raku b/challenge-260/feng-chang/raku/test.raku new file mode 100755 index 0000000000..49b576a879 --- /dev/null +++ b/challenge-260/feng-chang/raku/test.raku @@ -0,0 +1,24 @@ +#!/bin/env raku + +# The Weekly Challenge 260 +use Test; + +sub pwc-test(Str:D $script, Bool :$deeply? = False, *@input) { + my ($expect, $assertion) = @input.splice(*-2, 2); + my $p = run $script, |@input, :out; + if $deeply { + is-deeply $p.out.slurp(:close).chomp.words.Bag, $expect, $assertion; + } else { + is $p.out.slurp(:close).chomp, $expect, $assertion; + } +} + +# Task 1, Unique Occurrences +pwc-test './ch-1.raku', <1 2 2 1 1 3>, 1, 'Unique Occurrences: (1,2,2,1,1,3) => 1'; +pwc-test './ch-1.raku', <1 2 3>, 0, 'Unique Occurrences: (1,2,3) => 0'; +pwc-test './ch-1.raku', <-- -2 0 1 -2 1 1 0 1 -2 9>, 1, 'Unique Occurrences: (-2,0,1,-2,1,1,0,1,-2,9) => 1'; + +# Task 2, Dictionary Rank +pwc-test './ch-2.raku', 'CAT', 3, 'Dictionary Rank: CAT => 3'; +pwc-test './ch-2.raku', 'GOOGLE', 88, 'Dictionary Rank: GOOGLE => 88'; +pwc-test './ch-2.raku', 'SECRET', 255, 'Dictionary Rank: SECRET => 255'; diff --git a/challenge-261/ash/raku/ch-1.raku b/challenge-261/ash/raku/ch-1.raku new file mode 100644 index 0000000000..1c6a73c9ac --- /dev/null +++ b/challenge-261/ash/raku/ch-1.raku @@ -0,0 +1,19 @@ +# Solution to the Task 1 of The Weekly Challenge 261 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-261/#TASK1 + +# Test run: +# $ raku ch-1.raku +# 36 +# 9 +# 0 + +my @tests = + (1, 2, 3, 45), + (1, 12, 3), + (1, 2, 3, 4); + +for @tests -> @test { + my $sum = [+] @test; + my $dig = [+]((@test.map: *.comb).flat); + say ($dig - $sum).abs; +} diff --git a/challenge-261/ash/raku/ch-2.raku b/challenge-261/ash/raku/ch-2.raku new file mode 100644 index 0000000000..6864850b42 --- /dev/null +++ b/challenge-261/ash/raku/ch-2.raku @@ -0,0 +1,20 @@ +# Solution to the Task 2 of The Weekly Challenge 261 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-261/#TASK2 + +# Test run: +# $ raku ch-2.raku +# 24 +# 8 +# 2 + +my @tests = + ((5,3,6,1,12), 3), + ((1,2,4,3), 1), + ((5,6,7), 2); + +for @tests -> (@ints, $start is copy) { + my $ints = @ints.Bag; + + $start *= 2 while $ints{$start}; + say $start; +} diff --git a/challenge-261/e-choroba/perl/ch-1.pl b/challenge-261/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..7d99202d5e --- /dev/null +++ b/challenge-261/e-choroba/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::Util qw{ sum }; + +sub element_digit_sum(@ints) { + abs(sum(@ints) - sum(map { split // } @ints)) +} + +use Test::More tests => 4; + +is element_digit_sum(1, 2, 3, 45), 36, 'Example 1'; +is element_digit_sum(1, 12, 3), 9, 'Example 2'; +is element_digit_sum(1, 2, 3, 4), 0, 'Example 3'; +is element_digit_sum(236, 416, 336, 350), 1296, 'Example 4'; diff --git a/challenge-261/e-choroba/perl/ch-2.pl b/challenge-261/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..2cca2ac3f6 --- /dev/null +++ b/challenge-261/e-choroba/perl/ch-2.pl @@ -0,0 +1,15 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub multiple_by_two($start, @ints) { + $start *= 2 while grep $_ == $start, @ints; + return $start +} + +use Test::More tests => 3; + +is multiple_by_two(3, 5, 3, 6, 1, 12), 24, 'Example 1'; +is multiple_by_two(1, 1, 2, 4, 3), 8, 'Example 2'; +is multiple_by_two(2, 5, 6, 7), 2, 'Example 3'; diff --git a/challenge-261/eric-cheung/python/ch-1.py b/challenge-261/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..348279526f --- /dev/null +++ b/challenge-261/eric-cheung/python/ch-1.py @@ -0,0 +1,13 @@ +
+## arrInt = [1, 2, 3, 45] ## Example 1
+## arrInt = [1, 12, 3] ## Example 2
+## arrInt = [1, 2, 3, 4] ## Example 3
+arrInt = [236, 416, 336, 350] ## Example 4
+
+arrSplit = []
+for nLoop in arrInt:
+ arrSplit = arrSplit + [int(elem) for elem in str(nLoop)]
+
+## print (sum(arrInt))
+## print (sum(arrSplit))
+print (abs(sum(arrInt) - sum(arrSplit)))
diff --git a/challenge-261/eric-cheung/python/ch-2.py b/challenge-261/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..a275000b69 --- /dev/null +++ b/challenge-261/eric-cheung/python/ch-2.py @@ -0,0 +1,18 @@ +
+## Example 1
+## arrInt = [5, 3, 6, 1, 12]
+## nStart = 3
+
+## Example 2
+## arrInt = [1, 2, 4, 3]
+## nStart = 1
+
+## Example 3
+arrInt = [5, 6, 7]
+nStart = 2
+
+nFinal = nStart
+while nFinal in arrInt:
+ nFinal = nFinal * 2
+
+print (nFinal)
diff --git a/challenge-261/feng-chang/raku/ch-1.raku b/challenge-261/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..b913f2f35c --- /dev/null +++ b/challenge-261/feng-chang/raku/ch-1.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(*@ints); + +put @ints.sum - @intsĀ».combĀ».sum.sum; diff --git a/challenge-261/feng-chang/raku/ch-2.raku b/challenge-261/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..fbb2ac591f --- /dev/null +++ b/challenge-261/feng-chang/raku/ch-2.raku @@ -0,0 +1,7 @@ +#!/bin/env raku + +unit sub MAIN(*@ints); + +my $start = @ints.pop; +$start *= 2 while @ints.grep($start); +put $start; diff --git a/challenge-261/feng-chang/raku/test.raku b/challenge-261/feng-chang/raku/test.raku new file mode 100755 index 0000000000..d78a96d7db --- /dev/null +++ b/challenge-261/feng-chang/raku/test.raku @@ -0,0 +1,25 @@ +#!/bin/env raku + +# The Weekly Challenge 261 +use Test; + +sub pwc-test(Str:D $script, Bool :$deeply? = False, *@input) { + my ($expect, $assertion) = @input.splice(*-2, 2); + my $p = run $script, |@input, :out; + if $deeply { + is-deeply $p.out.slurp(:close).chomp.words.Bag, $expect, $assertion; + } else { + is $p.out.slurp(:close).chomp, $expect, $assertion; + } +} + +# Task 1, Element Digit Sum +pwc-test './ch-1.raku', <1 2 3 45>, 36, 'Element Digit Sum: (1,2,3,45) => 36'; +pwc-test './ch-1.raku', <1 12 3>, 9, 'Element Digit Sum: (1,12,3) => 9'; +pwc-test './ch-1.raku', <1 2 3 4>, 0, 'Element Digit Sum: (1,2,3,4) => 0'; +pwc-test './ch-1.raku', <236 416 336 350>, 1296, 'Element Digit Sum: (236, 416, 336, 350) => 1296'; + +# Task 2, Multiply by Two +pwc-test './ch-2.raku', <5 3 6 1 12>, 3, 24, 'Multiply by Two: @ints=(5,3,6,1,12), $start=3 => 24'; +pwc-test './ch-2.raku', <1 2 4 3>, 1, 8, 'Multiply by Two: @ints=(1,2,4,3), $start=1 => 8'; +pwc-test './ch-2.raku', <5 6 7>, 2, 2, 'Multiply by Two: @ints=(5,6,7), $start=2 => 2'; diff --git a/challenge-261/mark-anderson/blog-2.md b/challenge-261/mark-anderson/blog-2.md deleted file mode 100644 index 55b5699cf5..0000000000 --- a/challenge-261/mark-anderson/blog-2.md +++ /dev/null @@ -1,132 +0,0 @@ -# Weekly Challenge #260 - -### Task 2: Dictionary Rank -**Submitted by: Mark Anderson** - -You are given a word, ```$word```. - -Write a script to compute the dictionary rank of the given word. - -#### Example 1 -``` -Input: $word = 'CAT' -Output: 3 - -All possible combinations of the letters: -CAT, CTA, ATC, TCA, ACT, TAC - -Arrange them in alphabetical order: -ACT, ATC, CAT, CTA, TAC, TCA - -CAT is the 3rd in the list. -Therefore the dictionary rank of CAT is 3. -``` - -#### Example 2 -``` -Input: $word = 'GOOGLE' -Output: 88 -``` - -#### Example 3 -``` -Input: $word = 'SECRET' -Output: 255 -``` - ---- - -### Solution - -One approach is to create all permutations, sort them, and find the index of ```$word```. - -This is fine for short words but there's a better solution for long words. - -There are numerous videos on youtube explaining the algorithm - I think this a good one [https://www.youtube.com/watch?v=-MpL0X3AHAs](https://www.youtube.com/watch?v=-MpL0X3AHAs) - -Here's the gist of the algorithm: - -1. Find the rank of each letter. - - ``` - G O O G L E - 1 3 3 1 2 0 - ``` - -2. For each letter, find the number of letters to its right that have a lower rank. - - ``` - G O O G L E - 1 3 3 1 1 0 - ``` - -3. For each letter, take the number of repeating letters from that letter to the end of the string. - Take the factorial of each result and multiply them together. - - ``` - G O O G L E - 2!*2! 2! 1! 1! 1! 1! - ``` - - Starting with the first letter, there are 2 Gs and 2 Os so we end up with 2!*2!. - The next letter is O. From that letter to the end of the string there is just the repeating O so we end up with 2! - and so on. - -4. Take the terms from step 2 and divide them by the terms from step 3. - - ``` - G O O G L E - 1/4 3/2 3/1 1/1 1/1 0/1 - ``` - -5. Create the sequence ```$word```.end...0 and take the factorial of each term. - - ``` - G O O G L E - 5! 4! 3! 2! 1! 0! - ``` - -6. Multipy the terms from step 4 with the terms from step 5. - - ``` - G O O G L E - 120/4 (3*24)/2 3*6 1*2 1*1 0*1 - ``` - -7. Sum the terms from step 6 and add 1 for a result of 88. - ``` - G O O G L E - 30 + 36 + 18 + 2 + 1 + 0 + 1 = 88 - ``` -### My translation to Raku: - -``` -#!/usr/bin/env raku -use experimental :cached; - -say rank('google'); - -sub postfix:<!>($n) is cached { [*] 1..$n } - -sub rank($word) -{ - my @w = $word.comb; - my @ranks = @w.sort.squish.antipairs.Map{@w}; - my $bag = @ranks.BagHash; - - my @n = gather for @ranks -> $r - { - my @less-than = $bag.keys.grep(* < $r); - take ([+] $bag{@less-than}) / ([*] $bag.values>>!); - $bag{$r}-- - } - - 1 + [+] @n Z* (@ranks.end...0)>>! -} -``` - -[The full program.](https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-260/mark-anderson/raku/ch-2.raku) - -Thank you for reading my solution to the [Weekly Challenge #260 Task #2.](https://theweeklychallenge.org/blog/perl-weekly-challenge-260/) - -*-Mark Anderson* diff --git a/challenge-261/mark-anderson/raku/ch-1.raku b/challenge-261/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..c1144ce45b --- /dev/null +++ b/challenge-261/mark-anderson/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!/usr/bin/env raku +use Test; + +is element-digit-sum(1,2,3,45), |
