Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to swap values in a list when mirrored/folded in half?

I need to make a function that will swap all the values of a list with their mirrored opposite, 1st to last, 2nd to 2nd last, etc. I’m not allowed to use any Python library functions or a return value. Also I need to make the function still work with odd numbered lists, where the middle number wouldn’t be swapped anywhere.

The homework already comes with a main function that holds one list of normal numbers, and another list of random numbers.

def test(numbers):
    # I'm only allowed to type here

def main():
    numbers = [2, 3, 0, 5, 4, 1, 6, 9, 8, 7]
    print(numbers)
    test(numbers)
    print(numbers)

    numbers = [random.randint(0, 10) for i in range(10)]
    print(numbers)
    test(numbers)
    print(numbers)

I struggled for a bit but never got better than this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def test(numbers):
    #Im only allowed to type here
    n = int(len(numbers) / 2)
    numbers[:n], numbers[n:] = numbers[:n:-1], numbers[n - 1::-1]

I manage to get the right results for both lists, but the autograde says it’s wrong, and it doesn’t work with odd numbered lists.

[2, 3, 0, 5, 4, 1, 6, 9, 8, 7]
[7, 8, 9, 6, 1, 4, 5, 0, 3, 2]

How do I specifically just swap mirrored pairs inside the list while accounting for odd numbered lists?

I tried using loops, but any loop inside the function gets ignored, and I don’t know how I could make them work, even if I did know a method that worked through a loop.

edit:
No, I’m not supposed to reverse a list, I’m supposed to specifically swap pairs of values. I know a reverse list could give the same results, but that isn’t what I’m being asked. It’s supposed to work as if the list was being folded in half like paper, and swapping with their opposites.

>Solution :

You can iterate over half the list and use negative indexing to do the swap. Python support for:

i, j = j, i

to allow swapping of values is very cool IMHO.

def test(numbers):
    for i in range(len(numbers) // 2):
        j = i+1
        numbers[i], numbers[-j] = numbers[-j], numbers[i]

Here is a version with some alternate approaches that it sould like you can’t use:

def test(numbers):
    ## ==================
    ## If you can return a value and use built-in methods then
    ## ==================
    # return numbers[::-1]
    ## or
    # return list(reversed(numbers))
    ## ==================

    ## ==================
    ## Otherwise, if you can call a method to reverse it in-place
    ## ==================
    # numbers.reverse()
    ## ==================

    ## ==================
    ## Otherwise, manually reverse the list "in-place" yourself
    ## ==================
    for i in range(len(numbers) // 2):
        j = i+1
        numbers[i], numbers[-j] = numbers[-j], numbers[i]
    ## ==================
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading