Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two strings (char by char) and repeat last char of shortest one

Tags:

python

string

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)
like image 580
Hawk Student Avatar asked Nov 05 '16 02:11

Hawk Student


People also ask

How do I repeat a character string and concatenate it in R?

The following syntax explains how to repeat a character string N times and concatenate it in the same element. strrep ( my_string, 5) # Repeat & concatenate # [1] "ABCABCABCABCABC" As you can see, the RStudio console has returned a single character string that contains our input character string (i.e. “ABC”) five times.

How to append characters of two strings in alternate style?

We one by one append characters of both given strings in alternate style. Time Complexity: O (max (L1,L2)), Where L1 and L2 are the lengths of string 1 and string 2 respectively. Auxiliary Space: O (L1+L2), Where L1 and L2 are the lengths of string 1 and string 2 respectively.

How to combine two strings into one string in Python?

5) Print the concatenated string s1. The main () calls the stringconcatenate () function to combine the two strings. 2) The function gets the string s1 length using strlen (s1). 3) Append the character of string s2 [i] at s1 [i+j].Repeat this step by increasing i value until no character available in s2.

How to get the length of two strings in Python?

2) Read the entered two strings using gets () function as gets (s1) and gets (s2). 3) Get the length of the string s1 using string library function strlen (s1) and initialize to j.


3 Answers

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)

update

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

like image 140
Dekel Avatar answered Sep 20 '22 13:09

Dekel


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'
like image 44
gdlmx Avatar answered Sep 17 '22 13:09

gdlmx


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
$
like image 20
Ambrish Avatar answered Sep 19 '22 13:09

Ambrish