Problem Set 1 - Due Tue, Sep 11 at 23:59
- The CS111 Problem Set Guide gives an overview of psets, including a detailed description of individual and partner tasks
- Lecture 01 slides and notebook
- Lab 01
- Lecture 02 slides and notebook
- Goldwasser & Letscher, Chapter 3, sections 3.1-3.2, pages 89-103
- Our CS 111 Code Style Guide describes rules for Python coding style that you should follow.
About this Problem Set
This problem set will give you practice with basic Python interactive programs as well as creating graphic scenes with the
Task 1 is an individual problem in which you will get practice with debugging in the context of fixing a misbehaving program.
In Task 2, you will write a time profiler program that calculates and visualizes the time spent by a person per week on various activities.
- In Task 3 (Partner task), you will create a graphics scene using the
cs1graphicspackage. Use this shared Google Doc to find and record your pair programming partner. Please DO NOT use the students-cs111-f18 group to find partners. Remember, you can talk with other individuals and teams about high-level problem-solving strategies, but you cannot share any code with them.
The CS111 Problem Set Guide gives an overview of psets, including a detailed description of individual and partner tasks.
Read the PS01 FAQ -- it covers the most common issues students encounter when working on PS01.
In Spring 2018, students spent the following times (in hours) on these three tasks:
Task Average Median 25% took more than 10% took more than Task 1 2.9 2.8 3.9 4.3 Task 2 3.9 3.5 5.0 6.1 Task 3 3.2 3.0 4.0 5.0
Note: It is possible to satisfy the minimal requirements for Task 3 in very little time, and we do not have high artistic expectations for your graphics scene. But many students take much longer because they have fun with the graphics system want to make something that looks aesthetically pleasing.
All code for this assignment is available in the
ps01folder in the
cs111/downloaddirectory within your
csserver account, which you learned how to access in Lab 1.
- Guidelines for checking the correctness and quality of your solution are here.
Task 0: Ask or answer a question on students-cs111-f18
On a typical problem set, we expect that you will have many questions.
You can seek answers to these questions from instructors during office hours; from tutors during drop-in hours; from SI leaders during their sessions; and from fellow classmates.
Another important way to ask questions is to post them on the students-cs111-f18 Google group. This forum is monitored by the CS111 staff and your classmates, one of whom will try to answer your question in a timely fashion. To get you in the habit of posting to and reading this group, we require that every student posts at least one question or one answer on the students-cs111-f18 group in the first couple weeks of the semester.
When do I post my question or my answer?
- if your last name starts with A-L, post during the week Sep 4 - Sep 11 [before 11:59pm on Sep 11]
- if your last name starts with M-Z, you will post as part of the next pset (PS02)
Tips on posting to the course google group (students-cs111-f18)
- Questions can involve anything related to the course: this assignment, material from lecture or lab, Canopy setup, etc.
- Do not post a question that has already been asked
- Do not post python code (if you do, we will delete your post)
- Do not use students-cs111-f18 to find pset partners (use the pset partner google doc to facilitate partnering)
- Note that you can post to this group directly via email using the email address firstname.lastname@example.org.
Tips on email inbox management
With more about 120 students in the students-cs111-f18 group, your inbox will quickly fill with messages.
- We strongly recommend that you learn how to organize your Gmail using labels, filters, and multiple inboxes to deal with the volume of email.
- This is important so that you don't miss emails from the instructors or tutors, which might contain urgent information about the course.
- Here are two short YouTube videos to help you, of course, there are many more of this flavor available online: Video 1: Setting up labels. Video 2: Multiple Inboxes.
If you have difficulties talk to an instructor or tutor.
Task 1: Debugging Numbers
This is an individual problem which you must complete on your own, though you may ask for help from the CS111 staff.
It is rarely the case that code works the first time you test it. It often contains one or more so-called bugs that cause it to behave incorrectly. So much of the time you spend programming in the course will involve debugging, which is the process of finding and removing bugs.
One of the themes of this course is learning strategies for debugging. So we will have some problem set problems that focus on debugging. This is the first such problem, but there will be others. In these problems you will be given code that does not work, and your goal will be to debug it until it does work.
Here's a simple example of a program that does not behave as expected:
username = raw_input('What is your name? ') # line 1 print 'Hello, username' # line 2 n = raw_input('Enter an integer n: ') # line 3 print 'n is', n, '; 2*n is', 2*n # line 4
What is your name? Wendy Hello, username Enter an integer n: 23 n is 23 ; 2*n is 2323
(Important Note: In this pset, we use magenta text to highlight text entered by the user to distinguish it from text printed by the program, which is in black. If you actually run the program, the text entered by the user will also appear in black; it will not be displayed in magenta.)
This program misbehaves in two ways:
- Rather than displaying the name (
Wendyin this case) entered by the user, it always displays the string
- Rather than showing the double of the integer
nfor the expression
46in this case) it shows the two-fold concatenation of the the digts in the number (
"2323"in this case).
The first bug is due to the fact that in line 2, the occurrence of
username inside the
printed string denotes the literal string
"username" and not the value of the
username. This can be fixed by changing line 2 to be
print 'Hello, ' + username # fixed line 2, version 1, for 1st bug
print 'Hello,', username # fixed line 2, version 2, for 1st bug
The second bug occurs because in the expresion
2*n the value of
n is the
string of digits entered by the user (e.g., the 2-digit string
rather than the integer
23. This can be fixed by using the
to convert the string of digits
"23" to the integer
One way to apply this fix is to change line 4 to be
print 'n is', n, '; 2*n is', 2*int(n) # fixed line 4 for 2nd bug
Alternatively, we could leave line 4 unchanged, and instead change line 3 to be
n = int(raw_input('Enter an integer n: ')) # fixed line 3 for 2nd bug.
With either combination of fixes, the program now works as expected:
What is your name? Wendy Hello, Wendy Enter an integer n: 23 n is 23 ; 2*n is 46
This example illustrates that (1) even a very short program can contain multiple bugs and (2) often there is more than one way to fix a bug.
A Note on
Most examples involving
iis a variable holding the integer
fis a variable holding the floating point number
sis a variable holding the string
print('i is ' + str(i) + '; f is ' + str(f) + '; s is ' + s)
displays the characters
i is 42; f is 6.023; s is bunny
However, it turns out that in Python 2.7 (the version we use),
The argument(s) to
print 'i is', i, '; f is', f, '; s is', s
will also displays the characters
i is 42; f is 6.023; s is bunny
Passing comma-separated values with arbitrary types to
Bud Lojack is experimenting with a Python program that processes numbers entered by the user. He wants his program to behave as shown in the following examples, where text highlighted in magenta is entered by the user.
Sample Execution #1
Enter a nonnegative integer intNum: 17 The integer you entered is 17 Three times intNum is 51 17 concatenated copies of X is XXXXXXXXXXXXXXXXX The integer remainder of 17 divided by 3 is 2 The integer quotient of 17 divided by 3 is 5 The floating point quotient of 17 divided by 3 is 5.66666666667 The number of digits in 17 is 2 Enter a floating point number floatNum: 143.816 floatNum is 143.816 143.816 rounded to two places is 143.82 143.816 truncated to an integer is 143 143.816 rounded to an integer is 144 The maximum of 17 and 143.816 is 143.816
Sample Execution #2
Enter a nonnegative integer intNum: 6 The integer you entered is 6 Three times intNum is 18 6 concatenated copies of X is XXXXXX The integer remainder of 6 divided by 3 is 0 The integer quotient of 6 divided by 3 is 2 The floating point quotient of 6 divided by 3 is 2.0 The number of digits in 6 is 1 Enter a floating point number floatNum: 3.141 floatNum is 3.141 3.141 rounded to two places is 3.14 3.141 truncated to an integer is 3 3.141 rounded to an integer is 3 The maximum of 6 and 3.141 is 6
Click here to see Bud's program,
which attempts to implement the above behavior. His progam can also be found in the
in the file
Bud's program has numerous bugs that prevent it from working properly.
Your goal in this subtask is to fix Bud's program so that it behaves as indicated by the above examples.
ps01 folder is a file
debugNumsFixed.py that initially is a copy of Bud's program with
some additional header information at the top that you should fill out.
You should modify
debugNumsFixed.py by finding and fixing all the bugs it contains,
so that ultimately it has the correct behavior. You should also explain the bugs and
your fixes by defining string variables
fix2, etc. as described below.
Do not make any changes to
debugNumsBuggy.py. You should change only
Most changes require modifying/replacing existing lines in Bud's program. It is never necessary to completely remove any lines from Bud's program. It is necessary to add only one missing line to Bud's program.
You should use an incremental strategy for debugging. This means that, at every step, you should focus on the first line of the program that incorrect (i.e., either it prints the wrong output or the program at that line generates an error). Then attempt to fix this line, and test your modified program to see if it improves the behavior of the program. If your bug fix doesn't work, try again. If it does work, focus on the next broken line. Keep doing this until the behavior is correct.
At the bottom of
debugNumFixed.py, we want you to give a brief summary of each bug in Bud's program and how you fixed it. Here's how you should do that. Suppose you number your bugs 1, 2, 3, etc.
For each bug number n, you should assign a new variable named
bugn to a triple-quoted string that describes the bug.
In cases where a line of the program is wrong, give the line number from Bud's original
and explain why that line is wrong.
- In the one case where you need to add a line of missing code, explain why you need to add a new line of code.
For each bug number n, you should assign a new variable named
fixn to a triple-quoted string that describes how you fixed the bug in your program.
In cases where a line of the program is wrong, explain how you fixed the bug.
- In the one case where you need to add a line of missing code, explain what that code
does and the line where you added it in the program (using line numbers from Bud's original program).
For instance, for the example buggy program show above in the Background section, here are variables that summarize one pair of bug fixes
# Below, summarize your bug fixes using variables named bug1, fix1, # bug2, fix2, etc. that are assigned to triple-quoted strings. bug1 = '''Line 2 prints the characters in the string "username" rather than the value of the string stored in the variable named username''' fix1 = '''Change the printed string to be the result "Hello, " + username, which concatenates the string "Hello, " with the string stored in the variable username.''' bug2 = '''Because the variable n holds a string and not an integer, the expression 2*n in line 4 returns the result of concatenating that string with itself.''' fix2 = '''Change the expression 2*n in line 4 to 2*int(n), which uses int() to convert a string of digits to the corresponding integer.'''
Why should you use triple-quoted strings in these definitions? Because triple-quoted strings can have multiple lines, but strings delimited by only one pair of single or double quotes cannot.
Running Codder on Task 1
To test that your
debugNumsFixed.py program works as expected, you can use the Codder code feedback tool you saw in Lab 01, Part 3 to test your program. Follow these steps:
- Go to https://cs.wellesley.edu/codder
- Authenticate yourself with your Wellesley domain name and password (do not use your cs.wellesley.edu password!)
- From the drop-down menu, select ps01 as the activity.
- Click on the Choose file button, and use the pop-up file selection window
to navigate to your
- Click on the submit file button.
The Codder server will take a few seconds to respond with feedback on your code.
In the resulting report, pay attention to the parts marked in light purple, which highlight potential problems with your program. You should try to fix the reported issues in your program, and resubmit. You can resubmit your code as many times as you want.
Task 2: Time Profiler
This is an individual problem which you must complete on your own, and may ask for help from the CS111 staff.
Armed with Python knowledge from your first CS111 lectures and lab, and confident with your debugging skills from Task 1, you've decided to form a software startup that provides your fellow students with useful programs.
Since time is important to all Wellesley students, you've decided your first program should be a time profiler that helps students visualize how they spend their time during a typical week. The program will ask the user to enter a few pieces of information, and then will display a textual representation of the hours they spend in four activities during the week:
- Classwork: This includes time in class plus homework time. You may assume in general that two hours of homework time are required for every hour in class.
- Activities Outside of Class: This includes time spent in organized athletics, music activities, and clubs, as well as any jobs. Your program will ask the user to name this category.
- Sleep: This is the amount of time you spend sleeping each week.
- Free Time: For simplicity, this category encompasses all activities not covered by the other three activities above.
Each time you run your program (by pressing the green triangle in Canopy) it should prompt the user to enter information about these activities and display a time profile. Below are three example executions of the time profiling program that show how your program should behave. In each run, text in black is displayed by the program, while text highlighed in magenta is entered by the user. (As in Task 1, in your actual program, the text entered by the user will also be displayed in black; we use magenta here only to clarify the distinction between text entered by the user and text displayed by the program.)
Sample Execution #1
What is your name? Valentina How many classes are you taking this semester? 5 What is the average time in class per week this semester? 2.5 What shall we call the 'everything outside of class' category? fun How many hours per week do you spend on 'fun'? 15 How many hours per day do you sleep on average? 8 Weekly time profile for Valentina: 37.5 class hours: CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC 15.0 fun hours: XXXXXXXXXXXXXXX 56.0 sleep hours: SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS 59.5 free hours: FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
Sample Execution #2
What is your name? Emma How many classes are you taking this semester? 4 What is the average time in class per week this semester? 3.67 What shall we call the 'everything outside of class' category? extracurricular How many hours per week do you spend on 'extracurricular'? 22.5 How many hours per day do you sleep on average? 6.75 Weekly time profile for Emma: 44.04 class hours: CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC 22.5 extracurricular hours: XXXXXXXXXXXXXXXXXXXXXXX 47.25 sleep hours: SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS 54.21 free hours: FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
Sample Execution #3
What is your name? Sofia How many classes are you taking this semester? 4 What is the average time in class per week this semester? 3.2 What shall we call the 'everything outside of class' category? chill How many hours per week do you spend on 'chill'? 24 How many hours per day do you sleep on average? 6.55 Weekly time profile for Sofia: 38.4 class hours: CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC 24.0 chill hours: XXXXXXXXXXXXXXXXXXXXXXXX 45.85 sleep hours: SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS 59.75 free hours: FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
Note that each row of the profile has a number of characters that is the rounded version of the number of hours displayed on the row. So in Sofia's profile, there are 38
Ss and 60
Fs. . You should use
Xs to indicate the number of 'everything outside of class' hours. Also note that each number of hours has at least one digit after a decimal point, and at most two.
Note also that your program should ask the user for the name of the category used to describe the 'everything outside of class' category, and then use that name in the remainder of the program. In the first example above, Valentina chose to use the name 'fun', Emma chose to use the label 'extracurricular', and Sofia chose to use the name 'chill'. The user of the program gets to decide which name to use to describe that category, and your program incorporates that name into the profile.
Finally, note that the rows the profile are aligned at the colons that separate the title of each row from the repeated characters. The colons should be aligned correctly regardless of what name is chosen for the 'activities outside of class' category.
In your account on the
cs server is the CS111 download folder
cs111/download. This folder contains a subfolder name
ps01. Begin by copying this
ps01 folder to your computer.
In this task, you will write a time profiling Python program
timeProfiler.py from scratch in Canopy that has the behavior indicated by the sample executions above. You should save this file to the
Your goal is to create a
timeProfiler.py program that behaves exactly as indicated by the sample executions above, and works reasonably on other sample inputs.
Any major programming task like this should be broken down into smaller subtasks.
For example, start by writing code to solve the subtask of prompting the user for their name and displaying the header "Weekly time profile for (name):"
Sample Execution for Subtask 1
What is your name? Sofia Weekly time profile for Sofia:
Second, add a prompt for the number of sleep hours and add a corresponding line to the output display.
Sample Execution for Subtask 2
What is your name? Sofia How many hours per day do you sleep on average? 6.55 Weekly time profile for Sofia: 45.85 sleep hours
Third, add a prompt for the name of the 'outside of class activities' category.
Sample Execution for Subtask 3
What is your name? Sofia What shall we call the 'everything outside of class' category? chill How many hours per day do you sleep on average? 6.55 Weekly time profile for Sofia: 45.85 sleep hours
Fourth, add a prompt for the number of -- in this particular case, chill -- hours, and add a corresponding line to the output display.
Sample Execution for Subtask 3
What is your name? Sofia What shall we call the 'everything outside of class' category? chill How many hours per week do you spend on 'chill'? 24 How many hours per day do you sleep on average? 6.55 Weekly time profile for Sofia: 24.0 chill hours 45.85 sleep hours
Continue in this way until you have prompts for all inputs and are able to produce the following (of course, your output will vary depending on what the user enters for the 'outside of class' category). Your program should be able to reproduce the sample executions above for Valentina, Emma and Sofia.
Sample Execution for Subtask 5
What is your name? Sofia How many classes are you taking this semester? 4 What is the average time in class per week this semester? 3.2 What shall we call the 'everything outside of class' category? chill How many hours per week do you spend on 'chill'? 24 How many hours per day do you sleep on average? 6.55 Weekly time profile for Sofia: 38.4 class hours 24.0 chill hours 45.85 sleep hours 59.75 free hours
Your remaining subtasks can now add in the repeated characters to create the plot and align everything correctly.
Note that this breakdown into subtasks is just one suggestion. You can break them down any way you prefer. However, trying to write the code for the entire program without breaking it down is a bad idea.
timeProfiler.pyfile must start with comments at the top, identifying the author, username, problem set number, filename, and date. Follow this format:
# Wendy Wellesley # wwellesl # CS111 Problem Set 1 # timeProfiler.py # 09/11/2018
- The arithmetic operators
- The string operators
+(string concatenation) and
- The built-in functions
str. These are all explained in the Lecture 02 slides.
Your program should not use any other Python features that we have not studied yet, such as string methods, conditionals (i.e.,
ifstatements), lists, loops, or recursion.
- The arithmetic operators
- For a completely correct solution, spacing, alignment of colons, and the number of digits after a decimal point in a number are important. But you can receive partial credit for solutions that are not exactly correct.
You may assume that inputs are sensible, and the behavior of your program for nonsensical inputs is unspecified. This means that your program can do anything if an input is unspecied and still be considered correct. For example, if the number of classes specified is not an integer, the program could fail with an error message immediately, it could fail after reading in other inputs, or it could fail after printing the first line of the output display.
Here are inputs that are sensible:
- The name input may be any string, including the empty string.
- The number of classes is assumed to be an integer.
- The average time in class per week is expected to be a number (integer or floating point number)
- The number of extracurricular hours per week is expected to be a number (integer or floating point number)
- The number of hours slept per day is expected to be a number (integer or floating point number)
- The number of inputted hours or their total sum cannot be larger than the total number of hours in a week (168 hours). This would have as a result a negative value for the amount of free hours.
- The 'everything outside of class' category can be any string that is not one of the other category names (i.e, it should not be
Later in the semester, we will learn how to check that the inputs are sensible and then require the user to enter valid inputs.
Running Codder on Task 2
To test that your
timeProfiler.py program works as expected, you can use the Codder code feedback tool
you saw in Lab 01, Part 3 to test your program. Follow the same steps as
in using Codder for Task 1, except this time navigate to you
Task 3: Graphics Scene
This task is a partner problem in which you are required to work with a partner as part of a two-person team.
ps01 folder contains a copy of the
package. We have also included a copy of the
graphicsState package (used in labs and lectures). In this task, you are to create a new file,
scene.py, from scratch in the
ps01 folder that creates
your own novel scene on a
Canvas object. Since we have a limited
number of shapes at our disposal, we encourage abstract and creative
scene.py file must satisfy the following criteria:
Your file must start with comments at the top, identifying the authors, usernames, problem set number, filename, and date. Follow this format:
# Wendy Wellesley and Abby Stracksen # wwellesl, astracks # CS111 Problem Set 1 # scene.py # 09/12/2017
- Your file must appropriately
cs1graphicspackage (and the
cs1graphicsHelperpackage, if you opt to use it).
- Your canvas must have a descriptive title.
You must use at least one instance of each of the following classes:
- You must vary the border thickness and colors of some shapes.
- You must vary the interior colors for some of your fillable objects, including at least one that has transparent color.
- You must include an Image in your scene. Note that this requires that you include the image file in your
ps01folder when you submit your assignment (GIF files tend to work best). It is OK if your image has a background that is not transparent. Your image will appear in its original form in your graphics scene. Although
cs1graphicsincludes flipping, scaling and rotating, these operations do NOT work with imported images. If you try to flip/scale/rotate your image in
cs1graphics, it will turn completely black. To avoid this, you may use Photoshop (or similar image-editing software) to manipulate your image to the desired size/orientation, and then import it into your scene.
- You must position at least one object so that it is partially obscuring another object in your scene.
You are welcome to use other
cs1graphics features in addition to the ones listed above, but only the above features are required.
After completing this task, you must take a screenshot of your graphics scene, name it correctly, and upload it to the shared google drive folder here. See details below.
There is no Codder test for Task 3
Because Task 3 is an open-ended graphics problem, we do not supply a student-runnable version of Codder for this task.
Task 4: Honor Code Form
ps01 folder contains a file
honorcode.py with several variable assignments for
your name, the time you spent on the tasks, etc. Change the values in these variable
assignments to appropriate values.
How to turn in this Problem Set
In Task 3 you will take a screenshot of the output, which you will submit in addition to your code. Here are screenshot instructions.
Softcopy (electronic) submission (2 separate steps)
Submit your code (every student)
- Save your final
timeProfiler.pyfiles in the
- Each team member should save their
scene.pyfile in their
ps01folder. This file should contain a comment with names of both partners at the top.
- Also, any image file (GIF files tend to work best) that you use in your graphics scene must be included in the
Fill out the
honorcode.pyfile and save it in the
ps01folder as well.
- Note: It is critical that the name of the folder you submit is
ps01, and it includes files named
honorcode.py. In other words, don't rename the folder that you downloaded, do not delete or rename any files in the 'ps01' folder, and use the exactly the names we have specified for any new files. (Running Canopy will automatically create some new files in your
ps01folder; you should not touch or delete these automatically generated files.)
- Drop your entire
ps01folder in your
dropfolder on the
csserver using Cyberduck by 11:59pm on Tuesday, Sep 11, 2018.
- Failure to submit a code file before the deadline will result in zero credit for that code on PS01.
- Save your final
Submit your screenshot (one per pair of students)
Each pair of students in the course must upload a screenshot of their graphics scene to the the shared google drive folder.
- Take a screenshot of your graphics scene (screenshot instructions here).
- RENAME your screenshot with a short, unique descriptive name that begins with your CS account names.
- Drag your properly named screenshot into the shared google drive folder by 11:59pm on Tuesday, Sep 11, 2018.
Hardcopy (paper) submission
There is no hardcopy submission. Pset submission is entirely electronic.