Skip to content

Instantly share code, notes, and snippets.

@cibofdevs
Created July 16, 2019 15:20
Show Gist options
  • Save cibofdevs/33a36476df3d8477b050c64a7fe78ef7 to your computer and use it in GitHub Desktop.
Save cibofdevs/33a36476df3d8477b050c64a7fe78ef7 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.
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']
# Write your code here.
num_vowels = sum([1 for i in s if i in vowels])
print(num_vowels)
@kalahan2001
Copy link

#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']

Write your code here.

num_vowels=0
for i in s:
if i in vowels:
num_vowels+=1
print(num_vowels)

@IvanFrecia
Copy link

IvanFrecia commented Apr 25, 2021

Same solution using a for loop with acuum:

num_vowels = 0
for i in s:
if i in vowels:
num_vowels +=1
print(num_vowels)

@hadrocodium
Copy link

"""
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.
In your code, you use list comprehension and then a built-in function sum.

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)

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']
num_vowels = sum(1 for char in s if char in vowels)
print(num_vowels)

@Aliricoder
Copy link

Aliricoder commented Feb 8, 2022

this is working good too.

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']
num_vowels=0
for vow in s:
for vows in vowels:
if vow==vows:
num_vowels +=1

@websalem
Copy link

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']
num_vowels = 0
s = s.split()

Write your code here.

for word in s:
for char in word:
if char in vowels:
num_vowels = num_vowels + 1

print(num_vowels)

@Elijah57
Copy link

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
for alphabet in s:
if alphabet in vowels:
count += 1
sum_vowels = count

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment