diff options
Diffstat (limited to 'challenge-285')
100 files changed, 5100 insertions, 52 deletions
diff --git a/challenge-285/0rir/raku/ch-1.raku b/challenge-285/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..4842962ddf --- /dev/null +++ b/challenge-285/0rir/raku/ch-1.raku @@ -0,0 +1,63 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +285-1: No Connection Submitted by: Mohammad Sajid Anwar + +You are given a list of routes, @routes. Write a script to find the destination with no further outgoing connection. +Example 1 +Input: @routes = (["B","C"], ["D","B"], ["C","A"]) +Output: "A" + +"D" -> "B" -> "C" -> "A". +"B" -> "C" -> "A". +"C" -> "A". +"A". +Example 2 +Input: @routes = (["A","Z"]) +Output: "Z" + +=end comment + +=begin comment +The routes are not bidirectional or 'D' would be an answer in the +first example. +It is not clear that route lists are constrained to two elements. +But the question suggests that any intersection of paths defines +a end-point for all routes intersecting. +=end comment + +my @Test = + (<B C>, <D B>, <C A>), ("A"), + (<A Z>,), ("Z"), + (<A a b c Z>,), ("Z"), + (<A D>, <B C>, <D B>, <C A>), (), + (< A>, < A>), (), + (<A Z>, <Z A>), (), + (<A Z>, <B Z>), "Z", + (<A Z>, <B C>), <Z C>.sort, + (<A B>, <B C>, <B D>, <B E>, <E F>), + <C D F>.sort, + ; + +plan @Test ÷ 2; + +only task( List:D $a --> List) { + my @head = @$a.map( *.[0]); + my @tail = @$a.map( *.[*-1]); + (@tail ∖ @head).keys.List; +} + +for @Test -> $in, $exp { + is task($in).sort, $exp, "$exp <- $in.raku()"; +} + +done-testing; + +my @route = (["B","C"], ["D","D"], ["D","E"], ["F","G"]); + +say "\nInput: @routes = @route.raku()\nOutput: { task @route }"; + + diff --git a/challenge-285/0rir/raku/ch-2.raku b/challenge-285/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..fbb946f0bf --- /dev/null +++ b/challenge-285/0rir/raku/ch-2.raku @@ -0,0 +1,95 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +285-2: Making Change Submitted by: David Ferrone + +Compute the number of ways to make change for given amount in cents. By using the coins e.g. Penny, Nickel, Dime, Quarter and Half-dollar, in how many distinct ways can the total value equal to the given amount? Order of coin selection does not matter. + +A penny (P) is equal to 1 cent. +A nickel (N) is equal to 5 cents. +A dime (D) is equal to 10 cents. +A quarter (Q) is equal to 25 cents. +A half-dollar (HD) is equal to 50 cents. +Example 1 +Input: $amount = 9 +Output: 2 + +1: 9P +2: N + 4P +Example 2 +Input: $amount = 15 +Output: 6 + +1: D + 5P +2: D + N +3: 3N +4: 2N + 5P +5: N + 10P +6: 15P +Example 3 +Input: $amount = 100 +Output: 292 + +=end comment + +my @Test = + 0,0, $(1..4),1, $(5..9),2, + #$(10..14),4, $(15..19),6, $(20..24),9, + $(25..29),13, $(30..34),18, $(35..39),24, + #$(40..43),31, $(45..47),39, $(50..51),50, + $(55),62, $(60),77, $(69),93, + #$(70),112, $(75),134, $(84),159, + $(85),187, $(90),218, $(99),252, + #$(100),292, $(105),335, $(111),384, + #$(120),494, $(130),628, $(141),786, + #$(150),972, $(160),1190, $(171),1440, + #$(180),1730, $(190),2060, + 201,2435, + #211,2860, + #300,9590, + #504,59576, +; +plan 32; + +constant \P = 1; +constant \N = 5; +constant \D = 10; +constant \Q = 25; +constant \H = 50; + +multi task( UInt:D $n is copy --> UInt) { + return 0 if $n == 0; + return 1 if $n < 5; + $n -= $n % 5; # odd P change doesn't matter + my @P = 0, 5, 10 … $n; # P must be subbed as batch + my @N = 0, 5, 10 … $n; + my @D = 0, 10, 20 … $n - $n % D; + my @Q = 0, 25, 50 … $n - $n % Q; + my @H = 0, 50, 100 … $n - $n % H; + my $ret; + for @H -> \h { + for @Q -> \qu { # avoid q quoting construct + for @D -> \d { + for @N -> \n { + for @P -> \p { + if $n == sum p, n, d, qu, h { + ++ $ret; + last; + } } } } } } + return $ret; +} + +for @Test -> $in, $exp { + for @$in -> \n { + my $a = task( n); + is $a, $exp, "$exp <- { n }"; + } +} + +done-testing; + +my $amount = 180; +say "\nInput: \$amount = $amount\nOutput: { task $amount }"; diff --git a/challenge-285/arne-sommer/blog.txt b/challenge-285/arne-sommer/blog.txt new file mode 100644 index 0000000000..563ffcf0db --- /dev/null +++ b/challenge-285/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/no-connection.html diff --git a/challenge-285/arne-sommer/raku/ch-1.raku b/challenge-285/arne-sommer/raku/ch-1.raku new file mode 100755 index 0000000000..d55e3a25d0 --- /dev/null +++ b/challenge-285/arne-sommer/raku/ch-1.raku @@ -0,0 +1,25 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@routes where @routes.elems > 0, :v(:$verbose)); + +my %outgoing; + +for @routes -> $route +{ + my ($from, $to) = $route.split(/\s+/); + + say ": Route from '$from' to '$to'" if $verbose; + + %outgoing{$from}++; + %outgoing{$to} = 0 unless defined %outgoing{$to}; +} + +if $verbose +{ + for sort keys %outgoing -> $key + { + say ": '$key' has %outgoing{$key} outgoing connectiuon(s)" if $verbose; + } +} + +say %outgoing.grep( *.value == 0 )>>.key.join(", "); diff --git a/challenge-285/arne-sommer/raku/ch-2.raku b/challenge-285/arne-sommer/raku/ch-2.raku new file mode 100755 index 0000000000..d99bfd95c5 --- /dev/null +++ b/challenge-285/arne-sommer/raku/ch-2.raku @@ -0,0 +1,68 @@ +#! /usr/bin/env raku + +unit sub MAIN (UInt $amount, :v(:$verbose)); + +my %value; + +%value<penny> = 1; +%value<nickel> = 5; +%value<dime> = 10; +%value<quarter> = 25; +%value<half-dollar> = 50; + +my $ok = 0; + +for 0 .. $amount -> $penny +{ + my $sum-p = $penny * %value<penny>; + + for 0 .. $amount -> $nickel + { + my $sum-pn = $sum-p + $nickel * %value<nickel>; + + last if $sum-pn > $amount; + + for 0 .. $amount -> $dime + { + my $sum-pnd = $sum-pn + $dime * %value<dime>; + + last if $sum-pnd > $amount; + + for 0 .. $amount -> $quarter + { + my $sum-pndq = $sum-pnd + $quarter * %value<quarter>; + + last if $sum-pndq > $amount; + + for 0 .. $amount -> $half-dollar + { + my $sum = $sum-pndq + $half-dollar * %value<half-dollar>; + + last if $sum > $amount; + + if $sum == $amount + { + $ok++; + say ": " ~ + ( + prettyish($penny, 'P'), + prettyish($nickel, 'N'), + prettyish($dime, 'D'), + prettyish($quarter, 'Q'), + prettyish($half-dollar, 'HD') + ).grep( *.defined ).join(" + ") if $verbose; + } + } + } + } |
