python - funtion roll takes the element at position n on the stack and makes it the top element -


so i'm trying create function called roll takes element @ position n on stack , makes top element. example, consider stack s 1 2 3 4, left end bottom of stack , right end top (so number 4 @ top of stack). roll(s, 2) yield 1 2 4 3 , roll(s, 3) yield 1 3 4 2.

here have far

def roll(s: stack, n: int):     '''     >>> roll([1,2,3,4], 2)     1243     >>> roll([1,2,3,4], 3)     1 3 4 2     '''     s2 = stack()     if s.is_empty():         return null     else:         s2 = s[::-1] 

i have hardest time when comes reversing lists , picking elements it.

the instructions given problem says can't access element n positions top. have push top n - 1 elements on new stack. elements onto s after removing element n.

from stack import *  def roll (s, n):     '''perform roll operation on s.'''     t = stack()      in range (n - 1):         t.push(s.pop())      rollval = s.pop()      while t.isempty() == false:         s.push(t.pop())     s.push(rollval) 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -