2

Hope you all are doing well in these times.

here's my code:


def ab(n):


    first = 0
    last = -1 

    endprod = n[first] + n[last]
    endprod2 = n[first+1] + n[last-1]
    endprod3 = n[first+2] + n[last-2]
    endprod4 = n[first+3] + n[last-3]
    endprod5 = n[first+4] + n[last-4]

        
    endprod100 = endprod[::-1] + endprod2[::-1] + endprod3[::-1]+ endprod4[::-1]+ endprod5[::-1]

    return endprod100

I was able do to it, however mine isn't a loop. Is there a way to convert my code into a for loop. So, increment by 1 and decrement by 1.

Thanks,

2
  • Sorry about that, I edited that part. I'm just curious to know how I would increment it by 1 and decrement by 1 like you see in the code, without having to manually write it. Thanks :) Commented Oct 27, 2022 at 16:45
  • Your question is already answered, but I want to add that "endprod = n[first] + n[last]" followed by "endprod[::-1]" seems wasteful when you could do directly: "endprod = n[last] + n[first]" . Commented Oct 27, 2022 at 16:58

4 Answers 4

1

Try this:

def ab(n):
    result = ''
    for j in range(len(n) // 2):
        result += n[-j-1] + n[j]
    if len(n) % 2 == 1:
        result += n[len(n) // 2]
    return result

You also need the part

if len(n) % 2 == 1:
    result += n[len(n) // 2]

because your input string might have an odd number of characters

Examples:

>>> ab('0123456789')
'9081726354'
>>> ab('01234956789')
'90817263549'

If you want to reuse your original logic:

def ab(n):
    result = ''
    first = 0
    last = -1
    for j in range(len(n) // 2):
        result += n[last-j] + n[first+j]
    if len(n) % 2 == 1:
        result += n[len(n) // 2]
    return result
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks but would it be possible to convert my logic into a for loop ?
I'm just curious to know how I would increment it by 1 and decrement by 1 like you see in the code, without having to manually write it. Thanks :)
@Mr.Green Please check the new solution (which is by the way completely identical to the other one, you just need to drop the constants first and last, which are basically useless
Thanks Ricardo, could you just put comments, why have if len(n)%2 ==1: result + = n[len(n)//2] ?? thanks
1

You could also recurse it:

def ab(s):
    if len(s)>2:
        return s[-1]+s[0]+ab(s[1:-1])
    else:
        return s

But the last part of Riccardo's answer fits your question more closely.

Comments

0

you need to split your string for your loop, means first you broke your string to half then build your string, you could use zip to iterate over multiple iteratable. something like this:

def ab(s):
    out = ""
    for v0,v1 in zip(s[:len(s)//2 -1 :-1], s[:len(s)//2 ]):
        out += v0 + v1
    return out

the better version you should write without loop. like this:

out = "".join(map(lambda x: "".join(x), zip(s[:len(s)//2 -1 :-1], s[:len(s)//2 ])))

Comments

0

There are already a lot of good answers that are probably clearer, easier to read and much better suited for learning purposes than what I'm about to write. BUT... something I wanted to bring up (maybe just telling myself, because I tend to forget it) is that sometimes destroying the original argument can facilitate this sort of things.

In this case, you could keep "poping" left and right, shrinking your string until you exhaust it

def swap_destr(my_str):
    result = ""
    while len(my_str) > 1:
        result += my_str[-1]  # Last char
        result += my_str[0]   # First char
        my_str = my_str[1:-1] # Shrink it!
    return result + my_str  # + my_str just in case there 1 char left

print(swap_destr("0123456789"))
print(swap_destr("123456789"))

# Outputs:
# 9081726354
# 918273645

This is also a good problem to see (and play with) recursion:

def swap_recur(my_str):
    if len(my_str) <= 1:
        return my_str
    return my_str[-1] + my_str[0] + swap_recur(my_str[1:-1])

print(swap_recur("0123456789"))
print(swap_recur("123456789"))

# Outputs:
# 9081726354
# 918273645

3 Comments

To think it's only after reading yours that I realized there was no need to differentiate between len(str) == 1 and len(str) == 0... I must be very tired :)
Haha... well... sometimes explicit is better than implicit, right? 😉 ( @Swifty )
Indeed, but I doubt that applies to this case :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.