|
1 | | -Solution by Jaime Corchado, (@tortsnare)[https://twitter.com/tortsnare]. |
2 | | - |
3 | 1 | # Challenge #1 |
4 | 2 |
|
5 | | -Write a script to generate Van Eck’s sequence. |
| 3 | +Write a script that takes 2 or more strings as command line parameters |
| 4 | +and print the longest common substring. For example, the longest |
| 5 | +common substring of the strings “ABABC”, “BABCA” and “ABCBA” is string |
| 6 | +“ABC” of length 3. Other common substrings are “A”, “AB”, “B”, “BA”, |
| 7 | +“BC” and “C”. |
| 8 | + |
| 9 | +## Solution |
| 10 | + |
| 11 | +Substrings of the first argument are iterated by decreasing length. |
| 12 | + |
| 13 | +The script finds the `$substring` that is common within the list of |
| 14 | +parameters `@ARGV == grep(/$substring/,@ARGV)`. |
6 | 15 |
|
7 | 16 | # Challenge #2 |
8 | 17 |
|
9 | | -Using only the official postal (2-letter) abbreviations for the 50 U.S. states, |
10 | | -write a script to find the longest English word you can spell. |
| 18 | +Write a script to implement Priority Queue. It is like regular queue |
| 19 | +except each element has a priority associated with it. In a priority |
| 20 | +queue, an element with high priority is served before an element with |
| 21 | +low priority. Please check this wiki page for more informations. It |
| 22 | +should serve the following operations: |
| 23 | + |
| 24 | +1. `is_empty`: check whether the queue has no elements. |
| 25 | + |
| 26 | +2. `insert_with_priority`: add an element to the queue with an |
| 27 | +associated priority. |
| 28 | + |
| 29 | +3. `pull_highest_priority_element`: remove the element from the queue |
| 30 | +that has the highest priority, and return it. If two elements have the |
| 31 | +same priority, then return element added first. |
| 32 | + |
| 33 | +## Solution |
11 | 34 |
|
12 | | -# Challenge #3 |
| 35 | +The solution is a hashmap of priority and anonymous arrays. |
13 | 36 |
|
14 | | -Find the given city current time using the Geo DB Cities API. |
| 37 | +The anonymous array `[]` is accessed with a `$priority`. |
| 38 | +`$queue` is the hashmap. `$queue->{$priority}` is an anonymous array. |
| 39 | +`@{$queue->{$priority}}` wraps access to the prioritized queues as arrays. |
15 | 40 |
|
| 41 | +`pull_highest_priority_element()` parses the `$queue` to find the |
| 42 | +nonempty array with the largest priority, then returns the first entry. |
0 commit comments