Counter game

Sort by

recency

|

91 Discussions

|

  • + 0 comments
    def counterGame(n):
        turns = 0
        while n > 1:
            # Check if n is a power of 2
            if (n & (n - 1)) == 0:
                # If it's a power of 2, divide by 2
                n >>= 1  # Equivalent to n = n // 2
            else:
                # If not a power of 2, find the largest power of 2 less than n
                # This can be found by taking 2 raised to the power of (n.bit_length() - 1)
                # n.bit_length() gives the number of bits required to represent n,
                # which is equivalent to floor(log2(n)) + 1.
                # So, the largest power of 2 less than n is 2^(floor(log2(n))).
                # We can get this using a bit shift: 1 << (n.bit_length() - 1)
                largest_power_of_2 = 1 << (n.bit_length() - 1)
                n -= largest_power_of_2
            turns += 1
    
        # If the number of turns is odd, Louise wins (since she starts)
        # If the number of turns is even, Richard wins
        if turns % 2 == 1:
            return "Louise"
        else:
            return "Richard"
    
  • + 0 comments

    Python 3 Solution:

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'counterGame' function below.
    #
    # The function is expected to return a STRING.
    # The function accepts LONG_INTEGER n as parameter.
    #
    
    def counterGame(n):
        # Write your code here
        turns = 0
        while n > 1:
            power = 1 << (n.bit_length() - 1) 
            if n == power:
                n >>=1 
            else: 
                n -= power
            turns += 1
               
        return "Louise" if turns %2 != 0 else "Richard"
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        t = int(input().strip())
    
        for t_itr in range(t):
            n = int(input().strip())
    
            result = counterGame(n)
    
            fptr.write(result + '\n')
    
        fptr.close()
    
  • + 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"
    
  • + 0 comments

    Rust 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

    fn counter_game(n: i64) -> String {
        //Time complexity: O(log(n))
        //Space complexity (ignoring input): O(1)
        let mut n = n;
        let mut count_turns = 0;
        while n > 1 {
            let n_is_power_2 = n & (n - 1) == 0;
            if n_is_power_2 {
                while n > 1 {
                    n = n >> 1;
                    count_turns += 1;
                }
            } else {
                let mut pow_2 = 1;
                while pow_2 * 2 < n {
                    pow_2 = pow_2 << 1;
                }
                n = n - pow_2;
                count_turns += 1;
            }
        }
    
        if count_turns % 2 == 0 {
            return String::from("Richard");
        } else {
            return String::from("Louise");
        }
    }
    
  • + 0 comments
    #Python 3
    def counterGame(n):
        turns = 0
        while n > 1:
            power = 1 << (n.bit_length() - 1) #uses bit length to quickly calculate the largest power of 2 <= n
            if n == power:
                n >>=1 #use a bit shift to quickly divide by 2
            else: 
                n -= power
            turns += 1
               
        return "Louise" if turns %2 != 0 else "Richard"