CS101 - Homework 2

Due: 2018-02-28 11:59p

Objectives

[2 points] Create your file, add proper comments

From within the Thonny application, create a single Python file username_hw2.py with all your work for this assignment in it. Include a multi-line comment at the top with your name, the date, and your lab section. Create empty functions for each of aspectRatio(), flip(), and lastWord() and include a docstring comment for each.

[6 points] What was that error?

At this point, we have all generated errors in Python. You’ve typed something and gotten an angry red response. You should take the time to read these. Not everything they have to tell you is comprehensible right away, but you can get a good sense of what caused the error by looking at them. To get you familiar with the possible kinds of errors you might generate, we would like you to go on a bug hunt. For each of the following exceptions, write a line (or lines) of Python that causes them.

Of course, you test can them out in interactive mode, but you can’t just include these lines in your script, because the script will never run. To prevent Python from choking on them, make a large comment block in there, label it part 1 and then paste your error causing lines right into the comment so Python can’t see them.

[6 points] Aspect ratio

Write a function called aspectRatio(width, height). It should take in the width and height of a rectangle and return the aspect ratio (width/height). Of course, if the height is 0, you can’t do the division, so just return 0.

Example use:


>>> aspectRatio(4, 4)
1.0
>>> aspectRatio(3, 2)
1.5
>>> aspectRatio(3, 5)
0.6
>>> aspectRatio(6, 0)
0.0

[8 points] Number flip

Write a function called flip(number). It takes in an integer, converts it to a string, reverses it, turns it back into an integer and returns the result. You can assume you will always be given valid integers, and you do not need to worry about “leading 0s” on flipped numbers (e.g., flip(10) => 1).

Example use:


>>> flip(42)
24
>>> flip(378)
873
>>> flip(60)
6
>>> flip(5)
5

[Hint: we have not seen a reverse function for strings because there is no such built-in function. If you Google "reverse string Python" you will discover a slicing option you can try.]

[8 points] Word extractor

Write a function called lastWord(s) that extracts the last word from a string.

Start by using the technique we discussed in class (s.rfind(' ')) to identify the location of the last space in a string. Start by just returning the rest of the string. Of course it is possible that the string doesn’t have any spaces, so make sure your code returns the entire string in this case.

Example use:


>>> lastWord("Good morning, sunshine")
'sunshine'
>>> lastWord("Today is Tuesday")
'Tuesday'
>>>lastWord("Daisy")
'Daisy'

[2 extra points] Challenge: Improve the word extractor

The word extractor may have some small issues at this point: It may leave punctuation on the ends of the extracted word and if the string ends in a space (or several spaces), we might get the empty string. Improve the extractor so that it first trims off all of the space characters before extracting the word and then removes all punctuation from the start and end of the word.

[Challenge questions are optional and are worth a nominal amount of extra credit. They are strongly recommended for potential majors.]

Example use:


>>> lastWord("You should handle punctuation!")
'punctuation'
>>> lastWord('even "double quotes"')
'quotes'
>>> lastWord('and spaces at the end    ')
'end'

[Optional] CodingBat

You now know enough to be able to do some CodingBat exercises. In particular, Warmup-1, String-1, Logic-1, and Logic-2 are all sections that you could try. This is a great way to reinforce some of the things you have been learning.

Handing in your work

Before submitting, see the grading rubric and make sure you have followed all instructions correctly. Submit your file username_hw2.py using the CS 101 submit script (you may submit multiple times, just be sure you always spell your name the same way, and always specify the same Lab section).