algorithm 1. stackstuff(n) input: integer n 1) let s = empty stack 2) let x = -1 3) = 1 2n 4) s.push(i) 5) end 6) = 1 n 7) let x = s.pop() 8) end output: contents of x
to understanding, s.push(i) adds item on top of stack s. x = s.pop() removes item top of stack s , assigns x.
i got asked following question however:
if assume n > 0, lines 1 8 can replaced single line, be? a) let x = s.pop() (it can't this, since point in popping empty stack.) b) let x = -1 (again, serve no real point) c) let x = n+1 d) s.push(n) e) there no single line
my instinct e, there no single line question phrased in such way if there line, answer must either c or d (learning towards d), can't see how either of them entire algorithm in single line. can share input please, appreciated.
what final value of x?
how determined?
doing doesn't matter?
result when pop stack n times?
if can answer questions, should able figure out answer.
Comments
Post a Comment