Counting how many times each vowel appears
The problem is with this line:
vowelCounts = [aCount, eCount, iCount, oCount, uCount] = (0,0,0,0,0)
vowelCounts
does not get updated if you start incrementing aCount
later.
Setting a = [b, c] = (0, 0)
is equivalent to a = (0, 0)
and [b, c] = (0, 0)
. The latter is equivalent to setting b = 0
and c = 0
.
Reorder your logic as below and it will work:
aCount, eCount, iCount, oCount, uCount = (0,0,0,0,0)
for word in wordlist:
for letter in word:
# logic
vowelCounts = [aCount, eCount, iCount, oCount, uCount]
for vowel, count in zip(vowels, vowelCounts):
print('"{0}" occurs {1} times.'.format(vowel, count))
Indicate which vowel occurs the most in a string
There can be many ways. I am writing one of them. You can try it:
// for number of VOWELS
for (int i = 0; i < str.length(); i++)
{
ch = str.charAt(i);
ch = Character.toLowerCase(ch);
// is this a vowel
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
vowels++;
}
// which vowel occurs the most
if (ch == 'a')
vowelA++;
else if (ch == 'e')
vowelE++;
else if (ch == 'i')
vowelI++;
else if (ch == 'o')
vowelO++;
else if (ch == 'u')
vowelU++;
}
maxVowels = Math.max(vowelA, Math.max(vowelE, Math.max(vowelI, Math.max(vowelO, vowelU))));
Output:
Note: I have just added maxVowels = Math.max(vowelA, Math.max(vowelE, Math.max(vowelI, Math.max(vowelO, vowelU))));
before upper case logic and removed if-condition where you are storing maxVowels value.
Another Way: Replace maxVowels = Math.max(vowelA, Math.max(vowelE, Math.max(vowelI, Math.max(vowelO, vowelU))));
with Collections.max(Arrays.asList(vowelA, vowelE, vowelI, vowelO, vowelU));
is there a more efficient way to count how many vowels in a string?
It will likely be more efficient using a Counter
, then extracting the values for the vowels, and retuen them:
from collections import Counter
def count_vowels(phrase):
""" accepts a string and counts the number of each vowels.
returns a dictionary key --> values
of each vowel and their number of occurrences.
"""
vowels = "aeiou"
frequencies = Counter(phrase.lower())
return {vowel: frequencies[vowel] for vowel in vowels}
as a one-liner:
(as suggested by @stevenRumbalski in the comments)
from collections import Counter
def count_vowels(phrase):
""" accepts a string and counts the number of each vowels.
returns a dictionary key --> values
of each vowel and their number of occurrences.
"""
vowels = "aeiou"
return Counter(c for c in phrase.lower() if c in vowels)
Related Topics
How to Show a Pandas Dataframe into a Existing Flask HTML Table
How to Serialize Sqlalchemy Result to Json
How to Get the Coordinates of the Bounding Box in Yolo Object Detection
Convert the String 2.90K to 2900 or 5.2M to 5200000 in Pandas Dataframe
How to Make a Grade Calculator in Python
Deleting Dataframe Row in Pandas If a Combination of Column Values Equals a Tuple in a List
Matplotlib Bar Chart: Space Out Bars
Split String in a Spark Dataframe Column by Regular Expressions Capturing Groups
_Tkinter.Tclerror: No Display Name and No $Display Environment Variable
Most Efficient Way to Construct Similarity Matrix
Pandas - Tokenizing Data Expected 1 Field Saw Multiple
Split List into Lists Based on a Character Occurring Inside of an Element
Importing Large Tab-Delimited .Txt File into Python
Format/Suppress Scientific Notation from Pandas Aggregation Results
How to Update/Delete Rows in Bigquery from the Python API
Getting S3 Objects' Last Modified Datetimes With Boto