Robbie Hatley's Solutions To The Weekly Challenge #274
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here:
The Weekly Challenge for the week of 2024-06-16 through 2024-06-22 is #274. Its tasks are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 274-1: Goat Latin Submitted by: Mohammad Sajid Anwar You are given a sentence, $sentance. Write a script to convert $sentence to Goat Latin, a made up language similar to Pig Latin. Rules for Goat Latin: 1) If a word begins with a vowel ("a", "e", "i", "o", "u"), append "ma" to the end of the word. 2) If a word begins with consonant i.e. not a vowel, remove first letter and append it to the end then add "ma". 3) Add letter "a" to the end of first word in the sentence, "aa" to the second word, etc. Example 1: Input: $sentence = "I love Perl" Output: "Imaa ovelmaaa erlPmaaaa" Example 2: Input: $sentence = "Perl and Raku are friends" Output: "erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa" Example 3: Input: $sentence = "The Weekly Challenge" Output: "heTmaa eeklyWmaaa hallengeCmaaaa"
The tricky parts of this task are defining the terms "vowel", "consonant", "word", and "sentence". The definitions used in English won't work here. Instead, I define these terms as follows:
"vowel" = one of [aeiou] and case variants, with or without combining marks. (eg: ÅËiòU) "consonant" = any character which is not a "vowel". (eg: b7$@ÐgÑ茶z) "word" = any cluster of non-horizontal-whitespace characters. (eg: "79.m:v", "du#f", "$17") "sentence" = any string consisting of "words" separated by horizontal whitespace. (eg: "79.m:v du#f $17")
Based on those definitions, the solution I came up with was:
Robbie Hatley's Perl Solution to The Weekly Challenge 274-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 274-2: Bus Route Submitted by: Peter Campbell Smith Several bus routes start from a bus stop near my home, and go to the same stop in town. They each run to a set timetable, but they take different times to get into town. Write a script to find the times - if any - I should let one bus leave and catch a strictly later one in order to get into town strictly sooner. An input timetable consists of the service interval, the offset within the hour, and the duration of the trip. Example 1: Input: [ [12, 11, 41], [15, 5, 35] ] Output: [36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47] Route 1 leaves every 12 minutes, starting at 11 minutes past the hour (so 11, 23, ...) and takes 41 minutes. Route 2 leaves every 15 minutes, starting at 5 minutes past (5, 20, ...) and takes 35 minutes. At 45 minutes past the hour I could take the route 1 bus at 47 past the hour, arriving at 28 minutes past the following hour, but if I wait for the route 2 bus at 50 past I will get to town sooner, at 25 minutes past the next hour. Example 2: Input: [ [12, 3, 41], [15, 9, 35], [30, 5, 25] ] Output: [ 0, 1, 2, 3, 25, 26, 27, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 55, 56, 57, 58, 59 ]
I first make a list "@trips" of all "trips" within the next 4 hours (not 1 hour!!!), with each "trip" being a two-element array [dep,arr] giving departure and arrival times. Each departure and arrival time will be "minutes after start of current hour", which may in the next hour, or the next, hence the need to collect trips for next 4 hours rather than just 1.
I then sort @trips, first by departure time, then by arrival time.
Next, I mark those trips which should be skipped because there exists a trip with a later departure but earlier arrival with the word "skip".
Then, for each minute within the current hour, I check the next trip to see if it should be skipped; if so, I add that minute to a @skips list.
Finally, I return @skips.
Robbie Hatley's Perl Solution to The Weekly Challenge 274-2
That's it for challenge 274; see you on challenge 275!
Comments
Post a Comment