diff options
Diffstat (limited to 'challenge-261')
| -rw-r--r-- | challenge-261/ash/raku/ch-1.raku | 19 | ||||
| -rw-r--r-- | challenge-261/ash/raku/ch-2.raku | 20 | ||||
| -rwxr-xr-x | challenge-261/feng-chang/raku/ch-1.raku | 5 | ||||
| -rwxr-xr-x | challenge-261/feng-chang/raku/ch-2.raku | 7 | ||||
| -rwxr-xr-x | challenge-261/feng-chang/raku/test.raku | 25 | ||||
| -rw-r--r-- | challenge-261/mark-anderson/blog-2.md | 132 | ||||
| -rw-r--r-- | challenge-261/mark-anderson/raku/ch-1.raku | 12 | ||||
| -rw-r--r-- | challenge-261/mark-anderson/raku/ch-2.raku | 10 | ||||
| -rwxr-xr-x | challenge-261/peter-meszaros/perl/ch-1.pl | 74 | ||||
| -rwxr-xr-x | challenge-261/peter-meszaros/perl/ch-2.pl | 72 |
10 files changed, 244 insertions, 132 deletions
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/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), 36; +is element-digit-sum(1,12,3), 9; +is element-digit-sum(1,2,3,4), 0; +is element-digit-sum(236,416,336,350), 1296; + +sub element-digit-sum(+@ints) +{ + abs ([+] @ints) - [+] @ints.comb(/<digit>/) +} diff --git a/challenge-261/mark-anderson/raku/ch-2.raku b/challenge-261/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..8254214c62 --- /dev/null +++ b/challenge-261/mark-anderson/raku/ch-2.raku @@ -0,0 +1,10 @@ +#!/usr/bin/env raku +use Test; + +is multiply-by-two([5,3,6,1,12], 3), 24; +is multiply-by-two([1,2,4,3], 1), 8; +is multiply-by-two([5,6,7], 2), 2; + +multi multiply-by-two(@i, $s) { samewith(@i.BagHash, $s) } +multi multiply-by-two($b, $s) { return $s } +multi multiply-by-two($b, $s where ?$b{$s}) { $b{(0..$s)}:delete; samewith($b, $s*2) } diff --git a/challenge-261/peter-meszaros/perl/ch-1.pl b/challenge-261/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..976027a7f6 --- /dev/null +++ b/challenge-261/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,74 @@ +#!/usr/bin/env perl +# +# +# You are given an array of integers, @ints. +# +# Write a script to evaluate the absolute difference between element and digit +# sum of the given array. +# Example 1 +# +# Input: @ints = (1,2,3,45) +# Output: 36 +# +# Element Sum: 1 + 2 + 3 + 45 = 51 +# Digit Sum: 1 + 2 + 3 + 4 + 5 = 15 +# Absolute Difference: | 51 - 15 | = 36 +# +# Example 2 +# +# Input: @ints = (1,12,3) +# Output: 9 +# +# Element Sum: 1 + 12 + 3 = 16 +# Digit Sum: 1 + 1 + 2 + 3 = 7 +# Absolute Difference: | 16 - 7 | = 9 +# +# Example 3 +# +# Input: @ints = (1,2,3,4) +# Output: 0 +# +# Element Sum: 1 + 2 + 3 + 4 = 10 +# Digit Sum: 1 + 2 + 3 + 4 = 10 +# Absolute Difference: | 10 - 10 | = 0 +# +# Example 4 +# +# Input: @ints = (236, 416, 336, 350) +# Output: 1296 +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [1, 2, 3, 45], + [1, 12, 3], + [1, 2, 3, 4], + [236, 416, 336, 350], +]; + +sub element_digit_sum +{ + my $l = shift; + + my $elem_sum = 0; + my $digi_sum = 0; + + for my $e (@$l) { + $elem_sum += $e; + $digi_sum += $_ for split('', $e); + } + + return abs($elem_sum - $digi_sum); +} + +is(element_digit_sum($cases->[0]), 36, 'Example 1'); +is(element_digit_sum($cases->[1]), 9, 'Example 2'); +is(element_digit_sum($cases->[2]), 0, 'Example 3'); +is(element_digit_sum($cases->[3]), 1296, 'Example 4'); +done_testing(); + +exit 0; diff --git a/challenge-261/peter-meszaros/perl/ch-2.pl b/challenge-261/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..1cc344c748 --- /dev/null +++ b/challenge-261/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl +# +# You are given an array of integers, @ints and an integer $start.. +# +# Write a script to do the followings: +# +# a) Look for $start in the array @ints, if found multiply the number by 2 +# b) If not found stop the process otherwise repeat +# +# In the end return the final value. +# Example 1 +# +# Input: @ints = (5,3,6,1,12) and $start = 3 +# Output: 24 +# +# Step 1: 3 is in the array so 3 x 2 = 6 +# Step 2: 6 is in the array so 6 x 2 = 12 +# Step 3: 12 is in the array so 12 x 2 = 24 +# +# 24 is not found in the array so return 24. +# +# Example 2 +# +# Input: @ints = (1,2,4,3) and $start = 1 +# Output: 8 +# +# Step 1: 1 is in the array so 1 x 2 = 2 +# Step 2: 2 is in the array so 2 x 2 = 4 +# Step 3: 4 is in the array so 4 x 2 = 8 +# +# 8 is not found in the array so return 8. +# +# Example 3 +# +# Input: @ints = (5,6,7) and $start = 2 +# Output: 2 +# +# 2 is not found in the array so return 2. +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[5, 3, 6, 1, 12], 3], + [[1, 2, 4, 3], 1], + [[5, 6, 7], 2], +]; + +sub multiply_by_two +{ + my ($l, $s) = $_[0]->@*; + + START: + for my $i (@$l) { + if ($i == $s) { + $s *= 2; + goto START; + } + } + return $s; +} + +is(multiply_by_two($cases->[0]), 24, 'Example 1'); +is(multiply_by_two($cases->[1]), 8, 'Example 2'); +is(multiply_by_two($cases->[2]), 2, 'Example 3'); +done_testing(); + +exit 0; + |
