Aula07 - Ex05 adicionado (alinea d) não incluida)

This commit is contained in:
tiagorg 2022-11-11 14:43:34 +00:00
parent 5acd5cd67d
commit c795d877a4
2 changed files with 39 additions and 3 deletions

View File

@ -1,7 +1,7 @@
# Fundamentos de Programação # Fundamentos de Programação
## Aula 07 - [Slides](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/fp/slides/tp07-dictionaries.pdf) ## Aula 07 - [Slides](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/fp/slides/tp07-dictionaries.pdf)
### Tópico principal da aula: Dictionaries ### Tópico principal da aula: Dictionaries
#### Exercícios terminados: ex01, ex02, ex03, ex04 #### Exercícios em falta: ex05.d)
--- ---
*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new) *Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)

View File

@ -1,9 +1,12 @@
import re
# Constantes para indexar os tuplos: # Constantes para indexar os tuplos:
NAME,DATE,OPEN,MAX,MIN,CLOSE,VOLUME = 0,1,2,3,4,5,6 NAME, DATE, OPEN, MAX, MIN, CLOSE, VOLUME = 0, 1, 2, 3, 4, 5, 6
def main(): def main():
lst = loadStockFile("nasdaq.csv") lst = loadStockFile("datafiles/nasdaq.csv")
# Show just first and last tuples: # Show just first and last tuples:
print("first:", lst[1]) print("first:", lst[1])
print("last:", lst[-1]) print("last:", lst[-1])
@ -16,10 +19,12 @@ def main():
print("c) CSCO@11:", stocksDic['2020-10-12']['CSCO']) print("c) CSCO@11:", stocksDic['2020-10-12']['CSCO'])
print("c) AMZN@22:", stocksDic['2020-10-22']['AMZN']) print("c) AMZN@22:", stocksDic['2020-10-22']['AMZN'])
# Parte incompleta
port = {'NFLX': 100, 'CSCO': 80} port = {'NFLX': 100, 'CSCO': 80}
print("d) portfolio@01:", portfolioValue(stocksDic, port, "2020-10-01")) print("d) portfolio@01:", portfolioValue(stocksDic, port, "2020-10-01"))
print("d) portfolio@30:", portfolioValue(stocksDic, port, "2020-10-30")) print("d) portfolio@30:", portfolioValue(stocksDic, port, "2020-10-30"))
def loadStockFile(filename): def loadStockFile(filename):
lst = [] lst = []
with open(filename) as f: with open(filename) as f:
@ -32,24 +37,54 @@ def loadStockFile(filename):
lst.append(tup) lst.append(tup)
return lst return lst
def totalVolume(lst): def totalVolume(lst):
totVol = {} totVol = {}
# Complete ... # Complete ...
for tup in totVol:
if tup[NAME] not in totVol:
totVol[tup[NAME]] = tup[VOLUME]
else:
totVol[tup[NAME]] += tup[VOLUME]
return totVol return totVol
def maxValorization(lst): def maxValorization(lst):
vMax = {} vMax = {}
# Complete ... # Complete ...
for data in range(1, 31):
maxDiario = 0
maxDiarioComp = "No data"
for tup in lst:
pat = r"0[1-9]"
dia = tup[DATE].split('-')[2]
if re.match(pat, dia):
dia = dia[1]
if data == int(dia):
maxcomp = tup[OPEN]/tup[CLOSE] * 100
if maxcomp > maxDiario:
maxDiario = maxcomp
maxDiarioComp = tup[NAME]
if maxDiarioComp != "No data":
vMax[data] = (maxDiarioComp, f"{maxDiario:.1f}%")
return vMax return vMax
def stocksByDateByName(lst): def stocksByDateByName(lst):
dic = {} dic = {}
# Complete ... # Complete ...
for tup in lst:
if tup[DATE] not in dic:
dic[tup[DATE]] = {}
dic[tup[DATE]][tup[NAME]] = tup
return dic return dic
# Função não completa
def portfolioValue(stocks, portfolio, date): def portfolioValue(stocks, portfolio, date):
assert date in stocks assert date in stocks
val = 0.0 val = 0.0
@ -57,5 +92,6 @@ def portfolioValue(stocks, portfolio, date):
return val return val
if __name__ == "__main__": if __name__ == "__main__":
main() main()