DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge: Winning sequence

Weekly Challenge 356

Each week Mohammad S. Anwar sends out The Weekly Challenge, a chance for all of us to come up with solutions to two weekly tasks. My solutions are written in Python first, and then converted to Perl. It's a great way for us all to practice some coding.

Challenge, My solutions

Task 1: Kolakoski Sequence

Task

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.

My solution

The Kolakoski Sequence alternates between 1 and 2, so the equation I really need to solve is how many odd (1-based) integers between 1 and $int are there. This can be achieved by adding 1 to $int and taking the integer division of 2.

def kolakoski_sequence(number: int) -> int:
    return (number+1) // 2
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-1.py 4
2

$ ./ch-1.py 5
3

$ ./ch-1.py 6
3

$ ./ch-1.py 7
4

$ ./ch-1.py 8
4
Enter fullscreen mode Exit fullscreen mode

Task 2: Who Wins

Task

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?

My solution

Two hundred and eighty nine weeks since I first completed a weekly challenge, I finally submitted my own one, and it was accepted.

To me this is relatively straight forward, but maybe its because I watch the NFL. Go Patriots :-D

I start by determining the teams that are going to play in the second week (team one, plus the winners of the first round). This is sorted numerically.

def who_wins(results: str) -> str:
    week_two = sorted([
        1,
        2 if results[0] == "H" else 7,
        3 if results[1] == "H" else 6,
        4 if results[2] == "H" else 5,
    ])
Enter fullscreen mode Exit fullscreen mode

The next step is to calculate the two teams playing in the conference final, based on the results in the second round. The fourth game (first in week two) will always be team 1 at home to the lowest seeded winner from week one, while the highest seeded winner from week 1 hosts the second seeded team.

    week_three = sorted([
        week_two[0] if results[3] == "H" else week_two[3],
        week_two[1] if results[4] == "H" else week_two[2],
    ])
Enter fullscreen mode Exit fullscreen mode

Now that I know the teams in the final (with the highest seeded team as the home team), I switch the week_three list around if the away team won. Finally I return the expected string.

    if results[5] == "A":
        week_three.reverse()

    return f"Team {week_three[0]} defeated Team {week_three[1]}"
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-2.py HAHAHH
Team 2 defeated Team 6

$ ./ch-2.py HHHHHH
Team 1 defeated Team 2

$ ./ch-2.py HHHAHA
Team 4 defeated Team 2

$ ./ch-2.py HAHAAH
Team 4 defeated Team 6

$ ./ch-2.py HAAHAA
Team 5 defeated Team 1
Enter fullscreen mode Exit fullscreen mode

Top comments (0)