[Esame 20 Luglio 2021] Esercizio 2 #260
Replies: 2 comments
-
Sia |
Beta Was this translation helpful? Give feedback.
-
I risultati sono stati controllati con la seguente simulazione import random
def test(iterations, *tests):
print(", ".join(t.__name__ + ": " + str(sum(t() for _ in range(iterations)) / iterations) for t in tests))
l = 10
l2 = l*l
n = l2
balls = [None] * n
def mix_balls():
global balls
for i in range(l2):
balls[i] = random.randint(0, l2 - 1)
check_empty = lambda i: all(b != i for b in balls)
def check_free(i):
col = i % l
return all(check_empty(j) for j in range(i - col, i - col + l))and\
all(check_empty(j) for j in range(col, l2, step=l))
# a)
def a():
mix_balls()
return check_empty(0)
# b)
def b():
mix_balls()
# return all(b >= 2 * l for b in balls)
return check_free(0)
# c)
def c():
mix_balls()
return sum(check_free(i) for i in range(l2))
test(1000000, a, b, c) |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
All reactions