Skip to content

Instantly share code, notes, and snippets.

@gouf
Created January 15, 2014 08:51
Show Gist options
  • Save gouf/8432898 to your computer and use it in GitHub Desktop.
Save gouf/8432898 to your computer and use it in GitHub Desktop.
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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment