← Back to challenges

String Expansion

PythonHardstringslanguage_fundamentalsinterviewlogicregex

Instructions

Create a function that takes a string txt and expands it as per the following rules:

  1. The numeric values represent the occurrence of each letter preceding that numeric value.
string_expansion("3M2u5b2a1s1h2i1r") ➞ "MMMuubbbbbaashiir"
  1. 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.
string_expansion("3Mat")➞ "MMMaaattt"      # correct

string_expansion("3Mat") ➞ "MMMat"          # wrong
string_expansion("3Mat") ➞ "MatMatMat"      # wrong
  1. If there are consecutive numeric characters, ignore them all except last one.
string_expansion("3M123u42b12a") ➞ "MMMuuubbaa"
  1. If there are two consecutive alphabetic characters then the string will remain unchanged.
string_expansion("airforce") ➞ "airforce"
  1. Empty strings should return an empty string.
string_expansion("") ➞ ""

Notes

N/A

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.