Created
May 27, 2011 23:07
-
-
Save pdsouza29/996365 to your computer and use it in GitHub Desktop.
Ruby Quiz: LCD Numbers
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
# Preetam D'Souza | |
# 5.26.11 | |
# lcd.rb | |
# Usage: "ruby lcd.rb [-s SIZE] DIGITS" | |
# | |
# Input: Digits (0..9) and an optional size. | |
# Size defaults to 2. | |
# | |
# Output: Input digits in variable size | |
# 7-segment display to stdout. Size specifies | |
# the length of each segment. | |
# | |
# Example: ruby lcd.rb -s 1 0123 | |
# | |
# - - - | |
# | | | | | | |
# - - | |
# | | | | | | |
# - - - | |
### option parsing ### | |
s = $*.size > 1 ? $*.slice!(0..1)[1] : '2' | |
num = $*[0] | |
unless s =~ /^[1-9]\d*$/ and num =~ /^\d+$/ | |
puts "Usage: #$0 [-s SIZE] DIGITS" | |
exit | |
end | |
s = s.to_i | |
### rendering ### | |
hz = ' ' + '-'*s + ' ' # ' - ' | |
vt = ['|' + ' '*s + '|']*s # '| |' | |
vl = ['|' + ' '*s + ' ']*s # '| ' | |
vr = [' ' + ' '*s + '|']*s # ' |' | |
bk = ' ' * hz.size # ' ' | |
lcd = [ | |
[hz] << vt << bk << vt << hz, # 0 | |
[bk] << vr << bk << vr << bk, # 1 | |
[hz] << vr << hz << vl << hz, # 2 | |
[hz] << vr << hz << vr << hz, # 3 | |
[bk] << vt << hz << vr << bk, # 4 | |
[hz] << vl << hz << vr << hz, # 5 | |
[hz] << vl << hz << vt << hz, # 6 | |
[hz] << vr << bk << vr << bk, # 7 | |
[hz] << vt << hz << vt << hz, # 8 | |
[hz] << vt << hz << vr << hz, # 9 | |
] | |
### output ### | |
(0...(2*s+3)).each do |i| | |
puts num.chars.collect { |n| lcd[n.to_i].flatten[i] }.join ' ' | |
end |
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
s=$*.size>1?$*.slice!(0,2)[1].to_i: 2 | |
n=$*[0] | |
a=' '+'-'*s+' ' | |
b=['|'+' '*s+'|']*s | |
c=['|'+' '*s+' ']*s | |
d=[' '+' '*s+'|']*s | |
e=' '*a.size | |
l=[[a,b,e,b,a],[e,d,e,d,e], | |
[a,d,a,c,a],[a,d,a,d,a], | |
[e,b,a,d,e],[a,c,a,d,a], | |
[a,c,a,b,a],[a,d,e,d,e], | |
[a,b,a,b,a],[a,b,a,d,a]] | |
(0..(2*s+2)).each{|i|puts n.chars.map{|x|l[x.to_i].flatten[i]}.join' '} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment