Created
December 21, 2017 11:48
-
-
Save CavaTrendy/c3868ef11c81f3452cb7b1045e543027 to your computer and use it in GitHub Desktop.
MIT - Exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #### MIT SOLUTION### | |
| #Write a program that counts up the number of vowels contained in the string s. Valid vowels are: 'a', 'e', 'i', 'o' | |
| #'u'. For example, if s = 'azcbobobegghakl', your program should print: Number of vowels: 5 | |
| s = 'azcbobobegghakl' | |
| count = 0 | |
| for i in range(0, len(s)): | |
| if s[i] == 'a'or s[i] == 'e'or s[i] == 'i'or s[i] == 'o'or s[i] == 'u': | |
| count += 1 | |
| print("Number of vowels: "+ str(count)) | |
| ### My solution ### | |
| s = 'hgboobbobbooboboboboboobooboobbobobobqybooboboobobob' | |
| count = 0 | |
| for i in range(len(s)): # look into the length of the string, because i do not it in advance | |
| if s[i:].startswith("bob"): # look if in s the first character start with bob if not goes further | |
| count += 1 | |
| print ('Number of times bob occurs is:' , count) | |
| ### Other solution found online #### | |
| numBobs = 0 | |
| for i in range(1, len(s)-1): #look at the length from the second character and the last one | |
| if s[i-1:i+2] == 'bob': # if at the end of the string s or the beginning of the string +2 is equal to bob i add 1 | |
| numBobs += 1 | |
| print ('Number of times bob occurs is:', numBobs) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment