Aula09 - Exs 6 and 7 added

This commit is contained in:
TiagoRG 2022-12-21 10:37:42 +00:00
parent 3a8c1b14fa
commit b0f4ba4c67
3 changed files with 6 additions and 6 deletions

View File

@ -1,7 +1,7 @@
# Fundamentos de Programação
## Aula 09 - [Slides](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/fp/slides/tp09-searching+sorting.pdf)
### Tópicos principais da aula: Searching, Sorting, Lambda Expressions
#### Exercícios feitos: 1, 2, 3
#### Exercícios feitos: 1, 2, 3, 6, 7
---
*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)

View File

@ -3,14 +3,14 @@
# using the insertion sort algorithm.
# Modify it to accept a key= keyword argument that works like in list.sort.
def insertionSort(lst):
def insertionSort(lst, key=None):
# Traverse elements starting at position 1
for i in range(1, len(lst)):
# We know that lst[:i] is sorted
x = lst[i] # x is the element to insert next
# Elements in lst[:i] that are > x must move one position ahead
j = i - 1
while j >= 0 and lst[j] > x:
while j >= 0 and (key(lst[j]) > key(x) if key else lst[j] > x):
lst[j + 1] = lst[j]
j -= 1
# Then put x in the last emptied slot
@ -19,7 +19,6 @@ def insertionSort(lst):
return
def main():
# Original list
lst0 = ["paulo", "augusto", "maria", "paula", "bernardo", "tito"]
@ -46,6 +45,7 @@ def main():
print("All tests OK!")
if __name__ == "__main__":
main()

View File

@ -4,7 +4,7 @@
# polynomial2(a,b,c) deve devolver uma função f tal que
# f(x) seja o polinómio de segundo grau ax²+bx+c.
def polynomial2(a, b, c):
...
return lambda x: a*x**2 + b*x + c
# DESAFIO EXTRA:
@ -14,7 +14,7 @@ def polynomial2(a, b, c):
# polynomial(a), onde a=[a0, a1, ..., an], deve devolver uma função f tal que
# f(x) seja o polinómio a0*x**n + a1*x**(n-1) + ... + an.
def polynomial(coefs):
...
return lambda x: sum([coefs[i]*x**(len(coefs)-i-1) for i in range(len(coefs))])
def main():