DEV Community

Bernice Waweru
Bernice Waweru

Posted on • Edited 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.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay