Price list behind API: Difference between revisions

Content added Content deleted
(→‎{{header|Go}}: Fixed corner case.)
m (→‎{{header|Phix}}: corner case, decided prices deserve 2dp, even if always ".00")
Line 212: Line 212:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|Python}}
{{trans|Python}}
Note that defaulted arguments of the form mx=get_max_price() are not currently supported, hence a slightly hacky workaround.<br>
Note that defaulted arguments of the form mx=get_max_price() are not currently supported, hence a slightly hacky workaround, of -1 then -1==>get_max_price().<br>
If you defined constant mp = get_max_price(), then mx=mp style parameter defaulting would be fine.
Were you (or I) to define constant mp = get_max_price(), then mx=mp style parameter defaulting would be fine.
<lang Phix>requires("0.8.3") -- [assert() now accepts a 3rd param]
<lang Phix>constant price_list_size = 99_000 + rand(2_001) - 1,
constant price_list_size = 99_000 + rand(2_001) - 1,
price_list = sq_sub(sq_rand(repeat(100_000,price_list_size)),1),
price_list = sq_sub(sq_rand(repeat(100_000,price_list_size)),1),
delta_price = 1 -- Minimum difference between any two different prices.
delta_price = 1 -- Minimum difference between any two different prices.
Line 246: Line 247:
integer partmin = partmax + delta_price
integer partmin = partmax + delta_price
{partmax, partcount} = get_5k(partmin, mx, num)
{partmax, partcount} = get_5k(partmin, mx, num)
assert(partcount>0,"Price list from %.2f has too many same price",{partmin})
result = append(result,{partmin, partmax, partcount})
result = append(result,{partmin, partmax, partcount})
end while
end while
Line 255: Line 257:
printf(1,"Splits into %d bins of approx 5000 elements\n",{length(result)})
printf(1,"Splits into %d bins of approx 5000 elements\n",{length(result)})
for i=1 to length(result) do
for i=1 to length(result) do
printf(1," From %8.1f ... %8.1f with %d items.\n",result[i])
printf(1," From %8.2f ... %8.2f with %d items.\n",result[i])
end for
end for
if length(price_list) != sum(vslice(result,3)) then
assert(length(price_list)==sum(vslice(result,3)),"Whoops! Some items missing!")</lang>
printf(1,"\nWhoops! Some items missing:\n")
end if</lang>
{{out}}
{{out}}
<pre>
<pre>
Using 99714 random prices from 0 to 99999
Using 100957 random prices from 0 to 99999
Splits into 20 bins of approx 5000 elements
Splits into 21 bins of approx 5000 elements
From 0.0 ... 4977.0 with 5000 items.
From 0.00 ... 4838.00 with 4998 items.
From 4978.0 ... 10019.0 with 4999 items.
From 4839.00 ... 9765.00 with 4999 items.
From 10020.0 ... 15114.0 with 4999 items.
From 9766.00 ... 14602.00 with 4999 items.
From 15115.0 ... 19987.0 with 4998 items.
From 14603.00 ... 19575.00 with 5000 items.
From 19988.0 ... 25088.0 with 4996 items.
From 19576.00 ... 24515.00 with 4998 items.
From 25089.0 ... 30080.0 with 4995 items.
From 24516.00 ... 29476.00 with 5000 items.
From 30081.0 ... 35117.0 with 5000 items.
From 29477.00 ... 34386.00 with 5000 items.
From 35118.0 ... 40081.0 with 4999 items.
From 34387.00 ... 39289.00 with 4999 items.
From 40082.0 ... 45080.0 with 5000 items.
From 39290.00 ... 44349.00 with 5000 items.
From 45081.0 ... 50181.0 with 5000 items.
From 44350.00 ... 49265.00 with 4992 items.
From 50182.0 ... 55223.0 with 5000 items.
From 49266.00 ... 54262.00 with 4998 items.
From 55224.0 ... 60271.0 with 5000 items.
From 54263.00 ... 59289.00 with 4999 items.
From 60272.0 ... 65102.0 with 4999 items.
From 59290.00 ... 64191.00 with 5000 items.
From 65103.0 ... 70140.0 with 5000 items.
From 64192.00 ... 69119.00 with 4999 items.
From 70141.0 ... 75195.0 with 4997 items.
From 69120.00 ... 74095.00 with 4996 items.
From 75196.0 ... 80203.0 with 4998 items.
From 74096.00 ... 79144.00 with 4999 items.
From 80204.0 ... 85210.0 with 4999 items.
From 79145.00 ... 84093.00 with 4998 items.
From 85211.0 ... 90182.0 with 5000 items.
From 84094.00 ... 88961.00 with 4996 items.
From 90183.0 ... 95268.0 with 4999 items.
From 88962.00 ... 94051.00 with 4999 items.
From 95269.0 ... 104722.0 with 4736 items.
From 94052.00 ... 99038.00 with 5000 items.
From 99039.00 ... 100955.00 with 988 items.
</pre>
</pre>