This task is part of project09 which is due at 23:00 EDT on 2024-04-09.
You have the option to work with a partner on this task if you wish. Working with a partner requires more work to coordinate schedules, but if you work together and make sure that you are both understanding the code you write, you will make progress faster and learn more.
You can download the starter code for this task using this link.
You can submit this task using this link.
Put all of your work for this task into the file
checklist.py
(which is provided among the starter files)
For this task, you will write some functions that perform very basic operations with dictionaries, allowing them to be used as a kind of checklist. These checklists will be able to store strings naming tasks, along with booleans indicating whether each task is completed or not. An example helps illustrate this: The following dictionary represents a checklist for going camping:
{
"Tent": False,
"Sleeping bags": False,
"Cooking supplies": False,
"Camp chairs": False,
"Snacks": False,
"Meals": False
}
Each key represents something to be checked off, and the corresponding value represents whether it's checked off (True) or not (False).
After doing some packing for the trip, the dictionary might be filled out as follows:
{
"Tent": True,
"Sleeping bags": True,
"Cooking supplies": True,
"Camp chairs": True,
"Snacks": False,
"Meals": True
}
Your job is to write a series of simple functions for manipulating this type of dictionary:
addTask
which will allow a new task to be added.taskStatus
which will return the status of a task.completeTask
which will mark a task as completed.resetChecklist
which will mark every task in the list as uncompleted....plus some more complex functions for displaying, loading, and saving these dictionaries:
displayChecklist
which will format a dictionary as a checklist. This
function must use a helper function named lengthAndValue
for sorting,
which you must also define.saveChecklist
which will save a checklist to a file.loadChecklist
which will load a checklist from a file.Note: as usual, you will need to document each function you write. For this task, however, you do not need to worry about wasting fruit or wasting boxes.
The paragraphs below describe each function that you must define in detail.
addTask
This function must add a task to
a checklist, setting its initial value to
False
. These examples for addTask
demonstrate how
it works. It will accept two parameters: the checklist object to add to,
and the task name to add. In addition to adding the task, it must return
the (now modified) checklist object that it was
given.
There is one issue, however: a task cannot be added to a checklist if the
name requested is already being used by another task in that checklist.
In that case, this function must print a specific message
(see the examples), and must return
None
.
taskStatus
This function must return the task
status for a particular task from a
checklist. These examples for
taskStatus
demonstrate how it works.
As with addTask
, there's the possibility that the task specified is
invalid (in this case, because it isn't in the checklist). In that case,
taskStatus
must print a specific
message (see the examples), and
must return None
.
completeTask
This function must set the value of
a task in a checklist to True
. These
examples for completeTask
demonstrate how
it works. It will accept two parameters: the checklist object to add to,
and the name of the task to mark as completed. In addition to changing
the task value, it must return the (now modified) checklist object that
it was given.
As with addTask
and taskStatus
, there's a catch: if the task name
provided is not in the checklist, this function must print a specific
message (see the examples), and
must return None
.
resetChecklist
This function must reset the
value of every task in a checklist to
False
. These examples for
resetChecklist
demonstrate how it works. It
accept just one parameter: the checklist object to reset, and it must
return the (now modified) checklist object that it was
given.
Unlike the other functions, there's no condition under which this one
returns None
. Also note that while none of them may use a loop, this
function must use a loop.
lengthAndValue
This function accepts a string as its only
parameter, and it must return a
tuple containing first the length of the string, and then the string
itself. These examples for
lengthAndValue
show how it should work.
This function isn't related to checklists, but will be used as a sort key
in the displayChecklist
and saveChecklist
functions.
displayChecklist
This function accepts a checklist as its only
parameter, and it must print out
the checklist using the exact
format shown in these examples for
displayChecklist
. In particular, each task
in the checklist will be printed on a line that starts with either '[X] '
if it is completed, or '[ ] '
if it is not yet completed. In addition,
the items must be sorted according to the lengths of their names; shortest
to longest with ties broken by alphabetical order. You must use sort
or
sorted
with the key=
argument set to lengthAndValue
in order to
achieve this.
This function will only print, and will not return anything; its only parameter is the checklist object to display.
saveChecklist
This function accepts two parameters: a checklist and a
filename., and it must write the
checklist into the target file, deleting any previous contents that file
had. using the exact format shown in
these examples for saveChecklist
. The format
used in the file is exactly the same as the format used by
displayChecklist
, including how the items are sorted. You must use
sort
or sorted
with the key=
argument set to lengthAndValue
in
order to achieve this,
and you must also use with ... as
to open the
file.
This function will not print or return anything.
loadChecklist
This function accepts one parameter: a
filename., and it must load and
return the checklist that was stored in that file by the saveChecklist
function.. These loadChecklist
examples show how it should work. Note in
particular that if the file is not properly formatted, it should print a
specific error message and return
None
.
You must use with ... as
to open the
file.
All together, the function's you've defined in this task are a kind of "checklist API." We often have students ask us what "API" means, and we thought that building one yourself would be a good way to answer that question. API stands for Application Programming Interface, and it basically just means a bundle of function contracts that define what operations are supported by a certain library or code file. In this task, the "checklist API" says that the supported operations for a checklist are: add a task, check the status of a task, complete a task, and reset all tasks, plus you can print out a checklist, save it, or load it.
Other libraries that we use in this class each have their own API. For
example, the turtle
API defines what you can draw with turtle
, the
turtleBeads
API defines some additional drawing possibilities; the
wavesynth
API defines options for building music, and the optimism
API
defines operations for setting up test cases. We've also used built-in
Python APIs like the os
API, and you could consider the methods for a
particular type as an API as well, so we're using the "string API" when we
use methods like .split
or .lower
. Basically, just like a function
contract specifies what a function promises to do, and then the actual
code defines how that's done, an API defines what a series of related
functions will do, and then an implementation of that API defines the
actual functions.
One thing you will learn about in later classes is how to design an API: for this task, we just told you what to write, but when you're not working on class assignments, you'll have to figure out what's needed and how things should fit together yourself.
addTask
examples
These examples demonstrate how addTask
works. Note that the value for
the new task is always False
, and if the task can't be added because
the desired name is already being used, a message is printed and None
is returned.
In []:Out[]:addTask({}, 'New task')
In []:{'New task': False}
Out[]:addTask({'Task 1': False, 'Task 2': True}, 'New task')
In []:{'Task 1': False, 'Task 2': True, 'New task': False}
Out[]:addTask( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, }, 'Flashlights' )
In []:{ 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, 'Flashlights': False, }
PrintsaddTask({'only': True}, 'only')
That task already exists.
taskStatus
examples
These examples demonstrate how taskStatus
works. Note that if the
named task isn't in the checklist, a message is printed and None is
returned.
In []:Out[]:taskStatus({'Task 1': False, 'Task 2': True}, 'Task 1')
In []:False
Out[]:taskStatus( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, }, 'Tent' )
In []:False
Out[]:taskStatus( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, }, 'Meals' )
In []:True
PrintstaskStatus({}, 'Item')
That task does not exist.
completeTask
examples
These examples demonstrate how completeTask
works. Note that a message
is printed and None is returned if the task isn't already in the
checklist, or if a list is named instead of a task. Also note that if
the task is already completed, that's not an error, and it just stays
completed.
In []:Out[]:completeTask({'Task 1': False, 'Task 2': True}, 'Task 1')
In []:{'Task 1': True, 'Task 2': True}
Out[]:completeTask( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, }, 'Tent' )
In []:{ 'Tent': True, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, }
Out[]:completeTask( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, }, 'Meals' )
In []:{ 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, }
PrintscompleteTask({}, 'Anything')
That task does not exist.
resetChecklist
examples
These examples demonstrate how resetChecklist
works.
In []:Out[]:resetChecklist({'Task 1': False, 'Task 2': True})
In []:{'Task 1': False, 'Task 2': False}
Out[]:resetChecklist( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, } )
In []:{ 'Tent': False, 'Sleeping bags': False, 'Cooking supplies': False, 'Camp chairs': False, 'Snacks': False, 'Meals': False, }
Out[]:resetChecklist({})
{}
lengthAndValue
examples
These examples demonstrate how lengthAndValue
works. Note that
although it will be used for sorting, it does not itself do any sorting
or even involve a loop.
In []:Out[]:lengthAndValue('hello')
In []:(5, 'hello')
Out[]:lengthAndValue('hi')
In []:(2, 'hi')
Out[]:lengthAndValue('goodbye')
(7, 'goodbye')
displayChecklist
examples
These examples demonstrate how displayChecklist
works. Note that
whether a task is completed or not is indicated using either a capital
'X' or a space between square brackets. Also note that the keys of the
checklist are sorted by the lengths of their names, shortest to longest,
with ties broken by alphabetical order.
In []:PrintsdisplayChecklist({'only': True})
[X] onlyIn []:PrintsdisplayChecklist({'Task 1': False, 'Task 2': True})
[ ] Task 1 [X] Task 2In []:PrintsdisplayChecklist( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, } )
[ ] Tent [X] Meals [ ] Snacks [ ] Camp chairs [X] Sleeping bags [X] Cooking supplies
saveChecklist
examples
These examples demonstrate how saveChecklist
works. Note that the text
it writes into the specified file is the same as what gets printed by
displayChecklist
, including the sorting of the keys. Also note that if
the checklist is empty, nothing gets written to the file.
In []:FilesaveChecklist({'Task 1': False, 'Task 2': True}, 'checklist.txt')
checklist.txt[ ] Task 1 [X] Task 2In []:FilesaveChecklist( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, }, 'camping.txt' )
camping.txt[ ] Tent [X] Meals [ ] Snacks [ ] Camp chairs [X] Sleeping bags [X] Cooking suppliesIn []:FilesaveChecklist({}, 'empty.txt')
empty.txt
loadChecklist
examples
These examples demonstrate how loadChecklist
works. It operates as the
opposite of saveChecklist
. Note that in these examples, the file
contents shown must exist before the function is called; they are not
produced by it, unlike with saveChecklist
.
In []:Out[]:loadChecklist('checklist.txt')
File{'Task 1': False, 'Task 2': True}
checklist.txt[ ] Task 1 [X] Task 2In []:Out[]:loadChecklist('camping.txt')
File{ 'Tent': False, 'Meals': True, 'Snacks': False, 'Camp chairs': False, 'Sleeping bags': True, 'Cooking supplies': True, }
camping.txt[ ] Tent [X] Meals [ ] Snacks [ ] Camp chairs [X] Sleeping bags [X] Cooking suppliesIn []:Out[]:loadChecklist('empty.txt')
File{}
empty.txtIn []:PrintsloadChecklist('malformed.txt')
Invalid file format!Filemalformed.txtNot a checklist.
addTask
must return the correct result
addTask
function is run must match the solution result.addTask
must print the correct output
addTask
function is run must match the solution output.addTask
must return the correct result
addTask
function is run must match the solution result.addTask
should modify and return the same dictionary it receives as an argument.
addTask
and check sure that it returns the same dictionary that was given to it as an argument, instead of creating a new dictionary as its result.taskStatus
must return the correct result
taskStatus
function is run must match the solution result.taskStatus
must print the correct output
taskStatus
function is run must match the solution output.taskStatus
must return the correct result
taskStatus
function is run must match the solution result.completeTask
must return the correct result
completeTask
function is run must match the solution result.completeTask
must print the correct output
completeTask
function is run must match the solution output.completeTask
must return the correct result
completeTask
function is run must match the solution result.completeTask
should modify and return the same dictionary it receives as an argument.
completeTask
and check sure that it returns the same dictionary that was given to it as an argument, instead of creating a new dictionary as its result.resetChecklist
must return the correct result
resetChecklist
function is run must match the solution result.resetChecklist
must return the correct result
resetChecklist
function is run must match the solution result.resetChecklist
should modify and return the same dictionary it receives as an argument.
resetChecklist
and check sure that it returns the same dictionary that was given to it as an argument, instead of creating a new dictionary as its result.lengthAndValue
must return the correct result
lengthAndValue
function is run must match the solution result.lengthAndValue
must return the correct result
lengthAndValue
function is run must match the solution result.displayChecklist
must print the correct output
displayChecklist
function is run must match the solution output.displayChecklist
must print the correct output
displayChecklist
function is run must match the solution output.saveChecklist
must write the correct data into checklist.txt
checklist.txt
when your saveChecklist
function is run must match what the solution writes.saveChecklist
must write the correct data into checklist.txt
checklist.txt
when your saveChecklist
function is run must match what the solution writes.loadChecklist
must return the correct result.
loadChecklist
to check that
it can correctly load them.loadChecklist
must print the correct output
loadChecklist
function is run must match the solution output.loadChecklist
must return the correct result.
loadChecklist
to check that
it can correctly load them.loadChecklist
returns None
when the file is not
a checklist.
loadChecklist
with some files that are not
properly formatted. It should return None
.loadChecklist
prints the correct message when the file is not
a checklist.
loadChecklist
with some files that are not
properly formatted. It should print the correct error message.addTask
with 2 parameters
def
to define addTask
with 2 parametersreturn
statement
addTask
with 2 parameters, use return _
in at least one place.addTask
with 2 parameters, do not use any kind of loop.taskStatus
with 2 parameters
def
to define taskStatus
with 2 parametersreturn
statement
taskStatus
with 2 parameters, use return _
in at least one place.taskStatus
with 2 parameters, do not use any kind of loop.completeTask
with 2 parameters
def
to define completeTask
with 2 parametersreturn
statement
completeTask
with 2 parameters, use return _
in at least one place.completeTask
with 2 parameters, do not use any kind of loop.resetChecklist
with 1 parameter
def
to define resetChecklist
with 1 parameterreturn
statement
resetChecklist
with 1 parameter, use return _
in at least one place.resetChecklist
with 1 parameter, use any kind of loop in at least one place.lengthAndValue
with 1 parameter
def
to define lengthAndValue
with 1 parameterreturn
statement
lengthAndValue
with 1 parameter, use return _
in at least one place.lengthAndValue
with 1 parameter, do not use any kind of loop.displayChecklist
with 1 parameter
def
to define displayChecklist
with 1 parametersorted()
or .sort()
call which uses lengthAndValue
as the sort key
displayChecklist
with 1 parameter, use sorted(_, key=lengthAndValue)
or _.sort(key=lengthAndValue)
in at least one place.displayChecklist
with 1 parameter, use any kind of loop in at least one place.print
displayChecklist
with 1 parameter, call print
in at least one place.saveChecklist
with 2 parameters
def
to define saveChecklist
with 2 parametersopen
saveChecklist
with 2 parameters, call open
in at least one place.sorted()
or .sort()
call which uses lengthAndValue
as the sort key
saveChecklist
with 2 parameters, use sorted(_, key=lengthAndValue)
or _.sort(key=lengthAndValue)
in at least one place.saveChecklist
with 2 parameters, use any kind of loop in at least one place.write
saveChecklist
with 2 parameters, call write
in at least one place.saveChecklist
with 2 parameters
def
to define saveChecklist
with 2 parameterswith
statement
saveChecklist
with 2 parameters, use with _ as _:
___
in at least one place.saveChecklist
with 2 parameters, use any kind of loop in at least one place.open
saveChecklist
with 2 parameters, do not call open
.write
saveChecklist
with 2 parameters, call write
in exactly one place.loadChecklist
with 1 parameter
def
to define loadChecklist
with 1 parameteropen
loadChecklist
with 1 parameter, call open
in at least one place.loadChecklist
with 1 parameter, use any kind of loop in at least one place.loadChecklist
with 1 parameter
def
to define loadChecklist
with 1 parameterwith
statement
loadChecklist
with 1 parameter, use with _ as _:
___
in at least one place.loadChecklist
with 1 parameter, use any kind of loop in exactly one place.open
loadChecklist
with 1 parameter, do not call open
.