DNPotapov / Codewars-katas-

0 stars 0 forks source link

Simple Simple Simple String Expansion (6 kyu) #22

Open DNPotapov opened 1 year ago

DNPotapov commented 1 year ago
def string_expansion(s):
    res = ""
    count = 1
    for index, item in enumerate(s):
        if item.isdigit():
            count = int(item)
        elif item.isalpha() and index != 0 and s[index-1].isalpha():
            res += item * count
        elif item.isalpha() and index != 0:
            res += item * int(s[index-1])
        elif item.isalpha():
            res += item
    return res
DNPotapov commented 1 year ago

Given a string that includes alphanumeric characters ("3a4B2d") return the expansion of that string: The numeric values represent the occurrence of each letter preceding that numeric value. There should be no numeric characters in the final string.

Notes The first occurrence of a numeric value should be the number of times each character behind it is repeated, until the next numeric value appears If there are multiple consecutive numeric characters, only the last one should be used (ignore the previous ones) Empty strings should return an empty string. Your code should be able to work for both lower and capital case letters.

"3D2a5d2f" --> "DDDaadddddff" # basic example: 3 "D" + 2 "a" + 5 "d" + 2 "f" "3abc" --> "aaabbbccc" # not "aaabc", nor "abcabcabc"; 3 "a" + 3 "b" + 3 "c" "3d332f2a" --> "dddffaa" # multiple consecutive digits: 3 "d" + 2 "f" + 2 "a" "abcde" --> "abcde" # no digits "1111" --> "" # no characters to repeat "" --> "" # empty string

DNPotapov commented 1 year ago

https://www.codewars.com/kata/5ae326342f8cbc72220000d2