CodeEval
これよくわかんなかった。
一連の整数をpush、popして1個おきに出力。
popに1個おきに取り出す機能を書いたのが間違いか?
設問
Write a program implementing a stack inteface for integers.The interface should have 'push' and 'pop' functions. You will be asked to 'push' a series of integers and then 'pop' and print out every alternate integer.
Input sample:
The first argument will be a text file containing a series of space delimited integers, one per line. e.g.
1 2 3 4 10 -2 3 4
Output sample:
Print to stdout, every alternate integer(space delimited), one per line.
e.g.
4 2 4 -2
やってみた
#!/usr/bin/env ruby def push(n) @stack ||= [] @stack << n end def pop val = @stack.delete_at(-1) @stack.delete_at(-1) val end ARGF.lines.each do |line| line.split(/\s/).each{ |n| push(n.to_i) } pop_vals = [] begin val = pop pop_vals << val end while val puts pop_vals.join(' ') end