DEV Community

loading...

Consonant value - COdewars

nischay105 profile image Nischay Gowda ・1 min read

Day1 - Codewars Python

Kya - 5

Coding problem - Consonant value

Here is my solution with the necessary comments. Please suggest if any method to optimize the code.

def solve(s):

    # Form dict for a-z with 1-26
    import string
    alpha_dict = {}
    i = 0
    for letter in string.ascii_lowercase:
        i += 1
        alpha_dict[letter] = i

    list_word = []

    # For first iter, temp is empty string.
    temp =''
    for letter in s:
        if letter not in 'aeiou':
            temp += letter
        else:
            # The end of consecutive consonant words.
            list_word.append(temp)
            temp = ''


    # Remove empty strings
    if '' in list_word:
        list_word.remove('')

    print(list_word)

    # Get the coresponding leter value, and add for 
    # consecutive values.
    word_value = []
    for word in list_word:
        temp = 0
        for letter in word:
            temp += alpha_dict[letter]

        word_value.append(temp)

    return max(word_value)
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide