diff --git a/exercises/ex04/ex04a.py b/exercises/ex04/ex04a.py new file mode 100644 index 0000000..28a292c --- /dev/null +++ b/exercises/ex04/ex04a.py @@ -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([]) diff --git a/exercises/ex04/ex04b.py b/exercises/ex04/ex04b.py new file mode 100644 index 0000000..513b90e --- /dev/null +++ b/exercises/ex04/ex04b.py @@ -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) diff --git a/exercises/ex04/ex04c.py b/exercises/ex04/ex04c.py new file mode 100644 index 0000000..886b7fc --- /dev/null +++ b/exercises/ex04/ex04c.py @@ -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)