CS21 Lab 2: Numbers and Strings
Due Saturday, September 26, before midnight (US/Eastern local to Swarthmore)
Programming Tips
As you write your first programs, start using good programming practices now:
-
Use a comment at the top of the file to describe the purpose of the program (see example).
-
All programs should have a
main()
function (see example). -
Use variable names that describe the contents of the variables.
-
Write your programs incrementally and test them as you go. This is really crucial to success: don’t write lots of code and then test it all at once! Write a little code, make sure it works, then add some more and test it again.
-
Don’t assume that if your program passes the sample tests we provide that it is completely correct. Come up with your own test cases and verify that the program is producing the right output on them.
-
Avoid writing any lines of code that exceed 80 columns. In
emacs
, at the bottom, center of the window, there is an indication of both the line and the column of the cursor.
Are your files in the correct place?
Make sure all programs are saved to your cs21/labs/02
directory! Files
outside that directory will not be graded.
$ update21
$ cd ~/cs21/labs/02
$ pwd
/home/username/cs21/labs/02
$ ls
Questions-02.txt
(should see your program files here)
Goals
The goals for this lab assignment are:
-
Manipulate Python numeric and string data types
-
Learn to apply the accumulator pattern to numeric and string types to solve problems
-
Learn to iterate over data using
for
loops -
Practice string operations:
+
,*
,len()
-
Practice using the
range
function
1. Checkpoint
At the end of your lab session, be sure to run handin21
.
Your lab instructor will check your files, which should reflect that you have made non-trivial progress (in python or pseudocode) towards the solution. Note that if you have not made much progress towards your solution, we expect that you would have been actively seeking help during your lab over Slack.
A portion of your final lab grade is dependent on this checkpoint.
If there are circumstances which prevent you from making substantial progress on this lab, please contact your lab instructor as soon as possible.
2. addicted to disney+
Over the summer, you watched more disney+ than you can remember. Now that school’s started, you need to break this habit. A good first step is to figure out how much time you’re watching each day, on average.
Write a program called video.py
to keep track of your viewing habits. Your program should allow you to specify how many days you are logging and then, for each day, specify how many minutes you watched. After entering all the data, you should show the user how much they watched in total over that period and what their daily average was.
One example of running the program is shown below. User input is shown in bold.
$ python3 video.py
How many days will you log? 5
Day 1
How many minutes did you watch? 107
Day 2
How many minutes did you watch? 95
Day 3
How many minutes did you watch? 0
Day 4
How many minutes did you watch? 44
Day 5
How many minutes did you watch? 27
After 5 days, you watched 273 minutes.
That averages out to be 54.6 minutes per day.
Your program should have the following features:
-
Ask the user for the number of days to log.
-
For each day, ask for the number of minutes watched that day.
-
When all of the days are entered, print out the total number of minutes and the average number of minutes per day.
-
You can assume that each value input by the user will be a non-negative integer.
To print a blank line, you can call print() with no arguments. |
3. half speed and double speed
Speaking of streaming video, you’re going to be watching a lot of that this semester. Sometimes you’ll want to pay very close attention and watch on half speed. Other times you’ll want to zip through on double speed.
Write a program called stream.py
that simulates what happens when you watch at half speed and double speed. Ask the user the enter a string that might have been spoken in a video. Simulate half speed by inserting a period in between every letter of the string. Simulate double speed by dropping every other character.
Two examples of the running program are shown below. User input is shown in bold.
$ python3 stream.py
Enter the text from the video: Welcome to CS21!
In half speed: W.e.l.c.o.m.e. .t.o. .C.S.2.1.!.
In double speed: Wloet S1
$ python3 stream.py
Enter the text from the video: hairdodyeing immersestangle
In half speed: h.a.i.r.d.o.d.y.e.i.n.g. .i.m.m.e.r.s.e.s.t.a.n.g.l.e.
In double speed: hidden message
Features:
-
The user can enter a string of any length.
-
Your output will insert a period between each characters for the slow output.
-
Your output will only show every other character for the fast output.
4. arrows
Write called arrow.py
that prints an arrow shape on the screen made up of greater-than-signs. The user
enters an integer to determine how far out the arrow points.
This problem might seem very challenging at first! Below the sample output are some hints that might help you get started.
Some examples of running the program are shown below. User input is shown in bold.
$ python3 arrow.py
How long is the arrow? 2
>
>>
>
$ python3 arrow.py
How long is the arrow? 5
>
>>
>>>
>>>>
>>>>>
>>>>
>>>
>>
>
$ python3 arrow.py
How long is the arrow? 1
>
This program could benefit from some planning before you get started! Let’s think about how we can break down this problem into two smaller subproblems in order to get the final result we want.
4.1. the top half of the arrow
Let’s start by thinking about the top half of the arrow when the user enters 5 for the length of the arrow:
>
>>
>>>
>>>>
>>>>>
There are a few things to notice here that might help you:
-
The user specified length 5 for the arrow and there are 5 lines in the top half of the arrow. That means we might want a loop that repeats 5 times.
-
On line 1, there is 1 greater-than symbol. On line 2, there are 2 greater-than symbols, etc. How can string multiplication be used to generate the right number of greater-than symbols on each line?
You can repeat a string in python by using the * symbol. For example: 'hi' * 5 = 'hihihihihi' |
4.2. the bottom half of the arrow
Once you’ve got the top half of your arrow working, you can start thinking about the bottom half of the arrow. Here’s what the bottom half of the arrow looks like when the user enters 5 for the length of the arrow:
>>>>
>>>
>>
>
This looks a lot like the top half of the arrow with some exceptions:
-
There are only 4 lines, not 5, and the longest line has 4 greater-than symbols, not 5.
-
The lines count down 4, 3, 2, 1, instead of going up.
4.3. putting it together
Don’t try to make one giant loop that prints the whole arrow. You can have one loop for the top half. After that’s done, you can have one loop for the bottom half.
Your program should have the following features:
-
Ask the user for the length of the arrow.
-
You can assume the user enters an integer greater than or equal to 1.
-
The arrow should follow the same pattern as shown above
5. Optional Extra Challenge — diamonds
This part does not affect your grade so please only attempt this after completing the rest of your lab. It is simply an extra challenge, if you want to try it.
Write a new program, diamonds.py
, that draws a pattern similar to the arrows you drew above. Your program should ask the user for the number of rows in the diamond and the character that you want to use in the diamond. You will print out a suitable diamond pattern.
Here are some examples:
$ python3 diamonds.py
How many rows do you want (odd numbers only)? 5
What symbol do you want to use in the diamond? !
!
!!!
!!!!!
!!!
!
$ python3 diamonds.py
How many rows do you want (odd numbers only)? 7
What symbol do you want to use in the diamond? @
@
@@@
@@@@@
@@@@@@@
@@@@@
@@@
@
You can assume the user enters an odd number. You can decide what to do if the users enters an even number.
6. Answer the Questionnaire
Each lab will have a short questionnaire at the end. Please edit
the Questions-02.txt
file in your cs21/labs/02
directory
and answer the questions in that file.
Once you’re done with that, run handin21
again.
Turning in your labs….
Remember to run handin21
to turn in your lab files!
You may run handin21
as many times as you want. Each time it will
turn in any new work. We recommend running handin21
after
you complete each program or after you complete significant
work on any one program.