import unittest class TheTrip: def __init__(self, count): self.count = count self.costs = [] pass def inputCost(self, cost): self.costs.append( float(cost * 100) ) def calculateAverageCost(self): return float( sum(self.costs) / len(self.costs) ) / 100.00 def calculateMinCharge(self): average = self.calculateAverageCost() * 100.00 diff_values = [] for value in self.costs: differ = average - value diff_values.append(abs(differ)) return sum(diff_values) / 2.00 / 100.00 class TheTripTest(unittest.TestCase): def setUp(self): pass def testInsertValue(self): t = TheTrip(3) t.inputCost(10.00) t.inputCost(20.00) t.inputCost(30.00) self.assertEqual(20.00, t.calculateAverageCost()) self.assertEqual(10.00, t.calculateMinCharge()) def testInsertValue2(self): t = TheTrip(4) t.inputCost(15.00) t.inputCost(15.01) t.inputCost(3.00) t.inputCost(3.01) self.assertEqual(11.99, t.calculateMinCharge()) if __name__ == '__main__': unittest.main()
require 'test/unit' class TheTrip attr_reader :member, :costs, :average attr_writer :member, :costs def initialize(number) self.member = number self.costs = [] end def input_cost(cost) self.costs << cost @average = 0 self.costs.each {|value| @average += value } @average /= self.costs.length end def get_min_charge() charges = 0.00 self.costs.each { |value| charges += (value - self.average).abs } return charges / 2.00 end end class TheTripTest < Test::Unit::TestCase def test_case1 t = TheTrip.new(3) t.input_cost(10.00) t.input_cost(20.00) t.input_cost(30.00) assert_equal(20.00, t.average) assert_equal(10.00, t.get_min_charge()) end def test_case2 t = TheTrip.new(4) t.input_cost(15.00) t.input_cost(15.01) t.input_cost(3.00) t.input_cost(3.01) assert_equal(11.99, t.get_min_charge()) end end