Tuesday, February 16, 2010

Programmer - Shift a list to the right by 1 - Python

Programmer Question

I have an assignment that requires I:




Write a Python program that asks a user for a list and then rotates the lists' items to the right by one (last goes to first) and prints the rotated list




Example:



User Input: [1,2,3,4]

Program Output:
4
1
2
3


The second part of the assignment is to:




write a table with values of each
variable in the loop, with the table
having as many rows as stages in the
loop.




I'm embarrassed that I can't seem to figure out a way to do this using a loop (I don't know all the Python control flows/loops, but that shouldn't be an issue).



This is what I could come up with



a= input('enter a list')
a.insert(0, a.pop())
print(str(a)) # or we can print each item by itself


and



a = input('enter list : ')
print(a[len(a)-1])
a.pop()
for x in a:
print(x)


Can this be done in a single loop (while or for)?

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails