def scramble(word1, word2): # convert both to lists, easier to deal with word1_q = list(word1) word2_q = list(word2) if len(word2_q) > len(word1_q): # if word2 is bigger than word1, append enough zeroes on word1 zeroes = ['0'] * (len(word2_q) - len(word1_q)) word1_q.extend(zeroes) elif len(word1_q) - 1 > len(word2_q): # same if word1 is bigger than word2 # we do not want a "0" at the end, hence the -1 zeroes = ['0'] * (len(word1_q) - len(word2_q) - 1) word2_q.extend(zeroes) # we fill the last position with an empty character if len(word1_q) > len(word2_q): word2_q.append('') # combines both lists # appending an empty char in between return ''.join([item for sublist in zip(word1_q, word2_q) for item in sublist]) print scramble("scrambled", "eggs") print scramble("kipje", "kaasje") print scramble("aa", "zzz")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question