def zigzag(text): Implement a function that takes a string and returns a new string that is a zig-zag rearrangement of it like the one in the examples below.
• Parameters: text is a string (no validation needed)
• Examples:
zigzag('12345678') → '18273645'
zigzag('123456789') → '192837465'
zigzag('abcdefgh') → 'ahbgcfde'
zigzag('GeorgeMason') → 'GneoosragMe'
In Python please!
def zigzag(s): i = 0 j = len(s)-1 res = "" while(i<j): res += (s[i]+s[j]) i+=1 j-=1 if(len(s)%2==1): res += s[i] return res # Testing print(zigzag('12345678')) print(zigzag('123456789')) print(zigzag('abcdefgh')) print(zigzag('GeorgeMason'))
Get Answers For Free
Most questions answered within 1 hours.