From b0f4ba4c67be133b0f10d6ced80a750b8de74b52 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Wed, 21 Dec 2022 10:37:42 +0000 Subject: [PATCH] Aula09 - Exs 6 and 7 added --- 1ano/fp/aula09/README.md | 2 +- 1ano/fp/aula09/insertionSort.py | 6 +++--- 1ano/fp/aula09/polynomial.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/1ano/fp/aula09/README.md b/1ano/fp/aula09/README.md index aacdb1d..af70314 100644 --- a/1ano/fp/aula09/README.md +++ b/1ano/fp/aula09/README.md @@ -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) diff --git a/1ano/fp/aula09/insertionSort.py b/1ano/fp/aula09/insertionSort.py index 3903e5f..63305cd 100644 --- a/1ano/fp/aula09/insertionSort.py +++ b/1ano/fp/aula09/insertionSort.py @@ -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() diff --git a/1ano/fp/aula09/polynomial.py b/1ano/fp/aula09/polynomial.py index cf2a84c..9e17695 100644 --- a/1ano/fp/aula09/polynomial.py +++ b/1ano/fp/aula09/polynomial.py @@ -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():