Python Forum
Stuck on python quiz challenge
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stuck on python quiz challenge
#1
Hi Y'all.

I have recently purchased a book so I can teach my-self python. I'm just stick on one of the quizzes and need some guidance if possible. The question is :

Can you create a list l that prints [[1,2,3],[1,2,3]] but when I rotate only the first part by rotateRx(l[1]) and then print l it turns out that both parts have been rotated, i.e I get [[3,1,2],[3,1,2]].


I have tried to reproduce the output but only been able to rotate the second half. A bit baffled how you would rotate both. Any help would be appreciated.

My solution:

l=[[1,2,3],[1,2,3]]
def rotateRx4(alist):
         alist[0],alist[1],alist[2]=alist[-1],alist[0],alist[1]

    
rotateRx4(l[1])
print(l)
Reply
#2
Your question seems a bit scattered. There's no mention of rotation in your question, but then you say some part of it isn't working.

But the second sublist is rotated because that's the one that is fed to the rotation function.

l is [[1,2,3],[1,2,3]]
l[1] is the second element: [1,2,3]

You rotate (only) the second one because of line 6:
rotateRx4(l[1])
Reply
#3
If I understood you right, you want to apply rotation to the one part of a list and get rotated both parts of it.

inner_list = [1, 2, 3]
l = [inner_list, inner_list]
rotateRx4(l[1])
# now both sublists are rotated...
This is because lists are mutable in Python and the list l consist of two refs pointing to the same list. So, if you change one of them, the second will change too.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner stuck in Python book for kids mic81k 11 1,214 Nov-27-2023, 04:28 AM
Last Post: deanhystad
  QUIZ GUI Form_When admin panel is open, main quiz form is getting freeze Uday 4 786 Aug-25-2023, 08:24 PM
Last Post: deanhystad
  PySpark Coding Challenge cpatte7372 4 6,114 Jun-25-2023, 12:56 PM
Last Post: prajwal_0078
  string format challenge jfc 2 1,795 Oct-23-2021, 10:30 AM
Last Post: ibreeden
  Learning python, stuck on some code. stanceworksv8 2 3,490 Apr-02-2019, 01:51 AM
Last Post: stanceworksv8
  Very difficult challenge for me cristfp 1 2,762 Apr-01-2019, 08:45 PM
Last Post: Yoriz
  Python Music Quiz lynden 6 12,525 Nov-12-2018, 12:31 PM
Last Post: Tashbf
  Challenge with my string SpencerH 3 2,944 Oct-12-2018, 11:58 AM
Last Post: SpencerH
  Complex Program with matplotlib - Python Challenge maggie_b 1 2,428 Apr-08-2018, 04:05 AM
Last Post: Larz60+
  I'm teaching myself python and i'm stuck... stormrider 2 3,019 Dec-06-2017, 10:16 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020