Counter game

  • + 0 comments

    Python best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    def counter_game(n):
        # Time complexity: O(log(n))
        # Space complexity (ignoring input): O(1)
        if n == 1:
            return "Louise"
    
        count = 0
        while n > 1:
            is_power_of_2 = (n & (n - 1)) == 0
            if is_power_of_2:
                while n > 1:
                    n = n / 2
                    count += 1
            else:
                power_of_2 = 2
                while power_of_2 * 2 < n:
                    power_of_2 = power_of_2 * 2
                n = n - power_of_2
                count += 1
        if count % 2 == 0:
            return "Richard"
    
        return "Louise"