"Buying a car" Ruby codewars

I am trying to make a call to Ruby codewars and I am stuck since I passed the sample tests but cannot get past the final one. I get an error. Expected: [8, 597], instead it turned out: [8, 563].

Instructions:

The man has a fairly old car, worth $ 2,000. He saw a used car worth $ 8,000. He wants to keep his old car until he can buy a used one.

He thinks he can save $ 1,000 every month, but the prices of his old car and his new one are 1.5 percent a month. In addition, the percentage of losses increases by a fixed 0.5 percent at the end of every two months.

An example of interest lost per month:

If, for example, at the end of the first month the percentage of losses is 1, the end of the second month is the percentage of losses 1.5, the end of the third month is still 1.5, the end of the 4th month is 2, etc.

Can you help him? Our man finds it difficult to do all these calculations.

How many months will it take him to save enough money to buy he wants, and how much money will he leave?

def nbMonths(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth)
  months = 0
  leftover = 0
  currentSavings = 0
  until (currentSavings + startPriceOld) >= (startPriceNew)
    months += 1
    months.even? ? percentLossByMonth = percentLossByMonth + 0.5 : percentLossByMonth
    startPriceNew = startPriceNew * (1 - (percentLossByMonth/100))
    startPriceOld = startPriceOld * (1 - (percentLossByMonth/100))
    currentSavings = currentSavings + savingperMonth
  end
  leftover = currentSavings + startPriceOld - startPriceNew
  return [months, leftover.abs.to_i]
end

      

I don't want to look at solutions and I don't need one here, just a nudge in the right direction would be very helpful.

Also, what I get is that the code is probably not optimal in many ways, but I started coding 2 weeks ago, so I'll do my best.

Tnx guys

+3


source to share


2 answers


Your algorithm is good. But you have two coding errors:

1) percentLossByMonth

need to convert to float before dividing by 100 (5/100 = 0 while (5.to_f) / 100 = 0.05)



2) The instructions say that you need to return the nearest integer remaining, which is leftover.round

def nbMonths(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth)
  months = 0
  leftover = 0
  currentSavings = 0
  until (currentSavings + startPriceOld) >= (startPriceNew)
    months += 1
    percentLossByMonth += months.even? ? 0.5 : 0    
    startPriceNew = startPriceNew * (1 - (percentLossByMonth.to_f/100))
    startPriceOld = startPriceOld * (1 - (percentLossByMonth.to_f/100))
    currentSavings += savingperMonth
  end
  leftover = currentSavings + startPriceOld - startPriceNew
  return [months, leftover.round]
end

      

+3


source


The problem with your code has been identified, so I just suggest an alternative calculation.

r = 0.015
net_cost = 8000-2000
n = 1
months, left_over = loop do
  r += 0.005 if n.even?
  net_cost *= (1-r)
  tot = n*1000 - net_cost
  puts "n=#{n}, r=#{r}, net_cost=#{net_cost.to_i}, " +
    "savings=#{(n*1000).to_i}, deficit=#{-tot.to_i}"
  break [n, tot] if tot >= 0
  n += 1
end
  #=> [6, 766.15...]
months
  #=> 6
left_over
  #=> 766.15...

      



and printing

n=1, r=0.015, net_cost=5910, savings=1000, deficit=4910
n=2, r=0.020, net_cost=5791, savings=2000, deficit=3791
n=3, r=0.020, net_cost=5675, savings=3000, deficit=2675
n=4, r=0.025, net_cost=5534, savings=4000, deficit=1534
n=5, r=0.025, net_cost=5395, savings=5000, deficit=395
n=6, r=0.030, net_cost=5233, savings=6000, deficit=-766

      

+2


source







All Articles