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 = ((max - min)/2).floor
var delta = (max - min) / 2
while (count != n && delta > 0) {
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/2).floor
delta = delta / 2
}
}
return [max, count]
return [max, count]
Line 201: Line 202:
return res
return res
}
}
var numPrices = 1e5
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 100000 items with prices from 0 to 99998:
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 5043 with 5000 items
From 0 to 4964 with 5000 items
From 5044 to 10102 with 5001 items
From 4965 to 9992 with 5000 items
From 10103 to 15192 with 5000 items
From 9993 to 15063 with 5000 items
From 15193 to 20320 with 5000 items
From 15064 to 20130 with 5000 items
From 20321 to 25368 with 4998 items
From 20131 to 25063 with 4998 items
From 25369 to 30376 with 5003 items
From 25064 to 30014 with 4998 items
From 30377 to 35422 with 5001 items
From 30015 to 35002 with 5000 items
From 35423 to 40337 with 5001 items
From 35003 to 40030 with 5000 items
From 40338 to 45299 with 5000 items
From 40031 to 45058 with 5000 items
From 45300 to 50389 with 5001 items
From 45059 to 50199 with 4999 items
From 50390 to 55402 with 4998 items
From 50200 to 55133 with 4999 items
From 55403 to 60382 with 5001 items
From 55134 to 60139 with 4997 items
From 60383 to 65330 with 4999 items
From 60140 to 65097 with 5000 items
From 65331 to 70278 with 5001 items
From 65098 to 69972 with 4999 items
From 70279 to 75285 with 4999 items
From 69973 to 74932 with 5000 items
From 75286 to 80336 with 5000 items
From 74933 to 80041 with 5000 items
From 80337 to 85289 with 5000 items
From 80042 to 85214 with 5000 items
From 85290 to 90291 with 5000 items
From 85215 to 90241 with 4999 items
From 90292 to 95052 with 5001 items
From 90242 to 95353 with 5000 items
From 95053 to 99998 with 4996 items
From 95354 to 99998 with 4767 items
</pre>
</pre>