Skip to content

Instantly share code, notes, and snippets.

@andersonvom
Created September 11, 2022 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersonvom/2814c1031fe11b938d6dd2d26f897c8a to your computer and use it in GitHub Desktop.
Save andersonvom/2814c1031fe11b938d6dd2d26f897c8a to your computer and use it in GitHub Desktop.
Quantos pacotes sao necessarios para preencher um album de figurinhas? Simule aqui!
#!/usr/bin/env python
import json
import random
import sys
import statistics
def gerar_pacote(total, por_pacote):
return [ random.randint(1, total) for i in range(por_pacote) ]
def simular_pacotes_por_album(total, por_pacote):
album = set()
pacotes = 0
while len(album) < total:
album = album.union(gerar_pacote(total, por_pacote))
pacotes += 1
return pacotes
def estatisticas_total_de_pacotes(total, por_pacote, numero_albuns):
resultados = sorted([
simular_pacotes_por_album(total, por_pacote)
for i in range(numero_albuns)
])
return {
'min': resultados[0],
'25o percentil': round(statistics.quantiles(resultados, n=4)[0]),
'mediana': statistics.median(resultados),
'media': statistics.mean(resultados),
'moda': statistics.mode(resultados),
'max': resultados[-1],
}
# Usage: $0 <total-de-figurinhas> <figurinhas-por-pacote> <numero-de-albuns>
if __name__ == '__main__':
total = int(sys.argv[1])
por_pacote = int(sys.argv[2])
numero_albuns = int(sys.argv[3])
resultados = estatisticas_total_de_pacotes(total, por_pacote, numero_albuns)
print("Total de figurinhas: \t %s" % (total))
print("Figurinhas por pacote: \t %s" % (por_pacote))
print("Pacotes comprados:")
print(json.dumps(resultados, indent=2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment