This is not a graded assignment. You are NOT required to submit it. These problems are only to help you practice the transition of Python, should you feel you need the practice.
Here is an example of using filter in Python to select items from a list. It is very similar to filter in Pyret, except for the return type. In Python, filter returns a result that isn’t a list, but is easily converted to one by using the list operator, as shown in the code below"
def rem_item_by_descr(descr: str, todoLst: List[ToDoItemType]) -> List[ToDoItemType]:
"""remove item with given description from list"""
return list(filter(lambda item: item.Descr == descr, todoLst))
Continue expanding the to-do list exercise that we started in class by adding the following:
Write a function has-tag that consumes a tag (string) and a to-do item and returns a boolean indicating whether the tag is in the tag-list of the to-do item. Python has a built-in operator called in that is helpful here:
3 in [6, 2, 3]
returns true, for example. Note that Python uses True and False (with capitals) for the boolean values.
Write a function date_less_equal which takes two dates and returns a boolean indicating whether the first date is no later than the second date.
If you do this with and/or expressions, your computation will spill over a single line. Python’s syntax rules need you to explicitly wrap longer lines. To continue an expression on the next line, put a backslash (\) at the end of the line that needs to continue.
Write a function add_item that takes an item and a todo list, adds the item to the list, and returns the modified list. Here’s the first line of the definition:
def add_item(item: ToDoItemType, todoLst: List[ToDoItemType]) -> List[ToDoItemType]:
To make this work, you will need to create a VarType for ToDoItemType (as we did in class for DateType). The typing import on List becomes necessary to write the list type in this function header.
To check whether this worked, use the console/interactions area of your tool to run add-item manually (rather than through a test case). In PyCharm, to get your code to be accessible in the console area (at the bottom), highlight all the code you need, right click, then check "Execute Selection in Console". This acts as pressing "Run" in Pyret, in that the highlighted code is made available to you in the console for experimentation.
Using rem_item_by_descr as a guide, write a function find_items_with_tag that takes a tag and a to-do list and returns a list of to-do items that contain the tag. This function should NOT modify the original list.
Using rem_item_by_descr as a guide, write a function find_items_due_by that takes a date and a to-do list and returns a list of to-do items that are due no later than the given date. This function should NOT modify the original list.
Using rem_item_by_descr as a guide, write a function update_due_date that takes a description, a date, and a to-do list and returns a list of the same to-do items as in the original list, but with the date updated for the item with the given description.
You may find yourself wanting to write an if-statement as part of this computation. For reasons we will cover on Wednesday, you can’t write an if-statement inside a lambda expression in the obvious way. Make a separate helper function that takes a description, date, and to-do-item and returns a revised to-do-item (with the new date) if the description matches the one given (otherwise return the original to-do-item).
The syntax for if-statements is similar to Pyret:
if (3 < 4):
No end is needed, however, since Python uses indentation to indicate the end of the statement.
Make sure you can use ""Execute Selection in Console" to load your definitions and test them in the lower console window. Try calling the constructors or functions with the wrong type of arguments – what happens? Make various syntax errors in your code and get familiar with the error messages (misspell names, change indentation, etc). Write down any questions you have, and we’ll go through them on Wednesday.
To run your file and check for syntax errors in PyCharm, click the green arrow that appears to the right of the number for line 1 in the file editor. That will run your file, displaying the results of all print statements in the lower console. If you don’t have errors and you don’t have print statements, nothing will appear in the console except a message that your code "finished with exit code 0". This result actually means that everything was fine.
If you aren’t getting the green arrow, try adding adding then removing a space inside a variable name (that does the trick for me). I’ve also found that I am more likely to get the arrow when my file is within a project, rather than on its own. Go to "New Project" under the file menu, open the new project in a NEW window, then copy your existing code to the new file (you only need to do this once).
Files save automatically every time you press the run button.