Reputation: 19
Given two strings
s1
ands2
of same length.Create a new string consisting of the last character of s1 followed by the last character of s2, followed by the second to last character of s1, followed by the second to last character of s2, and so on (in other words the new string should consist of alternating characters of the reverse of s1 and s2).
For example,
if
s1="hello"
ands2="world"
, thens3
should be"odlllreohw"
.
I have tried this:
s3 = ''
for i in range(0, min(len(s1), len(s2))):
s3 = s3 + s1[i] + s2[i]
but I'm not sure how to have it reverse the letters from s1 and s2.
Upvotes: 1
Views: 3629
Reputation: 1
You need to start from the last character and loop for len(s1)
times.
s3 = ""
i=1
while i<=len(s1) :
j=-i
s3 = s3 + s1[j] + s2[j]
i=i+1
Upvotes: 0
Reputation: 55469
The problem specification says that s1
and s2
have the same length, so your code should use that assumption. Sure, it's nice to have code that is robust and can cope properly with a wide range of input data, but it's also an important part of coding to write code that conforms to the specifications, otherwise you end up with bloated code that replicates work done by other parts of the program.
Your solution is very close to what you need, you just need to add s1[i] + s2[i]
to the front of s3
.
s1 = 'hello'
s2 = 'world'
s3 = ''
for i in range(len(s1)):
s3 = s1[i] + s2[i] + s3
print(s3)
output
odlllreohw
Note that I've used range(len(s1))
instead of range(0, len(s1))
. When a range starts from zero (and goes up with a step of 1) we can omit the start
argument.
If you did need to handle input strings of unequal lengths you could handle that by using slicing to get the individual characters. This works because if you slice beyond the end of the string you get an empty string.
s1 = 'helloabc'
s2 = 'world'
s3 = ''
for i in range(max(len(s1), len(s2))):
s3 = s1[i:i+1] + s2[i:i+1] + s3
print(s3)
output
cbaodlllreohw
We get the same output if
s1 = 'hello'
s2 = 'worldabc'
But getting back to the case where we're assured that s1
& s2
have the same length, there are more Pythonic ways to do this. In Python, it's generally preferred to iterate directly over collections, rather than to iterate indirectly via indices. To iterate over two or more collections in parallel we use the zip
function, which yields tuples of the iterators passed to it, stopping when the shortest iterator runs out of items.
s1 = 'hello'
s2 = 'world'
s3 = ''
for u, v in zip(s1, s2):
s3 = u + v + s3
print(s3)
output
odlllreohw
The "textbook" way to do this is to use a list comprehension to create a list of the pairs of characters from s1
& s2
, the reversed
function to iterate backwards over that list, and the str.join
method to combine them into a single string.
s3 = ''.join(reversed([u + v for u, v in zip(s1, s2)]))
We could also use extended slicing to reverse the list, but using the reversed
iterator is more efficient since it doesn't need to build a copy of the list (of course, that doesn't matter much when the list is small).
s3 = ''.join([u + v for u, v in zip(s1, s2)][::-1])
Upvotes: 0
Reputation: 117
One line solution could be:
s3="".join([s1[::-1][x]+s2[::-1][x] for x in range(len(s1))])
Upvotes: 0
Reputation: 754
You can also use slice notation:
>>> str = "Hello"
>>> print str[::-1]
'olleH'
Upvotes: 0
Reputation: 5509
Try this
str1="hello"
str2="world"
str3=""
for c,d in zip(reversed(str1),reversed(str2)):
str3+=c+d
print (str3)
Output
odlllreohw
Upvotes: 0
Reputation: 778
In python you can access a string or a list by using a negative value to offset from the end. For instance:
s3 = s3 + s1[-1] + s2[-2]
Will retrieve the last character of s1 and the second to last character of s2.
Upvotes: 1