ia-guiao-de-programacao-fun.../aula2.py

46 lines
1.2 KiB
Python
Raw Permalink Normal View History

# Exercicio 4.1
impar = lambda x: x % 2 != 0
2024-09-17 10:51:44 +00:00
# Exercicio 4.2
positivo = lambda x: x > 0
2024-09-17 10:51:44 +00:00
# Exercicio 4.3
comparar_modulo = lambda x, y: abs(x) < abs(y)
2024-09-17 10:51:44 +00:00
# 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)
2024-09-17 10:51:44 +00:00
# Exercicio 4.5
ex5 = lambda f, g, h: lambda x, y, z: h(f(x, y), g(y, z))
2024-09-17 10:51:44 +00:00
# Exercicio 4.6
2024-09-17 10:51:44 +00:00
def quantificador_universal(lista, f):
return [x for x in lista if not f(x)] == []
2024-09-17 10:51:44 +00:00
# Exercicio 4.7
def quantificador_existencial(lista, f):
return [x for x in lista if f(x)] != []
# Exercicio 4.8
2024-09-17 10:51:44 +00:00
def subconjunto(lista1, lista2):
return [x for x in lista1 if x in lista2] == lista1
2024-09-17 10:51:44 +00:00
# Exercicio 4.9
2024-09-17 10:51:44 +00:00
def menor_ordem(lista, f):
return [x for _ in range(len(lista)) for x in lista if all(f(x, y) for y in lista if x != y)][0]
2024-09-17 10:51:44 +00:00
# Exercicio 4.10
2024-09-17 10:51:44 +00:00
def menor_e_resto_ordem(lista, f):
m = menor_ordem(lista, f)
return m, [x for x in lista if x != m]
2024-09-17 10:51:44 +00:00
# Exercicio 5.2
2024-09-17 10:51:44 +00:00
def ordenar_seleccao(lista, ordem):
return lista if len(lista) <= 1 else ordenar_seleccao([x for x in lista[1:] if ordem(x, lista[0])], ordem) + [lista[0]] + ordenar_seleccao([x for x in lista[1:] if not ordem(x, lista[0])], ordem)