Last active
December 20, 2019 15:17
-
-
Save Bundi-py/e9bbedc1f8fb437d7a1e019f77600897 to your computer and use it in GitHub Desktop.
Kod zadatog broja bacanja novčića videti koliko će se puta pojaviti niz od 2, 3, 4, 5 i 6 ponavljanja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
print('Koliko puta da bacim novčić?') | |
while True: | |
odgovor = input() | |
if odgovor.isdecimal(): | |
brojBacanja = int(odgovor) | |
break | |
with open('pismo_glava.txt', 'a+') as file: | |
for i in range (1, 100): | |
if random.randint(0, 1) == 0: | |
baci = '0' | |
file.write(baci) | |
else: | |
baci = '1' | |
file.write(baci) | |
with open('pismo_glava.txt', 'r') as file: | |
podaci = file.read() | |
print('Broj glava je:', podaci.count('000000')) # dodatak by tuxserbia | |
print('Broj pisama je:', podaci.count('111111')) # | |
for i in range(2, 5): # dodatak by Panta | |
print(f"{'0' * i} = {data.count('0' * i)}") # | |
Output: | |
Koliko puta da bacim novčić? | |
1000 | |
Niz od 6 glava je: 3 puta. | |
Niz od 6 pisama je: 4 puta. | |
00 = 105 | |
000 = 42 | |
0000 = 20 | |
00000 = 8 | |
******************* | |
Rešenje br. 2 by Deunan: | |
import random | |
brojNizova = 0 # broj nizova od 6 istih | |
trenutniStatus = 0 # prethodni bacen status, pismo ili glava (0 ili 1) | |
brojPonavljanja = 0 | |
for i in range(10000): | |
rand = random.randint(0, 1) | |
if trenutniStatus == rand: | |
brojPonavljanja += 1 | |
else: | |
trenutniStatus = rand | |
brojPonavljanja = 1 | |
if brojPonavljanja == 6: | |
brojNizova += 1 | |
brojPonavljanja = 0 | |
print(f"Broj nizova: {brojNizova}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment