Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Onyx

How would I write the definition for this def shift(my_list): "Shifts the list [x1,x2,...,xn] to [x2,x3,...,xn,x1]."

  • 3 years ago
  • 3 years ago

  • This Question is Closed
  1. Onyx
    Best Response
    You've already chosen the best response.
    Medals 0

    If you don't get what i am trying to do, i am defining a function that will shift the list to the right moving the first element to the end.

    • 3 years ago
  2. Aslander
    Best Response
    You've already chosen the best response.
    Medals 0

    You can try this - it may not be pretty, but it works. I threw in print statements so you could see whats happening. def shifting(): a = [1,2,3,4] for i in range(5): print "Original List:", a b = a[1:] b.append(a[0]) print "Ammended List:", b print a = b

    • 3 years ago
  3. Aslander
    Best Response
    You've already chosen the best response.
    Medals 0

    You could do it just once, or as many times as you wish, by changing the range(x).

    • 3 years ago
  4. Onyx
    Best Response
    You've already chosen the best response.
    Medals 0

    I am using the second one right now but it is a little confusing because there giving us problems they never taught like the one i asked you. So i was wondering if the one for this study board is better?

    • 3 years ago
  5. Aslander
    Best Response
    You've already chosen the best response.
    Medals 0

    I don't know the other course...sorry.

    • 3 years ago
  6. Onyx
    Best Response
    You've already chosen the best response.
    Medals 0

    Oh okay but have you had any problems with the course your using. Where the stuff asked for hasn't been taught or is it a good course?

    • 3 years ago
  7. Aslander
    Best Response
    You've already chosen the best response.
    Medals 0

    It is a very good course so far. So far, everything matches up quite good. Of course you need to do extra reading for some things, but that is to be expected from a free online course. The teacher-pupil aspect does not exist, so you need to be willing to work a little, and be autonomous.

    • 3 years ago
  8. Onyx
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank You

    • 3 years ago
  9. Keen
    Best Response
    You've already chosen the best response.
    Medals 0

    def shift(aList) aList.append(aList[0]) del aList[0] return aList

    • 3 years ago
  10. Aslander
    Best Response
    You've already chosen the best response.
    Medals 0

    nice!

    • 3 years ago
  11. Keen
    Best Response
    You've already chosen the best response.
    Medals 0

    :) I'm really warming to lists.

    • 3 years ago
  12. Onyx
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank You. I actually came up with the exact same script last night. I guess i was just cramming my brain too much because i took an hour break. Then I laid down and as I was laying down going to sleep i was like wouldn't it be easier to just take the first element and move to end using append then delete the first.

    • 3 years ago
  13. polpak
    Best Response
    You've already chosen the best response.
    Medals 0

    A couple of things to consider.. Do you wish to modify the list in place, or return a new list constructed by shifting the old one. In general I would recommend the latter since it has fewer side effects. #does not modify def shift(thelist): return thelist[1:] + thelist[0] #modifies def inplace_shift(thelist): thelist.append(thelist.pop(0))

    • 3 years ago
  14. Aslander
    Best Response
    You've already chosen the best response.
    Medals 0

    Oh that's nice polpak! I hadn't thought about how it would modify (or not) the list...I tried to break down Keen's function but found it really hard, now with your input I am thinking about how much I don't know about programming, :-) and I think I will try simplifying my own work. Thanks!

    • 3 years ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.