Skip to content

Instantly share code, notes, and snippets.

@bernardeli
Created August 24, 2011 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bernardeli/1167290 to your computer and use it in GitHub Desktop.
Save bernardeli/1167290 to your computer and use it in GitHub Desktop.
Decoder
text = "7,3,s,1,1,2,y,4,2,r,2,4,1,o,2,3, ,1,R,1,1,1,b,5,3,c,5,4,k,2,u"
decoded = []
text.split(",").inject(0) do |sum, char|
unless char.to_i.zero?
sum + char.to_i
else
decoded[sum] = char
sum = 0
end
end
puts decoded.join
class Decoder
def initialize(encoded)
@encoded = encoded
@decoded = []
end
attr_reader :encoded
attr_accessor :decoded
def decode
encoded.split(",").inject(0) do |sum, char|
unless char.to_i.zero?
sum + char.to_i
else
decoded[sum] = char
sum = 0
end
end
decoded.join
end
end
text = "7,3,s,1,1,2,y,4,2,r,2,4,1,o,2,3, ,1,R,1,1,1,b,5,3,c,5,4,k,2,u"
puts Decoder.new(text).decode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment