Initial commit

This commit is contained in:
github-classroom[bot] 2024-09-17 10:51:44 +00:00 committed by GitHub
commit 1c23e3eec1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 461 additions and 0 deletions

129
.gitignore vendored Normal file
View File

@ -0,0 +1,129 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/

37
README.md Normal file
View File

@ -0,0 +1,37 @@
# iia-ia-guiao-python
Programação ao Estilo Funcional em Python
# Como resolver o guião
1. Crie um virtual environment:
```bash
python3 -m venv venv
```
2. Active o virtual environment (precisa de repetir este passo sempre que começar uma nossa sessão/terminal):
```bash
source venv/bin/activate
```
3. Instale os requisitos:
```bash
pip install -r requirements.txt
```
4. Programe a sua solução.
5. Teste a sua solução.
```bash
pytest
```
6. Adicione o repositorio do professor como upstream.
```bash
git remote add upstream git@github.com:detiuaveiro/iia-ia-guiao-python.git
```
7. Actualize o seu codigo
```bash
git fetch upstream
git merge upstream/master
```

68
aula1.py Normal file
View File

@ -0,0 +1,68 @@
#Exercicio 1.1
def comprimento(lista):
pass
#Exercicio 1.2
def soma(lista):
pass
#Exercicio 1.3
def existe(lista, elem):
pass
#Exercicio 1.4
def concat(l1, l2):
pass
#Exercicio 1.5
def inverte(lista):
pass
#Exercicio 1.6
def capicua(lista):
pass
#Exercicio 1.7
def concat_listas(lista):
pass
#Exercicio 1.8
def substitui(lista, original, novo):
pass
#Exercicio 1.9
def fusao_ordenada(lista1, lista2):
pass
#Exercicio 1.10
def lista_subconjuntos(lista):
pass
#Exercicio 2.1
def separar(lista):
pass
#Exercicio 2.2
def remove_e_conta(lista, elem):
pass
#Exercicio 3.1
def cabeca(lista):
pass
#Exercicio 3.2
def cauda(lista):
pass
#Exercicio 3.3
def juntar(l1, l2):
pass
#Exercicio 3.4
def menor(lista):
pass
#Exercicio 3.6
def max_min(lista):
pass

34
aula2.py Normal file
View File

@ -0,0 +1,34 @@
#Exercicio 4.1
impar = None
#Exercicio 4.2
positivo = None
#Exercicio 4.3
comparar_modulo = None
#Exercicio 4.4
cart2pol = None
#Exercicio 4.5
ex5 = None
#Exercicio 4.6
def quantificador_universal(lista, f):
pass
#Exercicio 4.8
def subconjunto(lista1, lista2):
pass
#Exercicio 4.9
def menor_ordem(lista, f):
pass
#Exercicio 4.10
def menor_e_resto_ordem(lista, f):
pass
#Exercicio 5.2
def ordenar_seleccao(lista, ordem):
pass

BIN
guiao-prog.pdf Normal file

Binary file not shown.

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
mock
pytest

7
tests/__init__.py Normal file
View File

@ -0,0 +1,7 @@
import os
import sys
PROJECT_PATH = os.getcwd()
SOURCE_PATH = os.path.join(
PROJECT_PATH,"."
)
sys.path.append(SOURCE_PATH)

114
tests/test_aula1.py Normal file
View File

@ -0,0 +1,114 @@
import mock
import aula1
#Exercicio 1.1
@mock.patch('aula1.comprimento', side_effect = aula1.comprimento)
def test_comprimento(mock_comprimento):
assert mock_comprimento([1,2,3,4]) == 4
assert mock_comprimento.call_count == 5
#Exercicio 1.2
@mock.patch('aula1.soma', side_effect = aula1.soma)
def test_soma(mock_soma):
assert mock_soma([1,2,3,4]) == 10
assert mock_soma.call_count == 5
#Exercicio 1.3
@mock.patch('aula1.existe', side_effect = aula1.existe)
def test_existe(mock_existe):
assert mock_existe([1,2,3,4], 5) == False
assert mock_existe.call_count == 5
mock_existe.call_count = 0
assert mock_existe([1,2,3,4], 2) == True
assert mock_existe.call_count == 2
#Exercicio 1.4
@mock.patch('aula1.concat', side_effect = aula1.concat)
def test_concat(mock_concat):
assert mock_concat([1,2,3,4], [5,6]) == [1,2,3,4,5,6]
assert mock_concat.call_count in [3,4,5]
#Exercicio 1.5
@mock.patch('aula1.inverte', side_effect = aula1.inverte)
def test_inverte(mock_inverte):
assert mock_inverte([1,2,3,4]) == [4,3,2,1]
assert mock_inverte.call_count == 5
#Exercicio 1.6
@mock.patch('aula1.capicua', side_effect = aula1.capicua)
def test_capicua(mock_capicua):
assert mock_capicua([3,2,3])
assert mock_capicua([3,2,2,3])
assert not mock_capicua([1,2,3])
assert mock_capicua.call_count in [6,7]
#Exercicio 1.7
@mock.patch('aula1.concat_listas', side_effect = aula1.concat_listas)
def test_concat_listas(mock_concat_listas):
assert mock_concat_listas([[1,2], [3,4]]) == [1,2,3,4]
assert mock_concat_listas([[1,2], [3,4], [5]]) == [1,2,3,4,5]
#Exercicio 1.8
@mock.patch('aula1.substitui', side_effect = aula1.substitui)
def test_substitui(mock_substitui):
assert mock_substitui([1,2,3,4], 3, 5) == [1,2,5,4]
assert mock_substitui([1,2,3,4], 5, 6) == [1,2,3,4]
#Exercicio 1.9
@mock.patch('aula1.fusao_ordenada', side_effect = aula1.fusao_ordenada)
def test_fusao_ordenada(mock_fusao_ordenada):
assert mock_fusao_ordenada([1,2,3,4], [2,3,4,5]) == [1,2,2,3,3,4,4,5]
#Exercicio 1.10
@mock.patch('aula1.lista_subconjuntos', side_effect = aula1.lista_subconjuntos)
def test_lista_subconjuntos(mock_lista_subconjuntos):
assert sorted(mock_lista_subconjuntos([1,2,3])) == sorted([[],[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]])
#Exercicio 2.1
@mock.patch('aula1.separar', side_effect = aula1.separar)
def test_separar(mock_separar):
assert mock_separar([(1, 'a'), (2, 'b'), (3, 'c')]) == ([1,2,3], ['a','b','c'])
assert mock_separar.call_count == 4
#Exercicio 2.2
@mock.patch('aula1.remove_e_conta', side_effect = aula1.remove_e_conta)
def test_remove_e_conta(mock_remove_e_conta):
assert mock_remove_e_conta([1,6,2,5,5,2,5,2],2) == ([1,6,5,5,5],3)
assert mock_remove_e_conta.call_count == 9
# Exercicio 3.1
@mock.patch("aula1.cabeca", side_effect=aula1.cabeca)
def test_cabeca(mock_cabeca):
assert mock_cabeca([1, 2, 3, 4]) == 1
assert mock_cabeca.call_count == 1
# Exercicio 3.2
@mock.patch("aula1.cauda", side_effect=aula1.cauda)
def test_cauda(mock_cauda):
assert mock_cauda([1, 2, 3, 4]) == [2, 3, 4]
assert mock_cauda.call_count == 1
assert mock_cauda([1]) == []
assert mock_cauda.call_count == 2
#Exercicio 3.3
@mock.patch('aula1.juntar', side_effect = aula1.juntar)
def test_juntar(mock_juntar):
assert mock_juntar([1,2,3], ['a','b','c']) == [(1, 'a'), (2, 'b'), (3, 'c')]
assert mock_juntar.call_count == 4
assert mock_juntar([1,2,3], ['a','b','c','d']) == None
assert mock_juntar.call_count == 5
#Exercicio 3.4
@mock.patch('aula1.menor', side_effect = aula1.menor)
def test_menor(mock_menor):
assert mock_menor([1,2,3,0,5]) == 0
assert mock_menor.call_count in [5,6]
assert mock_menor([]) == None
#Exercicio 3.6
@mock.patch('aula1.max_min', side_effect = aula1.max_min)
def test_max_min(mock_max_min):
assert mock_max_min([5,4,3,1,6]) == (6,1)
assert mock_max_min.call_count == 5
assert mock_max_min([]) == None

70
tests/test_aula2.py Normal file
View File

@ -0,0 +1,70 @@
import mock
import types
import aula2
from aula2 import *
#Exercicio 4.1
def test_par_impar():
assert isinstance(impar, types.FunctionType)
assert impar(3)
assert not impar(4)
#Exercicio 4.2
def test_positivo():
assert isinstance(positivo, types.FunctionType)
assert positivo(3)
assert not positivo(-4)
#Exercicio 4.3
def test_comparar_modulo():
assert isinstance(comparar_modulo, types.FunctionType)
assert not comparar_modulo(-4, 2)
assert comparar_modulo(3, -4)
#Exercicio 4.4
def test_cart2pol():
assert isinstance(cart2pol, types.FunctionType)
assert cart2pol(0, 1) == (1.0, 1.5707963267948966)
#Exercicio 4.5
def test_ex5():
assert isinstance(ex5, types.FunctionType)
t = ex5(lambda x,y: x+y, lambda x,y: x*y, lambda x,y: x < y)
assert isinstance(t, types.FunctionType)
assert t(1,2,3) == True
#Exercicio 4.6
@mock.patch('aula2.quantificador_universal', side_effect = aula2.quantificador_universal)
def test_quantificador_universal(mock_qt_uni):
assert mock_qt_uni([11,12,13,14], lambda n: n > 10)
#Exercicio 4.8
@mock.patch('aula2.subconjunto', side_effect = aula2.subconjunto)
def test_subconjunto(mock_subconjunto):
assert mock_subconjunto([11,12,13,14], [11,12,13,14,15,16])
assert mock_subconjunto([11,12,13,14], [10,11,12,13,14])
assert mock_subconjunto([11,12,13,14], [10,11,12,13,14,15])
assert not mock_subconjunto([11,12,33,14], [10,11,12,13,14,15])
#Exercicio 4.9
@mock.patch('aula2.menor_ordem', side_effect = aula2.menor_ordem)
def test_menor_ordem(mock_menor_ordem):
assert mock_menor_ordem([1,-1,4,0], lambda x,y: x < y) == -1
assert mock_menor_ordem([1,-1,4,0], lambda x,y: x > y) == 4
#Exercicio 4.10
@mock.patch('aula2.menor_e_resto_ordem', side_effect = aula2.menor_e_resto_ordem)
def test_menor_e_resto_ordem(mock_menor_e_resto_ordem):
m, r = mock_menor_e_resto_ordem([1,-1,4,0], lambda x, y: x < y)
assert m == -1
assert sorted(r) == sorted([1,4,0])
m2, r2 = mock_menor_e_resto_ordem([1,-1,4,0], lambda x, y: x > y)
assert m2 == 4
assert sorted(r2) == sorted([1,-1,0])
#Exercicio 5.2a
@mock.patch('aula2.ordenar_seleccao', side_effect = aula2.ordenar_seleccao)
def test_ordenar_seleccao(mock_ordenar):
assert mock_ordenar([1,-1,4,0], lambda x, y: x < y) == [-1, 0, 1, 4]
assert mock_ordenar([1,-1,4,0], lambda x, y: x > y) == [4, 1, 0, -1]