![]() |
![]() |
|
home—info—lects—labs—exams—hws
—D2L—breeze (snow day)—tutor/PIs
Due Feb.27 (Thu) 17:00; submit to hw03-dropbox and a hardcopy in class or under my office door.
For this homework, type your answers in the top half of JES, and save to a file hw03.py.
You can start by pasting in all of the below, changing the first line to actually contain your name,
and then following the instructions for problems #1, #2, and #3.
A
Note: Runningtst will print output, but when it reaches the test cases for #2 and #3, it will give an error if you haven't yet written those functions. That's okay.
# your name # ITEC109 hw03 -- see http://www.radford.edu/~itec109/2014spring-ibarland/Homeworks/hw03.html # We will use some string-library functions for this hw: import string # A helper function, for this homework: # # substring -- given a string, a start-index, and a stop-index, # return the characters from start-index up to (but not including) stop-index. # Indices are 0-based. # Examples: substring( "radford", 3, 6) = "for" # substring( "radford", 0, 3) = "rad" # def substring(txt,start,stop): return txt[start:stop] ############################# #### Problem#1 (6pts) ## Consider the following 7 lines of code/comments: # inputs: height and diameter of a cone: height = 10 diam = 20 rad = diam/2 baseArea = 3.14 * pow(rad,2) # Our final answer: the volume of the cone. 1.0/3.0 * baseArea * height ## Replace the above lines with a function name `coneVolume` by filling in the blanks below; # coneVolume : float, float -> float # Given the height and diameter of a cone (in that order), # return its volume. # def __________ ( ________, _______ ): __________________ __________________ return ______________ #### Problem 1b (0pts): At the end of your file, inside `tst` below, uncomment the tests for `coneVolume`, # and verify that all the expected-outputs match the actual-ouputs when you run `tst()`. ############################## #### Problem #2 (7pts) # Convert the following lines of code with a function named `removeLead`: # The input: msg = "the leader" leadStart = string.find( msg, "lead" ) leadEnd = leadStart + len("lead") beginning = substring( msg, 0, leadStart ) ending = substring( msg, leadEnd, len(msg) ) # The answer: beginning + ending # removeLead : string -> string # Given a string containing the four letters "lead", # return the same string but with [the first occurence of] those four letters removed. # (See examples in `tst` below.) # ################################# #### Problem #3 (7pts) # Write the following function: # remove2FrontBack : string -> string # Given a string of length four or more, # return that string with the first two and last two characters removed. # (See examples in `tst` below.) # def tst(): print( "Problem #1:" ) print( "Expect: " + str(1043) + " (approx)" ) print( "Actual: " + str( coneVolume(10,20) ) ) print( "Expect: " + str(2086) + " (approx)" ) print( "Actual: " + str( coneVolume(20,20) ) ) print( "Expect: " + str(4172) + " (approx)" ) print( "Actual: " + str( coneVolume(10,40) ) ) print( "Expect: " + str(0) ) print( "Actual: " + str( coneVolume(0,20) ) ) print( "Expect: " + str(0) ) print( "Actual: " + str( coneVolume(10,0) ) ) print( "Problem #2:" ) print( "Expect: " + "the er" ) print( "Actual: " + removeLead("the leader") ) print( "Expect: " + "Anyone can " ) print( "Actual: " + removeLead("Anyone can lead") ) print( "Expect: " + "" ) print( "Actual: " + removeLead("lead") ) print( "Problem #3:" ) print( "Expect: " + "he" ) print( "Actual: " + remove2FrontBack( "behead" ) ) print( "Expect: " + "fore and aft" ) print( "Actual: " + remove2FrontBack( "before and after" ) ) print( "Expect: " + "x" ) print( "Actual: " + remove2FrontBack( "vwxyz" ) ) print( "Expect: " + "" ) print( "Actual: " + remove2FrontBack( "wxyz" ) ) |
home—info—lects—labs—exams—hws
—D2L—breeze (snow day)—tutor/PIs
©2014, Ian Barland, Radford University Last modified 2014.Feb.22 (Sat) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |