From ad5afaa24f905945daec75e9ebcb311d883436f7 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 22 Jul 2019 10:43:34 +0100 Subject: Longest substrings challenge --- challenge-018/simon-proctor/perl6/ch-1.p6 | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 challenge-018/simon-proctor/perl6/ch-1.p6 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 ); +} -- cgit From 1d03bfdae1130bbf391661bbb7e3f6fd1917e6c3 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 22 Jul 2019 14:05:50 +0100 Subject: Initial version of the queue --- challenge-018/simon-proctor/perl6/ch-2.p6 | 68 ++++++++++++++++++++++++++++++ challenge-018/simon-proctor/perl6/test.csv | 9 ++++ 2 files changed, 77 insertions(+) create mode 100644 challenge-018/simon-proctor/perl6/ch-2.p6 create mode 100644 challenge-018/simon-proctor/perl6/test.csv 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..79c7161676 --- /dev/null +++ b/challenge-018/simon-proctor/perl6/ch-2.p6 @@ -0,0 +1,68 @@ +#!/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 { + + 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; + } +} + +#| 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 ); + } + + while ! $queue.is-empty { + $queue.pull-highest-priority-element().say; + } +} \ 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 -- cgit From a517fd0485c34a5c8ad8575acefd11e910bd0763 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 22 Jul 2019 14:17:30 +0100 Subject: Update my queue to be iterable --- challenge-018/simon-proctor/perl6/ch-2.p6 | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/challenge-018/simon-proctor/perl6/ch-2.p6 b/challenge-018/simon-proctor/perl6/ch-2.p6 index 79c7161676..4b7858f317 100644 --- a/challenge-018/simon-proctor/perl6/ch-2.p6 +++ b/challenge-018/simon-proctor/perl6/ch-2.p6 @@ -16,7 +16,7 @@ role SingleQueue { } } -role OrderedQueue { +role OrderedQueue does Iterator does Iterable { has SingleQueue %!queues; @@ -50,19 +50,25 @@ role OrderedQueue { } 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(); + my $queue := OrderedQueue.new(); for $file.IO.lines -> $line { my ( $priority, $item ) = $line.split(","); $queue.insert-with-priority( $item, $priority ); } - while ! $queue.is-empty { - $queue.pull-highest-priority-element().say; - } + .say for $queue; } \ No newline at end of file -- cgit