Skip to content
Browse files

first attempt

  • Loading branch information...
1 parent 85e1e0e commit d02e7393fcba286e04dce6b547c7d8fb609d4e7a Richard Colley committed Feb 26, 2010
Showing with 122 additions and 0 deletions.
  1. +83 −0 multiplication_table.rb
  2. +39 −0 test_mult.rb
View
83 multiplication_table.rb
@@ -0,0 +1,83 @@
+=begin
+
+ multiplication_table (integer, heading = '', decorate = false)
+ returns a string object.
+=end
+
+def indent( text, cols )
+ text.split(/\r|\r\n|\n/).map {|line| " "*cols << line}
+end
+
+def make_heading( heading, width=0 )
+ !heading.empty? ? heading.center(width) << $/ : ""
+end
+
+def make_decoration(width)
+ "=" * width << $/
+end
+
+def multiplication_table( integer, heading = '', decorate = false)
+ times_table = []
+ if integer == 0
+ times_table[0] = [0]
+ else
+ integer.abs.times do |y|
+ times_table[y] = []
+ integer.abs.times do |x|
+ times_table[y][x]=(x+1)*(y+1)
+ end
+ end
+ end
+
+ max_value = times_table[times_table.size-1][times_table.size-1]
+ first_column_width = integer.to_s.size+1
+ other_column_width = max_value.to_s.size
+ table_width = first_column_width + 1 + (other_column_width+1) * (times_table.size-1)
+
+ heading_str = make_heading(heading, table_width)
+ decoration = decorate ? make_decoration(table_width) : ""
+ table_str = times_table.inject("") do |acc,row|
+ row.each_with_index do |v,i|
+ this_width = i==0 ? first_column_width : other_column_width
+ acc << v.to_s.rjust(this_width) << " "
+ end
+ acc << $/
+ end
+ indent( heading_str + decoration + table_str + decoration, 1 )
+end
+
+=begin
+ The above is the implementation, here's some code to do some tests.
+=end
+
+require 'getoptlong'
+
+def usage(msg)
+ puts msg if msg
+ puts "Usage: #{$0} [-d] [-h] <number> [<number>...]"
+ puts <<-EOF
+ Prints times tables for the given numbers.
+ If -d is given, some table decoration is applied.
+ If -h is given, a heading is applied to each table.
+ EOF
+ exit(1)
+end
+
+opts = GetoptLong.new(
+ [ "--decorate", "-d", GetoptLong::NO_ARGUMENT ],
+ [ "--heading", "-h", GetoptLong::NO_ARGUMENT ]
+)
+
+opt_decorate = opt_heading = false
+opts.each do |opt,arg|
+ case opt
+ when '--decorate' then opt_decorate = true
+ when '--heading' then opt_heading = true
+ else usage "Unknown option '#{opt}'"
+ end
+end
+
+while i = ARGV.shift
+ puts multiplication_table(i.to_i, opt_heading ? "x#{i}" : "", opt_decorate)
+ puts unless ARGV.empty?
+end
View
39 test_mult.rb
@@ -0,0 +1,39 @@
+require 'multiplication_table'
+require 'test/unit'
+
+class TC_Table < Test::Unit::TestCase
+ def test_table_12
+
+result_12 = <<-EOS
+ Table to 12
+ ================================================
+ 1 2 3 4 5 6 7 8 9 10 11 12
+ 2 4 6 8 10 12 14 16 18 20 22 24
+ 3 6 9 12 15 18 21 24 27 30 33 36
+ 4 8 12 16 20 24 28 32 36 40 44 48
+ 5 10 15 20 25 30 35 40 45 50 55 60
+ 6 12 18 24 30 36 42 48 54 60 66 72
+ 7 14 21 28 35 42 49 56 63 70 77 84
+ 8 16 24 32 40 48 56 64 72 80 88 96
+ 9 18 27 36 45 54 63 72 81 90 99 108
+ 10 20 30 40 50 60 70 80 90 100 110 120
+ 11 22 33 44 55 66 77 88 99 110 121 132
+ 12 24 36 48 60 72 84 96 108 120 132 144
+ ================================================
+EOS
+
+result_1 = " \n ===\n 1\n ===\n"
+result_3 = " \n 1 2 3\n 2 4 6\n 3 6 9\n"
+
+ result12 = multiplication_table 12, 'Table to 12', true
+ result1 = multiplication_table 1, '', true
+ result3 = multiplication_table 3
+ assert_equal result_12, result12
+ assert_equal result_1, result1
+ assert_equal result_3, result3
+
+ end
+
+end
+
+

0 comments on commit d02e739

Please sign in to comment.
Something went wrong with that request. Please try again.