DEV Community 👩‍💻👨‍💻

Bernice Waweru
Bernice Waweru

Posted on • Updated on

CodeWars Kata: Mumbling

For today's challenge, I want to focus on python's enumarate() function. Here's the challenge :

Instructions

Write a function that produces the following output.

accum("abcd") -> "A-Bb-Ccc-Dddd"
accum("RqaEzty") -> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
accum("cwAt") -> "C-Ww-Aaa-Tttt"
Enter fullscreen mode Exit fullscreen mode

Approach

Note that each first letter is capitalized in the output even if it was in lowercase in the input.

An idea that may be apparent to you first is to loop through the string, capitalize the first letter and add the lowercase letters depending on its index+1 in the string.

The solution can be implemented as follows

def accum(s):
    str = ""
    for i in range(0, len(s)):
        str += s[i].upper()
        str += s[i].lower()*i
        if i != len(s)-1:
            str += "-"
    return str
Enter fullscreen mode Exit fullscreen mode

Use enumerate()
However you can use enumerate() which takes a iterable and returns an enumerate object. It adds a counter as the key of the enumerate object.

Syntax enumerate(iterable, start)

Start is an optional parameter that provides the index value where the counter should start. The default is 0.

def accum(s):
    output = []
    for count, letter in enumerate(s):
        output.append(letter.upper() + letter.lower()*(count))
        print(output)
    return '-'.join(output)
Enter fullscreen mode Exit fullscreen mode

Let me know other solutions you develop.

Top comments (0)

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.