Last active
November 16, 2016 18:56
-
-
Save mvallebr/a8260e724dffc69722592de8dc42c154 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| def sortear(): | |
| return [5,1,1,2] | |
| def entrar(): | |
| return [int(x) for x in input().split()] | |
| l_sorteio = sortear() | |
| l_entrada = entrar() | |
| def print_resultado(descricao, d_resultado): | |
| l_resultado = [] | |
| for i in d_resultado.keys(): | |
| l_resultado.extend([i for _ in range(d_resultado[i])]) | |
| print (descricao, " ".join([str(x) for x in l_resultado])) | |
| # Encontre todos os numeros de entrada que aparecem em sorteio, considerando o numero de vezes que o numero aparece em sorteio | |
| # echo 1 2 3 3 3 2 4 | python3 problema_sorteio_bruno.py | |
| # SOLUCAO LOOP FOR | |
| from collections import defaultdict | |
| i_sorteio = 0 | |
| i_entrada = 0 | |
| l_entrada.sort() | |
| l_sorteio.sort() | |
| d_resultado = defaultdict(int) | |
| while i_sorteio < len(l_sorteio) and i_entrada < len(l_entrada): | |
| if l_sorteio[i_sorteio] == l_entrada[i_entrada]: | |
| d_resultado [l_sorteio[i_sorteio]] += 1 | |
| i_sorteio += 1 | |
| i_entrada += 1 | |
| elif l_sorteio[i_sorteio] > l_entrada[i_entrada]: | |
| i_entrada += 1 | |
| else: | |
| i_sorteio += 1 | |
| print ("resultado loop for: ", str(dict(d_resultado))) | |
| print_resultado ("resultado loop for: ", d_resultado) | |
| # SOLUCAO DICTS INTERMEDIARIOS COM DICT COMPREHENSION ou merge | |
| d_sorteio = {k : l_sorteio.count(k) for k in set(l_sorteio)} | |
| d_entrada = {k : l_entrada.count(k) for k in set(l_entrada)} | |
| print ("sorteio: ", str(d_sorteio)) | |
| print ("entrada: ", str(d_entrada)) | |
| d_resultado = {} | |
| for k in d_sorteio.keys() & d_entrada.keys(): | |
| d_resultado[k] = min(d_sorteio[k], d_entrada[k]) | |
| print ("resultado dicts + loop dicionarios: ", str(d_resultado)) | |
| print_resultado ("resultado dicts + loop dicionarios: ", d_resultado) | |
| d_resultado = {k : min(d_sorteio[k], d_entrada[k]) for k in d_sorteio.keys() & d_entrada.keys()} | |
| print ("resultado dicts + dict comprehension: ", str(d_resultado)) | |
| print_resultado ("resultado dicts + dict comprehension: ", d_resultado) | |
| # SOLUCAO 1 DICT COMPREHENSION | |
| d_resultado = {k : min(l_sorteio.count(k), l_entrada.count(k)) for k in set(l_sorteio) & set(l_entrada)} | |
| print ("resultado 1 dict comprehension: ", str(d_resultado)) | |
| print_resultado ("resultado 1 dict comprehension: ", d_resultado) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment