Ejercicio 4

This commit is contained in:
perro tuerto 2023-02-21 13:34:37 -08:00
parent 7bbe739561
commit e080f1e24f
3 changed files with 84 additions and 0 deletions

11
exercises/ex04/ex04a.py Normal file
View File

@ -0,0 +1,11 @@
def add(ls):
if len(ls) > 0:
ls = list(map(lambda x: str(x), ls))
ls[-1] = f"and {ls[-1]}"
print(", ".join(ls))
else:
print("")
add([0, 1, 2, 3])
add([])

40
exercises/ex04/ex04b.py Normal file
View File

@ -0,0 +1,40 @@
import random
def flip(times):
res = []
for i in range(times):
res.append(random.choice(["H", "T"]))
return res
def check(flips, reps, streaks):
last, h, t = "", 0, 0
for f in flips:
if last == f:
if f == "H":
h += 1
else:
t += 1
else:
h = 0
t = 0
if h == reps or t == reps:
streaks += 1
break
last = f
return streaks
def test(times, length=100, reps=6):
streaks = 0
for i in range(times):
print(f"Testing {int((i * 100) / times)}%")
streaks = check(flip(length), reps, streaks)
print(f"Streak length: {reps}")
print(f"Flips length: {length}")
print(f"Tests: {times}")
print(f"Result: {streaks} streaks ({(streaks * 100) / times}%).")
test(10000, reps=6)

33
exercises/ex04/ex04c.py Normal file
View File

@ -0,0 +1,33 @@
grid = [
[".", ".", ".", ".", ".", "."],
[".", "0", "0", ".", ".", "."],
["0", "0", "0", "0", ".", "."],
["0", "0", "0", "0", "0", "."],
[".", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "."],
["0", "0", "0", "0", ".", "."],
[".", "0", "0", ".", ".", "."],
[".", ".", ".", ".", ".", "."],
]
def show(grid):
for row in grid:
print("".join(row))
print("")
def reorder(grid):
res, curr = [], 0
while curr < len(grid[0]):
line = ""
for row in grid:
line += row[curr]
res.append(list(line))
curr += 1
return res
show(grid)
grid = reorder(grid)
show(grid)