Q2. of BORQ - LCD display
This is an old one, pretty famously seen on uva, etc. I found Choice to be better than OptParse, as said in Choice docs - it like writing poems for command-line parsing :D
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
# Author: Abhishek Mishra <ideamonk@gmail.com> | |
require 'rubygems' | |
require 'choice' | |
Choice.options do | |
banner 'q2_lcd_numbers.rb [-shv] string' | |
separator 'Optional:' | |
option :size do | |
short '-s' | |
desc 'digit size' | |
cast Integer | |
default 1 | |
end | |
separator 'Common:' | |
option :help do | |
short '-h' | |
long '--help' | |
desc 'Show this message.' | |
end | |
option :version do | |
short '-v' | |
long '--version' | |
desc 'Show version.' | |
action do | |
puts 'LCD generator 0.1' | |
exit | |
end | |
end | |
end | |
def paint_map(map,digit,size,start) | |
# upper, mid, bottom | |
for i in (start+1)...(start+size+1) | |
if $digits[digit][0] == 1 | |
map[[0,i]] = 2 | |
end | |
if $digits[digit][3] == 1 | |
map[[0 + size + 1,i]] = 2 | |
end | |
if $digits[digit][6] == 1 | |
map[[0 + size*2 + 2,i]] = 2 | |
end | |
end | |
# verticals | |
for i in 1...size+1 | |
if $digits[digit][1] == 1 | |
map[[i,0+start]] = 1 | |
end | |
if $digits[digit][2] == 1 | |
map[[i,size+1+start]] = 1 | |
end | |
if $digits[digit][4] == 1 | |
map[[i+size+1,0+start]] = 1 | |
end | |
if $digits[digit][5] == 1 | |
map[[i+size+1,size+1+start]] = 1 | |
end | |
end | |
end | |
def print_map(map, size, total_digits) | |
for j in 0...(size*2+3) | |
for i in 0...(total_digits*(size+3)) | |
if map[[j,i]] == 2 | |
print '-' | |
elsif map[[j,i]] == 1 | |
print '|' | |
else | |
print ' ' | |
end | |
end | |
puts '' | |
end | |
end | |
def drive_paint(map, string, size) | |
i = 0 | |
j = 0 | |
while (i<string.size*(size+3)) | |
paint_map(map,string[j..j].to_i,size,i) | |
i += size+3 | |
j += 1 | |
end | |
end | |
# _ 0 | |
# | | 1 2 | |
# - 3 | |
# | | 4 5 | |
# - 6 | |
$digits = [ | |
[1,1,1,0,1,1,1], [0,0,1,0,0,1,0], # 0 1 | |
[1,0,1,1,1,0,1], [1,0,1,1,0,1,1], # 2 3 | |
[0,1,1,1,0,1,0], [1,1,0,1,0,1,1], # 4 5 | |
[1,1,0,1,1,1,1], [1,0,1,0,0,1,0], # 6 7 | |
[1,1,1,1,1,1,1], [1,1,1,1,0,1,0], # 8 9 | |
] | |
string = ARGV[-1] | |
size = Choice.choices[:size] | |
digitmap = {} | |
drive_paint(digitmap, string, size) | |
print_map(digitmap, size, string.size) |
Initially I was doing a string[j].chr.to_i which seemed like a required bullshit, I wish it could've been string[j].to_i but string[i] gives back a decimal. Now one could talk about the C style '5'-'0' conversion to number, but then, that machine dependent (think ascii, ebcdic, etc). The slightly more ruby-ish way - string[j..j].to_i :)
Here's my number -
abhishekmishra@mbp [~/code/BORQ]> ruby q2_lcd_numbers.rb -s 3 9535009187
--- --- --- --- --- --- --- --- ---
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
--- --- --- --- --- ---
| | | | | | | | | | | | |
| | | | | | | | | | | | |
| | | | | | | | | | | | |
--- --- --- --- --- ---
Surprisingly the PyMos core code is 180 lines while this reaches upto 108. Python => more results per line? The absence of 'end' in python is one big reason for this. Besides I dont find my ruby code so ruby-ish at this stage.
Labels: Ruby
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home