From 7233b8ee67a80a5a2ab0cc3150337d81542b9554 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Sat, 19 Nov 2022 17:56:26 +0000 Subject: [PATCH] =?UTF-8?q?Exerc=C3=ADcios=203=20e=204=20da=20aula08=20adi?= =?UTF-8?q?cionados?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1ano/fp/aula08/README.md | 6 +++++ 1ano/fp/aula08/interests.py | 44 ++++++++++++++++++++++++++++++++++++ 1ano/fp/aula08/primesupto.py | 28 +++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 1ano/fp/aula08/README.md create mode 100644 1ano/fp/aula08/interests.py create mode 100644 1ano/fp/aula08/primesupto.py diff --git a/1ano/fp/aula08/README.md b/1ano/fp/aula08/README.md new file mode 100644 index 0000000..0a408af --- /dev/null +++ b/1ano/fp/aula08/README.md @@ -0,0 +1,6 @@ +# Fundamentos de Programação +## Aula 08 - [Slides 1](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/fp/slides/tp08-comprehensions.pdf) - [Slides 2](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/fp/slides/tp08-sets.pdf) +### Tópicos principais da aula: List Comprehensions, Sets + +--- +*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new) diff --git a/1ano/fp/aula08/interests.py b/1ano/fp/aula08/interests.py new file mode 100644 index 0000000..a727ff6 --- /dev/null +++ b/1ano/fp/aula08/interests.py @@ -0,0 +1,44 @@ + +def main(): + A = "reading" + B = "eating" + C = "traveling" + D = "writing" + E = "running" + F = "music" + G = "movies" + H = "programming" + + interests = { + "Marco": {A, D, E, F}, + "Anna": {E, A, G}, + "Maria": {G, D, E}, + "Paolo": {B, D, F}, + "Frank": {D, B, E, F, A}, + "Teresa": {F, H, C, D} + } + + print("a) Table of common interests:") + commoninterests = {(p1, p2): interests[p1].intersection(interests[p2]) + for p1 in interests for p2 in interests + if p1 != p2 and list(interests.keys()).index(p1) < list(interests.keys()).index(p2)} + print(commoninterests) + + print("b) Maximum number of common interests:") + maxCI = max([len(interest) for interest in commoninterests.values()]) + print(maxCI) + + print("c) Pairs with maximum number of matching interests:") + maxmatches = [pair for pair, interest in commoninterests.items() if len(interest) == maxCI] + print(maxmatches) + + print("d) Pairs with low similarity:") + lowJaccard = [(p1, p2) for p1 in interests for p2 in interests + if (p1 != p2 and list(interests.keys()).index(p1) < list(interests.keys()).index(p2)) + and len(interests[p1].intersection(interests[p2])) / len(interests[p1].union(interests[p2])) < 0.25] + print(lowJaccard) + + +# Start program: +if __name__ == "__main__": + main() diff --git a/1ano/fp/aula08/primesupto.py b/1ano/fp/aula08/primesupto.py new file mode 100644 index 0000000..50e7000 --- /dev/null +++ b/1ano/fp/aula08/primesupto.py @@ -0,0 +1,28 @@ +# Algorithm from https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Pseudocode +def primesUpTo(n): + assert n >= 2, "n must be >= 2" + A = [True for i in range(n+1)] + + for i in range(2, n+1): + if A[i]: + for j in range(i**2, n+1, i): + A[j] = False + + return set([i for i in range(2, n+1) if A[i]]) + + +def main(): + # Testing: + s = primesUpTo(1000) + print(s) + + # Do some checks: + assert primesUpTo(30) == {2,3,5,7,11,13,17,19,23,29} + assert len(primesUpTo(1000)) == 168 + assert len(primesUpTo(7918)) == 999 + assert len(primesUpTo(7919)) == 1000 + print("All tests passed!") + + +if __name__ == "__main__": + main()