Expand Alphabets - Spicy Coders

Recent

Thursday, November 30, 2017

Expand Alphabets



A string S is passed as input. S will contain multiple integer values followed by an alphabet. The program must expand the alphabets based on the previous integer value.


Input Format: The first line contains S. Output Format: The first line contains the expanded string value. 

Boundary Conditions: Length of S is from 2 to 100. 

Example Input/Output 1: 
Input: 
4a5h

Output: 
aaaahhhhh 

Explanation:
As it is 4a and 5h, four a's are printed followed by 5 h's 

Example Input/Output 2: 
Input: 
1k2b4k 

Output: 
kbbkkkk

Source Code://Python

import re
n=re.split('(\d+)',input().strip())                     //(\d+)=split numbers and char in string
for i in range(1,len(n)):
    if i%2==0:
        print(n[i]*int(n[i-1]),end="")


2 comments: