diff options
| author | Francis Whittle <code@powered.ninja> | 2019-07-27 15:01:29 +1000 |
|---|---|---|
| committer | Francis Whittle <code@powered.ninja> | 2019-07-28 18:10:42 +1000 |
| commit | 07035dc8beea0e1b7e773a1df41bc18f73f1c4b7 (patch) | |
| tree | 66bd0b1be7fb2cc8086ea5dca2c7e4c62435e581 /challenge-018/fjwhittle | |
| parent | 2e0eafd3724567921841ccf7a1f6deb844adc833 (diff) | |
| download | perlweeklychallenge-club-07035dc8beea0e1b7e773a1df41bc18f73f1c4b7.tar.gz perlweeklychallenge-club-07035dc8beea0e1b7e773a1df41bc18f73f1c4b7.tar.bz2 perlweeklychallenge-club-07035dc8beea0e1b7e773a1df41bc18f73f1c4b7.zip | |
fjwhittle challenge 018 solutions.
Diffstat (limited to 'challenge-018/fjwhittle')
| -rw-r--r-- | challenge-018/fjwhittle/perl6/ch-1.p6 | 23 | ||||
| -rw-r--r-- | challenge-018/fjwhittle/perl6/ch-2.p6 | 42 |
2 files changed, 65 insertions, 0 deletions
diff --git a/challenge-018/fjwhittle/perl6/ch-1.p6 b/challenge-018/fjwhittle/perl6/ch-1.p6 new file mode 100644 index 0000000000..2ae8fdf5ab --- /dev/null +++ b/challenge-018/fjwhittle/perl6/ch-1.p6 @@ -0,0 +1,23 @@ +#!/usr/bin/env perl6 + +sub all-substrings(Str $in) { + gather for (^($in.chars - 1)).hyper -> $i { + for (1..^$in.chars).hyper -> $j { + take $in.substr($i..$j) if $i <= $j; + } + } +} + +sub MAIN(*@strings) { + my @subs = all-substrings(@strings[0]).sort(- *.chars).unique; + + for @strings[1..*].hyper -> $B { + @subs .= grep: -> $A { + $A.chars <= $B.chars && (0..($B.chars - $A.chars)).grep: -> $i { + $B.substr-eq($A, $i) + } + } + }; + + say @subs.first; +} diff --git a/challenge-018/fjwhittle/perl6/ch-2.p6 b/challenge-018/fjwhittle/perl6/ch-2.p6 new file mode 100644 index 0000000000..bfab2c94c6 --- /dev/null +++ b/challenge-018/fjwhittle/perl6/ch-2.p6 @@ -0,0 +1,42 @@ +#!/usr/bin/env perl6 + +class Priority-Queue { + has Array %queue{UInt}; + + method is_empty { + not %queue.values.grep(*.elems); + } + + method insert_with_priority($element, UInt $prio = 0) { + %queue.push: $prio => $element; + } + + method pull_highest_priority_element { + %queue.pairs.grep(*.value.elems).sort(- *.key).first.value.shift; + } +} + +use Test; + +plan 7; + +my $q = Priority-Queue.new; + +is $q.is_empty, True, 'New queue is empty'; + +$q.insert_with_priority('foo'); +$q.insert_with_priority('baz', 1); +$q.insert_with_priority('bar', 0); + +is $q.is_empty, False, 'Added three elements, queue is not empty'; + +is $q.pull_highest_priority_element, 'baz', 'Pulled baz, second added with priority 1 first'; + +is $q.is_empty, False, 'Queue is still not empty'; + +is $q.pull_highest_priority_element, 'foo', 'Pulled foo, first added with priority 0 second'; + +is $q.pull_highest_priority_element, 'bar', 'Pulled bar, last added with priority 0 third'; + +is $q.is_empty, True, 'Queue is empty after all elements pulled'; + |
