Created
February 19, 2020 18:49
-
-
Save mayankdawar/f346fa8cf47552ff1b5cc8a1087d7376 to your computer and use it in GitHub Desktop.
Write code that will count the number of vowels in the sentence s and assign the result to the variable num_vowels. For this problem, vowels are only a, e, i, o, and u. Hint: use the in operator with vowels.
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
| s = "singing in the rain and playing in the rain are two entirely different situations but both can be fun" | |
| vowels = ['a','e','i','o','u'] | |
| count = 0 | |
| # Write your code here. | |
| for i in s: | |
| if i in vowels: | |
| count += 1 | |
| num_vowels = count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"""
Write code that will count the number of vowels in the sentence s and assign the result to the variable num_vowels. For this problem, vowels are only a, e, i, o, and u. Hint: use the in operator with vowels.
"""
I used generator comprehension.
A generator expression is like a list comprehension, but instead of finding all the items you're interested in and packing them into the list, it waits and yields each item out of the expression, one by one. Because a generator expression only has to yield one item at a time, it can lead to big savings in memory usage.
(https://stackoverflow.com/questions/364802/how-exactly-does-a-generator-comprehension-work)