diff options
| author | Jaime <42359730+bracteatus@users.noreply.github.com> | 2019-07-27 19:22:19 -0600 |
|---|---|---|
| committer | Jaime <42359730+bracteatus@users.noreply.github.com> | 2019-07-27 19:22:19 -0600 |
| commit | 85119de4e314c3ab930d7369e20222a6e2f61144 (patch) | |
| tree | 4312c4a490af01ecd39f0b1347d2c8c3c0c8ae45 /challenge-018 | |
| parent | 6e475c2d984d2396ed72b73919d0e03e8aca8e09 (diff) | |
| download | perlweeklychallenge-club-85119de4e314c3ab930d7369e20222a6e2f61144.tar.gz perlweeklychallenge-club-85119de4e314c3ab930d7369e20222a6e2f61144.tar.bz2 perlweeklychallenge-club-85119de4e314c3ab930d7369e20222a6e2f61144.zip | |
Update README
Solutions explained.
Diffstat (limited to 'challenge-018')
| -rw-r--r-- | challenge-018/jaime/README | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/challenge-018/jaime/README b/challenge-018/jaime/README index 62a35b5a51..c1fbac90da 100644 --- a/challenge-018/jaime/README +++ b/challenge-018/jaime/README @@ -6,6 +6,13 @@ common substring of the strings “ABABC”, “BABCA” and “ABCBA” is stri “ABC” of length 3. Other common substrings are “A”, “AB”, “B”, “BA”, “BC” and “C”. +## Solution + +Substrings of the first argument are iterated by decreasing length. + +The script finds the `$substring` that is common within the list of +parameters `@ARGV == grep(/$substring/,@ARGV)`. + # Challenge #2 Write a script to implement Priority Queue. It is like regular queue @@ -14,11 +21,22 @@ queue, an element with high priority is served before an element with low priority. Please check this wiki page for more informations. It should serve the following operations: -1. is_empty: check whether the queue has no elements. +1. `is_empty`: check whether the queue has no elements. -2. insert_with_priority: add an element to the queue with an +2. `insert_with_priority`: add an element to the queue with an associated priority. -3. pull_highest_priority_element: remove the element from the queue +3. `pull_highest_priority_element`: remove the element from the queue that has the highest priority, and return it. If two elements have the same priority, then return element added first. + +## Solution + +The solution is a hashmap of priority and anonymous arrays. + +The anonymous array `[]` is accessed with a `$priority`. +`$queue` is the hashmap. `$queue->{$priority}` is an anonymous array. +`@{$queue->{$priority}}` wraps access to the prioritized queues as arrays. + +`pull_highest_priority_element()` parses the `$queue` to find the +nonempty array with the largest priority, then returns the first entry. |
