-4

list= ["aeouis...,.,,esw","trees..,,ioee", '".....wwqqow..","...,...,,,","uouiyteerff..,,", ",w," ]

I need to create a second list as an output. the output for each element will the unique vowels that are present in that element and either High: for 75% or more vowels in the strin, medium: 40-75% of vowels in the element , low: if less that 40% of vowels in the element or

No vowels: if there are no vowels in the string.

and Null: if the string length is less than 5.

So the output will be like:[[a,e,o,u,i]low, [e,i,o]medium, No Vowels, No Vowels, [u,o,i,e]low, NULL]

Can we do this using a list Comprehension ??

NePtUnE
  • 889
  • 1
  • 7
  • 10

1 Answers1

0

I'd do it like this:

from enum import Enum
from typing import List, Set, Tuple

VOWELS = set("aeiou")


class VowelScore(Enum):
    HIGH = "High"             # vowels >= 0.75
    MEDIUM = "medium"         # 0.75 > vowels >= .040
    LOW = "low"               # 0.40 > vowels
    NO_VOWELS = "No vowels"   # no vowels
    NULL = "Null"             # len < 5


def get_vowel_score(element: str) -> VowelScore:
    if len(element) < 5:
        return VowelScore.NULL
    vowels = [c for c in element if c in VOWELS]
    if len(vowels) == 0:
        return VowelScore.NO_VOWELS
    vowel_ratio = len(vowels) / len(element)
    if vowel_ratio < 0.40:
        return VowelScore.LOW
    if vowel_ratio < 0.75:
        return VowelScore.MEDIUM
    return VowelScore.HIGH


def get_vowel_list(elements: List[str]) -> List[Tuple[Set[str], VowelScore]]:
    return [
        (VOWELS & set(element), get_vowel_score(element))
        for element in elements
    ]
Samwise
  • 68,105
  • 3
  • 30
  • 44