Wednesday, March 13, 2019

Left shift a string having n characters without using array - Python

#Left shift a string having n characters such that on left shift left most character will move to the rightmost.
#String to array conversion is not allowed 

Solution using python regex:

import re
string = "abcdefghijk"
ls = re.match('^\w', string)
string = re.sub(ls.group(),'',string)
string += ls.group()
print(string)

One-liner in Python:
string = string[1:] + string[0:1]

One-liner in JavaScript:
string = string.slice(1) + string.slice(0,1)


No comments:

Post a Comment