diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-07-29 00:48:17 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-07-29 00:48:17 +0100 |
| commit | ec6f40cb81da8e7bdae12e7d6572d2d33789dc14 (patch) | |
| tree | 4227c9b4648ac0c6bf9e05c55fdcae836ff4c926 /challenge-018/mark-senn | |
| parent | 6ca69faa7f8f98c37688a73e462638d2bebf5d78 (diff) | |
| download | perlweeklychallenge-club-ec6f40cb81da8e7bdae12e7d6572d2d33789dc14.tar.gz perlweeklychallenge-club-ec6f40cb81da8e7bdae12e7d6572d2d33789dc14.tar.bz2 perlweeklychallenge-club-ec6f40cb81da8e7bdae12e7d6572d2d33789dc14.zip | |
- Added solutions by Mark Senn.
Diffstat (limited to 'challenge-018/mark-senn')
| -rw-r--r-- | challenge-018/mark-senn/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-018/mark-senn/perl6/ch-1.p6 | 33 | ||||
| -rw-r--r-- | challenge-018/mark-senn/perl6/ch-2.p6 | 55 |
3 files changed, 89 insertions, 0 deletions
diff --git a/challenge-018/mark-senn/blog.txt b/challenge-018/mark-senn/blog.txt new file mode 100644 index 0000000000..a69c326878 --- /dev/null +++ b/challenge-018/mark-senn/blog.txt @@ -0,0 +1 @@ +https://engineering.purdue.edu/~mark/pwc-018.pdf diff --git a/challenge-018/mark-senn/perl6/ch-1.p6 b/challenge-018/mark-senn/perl6/ch-1.p6 new file mode 100644 index 0000000000..883a690d88 --- /dev/null +++ b/challenge-018/mark-senn/perl6/ch-1.p6 @@ -0,0 +1,33 @@ +# Perl Weekly Challenge - 018 +# Task #1 +# +# See +# engineering.purdue.edu/~mark/pwc-018.pdf +# for more information. + +# Run using Perl v6.d. +use v6.d; + +# Make an array of sets. +# Each element of the array will contain a set of all substrings +# for the corresponding word. +my SetHash @set; + +# For each word on the command line +for (0 .. @*ARGS.elems - 1) -> $i { + # Get the word. + my $word = @*ARGS[$i]; + # Compute and store every substring in the word in @set[$i]. + for (0 .. $word.chars - 1) -> $startpos { + for ($startpos .. $word.chars - 1) -> $endpos { + my $substr = $word.substr($startpos .. $endpos); + @set[$i]{$substr} = True; + } + } +} + +# Compute the intersection of all sets, +# sort by the number of characters, +# get the last entry (the one with the most characters), +# and print it. +([(&)] @set).keys.sort({.chars}).tail.say; diff --git a/challenge-018/mark-senn/perl6/ch-2.p6 b/challenge-018/mark-senn/perl6/ch-2.p6 new file mode 100644 index 0000000000..585b3c89cf --- /dev/null +++ b/challenge-018/mark-senn/perl6/ch-2.p6 @@ -0,0 +1,55 @@ +# Perl Weekly Challenge - 018 +# Task #2 +# +# See +# engineering.purdue.edu/~mark/pwc-018.pdf +# for more information. + +# Run using Perl v6.d. +use v6.d; + +# Priorites and values are stored in parallel arrays. +my (@priority, @value); + +sub insert_with_priority($priority, $value) +{ + @priority.push($priority); @value.push($value); +} + +sub pull_highest_priority_element +{ + (is_empty) and return Nil; + my $i = (0..^@priority).sort( {@priority[$^a] < @priority[$^b]}).head; + my $value = @value[$i]; + @priority.splice($i, 1, ()); @value.splice($i, 1, ()); + return $value; +} + +sub print_queue +{ + "print_queue".say; + for (0..^@priority) -> $i + { + " {@priority[$i]} {@value[$i]}".say; + } +} + +sub is_empty +{ + return !@priority.elems; +} + +print_queue; +insert_with_priority(5, "this is the first priority 5 item"); +print_queue; +insert_with_priority(5, "this is the second priority 5 item"); +print_queue; +insert_with_priority(4, "this is the first priority 4 item"); +print_queue; +pull_highest_priority_element.say; +print_queue; +pull_highest_priority_element.say; +print_queue; +pull_highest_priority_element.say; +print_queue; +print_queue; |
