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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • --last-failed: Zero in on just the tests that failed in your previous run
  • --only-changed: Test only the spec files you've modified in git
  • --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Practical examples included!

Watch Video 📹️

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay