From 85119de4e314c3ab930d7369e20222a6e2f61144 Mon Sep 17 00:00:00 2001 From: Jaime <42359730+bracteatus@users.noreply.github.com> Date: Sat, 27 Jul 2019 19:22:19 -0600 Subject: Update README Solutions explained. --- challenge-018/jaime/README | 24 +++++++++++++++++++++--- 1 file 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. -- cgit