-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathCarSaleman.rb
More file actions
36 lines (26 loc) · 1.03 KB
/
CarSaleman.rb
File metadata and controls
36 lines (26 loc) · 1.03 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
=begin
These salesmen talk too much!
Write a function that parses a car salesman's pitch and returns the total price
of the vehicle.
As the salesman talks, eventually there will be three numbers mentioned. The
down payment, the monthly fee, and the total number of months.
The monthly payment is always less than the down payment and both numbers will
be preceeded by a dollar sign.
The number of months will not be preceded by a dollar sign, and will be less
than 10 years.
=end
# My Solution
def car_price (salespitch)
salespitch.gsub!(",","")
temp = salespitch.scan /([$]\d{1,})/
monthly, downpayment = temp.flatten.map {|i| i[1..-1].to_i}.sort
temp = salespitch.scan /(\d{1,}\s([y]|[m]))/
months, n = temp[0][0].split(" ")
temp[0][1].downcase == "y" ? months = 12 * months.to_i : months = months.to_i
return (monthly*months)+downpayment
end
# Better Solution
def car_price (salespitch)
a,b,c = salespitch.gsub( /[^0-9$ ]/, '').strip.split.sort.map{ |v| v.tr("$","").to_i }
[a,b].min*(c < 11 ? c*12 : c) + [a,b].max
end