Add Python code companion for quantifiers lecture

main
Tait Hoyem 4 months ago
commit 8b09bba2c4

163
.gitignore vendored

@ -0,0 +1,163 @@
### Generated by gibo (https://github.com/simonwhitaker/gibo)
### https://raw.github.com/github/gitignore/4488915eec0b3a45b5c63ead28f286819c0917de/Python.gitignore
# 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/
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/
cover/
# 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
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .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
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__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/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

@ -0,0 +1,102 @@
PEOPLE = [
{
"name": "Tait Hoyem",
"has_moodle_account": True,
"has_taken_a_calculus_class": False,
"is_student": True,
"is_cs_student": True,
"is_ee_student": False,
"is_cpsc1820_student": True,
"has_visited_usa": True,
"is_smart": True,
"age": 24,
"enjoys_cpsc1820": False,
},
{
"name": "Jaspreet Kaur",
"has_moodle_account": True,
"has_taken_a_calculus_class": True,
"has_visited_usa": True,
"is_student": False,
"is_cs_student": False,
"is_ee_student": False,
"is_cpsc1820_student": False,
"is_smart": True,
"age": -1,
"enjoys_cpsc1820": True,
},
{
"name": "Ibrahim Abdulahi",
"has_moodle_account": True,
"has_taken_a_calculus_class": False,
"has_visited_usa": True,
"is_student": False,
"is_cs_student": False,
"is_ee_student": False,
"is_cpsc1820_student": False,
"is_smart": True,
"age": -1,
"enjoys_cpsc1820": False,
},
{
"name": "Issac Newton",
"has_moodle_account": True,
"has_taken_a_calculus_class": False,
"has_visited_usa": True,
"is_student": False,
"is_cs_student": False,
"is_ee_student": False,
"is_smart": True,
"is_cpsc1820_student": False,
"age": 482,
"enjoys_cpsc1820": True,
},
{
"name": "Albert Enstein",
"has_taken_a_calculus_class": True,
"has_moodle_account": True,
"has_visited_usa": False,
"is_student": False,
"is_cs_student": False,
"is_ee_student": False,
"is_smart": True,
"is_cpsc1820_student": False,
"age": 125,
"enjoys_cpsc1820": True,
},
{
"name": "John James",
"has_taken_a_calculus_class": False,
"has_moodle_account": True,
"has_visited_usa": False,
"is_student": True,
"is_smart": True,
"is_cs_student": True,
"is_ee_student": False,
"is_cpsc1820_student": True,
"age": 29,
"enjoys_cpsc1820": False,
},
{
"name": "Anthony Shih",
"has_taken_a_calculus_class": False,
"has_moodle_account": True,
"has_visited_usa": False,
"is_student": False,
"is_cs_student": False,
"is_ee_student": True,
"is_cpsc1820_student": True,
"is_smart": False,
"age": 19,
"enjoys_cpsc1820": False,
},
]
def is_student(x):
return x["is_student"]
def is_1820_student(x):
return x["is_cpsc1820_student"]
STUDENTS = list(filter(is_student, PEOPLE))
CPSC1820_STUDENTS = list(filter(is_1820_student, PEOPLE))

@ -0,0 +1,51 @@
from utils import implies
FRUITS = [
{
"name": "Banana",
"tropical": True,
"delicious": True,
"at_walmart": True,
},
{
"name": "Dragon Fruit",
"delicious": True,
"at_walmart": False,
"tropical": True,
},
{
"name": "Orange",
"delicious": False,
"at_walmart": True,
"tropical": True,
},
{
"name": "Mango",
"delicious": True,
"at_walmart": False,
"tropical": True,
},
{
"name": "Watermelon",
"delicious": True,
"at_walmart": True,
"tropical": True,
},
]
def T(x):
return x["tropical"]
def D(x):
return x["delicious"]
def W(x):
return x["at_walmart"]
U = FRUITS
# If a tropical fruit is delicious then it is available in Walmart.
# ∀x [(T(x) ∧ D(x)) → W(x)]
prop = all(map(lambda x: implies(T(x) and D(x), W(x)), U))
print("If a tropical fruit is delicious then it is available in Walmart.")
print("∀x [(T(x) ∧ D(x)) → W(x)]: ", prop)

@ -0,0 +1,59 @@
from data import STUDENTS, PEOPLE
def R(x):
return x["has_moodle_account"]
def R2(x):
return x["has_taken_a_calculus_class"]
def R3(x):
return not R2(x)
def M(x):
return x["has_visited_usa"]
def S(x):
return x["is_student"]
print("Where R(x) is a student who has a mododle account")
# ∀x R(x)
prop1 = all(map(R, STUDENTS))
print("∀x R(x): ", end="")
print(prop1)
# ∃x R(x)
prop2 = any(map(R, STUDENTS))
print("∃x R(x): ", end="")
print(prop2)
print()
print("Where R(x) is a student that has taken a calculsu course")
# ∀x P(x)
prop3 = all(map(R2, STUDENTS))
print("∀x P(x): ", end="")
print(prop3)
# ∃x ¬P(x)
props4 = any(map(R3, STUDENTS))
print("∃x ¬P(x): ", end="")
print(props4)
print()
print("Where M(x) is somebody who has been to the USA")
print("And U = {all students in CPSC 1820")
# Assume, U = {all students in CPSC 1820}
# ∃x M(x)
props5 = any(map(M, STUDENTS))
print("∃x M(x): ", end="")
print(props5)
# Assume, U = {all people}
# ∃x [S(x) ∧ M(x)]
props6 = any(map(lambda x: M(x) and S(x), PEOPLE))
print("∃x [S(x) ∧ M(x)]: ", end="")
print(props6)

@ -0,0 +1,60 @@
from data import CPSC1820_STUDENTS
from utils import implies
def C(x):
return x["is_cs_student"]
def E(x):
return x["is_ee_student"]
def S(x):
return x["is_smart"]
U = CPSC1820_STUDENTS
# Everyone is a CPSC student.
# ∀x C(x)
prop1 = all(map(C, U))
print("Every is a CPSC student")
print("∀x C(x): ", prop1)
print()
# Nobody is an ECE student.
# ∀x ¬E(x) or ¬∃x E(x)
prop2_a = all(map(lambda x: not E(x), U))
prop2_b = not any(map(E, U))
print("Nbody is an ECE student")
print("∀x ¬E(x) ", prop2_a)
print("¬∃x E(x) ", prop2_b)
print()
# All CPSC students are smart students
prop3 = all(map(lambda x: implies(C(x), S(x)), U))
print("All CPSC students are smart students")
print("∀x [C(x) → S(x)]: ", prop3)
print()
# Some CPSC students are smart students.
# ∃x [C(x) ∧ S(x)]
prop4 = any(map(lambda x: C(x) and S(x), U))
print("Some CPSC students are smart students")
print("∃x [C(x) ∧ S(x)]: ", prop4)
print()
# No CPSC student is an ECE student.”
# If x is a CPSC student, then that student is not an ECE student.
# ∀x [C(x) → ~E(x)]
# There does not exist a CPSC student who is also an ECE student.
# ¬∃x [C(x) ∧ E(x)]
prop5 = all(map(lambda x: implies(C(x), not E(x)), U))
prop6 = not any(map(lambda x: C(x) and E(x), U))
print("No CPSC student is an ECE student.")
print("∀x [C(x) → ~E(x)]: ", prop5)
print("¬∃x [C(x) ∧ E(x)]: ", prop6)
print()
# “If any ECE student is a smart student then he is also a CPSC student.”
# ∀x [(E(x) ∧ S(x)) → C(x)]
prop7 = all(map(lambda x: implies(E(x) and S(x), C(x)), U))
print("If any ECE student is a smart student, then he is also a CPSC student.")
print("∀x [(E(x) ∧ S(x)) → C(x)]: ", prop7)

@ -0,0 +1,2 @@
def implies(x,y):
return not (x and not y)
Loading…
Cancel
Save