Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #356 (“Kolakoski Sequence” and “Who Wins”)
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here: The Weekly Challenge
The Weekly Challenge for the week of 2026-01-12 through 2026-01-18 is #356. The tasks for challenge #356 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 356-1: Kolakoski Sequence Submitted by: Mohammad Sajid Anwar You are given an integer, $int > 3. Write a script to generate the Kolakoski Sequence of given length $int and return the count of 1 in the generated sequence. Please follow the wikipedia page for more informations. Example #1: Input: $int = 4 Output: 2 (1)(22)(11)(2) => 1221 Example #2: Input: $int = 5 Output: 3 (1)(22)(11)(2)(1) => 12211 Example #3: Input: $int = 6 Output: 3 (1)(22)(11)(2)(1)(22) => 122112 Example #4: Input: $int = 7 Output: 4 (1)(22)(11)(2)(1)(22)(1) => 1221121 Example #5: Input: $int = 8 Output: 4 (1)(22)(11)(2)(1)(22)(1)(22) => 12211212
To solve this problem, I simply use the generative formula given by
the Wikipedia article on "Kolakoski Sequence":
1. Start with interation index i=1.
2. Let sequence k be Kolakoski Sequence
3. Let sequence r be the sequence of run lengths.
4. For each iteration i
if k[i] has been defined
set r[i] = k[i]
otherwise
set r[i] = i
5. If i is odd
push r[i] copies of '1' as elements to the right end of sequence k
otherwise
push r[i] copies of '2' as elements to the right end of sequence k
Robbie Hatley's Perl Solution to The Weekly Challenge 356-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 356-2: Who Wins
Submitted by: Simon Green
It’s NFL playoff time. Since the 2020 season, seven teams from
each of the league’s two conferences (AFC and NFC) qualify for
the playoffs based on regular season winning percentage, with
a tie-breaking procedure if required. The top team in each
conference receives a first-round bye, automatically advancing
to the second round.
The following games are played. Some times the games are played
in a different order. To make things easier, assume the order
is always as below.
- Week 1: Wild card playoffs
- Team 1 gets a bye
- Game 1: Team 2 hosts Team 7
- Game 2: Team 3 hosts Team 6
- Game 3: Team 4 hosts Team 5
- Week 2: Divisional playoffs
- Game 4: Team 1 hosts the third seeded winner from the
previous week.
- Game 5: The highest seeded winner from the previous week
hosts the second seeded winner.
- Week 3: Conference final
- Game 6: The highest seeded winner from the previous week
hosts the other winner
You are given a six character string containing only H (home)
and A away which has the winner of each game. Which two teams
competed in the the conference final and who won?
Example #1:
Input: $results = "HAHAHH"
Output: "Team 2 defeated Team 6"
Example #2:
Input: $results = "HHHHHH"
Output: "Team 1 defeated Team 2"
Example #3:
Input: $results = "HHHAHA"
Output: "Team 4 defeated Team 2"
Example #4:
Input: $results = "HAHAAH"
Output: "Team 4 defeated Team 6"
Example #5:
Input: $results = "HAAHAA"
Output: "Team 5 defeated Team 1"
To solve this problem, I make lists of winners sorted by seeds for each of the first two weeks.
Robbie Hatley's Perl Solution to The Weekly Challenge 356-2
That's it for challenge 356; see you on challenge 357!
Comments
Post a Comment