Price list behind API: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
|||
Line 14: | Line 14: | ||
# Show ascending price ranges and the number of items covered by each range. |
# Show ascending price ranges and the number of items covered by each range. |
||
# Show output from a sample run here. |
# Show output from a sample run here. |
||
=={{header|11l}}== |
|||
{{trans|Python}} |
|||
<lang 11l>V price_list_size = random:(99'000 .< 101'000) |
|||
V price_list = (0 .< price_list_size).map(i -> random:(100'000)) |
|||
V delta_price = 1 |
|||
F get_prange_count(startp, endp) |
|||
R :price_list.filter(r -> Float(r) C @startp .. @endp).len |
|||
F get_max_price() |
|||
R max(:price_list) |
|||
F get_5k(Float mn, Float =mx; num = 5'000) |
|||
‘Binary search for num items between mn and mx, adjusting mx’ |
|||
V count = get_prange_count(mn, mx) |
|||
V delta_mx = (mx - mn) / 2 |
|||
L count != num & delta_mx >= :delta_price / 2 |
|||
mx += I count > num {-delta_mx} E +delta_mx |
|||
mx = Float(mx I/ 1) |
|||
(count, delta_mx) = (get_prange_count(mn, mx), delta_mx / 2) |
|||
R (mx, count) |
|||
F get_all_5k(mn = 0.0, Float mx = get_max_price(); num = 5'000) |
|||
‘Get all non-overlapping ranges’ |
|||
V (partmax, partcount) = get_5k(mn, mx, num) |
|||
V result = [(mn, partmax, partcount)] |
|||
L partmax < mx |
|||
V partmin = partmax + :delta_price |
|||
(partmax, partcount) = get_5k(partmin, mx, num) |
|||
assert(partcount > 0, ‘price_list from ’partmin‘ with too many of the same price’) |
|||
result.append((partmin, partmax, partcount)) |
|||
R result |
|||
print(‘Using ’price_list_size‘ random prices from 0 to ’get_max_price()) |
|||
V result = get_all_5k() |
|||
print(‘Splits into ’result.len‘ bins of approx 5000 elements’) |
|||
L(mn, mx, count) result |
|||
print(f:‘ From {mn:8.1} ... {mx:8.1} with {count} items.’) |
|||
I price_list.len != sum(result.map((mn, mx, count) -> count)) |
|||
print("\nWhoops! Some items missing:")</lang> |
|||
{{out}} |
|||
<pre> |
|||
Using 99472 random prices from 0 to 99999 |
|||
Splits into 20 bins of approx 5000 elements |
|||
From 0.0 ... 5114.0 with 4998 items. |
|||
From 5115.0 ... 10224.0 with 5000 items. |
|||
From 10225.0 ... 15241.0 with 4998 items. |
|||
From 15242.0 ... 20393.0 with 4999 items. |
|||
From 20394.0 ... 25419.0 with 5000 items. |
|||
From 25420.0 ... 30425.0 with 5000 items. |
|||
From 30426.0 ... 35412.0 with 4999 items. |
|||
From 35413.0 ... 40507.0 with 4999 items. |
|||
From 40508.0 ... 45485.0 with 4999 items. |
|||
From 45486.0 ... 50569.0 with 5000 items. |
|||
From 50570.0 ... 55634.0 with 4998 items. |
|||
From 55635.0 ... 60584.0 with 4999 items. |
|||
From 60585.0 ... 65477.0 with 5000 items. |
|||
From 65478.0 ... 70277.0 with 4999 items. |
|||
From 70278.0 ... 75470.0 with 4999 items. |
|||
From 75471.0 ... 80383.0 with 5000 items. |
|||
From 80384.0 ... 85449.0 with 4996 items. |
|||
From 85450.0 ... 90475.0 with 4999 items. |
|||
From 90476.0 ... 95475.0 with 5000 items. |
|||
From 95476.0 ... 104515.0 with 4490 items. |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |