a7q1 - print ans == expected assert ans == expected ##...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
## CS 116 Ru Wang r46wang 20319247 ## Assignment 7 Question 1 ## count_caps: string -> int ## consumes a string of words and returns a integer indicating how many words are made of uppercase alphabets. ## Examples: ## count_caps ("") -> 0 ## count_caps ("this is an apple") -> 0 ## count_caps ("ThIs Is aN AppLE") -> 0 ## count_caps ("THIS is an apple") -> 1 ## count_caps ("this is an APPLE") -> 1 ## count_caps ("THIS IS AN APPLE") -> 4 def count_caps (s) : listofwords = s.split() length = len(listofwords) pos = 0 num = 0 while (pos < length): if (listofwords[pos] == listofwords[pos].upper()): num = num + 1 pos = pos + 1 else: pos = pos + 1 return num ## Testing: ## count_caps("") -> 0 print "Test 1" expected = 0 ans = count_caps("") print ans == expected assert ans == expected ## count_caps ("this is an apple") -> 0 print "Test 2" expected = 0 ans = count_caps("this is an apple")
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: print ans == expected assert ans == expected ## count_caps ("ThIs Is aN AppLE") -> 0 print "Test 3" expected = 0 ans = count_caps ("ThIs Is aN AppLE") print ans == expected assert ans == expected ## count_caps ("THIS is an apple") -> 1 print "Test 4" expected = 1 ans = count_caps ("THIS is an apple") print ans == expected assert ans == expected ## count_caps ("this is an APPLE") -> 1 print "Test 5" expected = 1 ans = count_caps ("this is an APPLE") print ans == expected assert ans == expected ## count_caps ("THIS IS an APPLE") -> 3 print "Test 6" expected = 3 ans = count_caps ("THIS IS an APPLE") print ans == expected assert ans == expected ## count_caps ("THIS IS AN APPLE") -> 4 print "Test 7" expected = 4 ans = count_caps ("THIS IS AN APPLE") print ans == expected assert ans == expected...
View Full Document

This note was uploaded on 10/21/2010 for the course CS 116 taught by Professor T.vasiga during the Fall '09 term at Waterloo.

Page1 / 2

a7q1 - print ans == expected assert ans == expected ##...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online