Created
March 30, 2013 18:57
-
-
Save siers/5277930 to your computer and use it in GitHub Desktop.
answer to the question how do I draw digital numbers in ruby?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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