From c795d877a44e80d37f07053b5c8cd2d53da35865 Mon Sep 17 00:00:00 2001 From: tiagorg Date: Fri, 11 Nov 2022 14:43:34 +0000 Subject: [PATCH] =?UTF-8?q?Aula07=20-=20Ex05=20adicionado=20(alinea=20d)?= =?UTF-8?q?=20n=C3=A3o=20incluida)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1ano/fp/aula07/README.md | 2 +- 1ano/fp/aula07/stocks.py | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/1ano/fp/aula07/README.md b/1ano/fp/aula07/README.md index 6ac46b2..58a892a 100644 --- a/1ano/fp/aula07/README.md +++ b/1ano/fp/aula07/README.md @@ -1,7 +1,7 @@ # Fundamentos de Programação ## Aula 07 - [Slides](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/fp/slides/tp07-dictionaries.pdf) ### 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) diff --git a/1ano/fp/aula07/stocks.py b/1ano/fp/aula07/stocks.py index 0d224a6..8f74fa3 100755 --- a/1ano/fp/aula07/stocks.py +++ b/1ano/fp/aula07/stocks.py @@ -1,9 +1,12 @@ +import re + # 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(): - lst = loadStockFile("nasdaq.csv") + lst = loadStockFile("datafiles/nasdaq.csv") # Show just first and last tuples: print("first:", lst[1]) print("last:", lst[-1]) @@ -16,10 +19,12 @@ def main(): print("c) CSCO@11:", stocksDic['2020-10-12']['CSCO']) print("c) AMZN@22:", stocksDic['2020-10-22']['AMZN']) + # Parte incompleta port = {'NFLX': 100, 'CSCO': 80} print("d) portfolio@01:", portfolioValue(stocksDic, port, "2020-10-01")) print("d) portfolio@30:", portfolioValue(stocksDic, port, "2020-10-30")) + def loadStockFile(filename): lst = [] with open(filename) as f: @@ -32,24 +37,54 @@ def loadStockFile(filename): lst.append(tup) return lst + def totalVolume(lst): totVol = {} # Complete ... + for tup in totVol: + if tup[NAME] not in totVol: + totVol[tup[NAME]] = tup[VOLUME] + else: + totVol[tup[NAME]] += tup[VOLUME] return totVol + def maxValorization(lst): vMax = {} # 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 + def stocksByDateByName(lst): dic = {} # Complete ... + for tup in lst: + if tup[DATE] not in dic: + dic[tup[DATE]] = {} + dic[tup[DATE]][tup[NAME]] = tup return dic + +# Função não completa def portfolioValue(stocks, portfolio, date): assert date in stocks val = 0.0 @@ -57,5 +92,6 @@ def portfolioValue(stocks, portfolio, date): return val + if __name__ == "__main__": main()