Sie sind auf Seite 1von 4

######################################################

###### DICTIONARIES
######################################################

nombres = ['Ana', 'John', 'Denise', 'Katy']


notas = ['B', 'A+', 'A', 'A']
curso = ['IS141', 'MA121', 'FS131', 'BI111']
def obtener_notas(estudiante, nombre_lista, notas_lista, curso_lista):
i = nombre_lista.index(estudiante)
notas = notas_lista[i]
curso = curso_lista[i]
return (curso, notas)

print('Ana', obtener_notas('Ana',nombres,notas,curso))
######################################################
###### DICTIONARIOS
######################################################

notas = {'Ana':'B', 'John':'A+', 'Denise':'A', 'Katy':'A'}

########################################################
######## CREANDO UN DICIONARIO
########################################################

def cancion_a_frecuencias(cancion):
miDic = {}
for palabra in cancion:
if palabra in miDic:
miDic[palabra] += 1
else:
miDic[palabra] = 1
return miDic

she_loves_you = ['she', 'loves', 'you', 'yeah', 'yeah', 'yeah',


'she', 'loves', 'you', 'yeah', 'yeah', 'yeah',
'she', 'loves', 'you', 'yeah', 'yeah', 'yeah',

'you', 'think', "you've", 'lost', 'your', 'love',


'well', 'i', 'saw', 'her', 'yesterday-yi-yay',
"it's", 'you', "she's", 'thinking', 'of',
'and', 'she', 'told', 'me', 'what', 'to', 'say-yi-yay',

'she', 'says', 'she', 'loves', 'you',


'and', 'you', 'know', 'that', "can't", 'be', 'bad',
'yes', 'she', 'loves', 'you',
'and', 'you', 'know', 'you', 'should', 'be', 'glad',

'she', 'said', 'you', 'hurt', 'her', 'so',


'she', 'almost', 'lost', 'her', 'mind',
'and', 'now', 'she', 'says', 'she', 'knows',
"you're", 'not', 'the', 'hurting', 'kind',

'she', 'says', 'she', 'loves', 'you',


'and', 'you', 'know', 'that', "can't", 'be', 'bad',
'yes', 'she', 'loves', 'you',
'and', 'you', 'know', 'you', 'should', 'be', 'glad',
'oo', 'she', 'loves', 'you', 'yeah', 'yeah', 'yeah',
'she', 'loves', 'you', 'yeah', 'yeah', 'yeah',
'with', 'a', 'love', 'like', 'that',
'you', 'know', 'you', 'should', 'be', 'glad',

'you', 'know', "it's", 'up', 'to', 'you',


'i', 'think', "it's", 'only', 'fair',
'pride', 'can', 'hurt', 'you', 'too',
'pologize', 'to', 'her',

'Because', 'she', 'loves', 'you',


'and', 'you', 'know', 'that', "can't", 'be', 'bad',
'Yes', 'she', 'loves', 'you',
'and', 'you', 'know', 'you', 'should', 'be', 'glad',

'oo', 'she', 'loves', 'you', 'yeah', 'yeah', 'yeah',


'she', 'loves', 'you', 'yeah', 'yeah', 'yeah',
'with', 'a', 'love', 'like', 'that',
'you', 'know', 'you', 'should', 'be', 'glad',
'with', 'a', 'love', 'like', 'that',
'you', 'know', 'you', 'should', 'be', 'glad',
'with', 'a', 'love', 'like', 'that',
'you', 'know', 'you', 'should', 'be', 'glad',
'yeah', 'yeah', 'yeah',
'yeah', 'yeah', 'yeah', 'yeah'
]

beatles = cancion_a_frecuencias(she_loves_you)
print(beatles)

def palabras_mas_comunes(frecs):
valores = frecs.values()
mayor = max(frecs.values())
palabras = []
for k in frecs:
if frecs[k] == mayor:
palabras.append(k)
return (palabras, mayor)

def palabras_frecuentes(frecs, minVeces):


resultado = []
hecho = False
while not hecho:
temp = palabras_mas_comunes(frecs)
if temp[1] >= minVeces:
resultado.append(temp)
for w in temp[0]:
del(frecs[w]) #remueve palabra del diccionario
else:
hecho = True
return resultado

print()

print(palabras_frecuentes(beatles, 2))
######################################################
###### FIBONACCI CON UN DICCIONARIO
######################################################

def fib(n):
if n == 1:
return 1
elif n == 2:
return 2
else:
return fib(n-1) + fib(n-2)

def fib_efic(n, d):


if n in d:
return d[n]
else:
resp = fib_efic(n-1, d) + fib_efic(n-2, d)
d[n] = resp
print(d,resp)
return resp

d = {1:1, 2:2}

argAUsar = 34
print("")
print('usando fib')
print(fib(argAUsar))
print("")
print('usando fib_efic')
print(fib_efic(argAUsar, d))

######################################################
###### FIBONACCI CON UN DICCIONARIO: VARIABLE GLOBAL
######################################################

numFibLlamadas = 0
def fib(n):
global numFibLlamadas
numFibLlamadas += 1
if n == 1:
return 1
elif n == 2:
return 2
else:
return fib(n-1) + fib(n-2)

argAUsar = 34
print("")
print('usando fib')
print(fib(argAUsar))
print('Llama a funcion', numFibLlamadas)

numFibLlamadas = 0
def fib_efic(n, d):
global numFibLlamadas
numFibLlamadas += 1
if n in d:
return d[n]
else:
resp = fib_efic(n-1, d) + fib_efic(n-2, d)
d[n] = resp
## print(d,resp)
return resp

d = {1:1, 2:2}

print("")
print('usando fib_efic')
print(fib_efic(argAUsar, d))
print('Llama a funcion', numFibLlamadas)

Das könnte Ihnen auch gefallen