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 ![]()
require 'rubygems'require 'choice'Choice.options dobanner 'q2_lcd_numbers.rb [-shv] string'separator 'Optional:'option :size doshort '-s'desc 'digit size'cast Integerdefault 1endseparator 'Common:'option :help doshort '-h'long '--help'desc 'Show this message.'endoption :version doshort '-v'long '--version'desc 'Show version.'action doputs 'LCD generator 0.1'exitendendenddef paint_map(map,digit,size,start)# upper, mid, bottomfor i in (start+1)...(start+size+1)if $digits[digit][0] == 1map[[0,i]] = 2endif $digits[digit][3] == 1map[[0 + size + 1,i]] = 2endif $digits[digit][6] == 1map[[0 + size*2 + 2,i]] = 2endend# verticalsfor i in 1...size+1if $digits[digit][1] == 1map[[i,0+start]] = 1endif $digits[digit][2] == 1map[[i,size+1+start]] = 1endif $digits[digit][4] == 1map[[i+size+1,0+start]] = 1endif $digits[digit][5] == 1map[[i+size+1,size+1+start]] = 1endendenddef 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]] == 2print '-'elsif map[[j,i]] == 1print '|'elseprint ' 'endendputs ''endenddef drive_paint(map, string, size)i = 0j = 0while (i<string.size*(size+3))paint_map(map,string[j..j].to_i,size,i)i += size+3j += 1endend# _ 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 -
d3cipher@codecracker [~/code/BORQ]> ruby q2_lcd_numbers.rb -s 3 8109215281 --- --- --- --- --- --- --- | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | --- --- --- --- --- --- | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | --- --- --- --- --- --- ---
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.




seems… a quiet lengthy code for a blog… isn’t it???