Professional Documents
Culture Documents
Out[2]: 0.2343729461350695
1/6 = 0.166667
Roll \ tCount \ tProbability \ tDifference
1 166461 0.166461 -0.12
2 166429 0.166429 -0.14
3 166520 0.16652 -0.09
4 166467 0.166467 -0.12
5 166994 0.166994 0.2
6 167129 0.167129 0.28
Q. 45. Write a python program to find how many times do you have
to flip a coin to get k heads in a row?
In [4]: import random
def flip_coin():
return random.choice(['H', 'T']) # 'H' for Heads, 'T' for Tails
def find_k_heads_in_row(k):
flips_count = 0
consecutive_heads = 0
while consecutive_heads < k:
flip_result = flip_coin()
flips_count += 1
if flip_result == 'H':
consecutive_heads += 1
else:
consecutive_heads = 0
return flips_count
k_heads = 3 # You can adjust the value of k as desired.
flips_needed = find_k_heads_in_row(k_heads)
print(f"To get {k_heads} consecutive heads in a row, you need to flip the coin {flips_needed} times.")
To get 3 consecutive heads in a row, you need to flip the coin 3 times.
return neighbours
def recursive_backtracking(row, col, maze, visited):
visited[row][col] = True
neighbours = get_neighbours((row, col), len(maze), len(maze[0]))
random.shuffle(neighbours)
for nr, nc in neighbours:
if not visited[nr][nc]:
if nr == row:
maze[row][max(col, nc)] = " "
else:
maze[max(row, nr)][col] = " "
recursive_backtracking(nr, nc, maze, visited)
def generate_random_maze(rows, cols):
maze = create_empty_maze(rows, cols)
visited = [[False for _ in range(cols)] for _ in range(rows)]
start_row, start_col = random.randint(0, rows - 1), random.randint(0, cols - 1)
recursive_backtracking(start_row, start_col, maze, visited)
return maze
def print_maze(maze):
for row in maze:
print("".join(row))
maze_rows = 11 # You can adjust the number of rows and columns as desired.
maze_cols = 11
maze = generate_random_maze(maze_rows, maze_cols)
print_maze(maze)
# # # #
#
# # #
# #
##
# # #
# # #
# #
# #
-> 86
-> 81
-> 52
-> 88
-> 43
In [ ]: