Ad

I'm not sure why but this version (building a slice as we go) is faster on average
Still about 3x slower than str.split though

Code
Diff
  • def split(string, separator):
        split_list = []
        current_slice = ''
        for char in string:
            if char == separator:
                split_list.append(current_slice)
                current_slice = ''
            else:
                current_slice += char
        
        split_list.append(current_slice)
        return split_list
    • # ojeriejire rust version in python
    • def split(string, separator):
    • split_list = []
    • i = 0
    • current_slice = ''
    • for char in string:
    • if char == separator:
    • split_list.append(current_slice)
    • current_slice = ''
    • else:
    • current_slice += char
    • for j, c in enumerate(string):
    • if c == separator:
    • slice_ = string[i:j]
    • split_list.append(slice_)
    • i = j + 1
    • split_list.append(string[i:])
    • return split_list
    • split_list.append(current_slice)
    • return split_list

When writing a number in base 3, the number is even only if its digit sum is also
This function uses this fact as well as recursion to check whether a number is odd or even

To sum the digits, we sum their ascii codepoints. This works because the ascii values for 0, 1, 2 have the same parity as the digits they represent:

ord('0') == 48
ord('1') == 49
ord('2') == 50

This program also fulfils one of the points of the original brief because it almost always times out:

Bonus points for skirting the execution time limit without waits or unnecessary loops
Code
Diff
  • # By using base 3, there are fewer possible digits to check: only 0, 1, or 2
    def odd_even(n):
        # Make n positive; this won't affect whether it's even
        if n < 0: n *= -1
        
        # Express n in base 3 for easier parity checking
        b3_n = ''
        while n:
            b3_n = str(n%3) + b3_n
            n //= 3
        
        # Now we can sum the digits - if the answer is even, then n was even
        # Unfortunately we have a string, not an int, but we can convert with `ord`:
        digit_sum = sum(ord(char) for char in b3_n)
        
        # If the digit sum was even, so was n
        try:
            return odd_even(digit_sum)
        except RecursionError:
            # The fixed point of this process is 294 or 295, so we just check which it was
            return digit_sum == 295
            
    print(f'{odd_even(0) = }')
    print(f'{odd_even(1) = }')
    • import random
    • import time
    • def not_even(n):
    • pizza_time = time.ctime()
    • if n % 2 == 0:
    • return 'its not even', pizza_time
    • else:
    • return 'its still not even', pizza_time
    • return pizza_time
    • n = random.randint(0, 10000)
    • print(not_even(n))
    • # By using base 3, there are fewer possible digits to check: only 0, 1, or 2
    • def odd_even(n):
    • # Make n positive; this won't affect whether it's even
    • if n < 0: n *= -1
    • # Express n in base 3 for easier parity checking
    • b3_n = ''
    • while n:
    • b3_n = str(n%3) + b3_n
    • n //= 3
    • # Now we can sum the digits - if the answer is even, then n was even
    • # Unfortunately we have a string, not an int, but we can convert with `ord`:
    • digit_sum = sum(ord(char) for char in b3_n)
    • # If the digit sum was even, so was n
    • try:
    • return odd_even(digit_sum)
    • except RecursionError:
    • # The fixed point of this process is 294 or 295, so we just check which it was
    • return digit_sum == 295
    • print(f'{odd_even(0) = }')
    • print(f'{odd_even(1) = }')