-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuying_a_car_unit.pas
More file actions
47 lines (39 loc) · 1.08 KB
/
buying_a_car_unit.pas
File metadata and controls
47 lines (39 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{
6 kyu
Buying a car
https://www.codewars.com/kata/554a44516729e4d80b000012
}
unit buying_a_car_unit;
{$mode objfpc}{$H+}
interface
function nbMonths(startPriceOld, startPriceNew, savingperMonth: int64;
percentLossByMonth: double): string;
implementation
uses
SysUtils;
function nbMonths(startPriceOld, startPriceNew, savingperMonth: int64;
percentLossByMonth: double): string;
var
oldPrice, newPrice, savRate, savings, currLossPerc, balance: double;
month: integer;
begin
oldPrice := startPriceOld;
newPrice := startPriceNew;
savRate := savingperMonth;
savings := 0.0;
balance := oldPrice + savings - newPrice;
currLossPerc := percentLossByMonth;
month := 0;
while balance < 0.0 do
begin
month := month + 1;
if month mod 2 = 0 then
currLossPerc := currLossPerc + 0.5;
oldPrice := oldPrice * (100.0 - currLossPerc) / 100.0;
newPrice := newPrice * (100.0 - currLossPerc) / 100.0;
savings := savings + savingperMonth;
balance := oldPrice + savings - newPrice;
end;
Result := IntToStr(month) + ' ' + IntToStr(Round(balance));
end;
end.