func encode(str) { var table = ('a'..'z' -> join); str.chars.map { |c| var s = ''; table.sub!(Regex('(.*?)' + c), {|s1| s=s1; c + s1}); s.len; } } func decode(nums) { var table = ('a'..'z' -> join); nums.map { |n| var s = ''; table.sub!(Regex('(.{' + n + '})(.)'), {|s1, s2| s=s2; s2 + s1}); s; }.join; } %w(broood bananaaa hiphophiphop).each { |test| var encoded = encode(test); say "#{test}: #{encoded}"; var decoded = decode(encoded); print "in" if (decoded != test); say "correctly decoded to #{decoded}"; }