-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_data_gen.rb
More file actions
37 lines (30 loc) · 879 Bytes
/
sample_data_gen.rb
File metadata and controls
37 lines (30 loc) · 879 Bytes
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
require 'rubygems'
require 'json'
denominations = [1,5,10,25,50]
prices = (1..99).to_a
exchanges = prices.map do |price|
neg_price = 100-price
pos_path = denominations.reverse.map{ |coin_size|
result = price/coin_size
price = price % coin_size
result
}
pos_exch_num = pos_path.inject(0){|sum, n| sum + n.abs }
neg_path = denominations.reverse.map{ |coin_size|
result = neg_price/coin_size
neg_price = neg_price % coin_size
result * -1
}
neg_exch_num = neg_path.inject(0){|sum, n| sum + n.abs }
exch_num = nil; path = nil;
if neg_exch_num < pos_exch_num
path = neg_path; exch_num = neg_exch_num
else
path = pos_path; exch_num = pos_exch_num
end
path.reverse
end
data = {"denominations" => denominations, "exchanges" => exchanges}
File.open("resources/public/sample_output.json", 'w') do |f|
f.puts data.to_json
end