{"id":20641881,"url":"https://github.com/rogerisk/python-logical-thinking","last_synced_at":"2026-04-21T02:04:22.277Z","repository":{"id":244423536,"uuid":"815187089","full_name":"RogerIsk/python-logical-thinking","owner":"RogerIsk","description":"Python tasks that concentrate on logic","archived":false,"fork":false,"pushed_at":"2024-06-14T15:20:49.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-17T08:46:07.445Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RogerIsk.png","metadata":{"files":{"readme":"README-2.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-14T14:38:35.000Z","updated_at":"2024-06-14T15:20:52.000Z","dependencies_parsed_at":"2024-06-14T17:09:53.932Z","dependency_job_id":null,"html_url":"https://github.com/RogerIsk/python-logical-thinking","commit_stats":null,"previous_names":["rogerisk/python-logical-thinking"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RogerIsk%2Fpython-logical-thinking","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RogerIsk%2Fpython-logical-thinking/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RogerIsk%2Fpython-logical-thinking/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RogerIsk%2Fpython-logical-thinking/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RogerIsk","download_url":"https://codeload.github.com/RogerIsk/python-logical-thinking/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242692348,"owners_count":20170228,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T16:07:08.288Z","updated_at":"2026-04-21T02:04:22.251Z","avatar_url":"https://github.com/RogerIsk.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Python logical expressions\n\n## Description\n\nIn this exercise, we will practice on more complex logical expressions including\nvarious predicates and operators.\n\n##\n\n## Collection Basic\n\n## List\n\nA list is a built-in data structure that represents an ordered collection of items. Lists can be stored in variables just like Strings can. For example to store a list of car brands you could create a variable `car_brands` with a list of brands like `'BMW'`, `'Audi'`, and `'Volkswagen'`. This translates to the following code:\n\n```python\ncar_brands = ['BMW', 'Audi', 'Volkswagen']\n```\n\nElements in this list can be accessed by zero-based, numbered indices. For example `car_brands[0]` would return `BMW`.\n\nTo work with all elements in a list you can loop over its elements and for example print them like this:\n\n```python\nfor brand in car_brands:\n  print(brand)\n```\n\nLists can also be altered. Elements can be added using the `append` method:\n\n```python\ncar_brands.append('Volvo')\n```\n\nUsing indices elements can also be removed from a list again using the `pop` method:\n\n```python\ncar_brands.pop(0)\n```\n\n\n## Dictionary\n\nA dictionary is a built-in data structure that represents a collection of key-value pairs. Dictionaries (also called dicts) of course can also be stored in variables. For example let's store fruits, grouped by their color:\n\n```python\nfruits = {\n  'red': ['apple', 'cherry', 'strawberry'],\n  'orange':['orange', 'mango', 'peach'],\n  'yellow': ['banana', 'lemon']\n}\n```\n\nElements in this dictionary can be accessed by keys which are Strings. For example all red fruits can be accessed using `fruits['red']`. The nested value `apple` could be accessed using `fruits['red'][0]`.\n\nTo add an element to or update one in a dictionary you can use the key syntax as well or use the `update` method:\n\n```python\nfruits['green'] = ['watermelon']\nfruits.update({'green': ['watermelon']})\n```\n\nTo remove an element from a dictionary one can use the `pop` method with a specified key. For example to remove all yellow fruits:\n\n```python\nfruits.pop('yellow')\n```\n\nYou can also loop over dictionaries like you can loop over lists using the `items` method:\n\n```python\nfor color, colored_fruits in fruits.items()\n  print(color + ' fruits:')\n  for fruit in colored_fruits:\n    print('- ' + fruit)\n```\n\n\n##\n\n## Tasks\n\n###\n\n### Task 1:\n\nLet's consider the following list of users:\n\n```\nusers = [\n    {\n        \"name\": \"Holly\",\n        \"type\": \"Student\",\n        \"password\": \"hunter\",\n        \"modules\": [\n            {\n                \"title\": \"Computer basics\",\n                \"completed\": True\n            },\n            {\n                \"title\": \"Python basics\",\n                \"completed\": False\n            }\n        ]\n    },\n    {\n        \"name\": \"Peter\",\n        \"type\": \"Student\",\n        \"password\": \"pan\",\n        \"modules\": [\n            {\n                \"title\": \"Computer basics\",\n                \"completed\": False\n            }\n        ]\n    },\n    {\n        \"name\": \"Luke\",\n        \"type\": \"Student\",\n        \"password\": \"skywalker\",\n        \"modules\": [\n            {\n                \"title\": \"Computer basics\",\n                \"completed\": True\n            }\n        ]\n    },\n    {\n        \"name\": \"Janis\",\n        \"type\": \"Teacher\",\n        \"password\": \"joplin\"\n    }\n]\n```\n\nUsing the concepts, and some code from the previous exercises, define a function named `show_registration` that checks if a user is registered to a specific module.\n\nThe function will accept three input arguments:\n\n- **username**. `String`\n- **password**. `String`\n- **modulename**. `String`\n\nThe function may print any of the following messages:\n\n- `You are registered to the module {modulename}`.\nIt will print this message if the user is a student and in his `modules` key has a module with a `title` equal to the argument `modulename`\n- `You did not register to the module {modulename}`\nIt will print this message if the user is anonymous or is a student and in his `modules` key DOES NOT have a module with the given title.\n- `You are a teacher`\nIt will print this message if the username is a teacher.\n\n\u003e You may use additional functions to filter or store logical expressions to use in your `show_registration` function.\n\n\u003e You may use more than one conditional.\n\nUse the following code to test:\n\n```\nusername = input(\"What is your username? \")\npassword = input(f\"Type the password for username {username}: \")\nmodulename = input(\"What module do you want to check? \")\nshow_registration(username, password, modulename)\n```\n- Your result should look like this:\n\n```\nWhat is your username? Peter\nType the password for username Peter: pan\nWhat module do you want to check? Computer basics\nYou are registered to the module Computer basics.\n```\n```\nWhat is your username? Luke\nType the password for username Luke: skywalker\nWhat module do you want to check? Python basics\nYou did not register to the module Python basics.\n```\n```\nWhat is your username? Janis\nType the password for username Janis: joplin\nWhat module do you want to check? Django\nYou are a teacher.\n```\n```\nWhat is your username? Anonymous\nType the password for username Anonymous: empty\nWhat module do you want to check? Computer basics\nYou did not register to the module Computer basics.\n```\n\n# Task 2\n\nNow add another function named `has_completed_module` similar to `show_registration` with the same input arguments and the following differences:\n\n- This time, the function will do nothing if the user is valid and a teacher.\n- It will check if, besides having a particular module in the user's list, the module has the key `completed` set to `True`.\n\nUse the following code to test:\n\n```\nusername = input(\"What is your username? \")\npassword = input(f\"Type the password for username {username}: \")\nmodulename = input(\"What module do you want to check? \")\nshow_registration(username, password, modulename)\nhas_completed_module(username, password, modulename)\n```\n- Your result should look like this:\n\n```\nWhat is your username? Peter\nType the password for username Peter: pan\nWhat module do you want to check? Computer basics\nYou are registered to the module Computer basics.\nYou did not complete the module Computer basics.\n```\n```\nWhat is your username? Luke\nType the password for username Luke: skywalker\nWhat module do you want to check? Computer basics\nYou are registered to the module Computer basics.\nYou have completed the module Computer basics.\n```\n```\nWhat is your username? Janis\nType the password for username Janis: joplin\nWhat module do you want to check? Django\nYou are a teacher.\n```\n```\nWhat is your username? Anonymous\nType the password for username Anonymous: password\nWhat module do you want to check? Django\nYou did not register to the module Django.\nYou did not complete the module Django.\n```\n\n# Task 3\n\nNow we are given this list of modules:\n\n```\nmodules = [\n    {\n        \"name\": \"Computer basics\"\n    },\n    {\n        \"name\": \"Python basics\",\n        \"requirement\": \"Computer basics\"\n    },\n    {\n        \"name\": \"Django\",\n        \"requirement\": \"Python basics\"\n    }\n]\n```\n\nAdd a new function named `may_enroll` with three input parameters:\n\n- **username**. `String`\n- **password**. `String`\n- **modulename**. `String`\n\nThis function should return `True` in the following cases:\n\n- The user is anonymous and the module has no requirement (they can always register).\n- The user is a student and:\n    - The user is not already registered to that module.\n    - The module has no requirement or it has one and the user meets the requirement (is also registered to the requirement and its `completed` flag is set to `True`).\n\nIn any other case, the function should return `False`.\n\n\u003e Keep the code from previous exercises.\n\n\u003e A teacher cannot register to any module.\n\n\u003e An anonymous user will only be allowed to register if the module has no requirement. If that happens, the script should not execute any other logical expression.\n\n\u003e Use as many functions as needed to keep the logical expressions easy to read. Use semantic names for those functions to improve the readability. Example: is_anonymous(), has_no_requirement(), meets_requirement(), ...\n\n\u003e Use any concept learned on the slides: short-circuiting, grouping,...\n\nUse the following code to test:\n\n```\nusername = input(\"What is your username? \")\npassword = input(f\"Type the password for username {username}: \")\nmodulename = input(\"What module do you want to check? \")\nshow_registration(username, password, modulename)\nhas_completed_module(username, password, modulename)\nif may_enroll(username, password, modulename):\n    print(f\"You may register to the module {modulename}.\")\nelse:\n    print(f\"You may not register to the module {modulename}.\")\n```\n- Your result should look like this:\n\n```\nWhat is your username? Peter\nType the password for username Peter: pan\nWhat module do you want to check? Computer basics\nYou are registered to the module Computer basics.\nYou did not complete the module Computer basics.\nYou may not register to the module Computer basics.\n```\n\n```\nWhat is your username? Peter\nType the password for username Peter: pan\nWhat module do you want to check? Python basics\nYou did not register to the module Python basics.\nYou did not complete the module Python basics.\nYou may not register to the module Python basics.\n```\n\n```\nWhat is your username? Luke\nType the password for username Luke: skywalker\nWhat module do you want to check? Python basics\nYou did not register to the module Python basics.\nYou did not complete the module Python basics.\nYou may register to the module Python basics.\n```\n\n```\nWhat is your username? Janis\nType the password for username Janis: joplin\nWhat module do you want to check? Django\nYou are a teacher.\nYou may not register to the module Django.\n```\n\n```\nWhat is your username? Anonymous\nType the password for username Anonymous: password\nWhat module do you want to check? Computer basics\nYou did not register to the module Computer basics.\nYou did not complete the module Computer basics.\nYou may register to the module Computer basics.\n```\n\n```\nWhat is your username? Anonymous\nType the password for username Anonymous: password\nWhat module do you want to check? Django\nYou did not register to the module Django.\nYou did not complete the module Django.\nYou may not register to the module Django.\n```\n\n```\nWhat is your username? Peter\nType the password for username Peter: pan\nWhat module do you want to check? PHP\nYou did not register to the module PHP.\nYou did not complete the module PHP.\nYou may not register to the module PHP.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frogerisk%2Fpython-logical-thinking","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frogerisk%2Fpython-logical-thinking","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frogerisk%2Fpython-logical-thinking/lists"}