-9

There are three integer variables, a, b and c, which have been initialized. Write code to shift the values in these variables around so that a is given b’s original value, b is given c’s original value, and c is given a’s original value.

  • 5
    Welcome to Stack Overflow! It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive. – Martijn Pieters Dec 30 '14 at 17:05
  • 2
    This question appears to be off-topic because OP basically asks us to write code for him without any visible effort from his side. – Ondrej Janacek Dec 30 '14 at 17:51

1 Answers1

6

Use tuple assignment:

a, b, c = b, c, a

This takes the values of b, c, and a and assign them to a, b and c:

>>> a = 'foo'
>>> b = 'bar'
>>> c = 'baz'
>>> a, b, c = b, c, a
>>> a
'bar'
>>> b
'baz'
>>> c
'foo'

This works because the values referenced by b, c, a are gathered onto the stack first, and only then are those values assigned back to a, b, c.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343