DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge: Power Meeting

Weekly Challenge 349

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: Power String

Task

You are given a string.

Write a script to return the power of the given string.

The power of the string is the maximum length of a non-empty substring that contains only one unique character.

My solution

For this function, I use three variables:

  • current_letter is the current letter seen, starts as an empty string
  • current_length is the length of that letter
  • maximum_length is the maximum length seen so far.

I loop over each letter, and store that as the variable letter. If it is the same as current_letter, I increment the current_length by one and increment maximum_length if required. If it is different, I update the current_letter variable and set current_length to one.

def power_string(input_string: str) -> int:
    current_letter = ''
    current_length = 0
    max_length = 0

    for letter in input_string:
        if letter == current_letter:
            current_length += 1
            if current_length > max_length:
                max_length = current_length
        else:
            current_letter = letter
            current_length = 1

    return max_length
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-1.py textbook
2

$ ./ch-1.py aaaaa
5

$ ./ch-1.py hoorayyy
3

$ ./ch-1.py x
1

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

Task 2: Meeting Point

Task

You are given instruction string made up of U (up), D (down), L (left) and R (right).

Write a script to return true if following the instruction, you meet (0,0) at any point along the sequence.

My solution

In the original challenge, the "at any point along the sequence" did not appear. Therefore the solution I wrote was to count the number of each moves, and make sure the ups and downs were the same and the left and rights were the same. I use TDD when writing the solutions, and it failed on the last test.

Therefore the solution I came up was a more traditional approach. I define two variable x and y both set to zero. For each letter in the input_string I update x or y as required, or throw an error if there is an unexpected letter. After each move, I check if we are at the starting point (where x and y are both equal to 0). If true, I return True.

If the loop is exhausted, I return False.

def meeting_point(input_string: str) -> bool:
    x, y = 0, 0

    for move in input_string:
        if move == 'U':
            y += 1
        elif move == 'D':
            y -= 1
        elif move == 'L':
            x -= 1
        elif move == 'R':
            x += 1
        else:
            raise ValueError(f"Invalid move encountered: '{move}'")

        if x == 0 and y == 0:
            return True

    return False
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-2.py ULD
False

$ ./ch-2.py ULDR
True

$ ./ch-2.py UUURRRDDD
False

$ ./ch-2.py UURRRDDLLL
True

$ ./ch-2.py RRUULLDDRRUU
True
Enter fullscreen mode Exit fullscreen mode

Top comments (0)