Last active
March 15, 2016 05:55
-
-
Save JuniorPolegato/1c3b7995475af00a899e 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
arvore = [ | |
[0, False], | |
[1, True], | |
[1, False], | |
[2, False], | |
[2, True], | |
[2, False], | |
[3, False], | |
[3, True], | |
[2, False], | |
[1, False], | |
[2, False] | |
] | |
def resolver1(arvore): | |
NIVEL = 0 | |
VALOR = 1 | |
PESO = 2 | |
arvore = map(lambda (n, v): [n, v * 100., 1.], arvore) | |
p = len(arvore) - 1 | |
print p, arvore | |
while p: | |
if arvore[p][NIVEL] > arvore[p - 1][NIVEL]: | |
arvore[p - 1][VALOR] = arvore[p][VALOR] | |
elif arvore[p][NIVEL] == arvore[p - 1][NIVEL]: | |
arvore[p - 1][PESO] = arvore[p][PESO] + 1 | |
arvore[p - 1][VALOR] = ((arvore[p][VALOR] * arvore[p][PESO] | |
+ arvore[p - 1][VALOR]) / | |
arvore[p - 1][PESO]) | |
else: | |
p -= 1 | |
continue | |
del arvore[p] | |
if p == len(arvore): | |
p -= 1 | |
print p, arvore | |
return arvore[0][VALOR] | |
def resolver2(arvore): | |
arvore = map(lambda (n, v): [n, v * 100.], arvore) | |
print arvore | |
while len(arvore) > 1: | |
niveis, valores = zip(*arvore) | |
nivel_maior = max(niveis) | |
inicial = niveis.index(nivel_maior) | |
final = inicial + 1 | |
while final < len(niveis) and niveis[final] == nivel_maior: | |
final += 1 | |
arvore[inicial - 1][1] = sum(valores[inicial:final]) / (final - inicial) | |
del arvore[inicial:final] | |
print arvore | |
return arvore[0][1] | |
print "\nResolvido 1: %.2f%%\n" % resolver1(arvore) | |
print "\nResolvido 2: %.2f%%\n" % resolver2(arvore) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment