diff options
| -rw-r--r-- | challenge-018/simon-proctor/perl6/ch-1.p6 | 23 | ||||
| -rw-r--r-- | challenge-018/simon-proctor/perl6/ch-2.p6 | 74 | ||||
| -rw-r--r-- | challenge-018/simon-proctor/perl6/test.csv | 9 |
3 files changed, 106 insertions, 0 deletions
diff --git a/challenge-018/simon-proctor/perl6/ch-1.p6 b/challenge-018/simon-proctor/perl6/ch-1.p6 new file mode 100644 index 0000000000..ee828ecfd1 --- /dev/null +++ b/challenge-018/simon-proctor/perl6/ch-1.p6 @@ -0,0 +1,23 @@ +#!/usr/bin/env perl6 + +use v6; + +#| Find the longest common sub strings of a list of strings +sub MAIN ( + *@words where *.elems > 1 #= List of words to compare +) { + my @word-subs = @words.map( &all-substrings ); + .say for ([(&)] @word-subs).keys.sort( { $^b.codes <=> $^a.codes } ).grep( { state $len = $_.codes; $_.codes == $len }); +} + +sub all-substrings( Str $word ) { + my $len = $word.codes; + my @subs; + + for (1..$len) -> $sub-len { + for (0..$len-$sub-len) -> $start { + @subs.push( $word.substr($start,$sub-len) ); + } + } + return set( @subs ); +} diff --git a/challenge-018/simon-proctor/perl6/ch-2.p6 b/challenge-018/simon-proctor/perl6/ch-2.p6 new file mode 100644 index 0000000000..4b7858f317 --- /dev/null +++ b/challenge-018/simon-proctor/perl6/ch-2.p6 @@ -0,0 +1,74 @@ +#!/usr/bin/env perl6 + +role SingleQueue { + has @!items; + + method add-item( $item ) { + @!items.push( $item ); + } + + method get-item() { + return @!items.shift; + } + + method is-empty() { + @!items.elems == 0; + } +} + +role OrderedQueue does Iterator does Iterable { + + has SingleQueue %!queues; + + method is_empty() { + self.is-empty(); + } + + method is-empty() { + [&&] %!queues.kv.map( -> $k, $v { $v.is-empty } ); + } + + method insert_with_priority( $item, Int() $priority ) { + self.insert-with-priority( $item, $priority ); + } + + method insert-with-priority( $item, Int() $priority ) { + %!queues{$priority} //= SingleQueue.new(); + %!queues{$priority}.add-item( $item ); + } + + method pull_highest_priority_element() { + self.pull-highest-priority-element() + } + + method pull-highest-priority-element() { + return Nil if self.is-empty; + my $queue = %!queues.keys.sort( {$^b <=> $^a }).first; + my $value = %!queues{$queue}.get-item; + if %!queues{$queue}.is-empty { + %!queues{$queue}:delete; + } + return $value; + } + + method pull-one() { + self.pull-highest-priority-element // IterationEnd; + } + + method iterator() { + return self; + } +} + +#| Given a file in csv format "priority,item" add all the items to the queue +#| then print them in the given order +sub MAIN( $file ) { + my $queue := OrderedQueue.new(); + + for $file.IO.lines -> $line { + my ( $priority, $item ) = $line.split(","); + $queue.insert-with-priority( $item, $priority ); + } + + .say for $queue; +}
\ No newline at end of file diff --git a/challenge-018/simon-proctor/perl6/test.csv b/challenge-018/simon-proctor/perl6/test.csv new file mode 100644 index 0000000000..06209ce75b --- /dev/null +++ b/challenge-018/simon-proctor/perl6/test.csv @@ -0,0 +1,9 @@ +1,Apple +1,Pear +1,Orange +10,Carrot +9,Lettuce +1,Antelope +7,Seven +10,Another ten +11,Top Priority
\ No newline at end of file |
