""" Authors: Sohie Lee, Peter Mawhorter Consulted: Date: 2023-10-9 Purpose: Lab 6 for loops with strings practice solutions. """ def betterStarify(word): """ Returns a string with all of the letters from the given word, and with additional '*' characters inserted between them. """ result = '' # start with a blank result for letter in word[:-1]: # go through each letter, except the last one result += letter # add this letter to our result result += '*' # add a star result += word[-1] # add the last letter of the word as well return result # return our result # Note: Here's another implementation of betterStarify: def betterStarify(word): """ Returns a string with all of the letters from the given word, and with additional '*' characters inserted between them. """ result = '' # start with a blank result for letter in word: # go through all letters result += letter + '*' # add this letter and a star to our result return result[:-1] # return result, minus final star def isVowel(s): """ Returns True if s is a vowel; false otherwise. """ return len(s) == 1 and s.lower() in 'aeiou' def hasDoubleVowel(word): """ Returns True if the word has a double-letter which is a vowel, and False otherwise. """ for i in range(len(word) - 1): # the -1 here stops early this = word[i] following = word[i + 1] if isVowel(this) and this == following: return True return False import random def starTime(): """returns True 20% of the time; False otherwise. Note: random.random() returns a random number between 0 and 1 """ return random.random() > 0.80 def starryRow(length): """ Generates a row of 'length' dashes, but with some stars mixed in: each dash will become a star instead if `starTime` return True. """ result = '' # start with a blank result for i in range(length): # iterate 'length' times if starTime(): # check if this one should be a star result += '*' # add a star else: result += '-' # otherwise add a dash return result # return our result def starrySky(width, height): """ Returns a multi-line width x height starry sky string. """ result = '' # empty result for row in range(height): result += starryRow(width) # add a row result += '\n' # add a newline return result # return our result def funnyVoice(text): """ Returns a new string that's the same as the given text, except that each letter is randomly either capitalized or lower-cased, with a 50% chance of either. """ result = '' # empty result for letter in text: # copy each letter if random.random() < 0.5: # A 50/50 chance, since random() is 0-1 result += letter.lower() # make it lowercase else: result += letter.upper() # make it uppercase return result # This code is for testing things if __name__ == "__main__": print('Testing betterStarify') print(betterStarify('OMG'), "# should be O*M*G") print(betterStarify('hello'), "# should be h*e*l*l*o") print('---') print('Testing hasDoubleVowel') print(hasDoubleVowel('eel'), "# should be True for 'eel'") print(hasDoubleVowel('pool'), "# should be True for 'pool'") print(hasDoubleVowel('breadroot'), "# should be True for 'breadroot'") print(hasDoubleVowel('kelp'), "# should be False for 'kelp'") print(hasDoubleVowel('feather'), "# should be False for 'feather'") print(hasDoubleVowel('knight'), "# should be False for 'knight'") print(hasDoubleVowel('oreo'), "# should be False for 'oreo'") print('---') print('Testing starryRow') print(starryRow(10), "# should have 10 characters") print(starryRow(10), "# should be same length") print(starryRow(10)) print('---') print('Testing starrySky') print(starrySky(10, 10)) print(starrySky(15, 12)) print('---') print('Testing funnyVoice') print(funnyVoice('hello')) print(funnyVoice('SpongeBob SquarePants')) print('---')