Reputation: 147
I have two strings, (string1 and string2).
If they are equal in length, the function should return a string that is formed by alternating characters from each of the two strings.
If they are not equal in length, then the function extends the shorter string by repeating the last character until they are the same length and then alternates the characters of the two strings.
For example,
extendedString("abc", "def") => "adbecf"
extendedString("ab", "defg") => "adbebfbg"
I have written the part where it returns if the strings are the same length, but I have no idea how to repeat the last character.
def extendedString(string1, string2):
x = string1
y = string2
z = ""
if len(x) == len(y):
return "".join(i for j in zip(string1,string2) for i in j)
Upvotes: 9
Views: 4843
Reputation: 880
Simply add the last character to the shorter string until their length is same. In python, string*int is defined. For example "a"*3
is "aaa"
. So
x = x+x[-1]*(len(y)-len(x))
does what you need to do. Then you just need to recursively call the function with the new string values that have the same length.
def extendedString(string1,string2):
x=string1
y=string2
z=""
if len(x)==len(y):
return "".join(i for j in zip(string1,string2) for i in j)
elif len(x) < len(y):
x = x+x[-1]*(len(y)-len(x))
return extendedString(x,y)
else:
y = y+y[-1]*(len(x)-len(y))
return extendedString(x,y)
Upvotes: 2
Reputation: 4604
OK, I realize this has been answered into oblivion, but I had already started working on mine, so here it goes.
Note: this implementation will always start printing the shorter string first, if you want to always start off by printing the first char of string1
, then see my update below.
I like that you copy the input parameters as that is a good habit for preserving input, and I just modified it slightly to add a convention so len(x) <= len(y)
is always true. I also opted to not use other libraries but to implement the zip
myself.
def extendedString(string1, string2):
if len(string1) <= len(string2): # Convention: len(x) <= len(y)
x = string1
y = string2
else:
x = string2
y = string1
z=""
for i in range(len(x)): # Go through shorter string
z+=x[i] # Add the i-th char in x to z
z+=y[i] # Add the i-th char in y to z
if i < len(y): # If the second string is longer
for j in range(i+1, len(y)): # for the rest of the length
z+=x[i] # add the last char of x to z
z+=y[j] # add the j-th char of y to z
return z
print(extendedString("abc", "efg"))
print(extendedString("ab", "defg"))
print(extendedString("abcd", "ef"))
Output:
$ python zip.py
aebfcg
adbebfbg
eafbfcfd
This implementation will ensure that string1
is always printed first.
def extendedString(string1, string2):
x = string1
y = string2
z=""
if len(x) <= len(y):
shorter = x
longer = y
else:
shorter = y
longer = x
for i in range(len(shorter)):
z+=x[i]
z+=y[i]
if i < len(longer):
for j in range(i+1, len(longer)):
if shorter == x:
z+=x[i]
z+=y[j]
else:
z+=x[j]
z+=y[i]
return z
print(extendedString("abc", "efg"))
print(extendedString("ab", "defg"))
print(extendedString("abcd", "ef"))
Output:
$ python zip.py
aebfcg
adbebfbg
aebfcfdf
Upvotes: 1
Reputation: 6789
A one-liner solution that doesn't require itertools
:
def extendedString(a,b):
return ''.join(x+y for x,y in zip(*(s+s[-1]*(max(len(a),len(b))-len(s)) for s in (a,b))))
Output:
$ extendedString('abc','1234')
'a1b2c3c4'
$ extendedString('abc','12')
'a1b2c2'
Upvotes: 7
Reputation: 17074
a = "hell"
b = "heaven"
print "".join(i for j in itertools.izip_longest(a, b, fillvalue=a[-1] if len(a)<len(b) else b[-1]) for i in j)
Output: hheelalvleln
Upvotes: 1
Reputation: 62656
You can use the zip_longest
function from itertools.
Works like zip, but gives you the ability to fill the blanks (default filler is None, but you can change it:
import itertools
def extendedString(string1,string2):
filler = string2[-1] if len(string1)>len(string2) else string1[-1]
return "".join(i for j in itertools.zip_longest(string1, string2, fillvalue=filler) for i in j)
added the filler to be the last char of the shortest string (in case it's needed)
In [50]: extendedString("abc","def")
Out[50]: 'adbecf'
In [51]: extendedString("ab","defg")
Out[51]: 'adbebfbg'
If you are using python2 the function is itertools.izip_longest
Upvotes: 11
Reputation: 787
You can accomplish the case where the length is not equal by finding the shorter and the longer of the strings, and appending N
of the -1th
character of the shorter string to itself, where N
is the difference in length between the shorter and longer. From there, you return the same zip/join expression.
def extendedString(string1, string2):
if len(string1) == len(string2):
return "".join(i for j in zip(string1, string2) for i in j)
else:
longer, shorter = (string1, string2) if len(string1) > len(string2) else (string2, string1)
shorter = shorter + shorter[-1] * (len(longer) - len(shorter))
return "".join(i for j in zip(shorter, longer) for i in j)
Upvotes: 1
Reputation: 3677
First make both the strings of same length and then join. Something like:
def extendedString(string1,string2):
x=string1
y=string2
if len(x) < len(y):
x = x + x[-1] * (len(y) - len(x))
elif len(x) > len(y):
y = y + y[-1] * (len(x) - len(y))
return "".join(i for j in zip(x, y) for i in j)
print extendedString("abc", "def")
print extendedString("ab","defg")
print extendedString("defg","ab")
Output:
$ python test.py
adbecf
adbebfbg
daebfbgb
$
Upvotes: 2