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?
import codewars_test as test
from solution import probability
probabilities = [1]
for _ in range(99):
probabilities.append(sum(probabilities[-6:]) / 6)
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Get close enough")
def test_group():
@test.it("Say within 1 d.p. of accuracy")
def test_case():
user_probs = probability()
for n in range(100):
test.assert_equals(abs(user_probs[n] - probabilities[n]) < 0.1, True, "The error was " + str(abs(user_probs[n] - probabilities[n])))
@test.it("Say within 2 d.p. of accuracy")
def test_case():
user_probs = probability()
for n in range(100):
test.assert_equals(abs(user_probs[n] - probabilities[n]) < 0.01, True, "The error was " + str(abs(user_probs[n] - probabilities[n])))
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
import codewars_test as test # TODO Write tests from solution import sum_integers # test.assert_equals(actual, expected, [optional] message) @test.describe("Example tests") def test_group(): @test.it("Basic tests") def test_case(): test.assert_equals(sum_integers([10**7,1,2,3,1,2,3,-10**7]), 12)
- import codewars_test as test
- # TODO Write tests
from solution import sum- from solution import sum_integers
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example tests")
- def test_group():
- @test.it("Basic tests")
- def test_case():
test.assert_equals(sum([1]), 1)test.assert_equals(sum([1,2,3]), 6)test.assert_equals(sum([1,2,3,1,2,3]), 12)- test.assert_equals(sum_integers([10**7,1,2,3,1,2,3,-10**7]), 12)
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
import codewars_test as test # TODO Write tests from solution import sum # test.assert_equals(actual, expected, [optional] message) @test.describe("Example tests") def test_group(): @test.it("Basic tests") def test_case(): test.assert_equals(sum([1]), 1) test.assert_equals(sum([1,2,3]), 6) test.assert_equals(sum([1,2,3,1,2,3]), 12)
- import codewars_test as test
- # TODO Write tests
- from solution import sum
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example tests")
- def test_group():
- @test.it("Basic tests")
- def test_case():
- test.assert_equals(sum([1]), 1)
- test.assert_equals(sum([1,2,3]), 6)
- test.assert_equals(sum([1,2,3,1,2,3]), 12)
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:])
import codewars_test as test # TODO Write tests from solution import sum # test.assert_equals(actual, expected, [optional] message) @test.describe("Example tests") def test_group(): @test.it("Basic tests") def test_case(): test.assert_equals(sum([1]), 1) test.assert_equals(sum([1,2,3]), 6) test.assert_equals(sum([1,2,3,4,5,6]), 21) test.assert_equals(sum([1,2,3,4,5,6,7]), 28) test.assert_equals(sum([1,2,3,4,5,6,7,-1,-10]), 17)
- import codewars_test as test
- # TODO Write tests
- from solution import sum
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example tests")
- def test_group():
- @test.it("Basic tests")
- def test_case():
- test.assert_equals(sum([1]), 1)
- test.assert_equals(sum([1,2,3]), 6)
- test.assert_equals(sum([1,2,3,4,5,6]), 21)
- test.assert_equals(sum([1,2,3,4,5,6,7]), 28)
- test.assert_equals(sum([1,2,3,4,5,6,7,-1,-10]), 17)