From: Phrogz Date: 2007-02-10T07:15:20+09:00 Subject: Re: One-Liners (#113) On Feb 9, 10:00 am, Ruby Quiz wrote: > * Given a Numeric, provide a String representation with commas inserted between > each set of three digits in front of the decimal. For example, 1999995.99 > should become "1,999,995.99". More test cases: def test_commify( str ) maps = [ [1, "1"], [-1, "-1"], [0.001, "0.001"], [-0.001, "-0.001"], [999, "999"], [-999, "-999"], [999.1, "999.1"], [-999.1, "-999.1"], [999.12, "999.12"], [-999.12, "-999.12"], [999.123, "999.123"], [-999.123, "-999.123"], [9999, "9,999"], [-9999, "-9,999"], [9999.1, "9,999.1"], [-9999.1, "-9,999.1"], [9999.12, "9,999.12"], [-9999.12, "-9,999.12"], [9999.123, "9,999.123"], [-9999.123, "-9,999.123"], [12, "12"], [123, "123"], [1234, "1,234"], [12345, "12,345"], [123456, "123,456"], [1234567, "1,234,567"], [12345678, "12,345,678"], [-12, "-12"], [-123, "-123"], [-1234, "-1,234"], [-12345, "-12,345"], [-123456, "-123,456"], [-1234567, "-1,234,567"], [-12345678, "-12,345,678"] ] raise "Solution too long (#{str.length} characters)" unless str.length <= 80 maps.each{ |pair| quiz = pair[0] expected = pair[1] output = eval( str ) unless expected==output raise "quiz=#{quiz}; expected #{expected.inspect}, got #{output.inspect}" end } end