![]() |
![]() |
|
home—info—labs—exams—hws
—D2L—MediaSamples/—breeze (snow day)—tutor/PIs
Last time, we looked at using a loop to examine each character in a string, using the square-brackets (array-lookup) to look at the i'th character. We'll practice these skills today.
YOUR TASKS:
hint: As you write this, and you reach something which you wish was a built-in function, use the technique of wishful-thinking, and make up a helper-function to do what you want. Then you can come back later and write that, perhaps even getting some help.
Here are some test-cases for you:
# testCountUpperCase : -> None # def testCountUpperCase(): print("Actual: " + str( countUpperCase( "a" ) )) print("Expect: " + str( 0 )) print("Actual: " + str( countUpperCase( "A" ) )) print("Expect: " + str( 1 )) print("Actual: " + str( countUpperCase( "Aa" ) )) print("Expect: " + str( 1 )) print("Actual: " + str( countUpperCase( "aA" ) )) print("Expect: " + str( 1 )) print("Actual: " + str( countUpperCase( "AA" ) )) print("Expect: " + str( 2 )) print("Actual: " + str( countUpperCase( "abc" ) )) print("Expect: " + str( 0 )) print("Actual: " + str( countUpperCase( "ABC" ) )) print("Expect: " + str( 3 )) print("Actual: " + str( countUpperCase( "ABC" ) )) print("Expect: " + str( 3 )) print("Actual: " + str( countUpperCase( "?AbC!@#$42 Z " ) )) print("Expect: " + str( 3 )) return None |
Note: There are several ways to solve this problem; I can think of three that are all reasonable; some involve a so-far variable, and some don't.
hint: Your overall answer involves a boolean. That means your so-far variable should be a boolean — something likeseenItSoFar !
hint: If you want to update a boolean so-far variable, rather than something like “vowelsSoFar = vowelsSoFar + 1 ”, you might say something like “seenItSoFar = seenItSoFar or … ”,
# testContainsChar : -> None # def testContainsChar(): print("Actual: " + str( containsChar( "a", "a" ) )) print("Expect: " + str( True )) print("Actual: " + str( containsChar( "a", "b" ) )) print("Expect: " + str( False )) print("Actual: " + str( containsChar( "abcd", "a" ) )) print("Expect: " + str( True )) print("Actual: " + str( containsChar( "abcd", "b" ) )) print("Expect: " + str( True )) print("Actual: " + str( containsChar( "abcd", "d" ) )) print("Expect: " + str( True )) print("Actual: " + str( containsChar( "abcd", "x" ) )) print("Expect: " + str( False )) print("Actual: " + str( containsChar( "", "x" ) )) print("Expect: " + str( False )) print("Actual: " + str( containsChar( "Four score and seven years ago, our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.", "q" ) )) print("Expect: " + str( True )) return None # test : -> None # def test(): testCountUpperCase() testContainsChar() |
1The reason is that
we want to develop the skills so that we could write
home—info—labs—exams—hws
—D2L—MediaSamples/—breeze (snow day)—tutor/PIs
©2014, Ian Barland, Radford University Last modified 2014.Apr.28 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |