S
S
sasha3002018-09-16 16:49:42
Python
sasha300, 2018-09-16 16:49:42

How to swap the first and second halves of an ODD list?

Hello!
Here is tz:

Given a list of numeric values ​​with N elements. Swap
the first and second halves of the list.

Made a script:
a = [4,6,7,8,1,5]
b = len(a)//2
for i in range(b):
    temp = a[b]
    a[b] = a[i]
    a[i] = temp
    b = b + 1    
print(a)

The code completes the task, but if the list is odd, i.e. for example:
a = [4,6,7,8,1,5,3]

then in the output I get:
[8, 1, 5, 4, 6, 7, 3]

Those. the last value of the list a[-1] "flies". The output should look something like:
8,1,5,3, 4,6,7
or
1,5,3, 4,6,7,8

In general, without a difference, in the first half there will be more values ​​or in the second, the main thing is how to implement this business?
I thought I could use if to make a condition for an odd list, but for a couple of hours I've been thinking and nothing sensible comes to mind =(
Thanks in advance for the answers!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman Kitaev, 2018-09-16
@sasha300

a = [4,6,7,8,1,5,3]
b = len(a) // 2
print(a[b:] + a[:b])

And you don’t need to reinvent the wheel, read the basics of python
. If slices of lists are strictly prohibited in the task, then iterate over the length of the list, add the length of the first half of the list there and take the remainder of the division by the length of the list, so that when your iterator goes beyond the list, the index goes to the top of the list.
I don’t know why you need a solution if you (probably) want to think with your head, but here:
def f(a):
    b = len(a) // 2
    c = []
    for i in range(len(a)):
        idx = (i + b) % len(a)
        c.append(a[idx])
    return c

PS The first option is the pythonic way. The second is the collective farm way

Y
yahabrovec, 2018-09-16
@yahabrovec

The second is in fact your array but inverted

Those. the last value of the list a[-1] "flies". The output should be something similar:
8,1,5,3, 4,6,7
or
1,5,3, 4,6,7,8
I'm talking about a solution. You can add a condition like
if b / 10 != 0: # тоесть массив нечетный
      z = b/2 # делим список пополам
      result = [] # переменная для записи результата
      while z >= 0: # перебор значений массива от середины до начала
           result.append(a[z]) # добавление каждого значения
           z -= 1
           if not z: # если z вышел за пределы размера массива
             z = b/2 # то мы его обнуляем
      while z < b:
           result.append(a[z]) # добавление всех значений после массива
           z+=1
result.append(a[-1]) # добавление в конец последнее значение массива
print(result)

The code is a little hacky and you have to jump a little with a tambourine, but I think I got the point across.

S
sasha300, 2018-09-16
@sasha300

Thanks for the script!
But while has not yet been studied.
In the previous answer, Roman Kitaev solved the issue with 3 lines of code:

a = [4,6,7,8,1,5,3]
b = len(a) // 2
print(a[b:] + a[:b])

Simply awesome and beautiful solution!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question