gouf
1/15/2014 - 8:51 AM

stupid coding. cypher encode / decode program. if you have boring, try refactoring.

stupid coding. cypher encode / decode program. if you have boring, try refactoring.

def decrypt cypher
  decoder = cypher.to_s.split('.').last.split(//).map{|x| x.to_i}
  code = cypher.to_s.split('.').first.split(//)
  decoder.inject("") {|res, x|
    res += proc {|x| 
      if x == 0 then
        " "
      else
        proc {|x| z = 'a'; x.times{z = z.succ}; z}.call(
          code.shift(x).inject(:+).to_i
        )
      end
    }.call(x)
  }
end
p cypher = "212323146124793.3320331"
p decrypt cypher #=> "hello world"

words = "hello world"
p str_num = proc {|x|
  res = proc {|y| 
    res = ['']
    until res.last.nil?
      res << y.shift(2).inject(:+)
    end
    res.pop; res.shift
    
    proc {|z|
      str = ''
      res = z.inject(''){|res, i|
        res += proc{|j|
          a = 'a';
          c = 0
          flg = false
          until a == j
            if j =~ /[a-zA-Z]\s/ then
              a = 'a'
              j = j[0]
              c = 0
              flg = 'b'
            elsif j =~ /\s[a-zA-Z]/ then
              a = 'a'
              j = j[1]
              c = 0
              flg = 'f'
            else
              a = a.succ
              c += 1
            end
          end
          case flg
          when 'f'
            str += 0.to_s
          when 'b'
            str += "#{c.to_s.split(//).size}0"
          else
            str += c.to_s.split(//).size.to_s
          end
          c
        }.call(i).to_s
      } + ".#{str}"
    }.call(res)
  }.call(x.split(//))
}.call(words.downcase) #=> "212323146124793.3320331"

p decrypt str_num #=> "hello world"