1 июня 2023
P D P D P
A P 1. возможно было неясно. но лучше чем что? у человека ассерт на функцию которая судя по названию возвращает bool там что ни == ни is not None сюда не подходят. у человека ассереквал значит единственный вариант за который я мог спросить это assertIsNot. но да ладно 2. без скринов да. но кто мог знать что человек не различает код и вывод в консоль. а если рассмотрел то это в нашем контексте тестов вредный совет что думаю не приветствуется( на который я хотел указать а мне доказывают обратное) 3. а как назвать человека который играет кармой с плохими советами? или тут кармовойны лучше?
Всем привет!
Решал интерeсную задачу, но не решил
Условие:
For this exercise, we’ll try doing an experiment. If you flip a coin 100 times and write down an “H” for each heads and “T” for each tails, you’ll create a list that looks like “T T T T H H H H T T.” If you ask a human to make up 100 random coin flips, you’ll probably end up with alternating head-tail results like “H T H T H H T H T T,” which looks random (to humans), but isn’t mathematically random. A human will almost never write down a streak of six heads or six tails in a row, even though it is highly likely to happen in truly random coin flips. Humans are predictably bad at being random.
Write a program to find out how often a streak of six heads or a streak of six tails comes up in a randomly generated list of heads and tails. Your program breaks up the experiment into two parts: the first part generates a list of randomly selected 'heads' and 'tails' values, and the second part checks if there is a streak in it. Put all of this code in a loop that repeats the experiment 10,000 times so we can find out what percentage of the coin flips contains a streak of six heads or tails in a row. As a hint, the function call random.randint(0, 1) will return a 0 value 50% of the time and a 1 value the other 50% of the time.
Мое решение:
import random
numberOfStreaks = 0
ist = []
six_h = 'H' * 6
six_t = 'T' * 6
for experimentNumber in range(10000):
# Code that creates a list of 100 'heads' or 'tails' values.
x = random.randint(0, 1)
if x == 0:
ist.append('H')
else:
ist.append('T')
# Code that checks if there is a streak of 6 heads or tails in a row.
if six_h in ist or six_t in ist:
numberOfStreaks += 1
print(numberOfStreaks)
Подскажите, где может быть ошибка ? Я думаю что в if six_h in ist or six_t in ist, но не знаю в чем
M import random numberOfStreaks = 0 six_h = ['H'] * 6 six_t = ['T'] * 6 for experimentNumber in range(10000): # Code that creates a list of 100 'heads' or 'tails' values. listik = [] for coin in range(100): side = random.randint(0, 1) if side == 0: listik.append('H') else: listik.append('T') if six_h in listik or six_t in listik: numberOfStreaks += 1 print(numberOfStreaks)
>>> sum(1 for _ in range(10000) if any(x in ''.join(random.choices(['H', 'T'], k=100)) for x in ['H'*6, 'T'*6])) / 10000
0.7943
>>> sum(1 for _ in range(10000) if any(x in ''.join(random.choices(['H', 'T'], k=100)) for x in ['H'*6, 'T'*6])) / 10000
0.7918
>>> sum(1 for _ in range(10000) if any(x in ''.join(random.choices(['H', 'T'], k=100)) for x in ['H'*6, 'T'*6])) / 10000
0.7913
>>> sum(1 for _ in range(10000) if any(x in ''.join(random.choices(['H', 'T'], k=100)) for x in ['H'*6, 'T'*6])) / 10000
0.7977
>>> sum(1 for _ in range(10000) if any(x in ''.join(random.choices(['H', 'T'], k=100)) for x in ['H'*6, 'T'*6])) / 10000
0.7995
import random
num_experiments = 10000
streak_count = 0
for _ in range(num_experiments):
flips = []
for _ in range(100):
flip = random.randint(0, 1)
if flip == 0:
flips.append('H')
else:
flips.append('T')
has_streak = False
for i in range(len(flips) - 5):
if flips[i:i+6] == ['H', 'H', 'H', 'H', 'H', 'H'] or flips[i:i+6] == ['T', 'T', 'T', 'T', 'T', 'T']:
has_streak = True
break
if has_streak:
streak_count += 1
print(streak_count / 100)