From 30df314a2f53b61904f3524c7aa23499f11329e2 Mon Sep 17 00:00:00 2001 From: Tiago Garcia Date: Sat, 28 Sep 2024 17:49:51 +0100 Subject: [PATCH] Add Ex4 Signed-off-by: Tiago Garcia --- aula2.py | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/aula2.py b/aula2.py index 9dcf700..17d1b4b 100644 --- a/aula2.py +++ b/aula2.py @@ -1,34 +1,45 @@ -#Exercicio 4.1 -impar = None +# Exercicio 4.1 +impar = lambda x: x % 2 != 0 -#Exercicio 4.2 -positivo = None +# Exercicio 4.2 +positivo = lambda x: x > 0 -#Exercicio 4.3 -comparar_modulo = None +# Exercicio 4.3 +comparar_modulo = lambda x, y: abs(x) < abs(y) -#Exercicio 4.4 -cart2pol = None +# Exercicio 4.4 +cart2pol = lambda x, y: ((x**2 + y**2)**0.5, sum([((-1)**n * (y/x)**(2*n + 1)) / (2*n + 1) for n in range(100000)]) if x != 0 else 3.141592653589793/2) -#Exercicio 4.5 -ex5 = None +# Exercicio 4.5 +ex5 = lambda f, g, h: lambda x, y, z: h(f(x, y), g(y, z)) -#Exercicio 4.6 + +# Exercicio 4.6 def quantificador_universal(lista, f): - pass + return [x for x in lista if not f(x)] == [] -#Exercicio 4.8 + +# Exercicio 4.7 +def quantificador_existencial(lista, f): + return [x for x in lista if f(x)] != [] + + +# Exercicio 4.8 def subconjunto(lista1, lista2): - pass + return [x for x in lista1 if x in lista2] == lista1 -#Exercicio 4.9 + +# Exercicio 4.9 def menor_ordem(lista, f): - pass + return [x for _ in range(len(lista)) for x in lista if all(f(x, y) for y in lista if x != y)][0] -#Exercicio 4.10 + +# Exercicio 4.10 def menor_e_resto_ordem(lista, f): - pass + m = menor_ordem(lista, f) + return m, [x for x in lista if x != m] -#Exercicio 5.2 + +# Exercicio 5.2 def ordenar_seleccao(lista, ordem): pass