diff options
| author | chirvasitua <stuart-little@users.noreply.github.com> | 2020-11-29 10:00:44 -0500 |
|---|---|---|
| committer | chirvasitua <stuart-little@users.noreply.github.com> | 2020-11-29 10:00:44 -0500 |
| commit | 276194786b6decdb08bd065205e399dcafa0ed62 (patch) | |
| tree | ab62447b02566af4481900fcfaad2af5ddbbd88e /challenge-018 | |
| parent | e5f5309fd45b7e190c3bf574304d3b9b42ce185a (diff) | |
| download | perlweeklychallenge-club-276194786b6decdb08bd065205e399dcafa0ed62.tar.gz perlweeklychallenge-club-276194786b6decdb08bd065205e399dcafa0ed62.tar.bz2 perlweeklychallenge-club-276194786b6decdb08bd065205e399dcafa0ed62.zip | |
1st commit on 017-020
Diffstat (limited to 'challenge-018')
| -rw-r--r-- | challenge-018/stuart-little/README | 1 | ||||
| -rwxr-xr-x | challenge-018/stuart-little/raku/ch-1.p6 | 6 | ||||
| -rwxr-xr-x | challenge-018/stuart-little/raku/ch-2.p6 | 41 |
3 files changed, 48 insertions, 0 deletions
diff --git a/challenge-018/stuart-little/README b/challenge-018/stuart-little/README new file mode 100644 index 0000000000..78439907de --- /dev/null +++ b/challenge-018/stuart-little/README @@ -0,0 +1 @@ +Solutions by Stuart Little diff --git a/challenge-018/stuart-little/raku/ch-1.p6 b/challenge-018/stuart-little/raku/ch-1.p6 new file mode 100755 index 0000000000..b53f9d8f9e --- /dev/null +++ b/challenge-018/stuart-little/raku/ch-1.p6 @@ -0,0 +1,6 @@ +#!/usr/bin/env perl6 +use v6; + +say (0..( my $minword=@*ARGS.min(*.chars); $minword.chars ).[1]).combinations(2).map({ $minword.substr($_[0]..^$_[1]) }).grep({ @*ARGS.all.contains($_) }).max(*.chars) + +# run as <script> <space-separated strings, each enclosed in quotes if it contains spaces> diff --git a/challenge-018/stuart-little/raku/ch-2.p6 b/challenge-018/stuart-little/raku/ch-2.p6 new file mode 100755 index 0000000000..f7cb9541c3 --- /dev/null +++ b/challenge-018/stuart-little/raku/ch-2.p6 @@ -0,0 +1,41 @@ +#!/usr/bin/env perl6 +use v6; + +class Item { + has Int $.val; + has Int $.priority; +} + +class PriorityQueue is Array { + + method is_empty { + return self.elems==0 + } + + method insert_with_priority($val,$priority) { + my $item=Item.new(val=>$val, priority=>$priority); + my @rhs=self.grep({ $_.priority >= $priority }); + @rhs.unshift: $item; + self=(|self.grep({ $_.priority < $priority }), |@rhs) + } + + method pull_highest_priority_element { + return self.pop.val + } + + +} + +my $prq = PriorityQueue.new; + +say "Is the queue empty initially? ", $prq.is_empty; + +for ( 1=>2, 3=>2, -1=>0, 10=>5 ) { + $prq.insert_with_priority($_.key,$_.value); +} + +say "Queue after adding some items: ", $prq; +say "Is the queue empty now? ", $prq.is_empty; +say "Highest-priority item: ", $prq.pull_highest_priority_element; + +# run as <script> |
