Graham Smith wrote:
Hi,

This is my attempt to comply with the tests Matthew posted last night.



#!/usr/bin/ruby

def bodgy_rounding(amt)

if amt >= 0.0 x = amt % 5.0
                y = amt - x
                round = x > 2.0 ? y+5 : y
                rem = round - amt

        elsif amt > -3.0
                rem = 0.0 - amt
round = 0.0 else
                rem = 0.0
                round = amt
        end

        return [round, rem]
end

This is how I'd code this:

$cat nrs_payroll.rb

def bodgy_rounding(x)

        remainder = x % 5.0
        base_amount = x - remainder
        remainder > 2 ? (return base_amount+5):(return base_amount)
end

$cat simple_br_test.rb

#!/usr/bin/env ruby
require 'test/unit'
require 'nrs_payroll.rb'

class SimpleBodgyRoundingTest < Test::Unit::TestCase
        def test_bodgy_rounding
                assert_equal(10, bodgy_rounding(12))
                assert_equal(15, bodgy_rounding(13))
                assert_equal(0, bodgy_rounding(1))
                assert_equal(0, bodgy_rounding(0))
                assert_equal(5, bodgy_rounding(7))
                assert_equal(10, bodgy_rounding(7.01))
        end

        def test_negative_bodgy_rounding
                assert_equal(0, bodgy_rounding(-1))
                assert_equal(0, bodgy_rounding(0))
                assert_equal(0, bodgy_rounding(-2))
                assert_equal(-5, bodgy_rounding(-3))
#               assert_equal(-7.39, bodgy_rounding(-7.39))
                assert_equal(-10, bodgy_rounding(-10))
#               assert_equal(-71.39, bodgy_rounding(-71.39))
#               assert_equal(-72.39, bodgy_rounding(-72.39))
#               assert_equal(-73.39, bodgy_rounding(-73.39))
#               assert_equal(-74.39, bodgy_rounding(-74.39))
        end
end


Then, run

$./simple_br_test.rb

Output:

Loaded suite simple_br_test.rb
Started
..
Finished in 0.002205 seconds.

2 tests, 11 assertions, 0 failures, 0 errors




O Plameras


_______________________________________________
coders mailing list
[email protected]
http://lists.slug.org.au/listinfo/coders

Reply via email to