def num_vowels(s): '''Return the number of vowels in string s. The letter "y" is not treated as a vowel.''' count = 0 for char in s: if char in "aAeEiIoOuU": count = count + 1 return count def remove_spaces(s): '''Return s with any spaces removed.''' s_no_spaces = "" for char in s: if char != " ": s_no_spaces += char return s_no_spaces def count_matches(s1, s2): '''Return the number of chars in s1 that appear in s2.''' common_chars = 0 for char in s1: if char in s2: common_chars += 1 return common_chars if __name__ == '__main__': #test them out print num_vowels('hello there fellow'), "sb", 6 print num_vowels('jjjddjsjsjd jjjj'),"sb", 0 print num_vowels(''),"sb",0 print num_vowels('a'),"sb",1 print num_vowels('b'),"sb",0 #test them out print remove_spaces('hello there fellow'),"sb",'hellotherefellow' print remove_spaces('jskrjeksuuyj'),"sb",'jskrjeksuuyj' print remove_spaces(''),"sb",'' print remove_spaces(' '),"sb",'' print remove_spaces(' b'),"sb",'b' print count_matches("abcde","cdefg"), "sb", 3 print count_matches("abcde","fghij"), "sb", 0 print count_matches("abcde",''), "sb", 0 print count_matches('',''), "sb", 0 print count_matches('','abcde'), "sb", 0 #now, let's write some code to reverse a list. We won't create a function. #just to see that this is possible. s = 'abc' reverse = "" # An accumulator for char in s: reverse = char + reverse print reverse, "sb", 'cba'