Ad
EPiphFailed Tests

Bonk

from random import randint

playing_board = [0] * 100 # board is 100 positions long
position = 0 # player starts at position 0 on the board
while position < 100: # end when position moves off board
    playing_board[position] = 1 # player was here
    position += randint(1, 6) # throw a die and move

# what's the probability that playing_board[n] == 1?
# there is a prize if you land on position n
# but a cost for playing the game
# let's run N times

def probability():
    N = 10000 # maybe this isn't big enough
    successes = [0] * 100
    for _ in range(N):
        playing_board = [0] * 100
        position = 0
        while position < 100:
            successes[position] += 1
            position += randint(1, 6)
    ret = []
    for s in successes:
        ret.append(s / N)
    return ret

# what if we add a rule
# if the player lands on square 99, they teleport back to 0

playing_board = [0] * 100 # board is 100 positions long
position = 0 # player starts at position 0 on the board
while position < 100: # end when position moves off board
    playing_board[position] = 1 # player was here
    position += randint(1, 6) # throw a die and move
    if position == 99: position = 0 # snakey snake

# can you calculate the probability of the prize now?
Code
Diff
  • sum_integers = lambda arr, r=0: [r := r + n for n in arr][-1]
    
        
    • def sum_integers(arr):
    • result = 0
    • [result := result + n for n in arr]
    • return result
    • sum_integers = lambda arr, r=0: [r := r + n for n in arr][-1]
Code
Diff
  • def sum_integers(arr):
        result = 0
        for i in range(min(arr), max(arr) + 1):
            result += i * arr.count(i)
        return result
        
    • def sum(arr):
    • def sum_integers(arr):
    • result = 0
    • for i in set(arr):
    • for i in range(min(arr), max(arr) + 1):
    • result += i * arr.count(i)
    • return result
Code
Diff
  • def sum(arr):
        result = 0
        for i in set(arr):
            result += i * arr.count(i)
        return result
        
    • def sum(arr):
    • result = 0
    • for i in arr:
    • result += i
    • for i in set(arr):
    • result += i * arr.count(i)
    • return result
Code
Diff
  • def sum(arr):
        if not arr:
            return 0
        if len(arr) == 1:
            return arr[0]
        return sum(arr[:len(arr) // 2]) + sum(arr[len(arr) // 2:])
    • def sum(arr):
    • if not arr:
    • return 0
    • return arr[0] + sum(arr[1:])
    • if len(arr) == 1:
    • return arr[0]
    • return sum(arr[:len(arr) // 2]) + sum(arr[len(arr) // 2:])