[Python][Ruby] mini-pickle (solver for pythonchallenge level 5)
Few days, I was having fun with pythonchallenge.com as you do.
I like the idea of the challanges, I think any programmers could enjoy it.
but unfortunally. it requires some Python specific modules.
fmm...there are hints which hard to notice without python experience.
Here may be help for non-Python programers. ( but spoiler warning )
I've implemented those python's modules for several languages.
If you'd like to implement it by yourself. hints for the lv5: PEP 307
class Unpickler def initialize @mark, @memo, @stack = Object.new, {}, [] end def marker @stack.rindex @mark end def load(stream) loop do case stream.getc.chr when "I" @stack << stream.readline.chomp.to_i when "S" @stack << eval(stream.readline.chomp) when "t", "l" # e.g) ... [ 1,2,MARKER,3,4 ] => [ 1,2,[3,4] ] @stack[marker..-1] = [ @stack[marker+1..-1] ] when "(" @stack << @mark when "a" # e.g) ... [ [ ],1,2 ] => [ [ 1, 2] ] v = @stack.pop; @stack.last << v when "p" @memo[stream.readline.chomp] = @stack.last when "g" @stack << @memo[stream.readline.chomp] when "." return @stack.pop end end end end if $0 == __FILE__ and ARGV.length == 1 banner = ARGV.first puts Unpickler.new.load(open(banner)).map{|row|row.inject(""){|r,(k,v)|r+=k*v}} end