0
  • I have one string 'abcdef'

  • Need to print element like first element, last element, second element, second last element

  • Expected out is afbecd

  • Another output for abcde Expected is aebdc

  • Can we do it in without creating a extra list

pseudo code:

str1 = 'abcdef'
i= 0
j = 1
new_str = ''
while (i < len(str1) && j > len(str1) and i!=j):
   new_str = str1[i] + str1[j]
1
  • What part are you stuck on? Can you show some real code? Commented Oct 4, 2021 at 6:16

4 Answers 4

2

Without any spurious memory usage, you can use some lazy iterators/generators with reversed and zip

def interleave(s):
    gen = (c for pair in zip(s, reversed(s)) for c in pair)
    return "".join(next(gen) for _ in s)

>>> interleave("abcdef")
'afbecd'
>>> interleave("abcde")
'aebdc'

You can introduce more utils to shorten the code even more:

from itertools import chain  

def interleave(s):
    gen = chain.from_iterable(zip(s, reversed(s)))
    return "".join(next(gen) for _ in s)
Sign up to request clarification or add additional context in comments.

8 Comments

seems like the ideal solution, I just went with the op approach.
Both are valid. For beginners, it is always helpful to understand the "manual" loopy approach before heaving the kitchen sink of built-ins and library functions at a simple problem.
Next gen is already old, the next next gen is all the hype now :-)
@don'ttalkjustcode :-) That's a creative approach. And a seriously elegant one to boost. You should go all the way tho with the abstraction layers and generators: gen = cycle(f(s) for f in (iter, reversed))
If only Python 3 still had apply, then I think cycle(map(apply, (iter, reversed), repeat(s))) would work.
|
1

With your approach you can do:

str1 = 'abcdef'
i= 0
j = len(str1) - 1
new_str = ''
while (j>i):
   new_str += str1[i] + str1[j]
   i+=1
   j-=1

if len(str1) % 2 != 0:
    new_str += str1[j]
print(new_str)

Output:

afbecd

1 Comment

Could also do if j == i: instead.
0

The way I see it, you have two possibilities based on string length here but they really only differ in how you handle the middle character. Therefore, an input of abcdefg should give you an output of agbfced whereas the output for abcdef would be afbecd.

Given the above, I think this should work:

def zip_str(input_str):
    width = len(input_str)
    half_width = int(width / 2)
    new_str = ""
    for i in range(0, half_width):
        new_str += input_str[i] + input_str[-1 - i]
    return new_str if width % 2 == 0 else new_str + input_str[half_width]

Comments

0

A not particularly efficient but simple and imho fun way is to keep reversing the remaining string (Try it online!):

s = 'abcdef'

result = ''
while s:
   *s, char = reversed(s)
   result += char

print(result)

Or, since you actually say you want to print (Try it online!):

s = 'abcdef'

while s:
   *s, char = reversed(s)
   print(char, end='')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.