Price list behind API: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 18: | Line 18: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V price_list_size = random:(99'000 .< 101'000) |
||
V price_list = (0 .< price_list_size).map(i -> random:(100'000)) |
V price_list = (0 .< price_list_size).map(i -> random:(100'000)) |
||
Line 57: | Line 57: | ||
I price_list.len != sum(result.map((mn, mx, count) -> count)) |
I price_list.len != sum(result.map((mn, mx, count) -> count)) |
||
print("\nWhoops! Some items missing:")</ |
print("\nWhoops! Some items missing:")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 87: | Line 87: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 176: | Line 176: | ||
fmt.Println("Something went wrong - grand total of", total, "doesn't equal", numPrices, "\b!") |
fmt.Println("Something went wrong - grand total of", total, "doesn't equal", numPrices, "\b!") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 206: | Line 206: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="julia"># Sample price generation |
||
const price_list_size = rand(99000:100999) |
const price_list_size = rand(99000:100999) |
||
const price_list = rand(0:99999, price_list_size) |
const price_list = rand(0:99999, price_list_size) |
||
Line 253: | Line 253: | ||
testpricelist() |
testpricelist() |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Using 100299 random prices from 0 to 99990. |
Using 100299 random prices from 0 to 99990. |
||
Line 282: | Line 282: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="nim">import math, strformat |
||
const MinDelta = 1.0 |
const MinDelta = 1.0 |
||
Line 340: | Line 340: | ||
if total != numPrices: |
if total != numPrices: |
||
echo &"Something went wrong: grand total of {total} doesn't equal {numPrices}!"</ |
echo &"Something went wrong: grand total of {total} doesn't equal {numPrices}!"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 368: | Line 368: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use List::Util <min max shuffle>; |
use List::Util <min max shuffle>; |
||
Line 415: | Line 415: | ||
$max = $actualMax if $max > $actualMax; |
$max = $actualMax if $max > $actualMax; |
||
printf " From %6d to %6d with %4d items\n", $min, $max, $subtotal |
printf " From %6d to %6d with %4d items\n", $min, $max, $subtotal |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Using 100047 items with prices from 0 to 10000: |
<pre>Using 100047 items with prices from 0 to 10000: |
||
Line 444: | Line 444: | ||
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> |
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> |
||
Were you (or I) to define 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. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0.8.3"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- [assert() now accepts a 3rd param]</span> |
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0.8.3"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- [assert() now accepts a 3rd param]</span> |
||
Line 492: | Line 492: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">price_list</span><span style="color: #0000FF;">)==</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)),</span><span style="color: #008000;">"Whoops! Some items missing!"</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">price_list</span><span style="color: #0000FF;">)==</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)),</span><span style="color: #008000;">"Whoops! Some items missing!"</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 521: | Line 521: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">import random |
||
#%%Sample price generation |
#%%Sample price generation |
||
Line 567: | Line 567: | ||
if len(price_list) != sum(count for mn, mx, count in result): |
if len(price_list) != sum(count for mn, mx, count in result): |
||
print("\nWhoops! Some items missing:")</ |
print("\nWhoops! Some items missing:")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 595: | Line 595: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
<lang |
<syntaxhighlight lang="raku" line># 20210208 Raku programming solution |
||
my \minDelta = 1; |
my \minDelta = 1; |
||
Line 641: | Line 641: | ||
printf " From %6d to %6d with %4d items\n", $min, $max, $subtotal |
printf " From %6d to %6d with %4d items\n", $min, $max, $subtotal |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 673: | Line 673: | ||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "random" for Random |
||
import "/math" for Nums |
import "/math" for Nums |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 729: | Line 729: | ||
if (total != numPrices) { |
if (total != numPrices) { |
||
System.print("Something went wrong - grand total of %(total) doesn't equal %(numPrices)!") |
System.print("Something went wrong - grand total of %(total) doesn't equal %(numPrices)!") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |