Skip to content

Instantly share code, notes, and snippets.

@siers
Created March 30, 2013 18:57
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 siers/5277930 to your computer and use it in GitHub Desktop.
Save siers/5277930 to your computer and use it in GitHub Desktop.
answer to the question how do I draw digital numbers in ruby?
#!/usr/bin/env ruby
$, = ' '
class Digits
RAW = [
" 111 ",
"4 2",
"4 2",
" 333 ",
"7 5",
"7 5",
" 666 ",
].join("\n")
def self.single(n)
digits = [
[1,2,5,6,7,4],
[2,5],
[1,2,3,7,6],
[1,2,3,5,6],
[4,2,3,5],
[1,4,3,5,6],
[1,4,3,5,6,7],
[1,2,5],
[1,2,3,4,5,6,7],
[1,2,3,4,5,6],
]
RAW.gsub(/[^\s#{digits[n]}]/, ' ').gsub(/[^\s]/, 'x').split("\n")
end
def self.draw(n)
n.to_s.split('').map(&:to_i).map(&Digits.method(:single)).reduce(:zip).map(&:join)
end
end
N = 63234567
if rand > 0.5
puts Digits.draw(N)
else
# shorter version of it
puts proc{|x,n|"#{n}".split('').map{|n|x[0].gsub(/[#{x[1][n.to_i]}]/,?\s).gsub(/\d/,'x').split("\n")}.reduce(:zip).map(&:join)}.call([" 11 \n4 2\n 33 \n7 5\n 66 \n", [[3],[1,3,4,6,7],[4,5],[4,7],[1,6,7],[2,7],[2],[3,4,6,7],[],[7]]], N)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment