Price list behind API: Difference between revisions
Content added Content deleted
(Added Wren) |
(→{{header|Wren}}: Didn't satisfy the task requirements previously.) |
||
Line 171: | Line 171: | ||
var rand = Random.new() |
var rand = Random.new() |
||
var minDelta = 1 |
|||
var getMaxPrice = Fn.new { |prices| Nums.max(prices) } |
var getMaxPrice = Fn.new { |prices| Nums.max(prices) } |
||
Line 178: | Line 179: | ||
var get5000 = Fn.new { |prices, min, max, n| |
var get5000 = Fn.new { |prices, min, max, n| |
||
var count = getPrangeCount.call(prices, min, max) |
var count = getPrangeCount.call(prices, min, max) |
||
var delta = |
var delta = (max - min) / 2 |
||
while (count != n && delta > |
while (count != n && delta >= minDelta/2) { |
||
max = (count > n) ? max-delta : max+delta |
max = ((count > n) ? max-delta : max+delta).floor |
||
count = getPrangeCount.call(prices, min, max) |
count = getPrangeCount.call(prices, min, max) |
||
delta = |
delta = delta / 2 |
||
} |
} |
||
return [max, count] |
return [max, count] |
||
Line 201: | Line 202: | ||
return res |
return res |
||
} |
} |
||
var numPrices = |
var numPrices = rand.int(99000, 101001) |
||
var maxPrice = 1e5 |
var maxPrice = 1e5 |
||
var prices = List.filled(numPrices, 0) // list of prices |
var prices = List.filled(numPrices, 0) // list of prices |
||
Line 225: | Line 226: | ||
Sample run: |
Sample run: |
||
<pre> |
<pre> |
||
Using |
Using 99756 items with prices from 0 to 99998: |
||
Split into 20 bins of approx 5000 elements: |
Split into 20 bins of approx 5000 elements: |
||
From 0 to |
From 0 to 4964 with 5000 items |
||
From |
From 4965 to 9992 with 5000 items |
||
From |
From 9993 to 15063 with 5000 items |
||
From |
From 15064 to 20130 with 5000 items |
||
From |
From 20131 to 25063 with 4998 items |
||
From |
From 25064 to 30014 with 4998 items |
||
From |
From 30015 to 35002 with 5000 items |
||
From |
From 35003 to 40030 with 5000 items |
||
From |
From 40031 to 45058 with 5000 items |
||
From |
From 45059 to 50199 with 4999 items |
||
From |
From 50200 to 55133 with 4999 items |
||
From |
From 55134 to 60139 with 4997 items |
||
From |
From 60140 to 65097 with 5000 items |
||
From |
From 65098 to 69972 with 4999 items |
||
From |
From 69973 to 74932 with 5000 items |
||
From |
From 74933 to 80041 with 5000 items |
||
From |
From 80042 to 85214 with 5000 items |
||
From |
From 85215 to 90241 with 4999 items |
||
From |
From 90242 to 95353 with 5000 items |
||
From |
From 95354 to 99998 with 4767 items |
||
</pre> |
</pre> |