Price list behind API: Difference between revisions

no edit summary
(Added Go)
No edit summary
Line 128:
From 90121 to 95102 with 4998 items
From 95103 to 99999 with 4809 items
</pre>
 
=={{header|Julia}}==
{{trans|Python}}
<lang julia># Sample price generation
const price_list_size = rand(99000:100999)
const price_list = rand(0:99999, price_list_size)
const delta_price = 1 # Minimum difference between any two different prices.
 
""" The API provides these two """
get_prange_count(startp, endp) = sum([startp <= r <= endp for r in price_list])
get_max_price() = maximum(price_list)
 
""" Binary search for num items between mn and mx, adjusting mx """
function get_5k(mn=0, mx=get_max_price(), num=5_000)
count = get_prange_count(mn, mx)
delta_mx = (mx - mn) / 2
while count != num && delta_mx >= delta_price / 2
mx += (count > num ? -delta_mx : +delta_mx)
mx = floor(mx)
count, delta_mx = get_prange_count(mn, mx), delta_mx / 2
end
return mx, count
end
 
""" Get all non-overlapping ranges """
function get_all_5k(mn=0, mx=get_max_price(), num=5_000)
partmax, partcount = get_5k(mn, mx, num)
result = [(mn, partmax, partcount)]
while partmax < mx
partmin = partmax + delta_price
partmax, partcount = get_5k(partmin, mx, num)
push!(result, (partmin, partmax, partcount))
end
return result
end
 
function testpricelist()
println("Using $price_list_size random prices from 0 to $(get_max_price()).")
result = get_all_5k()
println("Splits into $(length(result)) bins of approximately 5000 elements.")
for (mn, mx, count) in result
println(" From $(Float32(mn)) ... $(Float32(mx)) with $count items.")
end
if length(price_list) != sum([x[3] for x in result])
print("\nWhoops! Some items missing.")
end
end
 
testpricelist()
</lang>{{out}}
<pre>
Using 100299 random prices from 0 to 99990.
Splits into 21 bins of approximately 5000 elements.
From 0.0 ... 4911.0 with 4998 items.
From 4912.0 ... 9832.0 with 5000 items.
From 9833.0 ... 14841.0 with 5000 items.
From 14842.0 ... 19756.0 with 4999 items.
From 19757.0 ... 24782.0 with 4994 items.
From 24783.0 ... 29751.0 with 4999 items.
From 29752.0 ... 34655.0 with 5000 items.
From 34656.0 ... 39748.0 with 5000 items.
From 39749.0 ... 44819.0 with 4999 items.
From 44820.0 ... 49908.0 with 5000 items.
From 49909.0 ... 54898.0 with 4999 items.
From 54899.0 ... 59700.0 with 4999 items.
From 59701.0 ... 64767.0 with 4999 items.
From 64768.0 ... 69824.0 with 4999 items.
From 69825.0 ... 74765.0 with 4999 items.
From 74766.0 ... 79654.0 with 4999 items.
From 79655.0 ... 84674.0 with 5000 items.
From 84675.0 ... 89602.0 with 4999 items.
From 89603.0 ... 94715.0 with 5000 items.
From 94716.0 ... 99666.0 with 4997 items.
From 99667.0 ... 100309.0 with 320 items.
</pre>
 
4,105

edits