Exercícios 3 e 4 da aula08 adicionados
This commit is contained in:
parent
6dfdd8ccb8
commit
7233b8ee67
|
@ -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)
|
|
@ -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()
|
|
@ -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()
|
Loading…
Reference in New Issue