Lewis Firmin
Lewis Firmin

Reputation: 9

How to print the least used vowel in a string?

I want the user to be able to enter a piece of string (I know how to do this)

Then I want Python to count how many times each vowel (aeiou) is used.

After counting how many times each vowel is used, I need the program to return the least used vowel/s which is/are used at least once. If a vowel is not used it should not be returned. In the event of a tie for least used both should be returned.

If no vowels are used, it should print an error code "No vowels were used" (I know how to do this)

For example: If this is how many times the vowels were used in a sentence:

a=4
b=2
c=0
d=0
e=2

It should print "The least USED vowels were b and c, with 2 uses".

Upvotes: 0

Views: 198

Answers (3)

S P
S P

Reputation: 11

I would suggest to sort the letters based on their values and remove the consonants and print the first vowel.

Upvotes: 0

Dekel
Dekel

Reputation: 62636

You can:

  1. Remove all non-vowels chars from the string.
  2. Split the string by chars.
  3. use the Counter on the list you got from #2.

import re
from collections import Counter
s = 'asdfwerasdfwaxciduso'
only_vowels = re.sub(r"[^aeiou]", "", s)
c = Counter(list(only_vowels))
c.most_common()[-1]

Upvotes: 1

zenlc2000
zenlc2000

Reputation: 451

Do it the hard way - letter by letter. Iterate over the string with a for loop and for each letter increment a dictionary value. At the end, check the keys with vowels and find the smallest.

Upvotes: 0

Related Questions