diff --git a/1ano/fp/aula07/README.md b/1ano/fp/aula07/README.md new file mode 100644 index 0000000..b711b9a --- /dev/null +++ b/1ano/fp/aula07/README.md @@ -0,0 +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 + +--- +*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new) \ No newline at end of file diff --git a/1ano/fp/aula07/aula07.pdf b/1ano/fp/aula07/aula07.pdf new file mode 100755 index 0000000..dbf4bb0 Binary files /dev/null and b/1ano/fp/aula07/aula07.pdf differ diff --git a/1ano/fp/aula07/coins.py b/1ano/fp/aula07/coins.py new file mode 100755 index 0000000..c5f4f3c --- /dev/null +++ b/1ano/fp/aula07/coins.py @@ -0,0 +1,79 @@ +# A set of functions to deal with bags of money. +# +# JMR 2017 + + +# Face values of coins (in cents): +COINS = [200, 100, 50, 20, 10, 5, 2, 1] + +def value(bag): + """Return total amount in a bag.""" + ... + + +def transfer1coin(bag1, c, bag2): + """Try to transfer one coin of value c from bag1 to bag2. + If possible, transfer coin and return True, otherwise return False.""" + ... + + +def transfer(bag1, amount, bag2): + """Try to transfer an amount from bag1 to bag2. + If possible, transfer coins and return True, + otherwise, return False and leave bags with same values.""" + if amount == 0: + return True + if value(bag1) < amount: + return False + ... + + +def strbag(bag): + """Return a string representing the contents of a bag.""" + # You may want to change this to produce a more user-friendly + # representation such as "4x200+3x50+1x5+3x1=958". + return str(bag) + ... + + +def main(): + # A bag of coins is represented by a dict of {coin: number} items + bag1 = {1: 4, 2: 0, 5:1, 10: 0, 20: 5, 50: 4, 100: 2, 200: 1} + bag2 = {} + + # Test the value function. + assert value({}) == 0 + assert value({1:7, 5:2, 20:4, 100:1}) == 197 + + # Test the strbag function. + print( strbag({1:7, 5:2, 20:4, 100:1}) ) # 1x100+4x20+2x5+7x1=197 + print( strbag({1:7, 5:2, 10:0, 20:4, 100:1}) ) # 1x100+4x20+2x5+7x1=197 + + print("bag1:", strbag(bag1)) # bag1: 1x200+2x100+4x50+5x20+1x5+4x1=709 + print("bag2:", strbag(bag2)) # bag2: =0 + + print(transfer1coin(bag1, 10, bag2)) # False! + print("bag1:", strbag(bag1)) # bag1: 1x200+2x100+4x50+5x20+1x5+4x1=709 + print("bag2:", strbag(bag2)) # bag2: =0 + + print(transfer1coin(bag1, 20, bag2)) # True + print("bag1:", strbag(bag1)) # bag1: 1x200+2x100+4x50+4x20+1x5+4x1=689 + print("bag2:", strbag(bag2)) # bag2: 1x20=20 + + print(transfer1coin(bag1, 20, bag2)) # True + print("bag1:", strbag(bag1)) # bag1: 1x200+2x100+4x50+3x20+1x5+4x1=669 + print("bag2:", strbag(bag2)) # bag2: 2x20=40 + + print(transfer(bag1, 157, bag2)) # True (should be easy) + print("bag1:", strbag(bag1)) # bag1: 1x200+1x100+3x50+3x20+2x1=512 + print("bag2:", strbag(bag2)) # bag2: 1x100+1x50+2x20+1x5+2x1=197 + + print(transfer(bag1, 60, bag2)) # not easy, but possible... + print("bag1:", strbag(bag1)) + print("bag2:", strbag(bag2)) + + return + +if __name__ == "__main__": + main() + diff --git a/1ano/fp/aula07/countLetters.py b/1ano/fp/aula07/countLetters.py new file mode 100644 index 0000000..3ac32f0 --- /dev/null +++ b/1ano/fp/aula07/countLetters.py @@ -0,0 +1,26 @@ +import sys + + +def main(): + # Read the file + f = open(sys.argv[1], 'r') + text = f.read() + f.close() + + # Count the letters + letters = {} + for c in text: + if c.isalpha(): + c = c.lower() + if c in letters: + letters[c] += 1 + else: + letters[c] = 1 + + # Print the results + for c in sorted(letters.keys()): + print(c, letters[c]) + + +if __name__ == "__main__": + main() diff --git a/1ano/fp/aula07/nasdaq.csv b/1ano/fp/aula07/nasdaq.csv new file mode 100755 index 0000000..53e5a3a --- /dev/null +++ b/1ano/fp/aula07/nasdaq.csv @@ -0,0 +1,220 @@ +CSCO 2020-10-01 39.1 39.31 38.57 38.8 20763541 +CSCO 2020-10-02 38.05 38.55 37.97 38.27 24492961 +CSCO 2020-10-05 38.48 38.79 38.28 38.57 26929054 +CSCO 2020-10-06 38.5 39.35 38.4 38.57 28430813 +CSCO 2020-10-07 38.98 39.58 38.78 39.4 20593711 +CSCO 2020-10-08 39.8 40.18 39.66 39.79 19031346 +CSCO 2020-10-09 40.15 40.23 39.72 39.85 16167474 +CSCO 2020-10-12 39.93 40.53 39.63 40.36 22757245 +CSCO 2020-10-13 40.54 40.61 39.71 39.81 23438266 +CSCO 2020-10-14 39.85 40.42 39.76 39.89 16444686 +CSCO 2020-10-15 39.47 40 39.35 39.97 15456971 +CSCO 2020-10-16 39.88 40.3 39.55 40.16 21295117 +CSCO 2020-10-19 40.15 40.38 39.16 39.3 19600466 +CSCO 2020-10-20 39.44 39.51 39.03 39.2 16564890 +CSCO 2020-10-21 39.31 39.57 38.98 39.06 17016266 +CSCO 2020-10-22 38.85 38.96 38.38 38.82 20399040 +CSCO 2020-10-23 38.93 38.97 38.65 38.82 15556084 +CSCO 2020-10-26 38.4 38.43 37.15 37.67 29146548 +CSCO 2020-10-27 37.38 37.5 36.59 36.87 37911471 +CSCO 2020-10-28 36.32 36.51 35.68 35.71 34561490 +CSCO 2020-10-29 35.51 36.07 35.28 35.69 35331966 +CSCO 2020-10-30 35.6 35.93 35.49 35.9 29159659 +NFLX 2020-10-01 506.03 529.55 503.6 527.51 8153710 +NFLX 2020-10-02 516.43 526.37 502.7 503.06 6071165 +NFLX 2020-10-05 506.8 520.99 501.7 520.65 4088069 +NFLX 2020-10-06 518.72 521.82 504.16 505.87 4199034 +NFLX 2020-10-07 518 536.49 515.82 534.66 7988683 +NFLX 2020-10-08 533.48 535 524.15 531.79 5386488 +NFLX 2020-10-09 537.83 544.28 535 539.44 4781259 +NFLX 2020-10-12 548.81 551.81 538.24 539.81 5391074 +NFLX 2020-10-13 540.56 557.65 537.2 554.09 5602498 +NFLX 2020-10-14 562.61 572.49 541 541.45 9510081 +NFLX 2020-10-15 545.52 551.22 535.1 541.94 5113126 +NFLX 2020-10-16 549.5 554.33 530.03 530.79 6347363 +NFLX 2020-10-19 537.07 541.8 525.38 530.72 7567506 +NFLX 2020-10-20 528.14 533.78 522.26 525.42 10047175 +NFLX 2020-10-21 501.03 506.85 488.25 489.05 17405708 +NFLX 2020-10-22 494.69 495.14 482 485.23 6997922 +NFLX 2020-10-23 488.11 490.06 481.35 488.28 4927862 +NFLX 2020-10-26 487.03 496.82 478.9 488.24 6186087 +NFLX 2020-10-27 490.01 490.49 482.93 488.93 3627175 +NFLX 2020-10-28 486.36 494 483.28 486.24 5992731 +NFLX 2020-10-29 488.5 513.9 479.34 504.21 11120689 +NFLX 2020-10-30 502.01 505.88 472.21 475.74 7813897 +INTC 2020-10-01 52.4 52.72 51.99 52.24 22127580 +INTC 2020-10-02 51.5 51.98 50.99 51.01 25811766 +INTC 2020-10-05 51.3 51.81 51.27 51.69 20883245 +INTC 2020-10-06 51.71 52.7 51.23 51.37 25642790 +INTC 2020-10-07 51.85 52.9 51.79 52.67 21978673 +INTC 2020-10-08 52.91 53.58 52.67 53.37 22369537 +INTC 2020-10-09 53.55 53.85 52.67 52.82 24343898 +INTC 2020-10-12 53.55 54.2 53.21 53.88 27303474 +INTC 2020-10-13 54.27 54.29 53.62 53.83 20005769 +INTC 2020-10-14 54.01 54.4 53.51 53.55 21885105 +INTC 2020-10-15 52.75 53.98 52.62 53.85 18272865 +INTC 2020-10-16 53.9 54.7 53.9 54.16 23421388 +INTC 2020-10-19 54.47 56.23 53.84 54.58 43537219 +INTC 2020-10-20 54.6 54.7 53.27 53.43 27224899 +INTC 2020-10-21 53.14 54.07 52.82 53.5 21866025 +INTC 2020-10-22 53.12 54.22 53.07 53.9 41522739 +INTC 2020-10-23 48.35 48.67 47.66 48.2 97322490 +INTC 2020-10-26 47.85 47.86 46.25 46.72 54161291 +INTC 2020-10-27 45.98 46.41 45.36 45.64 50130518 +INTC 2020-10-28 44.89 45.29 44.2 44.25 45066011 +INTC 2020-10-29 44.24 44.47 43.92 44.11 36796801 +INTC 2020-10-30 44.1 44.8 43.61 44.28 46794171 +QCOM 2020-10-01 119.93 120.21 117.7 119.52 5316766 +QCOM 2020-10-02 116.78 118.66 115.43 115.47 5834337 +QCOM 2020-10-05 116.9 120.65 116.73 120.52 5835936 +QCOM 2020-10-06 120.45 122.52 119.1 119.54 7636963 +QCOM 2020-10-07 121.24 123.36 120.65 123.03 7410347 +QCOM 2020-10-08 124 124.43 121.84 122.34 4757536 +QCOM 2020-10-09 123.93 125.89 122.79 124.87 11073412 +QCOM 2020-10-12 127.7 127.7 124.95 126.69 7893835 +QCOM 2020-10-13 127.47 128.18 125.4 127.46 7605706 +QCOM 2020-10-14 129.08 132.42 128.93 129.88 13183613 +QCOM 2020-10-15 127.3 129.14 126.65 128.58 7377948 +QCOM 2020-10-16 129.7 130.19 127.74 129.03 6626992 +QCOM 2020-10-19 130.13 131.96 127.7 128.42 5765840 +QCOM 2020-10-20 129.18 129.33 127.51 128.3 4413343 +QCOM 2020-10-21 128.04 130.42 126.37 128.55 4343992 +QCOM 2020-10-22 129.51 129.59 126.86 128.38 3897002 +QCOM 2020-10-23 128.59 129.1 127.1 128.88 3943056 +QCOM 2020-10-26 127.53 128.67 124.4 126.2 6065923 +QCOM 2020-10-27 126.51 126.96 125.19 125.91 5825518 +QCOM 2020-10-28 123.5 123.82 121.05 121.58 8234821 +QCOM 2020-10-29 122.06 127.69 121.89 126.44 7053259 +QCOM 2020-10-30 123.68 125.25 121.6 123.36 7814462 +EA 2020-10-01 132.07 134.06 131.46 132.78 1868992 +EA 2020-10-02 131.37 132.24 127.71 129.07 1751296 +EA 2020-10-05 130.09 132.95 129.07 130.72 1270110 +EA 2020-10-06 130.36 130.4 124.52 124.84 3753713 +EA 2020-10-07 126.41 126.5 123.97 125.01 3257483 +EA 2020-10-08 126.1 126.38 124.42 125.99 1959558 +EA 2020-10-09 126.86 128.44 125.92 127.45 1991745 +EA 2020-10-12 128.98 132.07 127.83 130.88 2106617 +EA 2020-10-13 131.87 133.54 130.16 132.94 2117365 +EA 2020-10-14 133.79 133.86 131.74 132.75 2520501 +EA 2020-10-15 131.07 132.41 129.88 131.34 1903434 +EA 2020-10-16 132.5 133.26 131.05 131.92 1925033 +EA 2020-10-19 133.14 133.89 129.51 129.87 1454331 +EA 2020-10-20 130.42 130.6 127.09 127.36 2249230 +EA 2020-10-21 127.98 129.45 127.3 127.65 2200937 +EA 2020-10-22 127.78 128.01 125.4 126.92 1792367 +EA 2020-10-23 127.24 127.65 125.53 126.77 1215209 +EA 2020-10-26 126.41 127.5 125.03 126.68 2166019 +EA 2020-10-27 127.36 127.58 126.1 126.62 1452780 +EA 2020-10-28 125.62 127.07 124.81 125.6 2079571 +EA 2020-10-29 125.79 126.17 122.77 123.9 2633669 +EA 2020-10-30 122.99 124.09 118.52 119.83 3313123 +PTI 2020-10-01 1.08 1.12 1.08 1.1 205885 +PTI 2020-10-02 1.08 1.13 1.05 1.07 291697 +PTI 2020-10-05 1.07 1.14 1.07 1.12 516326 +PTI 2020-10-06 1.12 1.17 1.09 1.09 377764 +PTI 2020-10-07 1.11 1.18 1.1 1.17 410158 +PTI 2020-10-08 1.18 1.2 1.15 1.17 211728 +PTI 2020-10-09 1.16 1.19 1.15 1.15 105444 +PTI 2020-10-12 1.15 1.18 1.12 1.14 209001 +PTI 2020-10-13 1.14 1.2 1.14 1.19 208808 +PTI 2020-10-14 1.18 1.35 1.15 1.17 2017692 +PTI 2020-10-15 1.16 1.23 1.11 1.19 408194 +PTI 2020-10-16 1.2 1.25 1.16 1.23 378098 +PTI 2020-10-19 1.24 1.25 1.16 1.17 320322 +PTI 2020-10-20 1.19 1.19 1.14 1.17 244802 +PTI 2020-10-21 1.17 1.17 1.12 1.13 181417 +PTI 2020-10-22 1.14 1.15 1.1 1.12 165814 +PTI 2020-10-23 1.14 1.14 1.1 1.11 134945 +PTI 2020-10-26 1.12 1.14 1.05 1.08 373833 +PTI 2020-10-27 1.11 1.11 1.05 1.06 253591 +PTI 2020-10-28 1.06 1.06 0.98 1.02 631401 +PTI 2020-10-29 1.01 1.05 1 1.01 305993 +PTI 2020-10-30 1.01 1.02 0.94 0.98 325320 +TSLA 2020-10-01 440.76 448.88 434.42 448.16 50741454 +TSLA 2020-10-02 421.39 439.13 415 415.09 71430025 +TSLA 2020-10-05 423.35 433.64 419.33 425.68 44722786 +TSLA 2020-10-06 423.79 428.78 406.05 413.98 49146259 +TSLA 2020-10-07 419.87 429.9 413.85 425.3 43127709 +TSLA 2020-10-08 438.44 439 425.3 425.92 40421116 +TSLA 2020-10-09 430.13 434.59 426.46 434 28925656 +TSLA 2020-10-12 442 448.74 438.58 442.3 38791133 +TSLA 2020-10-13 443.35 448.89 436.6 446.65 34463665 +TSLA 2020-10-14 449.78 465.9 447.35 461.3 48045394 +TSLA 2020-10-15 450.31 456.57 442.5 448.88 35672354 +TSLA 2020-10-16 454.44 455.95 438.85 439.67 32775879 +TSLA 2020-10-19 446.24 447 428.87 430.83 36287843 +TSLA 2020-10-20 431.75 431.75 419.05 421.94 31656289 +TSLA 2020-10-21 422.7 432.95 421.25 422.64 32370461 +TSLA 2020-10-22 441.92 445.23 424.51 425.79 39993191 +TSLA 2020-10-23 421.84 422.89 407.38 420.63 33716980 +TSLA 2020-10-26 411.63 425.76 410 420.28 28239161 +TSLA 2020-10-27 423.76 430.5 420.1 424.68 22686506 +TSLA 2020-10-28 416.48 418.6 406 406.02 25451409 +TSLA 2020-10-29 409.96 418.06 406.46 410.83 22655308 +TSLA 2020-10-30 406.9 407.59 379.11 388.04 42587639 +VOD 2020-10-01 13.27 13.32 13.14 13.25 3591182 +VOD 2020-10-02 13.16 13.52 13.15 13.46 2979065 +VOD 2020-10-05 14.04 14.2 13.95 14.19 6282375 +VOD 2020-10-06 14.49 14.51 14.2 14.23 4309491 +VOD 2020-10-07 14.3 14.36 14.12 14.22 2174561 +VOD 2020-10-08 14.51 14.63 14.44 14.57 3034816 +VOD 2020-10-09 14.6 14.68 14.44 14.63 2258206 +VOD 2020-10-12 14.73 14.83 14.66 14.77 2212874 +VOD 2020-10-13 14.67 14.69 14.55 14.6 2635563 +VOD 2020-10-14 14.43 14.5 14.33 14.37 1908486 +VOD 2020-10-15 14.15 14.33 14.12 14.32 3918735 +VOD 2020-10-16 14.11 14.34 14.1 14.28 4334682 +VOD 2020-10-19 14.35 14.42 14.19 14.21 4436631 +VOD 2020-10-20 14.41 14.59 14.39 14.46 3056450 +VOD 2020-10-21 14.13 14.42 14.04 14.33 8730294 +VOD 2020-10-22 14.25 14.58 14.25 14.55 6935782 +VOD 2020-10-23 14.8 14.93 14.72 14.84 6919188 +VOD 2020-10-26 14.62 14.62 14.41 14.52 4442371 +VOD 2020-10-27 14.2 14.25 13.91 13.98 4558518 +VOD 2020-10-28 13.45 13.56 13.28 13.43 6759000 +VOD 2020-10-29 13.29 13.64 13.2 13.55 5912464 +VOD 2020-10-30 13.49 13.53 13.33 13.51 3372568 +ERIC 2020-10-01 10.77 10.8 10.64 10.72 6537212 +ERIC 2020-10-02 10.52 10.66 10.5 10.61 5451639 +ERIC 2020-10-05 10.76 10.79 10.7 10.77 4362498 +ERIC 2020-10-06 10.84 10.86 10.62 10.67 4972198 +ERIC 2020-10-07 10.66 10.7 10.61 10.65 5990623 +ERIC 2020-10-08 10.8 10.8 10.71 10.79 5180376 +ERIC 2020-10-09 10.89 10.93 10.84 10.87 4633664 +ERIC 2020-10-12 10.91 10.93 10.82 10.86 7169230 +ERIC 2020-10-13 10.85 10.87 10.7 10.73 8288522 +ERIC 2020-10-14 10.75 10.84 10.73 10.82 7771599 +ERIC 2020-10-15 10.54 10.8 10.53 10.8 13022498 +ERIC 2020-10-16 10.74 10.81 10.7 10.7 9317609 +ERIC 2020-10-19 11.02 11.13 10.9 10.94 12575083 +ERIC 2020-10-20 11.13 11.33 11.1 11.13 15019952 +ERIC 2020-10-21 12.07 12.48 12.05 12.44 24980230 +ERIC 2020-10-22 12.49 12.52 12.22 12.44 11322692 +ERIC 2020-10-23 12.26 12.42 12.24 12.36 7844003 +ERIC 2020-10-26 12.33 12.38 11.97 12.08 7164321 +ERIC 2020-10-27 11.92 12.04 11.77 11.78 15622624 +ERIC 2020-10-28 11.39 11.46 11.32 11.38 7895540 +ERIC 2020-10-29 11.35 11.41 11.26 11.33 8549142 +ERIC 2020-10-30 11.24 11.3 11.12 11.21 4374440 +AMZN 2020-10-01 3208 3224 3172 3221.26 4971922 +AMZN 2020-10-02 3153.63 3195.8 3123 3125 5613098 +AMZN 2020-10-05 3145.84 3202.53 3140.85 3199.2 3775332 +AMZN 2020-10-06 3165 3182 3090 3099.96 5086857 +AMZN 2020-10-07 3135 3200 3132.39 3195.69 4309439 +AMZN 2020-10-08 3224.99 3233.29 3174.99 3190.55 3174114 +AMZN 2020-10-09 3210 3288.99 3197.83 3286.65 4907871 +AMZN 2020-10-12 3349.94 3496.24 3339.55 3442.93 8364198 +AMZN 2020-10-13 3467.99 3492.38 3424.22 3443.63 5744697 +AMZN 2020-10-14 3447 3464.88 3340 3363.71 5828916 +AMZN 2020-10-15 3292.01 3355.88 3280 3338.65 5223448 +AMZN 2020-10-16 3363.23 3399.66 3160 3272.71 6474353 +AMZN 2020-10-19 3299.61 3329 3192.74 3207.21 5223626 +AMZN 2020-10-20 3222.28 3266 3192.01 3217.01 4509678 +AMZN 2020-10-21 3212.5 3233.88 3160 3184.94 4592736 +AMZN 2020-10-22 3189.87 3198.75 3121.94 3176.4 4212005 +AMZN 2020-10-23 3191 3205.33 3140 3204.4 3466687 +AMZN 2020-10-26 3198.74 3282.98 3153.3 3207.04 5901151 +AMZN 2020-10-27 3224.94 3291.66 3211.3 3286.33 4291047 +AMZN 2020-10-28 3249.3 3264.02 3162.47 3162.78 5588330 +AMZN 2020-10-29 3201.27 3257.25 3164 3211.01 6596529 +AMZN 2020-10-30 3157.75 3167 3019 3036.15 8396081 diff --git a/1ano/fp/aula07/stocks.py b/1ano/fp/aula07/stocks.py new file mode 100755 index 0000000..0d224a6 --- /dev/null +++ b/1ano/fp/aula07/stocks.py @@ -0,0 +1,61 @@ + +# Constantes para indexar os tuplos: +NAME,DATE,OPEN,MAX,MIN,CLOSE,VOLUME = 0,1,2,3,4,5,6 + +def main(): + lst = loadStockFile("nasdaq.csv") + # Show just first and last tuples: + print("first:", lst[1]) + print("last:", lst[-1]) + + print("a) totVol=", totalVolume(lst)) + + print("b) maxVal=", maxValorization(lst)) + + stocksDic = stocksByDateByName(lst) + print("c) CSCO@11:", stocksDic['2020-10-12']['CSCO']) + print("c) AMZN@22:", stocksDic['2020-10-22']['AMZN']) + + 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: + for line in f: + parts = line.strip().split('\t') + name = parts[NAME] + date = parts[DATE] + tup = (name, date, float(parts[OPEN]), float(parts[MAX]), + float(parts[MIN]), float(parts[CLOSE]), int(parts[VOLUME])) + lst.append(tup) + return lst + +def totalVolume(lst): + totVol = {} + # Complete ... + + return totVol + +def maxValorization(lst): + vMax = {} + # Complete ... + + return vMax + +def stocksByDateByName(lst): + dic = {} + # Complete ... + + return dic + +def portfolioValue(stocks, portfolio, date): + assert date in stocks + val = 0.0 + # Complete ... + + return val + +if __name__ == "__main__": + main() diff --git a/1ano/fp/aula07/telefone.py b/1ano/fp/aula07/telefone.py new file mode 100755 index 0000000..0e60a8f --- /dev/null +++ b/1ano/fp/aula07/telefone.py @@ -0,0 +1,89 @@ +# Complete este programa como pedido no guião da aula. + +def listContacts(dic): + """Print the contents of the dictionary as a table, one item per row.""" + print("{:>12s} : {:^35s} : {:<12s}".format("Numero", "Nome", "Morada")) + for num in dic: + print("{:>12s} : {:^35s} : {:<12s}".format(num, dic[num][0], dic[num][1])) + + +def addContact(dic): + name = input("Nome: ") + number = input("Número: ") + address = input("Morada: ") + dic[number] = (name, address) + + +def removeContact(dic): + number = input("Número: ") + del dic[number] + + +def searchContact(dic): + number = input("Número: ") + print(f"""Nome: {dic[number][0] if number in dic else number} +Morada: {dic[number][1] if number in dic else '----'}""") + + +def filterPartName(contacts, partName): + """Returns a new dict with the contacts whose names contain partName.""" + newDict = {} + for num in contacts: + if partName in contacts[num]: + newDict[num] = contacts[num] + return newDict + + +def menu(): + """Shows the menu and gets user option.""" + print() + print("(L)istar contactos") + print("(A)dicionar contacto") + print("(R)emover contacto") + print("Procurar (N)úmero") + print("Procurar (P)arte do nome") + print("(T)erminar") + op = input("opção? ").upper() # converts to uppercase... + return op + + +def main(): + """This is the main function containing the main loop.""" + + # The list of contacts (it's actually a dictionary!): + contactos = {"234370200": ("Universidade de Aveiro", "Campus de Santiago"), + "727392822": ("Cristiano Aveiro", "Casa"), + "387719992": ("Maria Matos", "Rua da Matos, 123"), + "887555987": ("Marta Maia", "Rua da Maia, 123"), + "876111333": ("Carlos Martins", "Rua dos Martins, 123"), + "433162999": ("Ana Bacalhau", "Rua do Bacalhau, 123"), + } + + op = "" + while op != "T": + op = menu() + if op == "T": + print("Fim") + elif op == "L": + print("Contactos:") + listContacts(contactos) + elif op == "A": + print("Novo contacto:") + addContact(contactos) + elif op == "R": + print("Remover contacto:") + removeContact(contactos) + elif op == "N": + print("Procurar número:") + searchContact(contactos) + elif op == "P": + print("Procurar parte do nome:") + partName = input("Parte do nome: ") + newDict = filterPartName(contactos, partName) + listContacts(newDict) + else: + print("Não implementado!") + + +# O programa começa aqui +main() diff --git a/1ano/fp/aula07/tp07-codecheck/ex01.md b/1ano/fp/aula07/tp07-codecheck/ex01.md new file mode 100644 index 0000000..7d67a7f --- /dev/null +++ b/1ano/fp/aula07/tp07-codecheck/ex01.md @@ -0,0 +1,22 @@ +# Ex01 + +### A próxima linha a ser executada é: + +```python +# Primeiras 5 vezes: +value = input("Enter value: ") + +# Última vez (quando aparecer 0 em Key: +print(data[3]) +``` +