Coding
These are short programs.
For part a: Fill in the blank with the code that best completes the program.
Try to solve them all on paper before you enter your answers in JES.
# (1) Takes in two strings and checks to see if they are equal.
#Returns 1 if they are and 0 if they are not.
def strEqual(str1, str2):
return _______ == ________
# (2) Takes in a string and adds it to the reverse of itself, then prints out the result.
def revAdd(string):
print _______ + string[ : : _______ ]
# (3) Takes in a string and returns the number of vowels (aeiou)
def vowels(string):
print string._______('a') + string._______('e') + ___.count('i') + _________________ + ________________
# (4) Takes in a string and returns the number of words (by counting the spaces)
def wordCount(string):
print string._______(' ') + _____
# (5) Takes in a list of numbers and prints out their sum
def addNumbers(list):
sum = 0
for number in _________:
sum = _______ + ________
return sum
These functions are suppose to perform the stated task, but each one conatins a bug that will cause it not to function properly. Circle the bug.
# (6) Takes in two strings and checks to see if they are equal.
#Returns 1 if they are and 0 if they are not.
def strEqual(str1, str2):
return str1 = str2
# (7) Takes in a string and adds it to the reverse of itself, then prints out the result.
def revAdd(string):
print string + str[ : : 1 ]
# (8) Takes in a string and returns the number of vowels (aeiou)
def vowels(string):
print string.count('a') + string.count('e') + string.count('I') + string.count('o') + string.count('u')
# (9) Takes in a string and returns the number of words (by counting the spaces)
def wordCount(string):
print string.count(' ') + 2
# (10) Takes in a list of numbers and prints out their sum
def addNumbers(list):
sum = 1
for number in list:
sum = sum + number
return sum
You write the code for these...
# (11) Write a function named greaterString() that takes in two strings and returns the one that is greater.
# (12) Write a function named palindromeCheck() that checks to see if a string is a palindrome.
It takes in a string and compares it to the reverse of itself.
If they are equal, it returns true, if not , then it returns false.
# (13) Write a function named sentenceCount() that takes in a string and returns the number of sentences. ( by counting periods '.' )
# (14) Write a function called listAverage() that takes in a list of numbers and returns their average value.
(by dividing their sum by the number of items in the list)
Short Answers.
# (15a) What is the top-down method of coding?
# (15b) How is it different from the bottom-up design?
Coding question on Lists/Random module
Write a function called cardGame() which doesn't take in any input. The function makes a list of numbers from range 1 to 10 and then shuffles that list. If the sum of the first 5 numbers in the shuffled list is greater than 25, then it prints out "Won". If not, it prints "Lost". You must use the addNumbers function you just wrote above to compute the sum of the first 5 numbers.
Links to this Page