Bin given limits: Difference between revisions
→{{header|REXX}}: minor formatting correction
(Added Algol 68) |
Walterpachl (talk | contribs) (→{{header|REXX}}: minor formatting correction) |
||
(12 intermediate revisions by 10 users not shown) | |||
Line 42:
Show output here, on this page.
=={{header|11l}}==
{{trans|Python}}
<
V lo = 0
V hi = a.len
Line 90 ⟶ 89:
466, 23,707,467, 33,670,921,180,991,396,160,436,717,918, 8,374,101,684,727,749]
bins = bin_it(limits, data)
bin_print(limits, bins)</
{{out}}
Line 118 ⟶ 117:
>= 720 := 59
</pre>
=={{header|Action!}}==
<
PROC Count(INT ARRAY limits INT nLimits INT ARRAY data INT nData INT ARRAY bins)
Line 186 ⟶ 184:
Test(limits1,6,data1,50) PutE()
Test(limits2,10,data2,200)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bin_given_limits.png Screenshot from Atari 8-bit computer]
Line 210 ⟶ 208:
>=720: 59
</pre>
=={{header|Ada}}==
This example works with Ada 2012. The definition of the subtype Limits_Array employs a dynamic predicate to ensure that the limits array is sorted. The solution defines the binning types and operations within an Ada package, providing modularity and simplifying the code in the main procedure.
'''package specification:'''
<
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
Line 223 ⟶ 220:
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array);
end binning;
</syntaxhighlight>
'''package body:'''
<
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Line 294 ⟶ 291:
end binning;
</syntaxhighlight>
'''main procedure:'''
<
with binning; use binning;
Line 330 ⟶ 327:
Print (Limits => Limits_2, Bin_Result => Bin_2);
end Main;
</syntaxhighlight>
{output}
<pre>
Line 355 ⟶ 352:
>= 720 : 59
</pre>
=={{header|ALGOL 68}}==
<
# returns an array of "bins" containing the counts of the data items #
# that fall into the bins given the limits #
Line 393 ⟶ 389:
bin number +:= 1
OD;
print( ( "
, ": ", whole( bins[ UPB bins ], -4 )
, newline
Line 430 ⟶ 426:
show bins( limits, data INTOBINS limits )
END
END</
{{out}}
<pre>
Line 440 ⟶ 436:
>= 53 and < 67: 9
>= 67 and < 83: 5
data set 2
Line 453 ⟶ 449:
>= 591 and < 634: 6
>= 634 and < 720: 16
</pre>
=={{header|AutoHotkey}}==
<
bin := [], counter := 0
for i, val in data {
Line 473 ⟶ 468:
}
return output .= (prevlimit ? prevlimit : "-∞") ", ∞ : " ((x:=bin["∞"].Count())?x:0) "`n"
}</
Examples:<
data := [95,21,94,12,99,4,70,75,83,93,52,80,57,5,53,86,65,17,92,83,71,61,54,58,47,16
, 8, 9,32,84,7,87,46,19,30,37,96,6,98,40,79,97,45,64,60,29,49,36,43,55]
Line 491 ⟶ 486:
,466, 23,707,467, 33,670,921,180,991,396,160,436,717,918, 8,374,101,684,727,749]
MsgBox, 262144, , % Bin_given_limits(limits, data)
return</
{{out}}
<pre>
Line 514 ⟶ 509:
720, ∞ : 59
</pre>
=={{header|C}}==
<
#include <stdlib.h>
Line 598 ⟶ 592:
return EXIT_SUCCESS;
}</
{{out}}
Line 624 ⟶ 618:
>= 720 : 59
</pre>
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System;
public class Program
{
static void Main()
{
PrintBins(new [] { 23, 37, 43, 53, 67, 83 },
95,21,94,12,99,4,70,75,83,93,52,80,57,5,53,86,65,17,92,83,71,61,54,58,47,
16, 8, 9,32,84,7,87,46,19,30,37,96,6,98,40,79,97,45,64,60,29,49,36,43,55
);
Console.WriteLine();
PrintBins(new [] { 14, 18, 249, 312, 389, 392, 513, 591, 634, 720 },
445,814,519,697,700,130,255,889,481,122,932, 77,323,525,570,219,367,523,442,933,416,589,930,373,202,
253,775, 47,731,685,293,126,133,450,545,100,741,583,763,306,655,267,248,477,549,238, 62,678, 98,534,
622,907,406,714,184,391,913, 42,560,247,346,860, 56,138,546, 38,985,948, 58,213,799,319,390,634,458,
945,733,507,916,123,345,110,720,917,313,845,426, 9,457,628,410,723,354,895,881,953,677,137,397, 97,
854,740, 83,216,421, 94,517,479,292,963,376,981,480, 39,257,272,157, 5,316,395,787,942,456,242,759,
898,576, 67,298,425,894,435,831,241,989,614,987,770,384,692,698,765,331,487,251,600,879,342,982,527,
736,795,585, 40, 54,901,408,359,577,237,605,847,353,968,832,205,838,427,876,959,686,646,835,127,621,
892,443,198,988,791,466, 23,707,467, 33,670,921,180,991,396,160,436,717,918, 8,374,101,684,727,749);
}
static void PrintBins(int[] limits, params int[] data)
{
int[] bins = Bins(limits, data);
Console.WriteLine($"-∞ .. {limits[0]} => {bins[0]}");
for (int i = 0; i < limits.Length-1; i++) {
Console.WriteLine($"{limits[i]} .. {limits[i+1]} => {bins[i+1]}");
}
Console.WriteLine($"{limits[^1]} .. ∞ => {bins[^1]}");
}
static int[] Bins(int[] limits, params int[] data)
{
Array.Sort(limits);
int[] bins = new int[limits.Length + 1];
foreach (int n in data) {
int i = Array.BinarySearch(limits, n);
i = i < 0 ? ~i : i+1;
bins[i]++;
}
return bins;
}
}</syntaxhighlight>
{{out}}
<pre>
-∞ .. 23 => 11
23 .. 37 => 4
37 .. 43 => 2
43 .. 53 => 6
53 .. 67 => 9
67 .. 83 => 5
83 .. ∞ => 13
-∞ .. 14 => 3
14 .. 18 => 0
18 .. 249 => 44
249 .. 312 => 10
312 .. 389 => 16
389 .. 392 => 2
392 .. 513 => 28
513 .. 591 => 16
591 .. 634 => 6
634 .. 720 => 16
720 .. ∞ => 59
</pre>
=={{header|C++}}==
<
#include <cassert>
#include <iomanip>
Line 688 ⟶ 749:
std::cout << "\nExample 2:\n";
print_bins(limits2, bins(limits2, data2));
}</
{{out}}
Line 714 ⟶ 775:
>= 720 : 59
</pre>
=={{header|CLU}}==
<
% CLU allows arrays to start at any index; the result array
% will have the same lower bound as the limit array.
Line 869 ⟶ 860:
display_bins[int](limits1, data1)
display_bins[int](limits2, data2)
end start_up</
{{out}}
<pre> -inf - 23 : 11
Line 892 ⟶ 883:
720 - inf : 59
------------------------------------------</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
global limits[] data[] .
#
proc count . .
len cnt[] len limits[] + 1
#
for e in data[]
for i to len limits[]
if e < limits[i]
break 1
.
.
cnt[i] += 1
.
for i to len limits[]
print "< " & limits[i] & " : " & cnt[i]
.
print "Rest: " & cnt[i]
print ""
.
limits[] = [ 23 37 43 53 67 83 ]
data[] = [ 95 21 94 12 99 4 70 75 83 93 52 80 57 5 53 86 65 17 92 83 71 61 54 58 47 16 8 9 32 84 7 87 46 19 30 37 96 6 98 40 79 97 45 64 60 29 49 36 43 55 ]
count
#
limits[] = [ 14 18 249 312 389 392 513 591 634 720 ]
data[] = [ 445 814 519 697 700 130 255 889 481 122 932 77 323 525 570 219 367 523 442 933 416 589 930 373 202 253 775 47 731 685 293 126 133 450 545 100 741 583 763 306 655 267 248 477 549 238 62 678 98 534 622 907 406 714 184 391 913 42 560 247 346 860 56 138 546 38 985 948 58 213 799 319 390 634 458 945 733 507 916 123 345 110 720 917 313 845 426 9 457 628 410 723 354 895 881 953 677 137 397 97 854 740 83 216 421 94 517 479 292 963 376 981 480 39 257 272 157 5 316 395 787 942 456 242 759 898 576 67 298 425 894 435 831 241 989 614 987 770 384 692 698 765 331 487 251 600 879 342 982 527 736 795 585 40 54 901 408 359 577 237 605 847 353 968 832 205 838 427 876 959 686 646 835 127 621 892 443 198 988 791 466 23 707 467 33 670 921 180 991 396 160 436 717 918 8 374 101 684 727 749 ]
count
</syntaxhighlight>
=={{header|Factor}}==
Factor provides the <code>bisect-right</code> word in the <code>sorting.extras</code> vocabulary. See the implementation [https://docs.factorcode.org/content/word-bisect-right%2Csorting.extras.html here].
<
math.statistics sequences sequences.extras sorting.extras ;
Line 942 ⟶ 962:
160 436 717 918 8 374 101 684 727 749
}
{ 14 18 249 312 389 392 513 591 634 720 } .bins</
{{out}}
<pre>
Line 969 ⟶ 989:
=={{header|FreeBASIC}}==
<
dim as uinteger n = ubound(limits), j, i
for i = 0 to ubound(dat)
Line 1,020 ⟶ 1,040:
print ">= ";limits2(i-1);" and < ";limits2(i);": ";bins2(i)
next i
print ">= ";limits2(ubound(limits2));": ";bins2(ubound(bins2))</
{{out}}
<pre>=====EXAMPLE ONE=====
Line 1,043 ⟶ 1,063:
>= 634 and < 720: 16
>= 720: 59</pre>
=={{header|Go}}==
<
import (
Line 1,105 ⟶ 1,124:
printBins(limitsList[i], bins)
}
}</
{{out}}
Line 1,133 ⟶ 1,152:
>= 720 = 59
</pre>
=={{header|Haskell}}==
Splitting the data into bins may be done using the monadic nature of a tuple. Here tuple plays role of the Writer monad, so that sequential partitioning by each bin boundary adds new bin contents.
<
import Data.List (partition)
Line 1,148 ⟶ 1,166:
binCounts :: Ord a => [a] -> [a] -> [Int]
binCounts b = fmap length . binSplit b</
<pre>λ> binSplit [2,4,7] [1,4,2,6,3,8,9,4,1,2,7,4,1,5,1]
Line 1,157 ⟶ 1,175:
More efficient binning procedure exploits the binary search tree.
<
import Data.Foldable (toList)
Line 1,178 ⟶ 1,196:
add x (Node y l r) = if x < y
then Node y (add x l) r
else Node y l (add x r)</
Tasks examples
<
task bs ns = mapM_ putStrLn
Line 1,216 ⟶ 1,234:
, 876, 959, 686, 646, 835, 127, 621, 892, 443, 198, 988, 791, 466, 23
, 707, 467, 33, 670, 921, 180, 991, 396, 160, 436, 717, 918, 8, 374
, 101, 684, 727, 749]</
<pre>λ> task bins1 data1
Line 1,239 ⟶ 1,257:
16 in [634, 720)
59 in [720, ∞)</pre>
=={{header|J}}==
'''Solution:'''
Using <code>Idotr</code> from [[j:User:Brian_Schott/Histogram|this JWiki page]]
<
binnedData=: adverb define
bidx=. i.@>:@# x NB. indicies of bins
Line 1,250 ⟶ 1,267:
require 'format/printf'
printBinCounts=:
counts =. y
'%2d in [ -∞, %3d)' printf ({. counts) , {. x
'%2d in [%3d, %3d)' printf (}.}: counts) ,. 2 ]\ x
'%2d in [%3d, ∞]' printf ({: counts) , {: x
)</
'''Required Examples:'''
<
data1=: , 0&".;._2 noun define
95 21 94 12 99 4 70 75 83 93 52 80 57 5 53 86 65 17 92 83 71 61 54 58 47
Line 1,294 ⟶ 1,311:
6 in [591, 634)
16 in [634, 720)
59 in [720, ∞]</
=={{header|Java}}==
<
import java.util.Collections;
import java.util.List;
Line 1,365 ⟶ 1,382:
printBins(limits, bins(limits, data));
}
}</
{{out}}
<pre>
Line 1,390 ⟶ 1,407:
>= 720 : 59
</pre>
=={{header|jq}}==
The following takes advantage of jq's built-in filter for conducting a binary
Line 1,399 ⟶ 1,415:
indefinitely large number of items to be processed. These items
could, but need not, be presented one line at a time.
<
# .limits holds an array defining the limits, and
# .count[$i] holds the count of bin $i, where bin[0] is the left-most bin
Line 1,416 ⟶ 1,432:
# Main program
reduce inputs as $x ({$limits, count: []}; bin($x))
| pp</
{{out}}
Invocation:
<syntaxhighlight lang="sh">
< data.json jq -rn --argfile limits limits.json -f program.jq
</syntaxhighlight>
Example 1:
<syntaxhighlight lang="text">< 23 => 11
< 37 => 4
< 43 => 2
Line 1,430 ⟶ 1,446:
< 67 => 9
< 83 => 5
>= 83 => 13</
Example 2:
<syntaxhighlight lang="text">< 14 => 3
< 18 => 0
< 249 => 44
Line 1,443 ⟶ 1,459:
< 634 => 6
< 720 => 16
>= 720 => 59</
=={{header|Julia}}==
{{trans|Python}}
<
function bisect_right(array, x, low = 1, high = length(array) + 1)
while low < high
Line 1,500 ⟶ 1,515:
testbins()
</
<pre>
RC FIRST EXAMPLE:
Line 1,524 ⟶ 1,539:
>= 720 := 59
</pre>
=={{header|Lua}}==
Array indexing is 1-based, as is customary for Lua:
<
function binner(limits, data)
local bins = setmetatable({}, {__index=function() return 0 end})
Line 1,569 ⟶ 1,583:
bins = binner(limits, data)
printer(limits, bins)
</syntaxhighlight>
{{out}}
<pre>PART 1:
Line 1,592 ⟶ 1,606:
[634,720) : 16
[720, +∞) : 59</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
data = {95, 21, 94, 12, 99, 4, 70, 75, 83, 93, 52, 80, 57, 5, 53, 86,
65, 17, 92, 83, 71, 61, 54, 58, 47, 16, 8, 9, 32, 84, 7, 87, 46,
Line 1,621 ⟶ 1,634:
limits = {{-\[Infinity]}~Join~limits~Join~{\[Infinity]}};
BinCounts[data, limits]
MapThread[{#2, #1} &, {%, Partition[First[limits], 2, 1]}] // Grid</
{{out}}
<pre>{11, 4, 2, 6, 9, 5, 13}
Line 1,644 ⟶ 1,657:
{634,720} 16
{720,\[Infinity]} 59</pre>
=={{header|Nim}}==
{{trans|Python}}
<
func binIt(limits, data: openArray[int]): seq[Natural] =
Line 1,699 ⟶ 1,711:
160, 436, 717, 918, 8, 374, 101, 684, 727, 749]
let bins2 = binIt(Limits2, Data2)
binPrint(Limits2, bins2)</
{{out}}
Line 1,723 ⟶ 1,735:
>= 634 .. < 720 := 16
>= 720 := 59</pre>
=={{header|Objective-C}}==
<
NSArray<NSNumber *> *bins(NSArray<NSNumber *> *limits, NSArray<NSNumber *> *data) {
Line 1,788 ⟶ 1,799:
}
return 0;
}</
{{out}}
<pre>
Line 1,813 ⟶ 1,824:
>= 720 : 59
</pre>
=={{header|Perl}}==
Borrowed <tt>bisect_right</tt> from Julia entry.
<
use warnings; no warnings 'uninitialized';
use feature 'say';
Line 1,872 ⟶ 1,882:
my @limits = (0, @{$tests[$_]{limits}}, Inf);
say bin_format \@limits, bin_it(\@limits,\@{$tests[$_]{data}});
}</
{{out}}
<pre>[ 0, 23) => 11
Line 1,895 ⟶ 1,905:
But if we were to take to heart the warning that the input data was scary-big, then perhaps using a more efficient routine to classify the data into bins would be prudent (boilerplate/input/output same as above).
<
for (@tests) {
Line 1,904 ⟶ 1,914:
printf "[%3d, %3d) => %3d\n", $lim[$_], ($lim[$_+1] == Inf ? 'Inf' : $lim[$_+1]), $$data_bins[$_] for 0..@lim-2;
print "\n";
}</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">bin_it</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">limits</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">)</span>
Line 1,948 ⟶ 1,957:
<span style="color: #000000;">466</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">23</span><span style="color: #0000FF;">,</span><span style="color: #000000;">707</span><span style="color: #0000FF;">,</span><span style="color: #000000;">467</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">33</span><span style="color: #0000FF;">,</span><span style="color: #000000;">670</span><span style="color: #0000FF;">,</span><span style="color: #000000;">921</span><span style="color: #0000FF;">,</span><span style="color: #000000;">180</span><span style="color: #0000FF;">,</span><span style="color: #000000;">991</span><span style="color: #0000FF;">,</span><span style="color: #000000;">396</span><span style="color: #0000FF;">,</span><span style="color: #000000;">160</span><span style="color: #0000FF;">,</span><span style="color: #000000;">436</span><span style="color: #0000FF;">,</span><span style="color: #000000;">717</span><span style="color: #0000FF;">,</span><span style="color: #000000;">918</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">374</span><span style="color: #0000FF;">,</span><span style="color: #000000;">101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">684</span><span style="color: #0000FF;">,</span><span style="color: #000000;">727</span><span style="color: #0000FF;">,</span><span style="color: #000000;">749</span><span style="color: #0000FF;">}</span>
<span style="color: #000000;">bin_print</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bin_it</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">))</span>
<!--</
{{out}}
<pre>
Line 1,973 ⟶ 1,982:
>= 720 := 59
</pre>
=={{header|Python}}==
This example uses binary search through the limits to assign each number to its bin, via standard module bisect.bisect_right.<br>
The Counter module is ''not'' used as the number of bins is known allowing faster array access for incrementing bin counts versus dict lookup.
<
def bin_it(limits: list, data: list) -> list:
Line 2,015 ⟶ 2,023:
466, 23,707,467, 33,670,921,180,991,396,160,436,717,918, 8,374,101,684,727,749]
bins = bin_it(limits, data)
bin_print(limits, bins)</
{{out}}
Line 2,041 ⟶ 2,049:
>= 634 .. < 720 := 16
>= 720 := 59</pre>
=={{header|Quackery}}==
<syntaxhighlight lang="Quackery"> [ 2dup peek 1+ unrot poke ] is inc ( [ n --> [ )
[ dup size 1+ 0 swap of
swap rot witheach
[ over
findwith [ over > ] drop
swap dip inc ]
drop ] is ->bins ( [ [ --> [ )
[ tuck ->bins
say "Less than "
over 0 peek echo
say ": " behead echo cr
dup size 1 - times
[ over i^ peek echo
say " or more, "
say "but less than "
over i^ 1+ peek echo
say ": "
dup i^ peek echo cr ]
swap
-1 peek echo
say " or more: "
-1 peek echo cr ] is report ( [ [ --> )
' [ 95 21 94 12 99 4 70 75 83 93
52 80 57 5 53 86 65 17 92 83
71 61 54 58 47 16 8 9 32 84
7 87 46 19 30 37 96 6 98 40
79 97 45 64 60 29 49 36 43 55 ]
' [ 23 37 43 53 67 83 ]
say "Part 1" cr cr
report cr cr
' [ 445 814 519 697 700 130 255 889 481 122
932 77 323 525 570 219 367 523 442 933
416 589 930 373 202 253 775 47 731 685
293 126 133 450 545 100 741 583 763 306
655 267 248 477 549 238 62 678 98 534
622 907 406 714 184 391 913 42 560 247
346 860 56 138 546 38 985 948 58 213
799 319 390 634 458 945 733 507 916 123
345 110 720 917 313 845 426 9 457 628
410 723 354 895 881 953 677 137 397 97
854 740 83 216 421 94 517 479 292 963
376 981 480 39 257 272 157 5 316 395
787 942 456 242 759 898 576 67 298 425
894 435 831 241 989 614 987 770 384 692
698 765 331 487 251 600 879 342 982 527
736 795 585 40 54 901 408 359 577 237
605 847 353 968 832 205 838 427 876 959
686 646 835 127 621 892 443 198 988 791
466 23 707 467 33 670 921 180 991 396
160 436 717 918 8 374 101 684 727 749 ]
' [ 14 18 249 312 389 392 513 591 634 720 ]
say "Part 2" cr cr
report</syntaxhighlight>
{{out}}
<pre>Part 1
Less than 23: 11
23 or more, but less than 37: 4
37 or more, but less than 43: 2
43 or more, but less than 53: 6
53 or more, but less than 67: 9
67 or more, but less than 83: 5
83 or more: 13
Part 2
Less than 14: 3
14 or more, but less than 18: 0
18 or more, but less than 249: 44
249 or more, but less than 312: 10
312 or more, but less than 389: 16
389 or more, but less than 392: 2
392 or more, but less than 513: 28
513 or more, but less than 591: 16
591 or more, but less than 634: 6
634 or more, but less than 720: 16
720 or more: 59</pre>
=={{header|R}}==
This is R's bread and butter. Even with only the base library, the only thing stopping us from giving a one-line solution is the task's requirement of using two functions.
Code such as 0:length(limits) is generally considered bad practice, but it didn't cause any problems here. To my amazement, this code works even if limits is of size 0 or 1. Even the <= printing doesn't break!
<
data1 <- c(95,21,94,12,99,4,70,75,83,93,52,80,57,5,53,86,65,17,92,83,71,61,54,58,47,
16,8,9,32,84,7,87,46,19,30,37,96,6,98,40,79,97,45,64,60,29,49,36,43,55)
Line 2,060 ⟶ 2,160:
466,23,707,467,33,670,921,180,991,396,160,436,717,918,8,374,101,684,727,749)
createBin <- function(limits, data) sapply(0:length(limits), function(x) sum(findInterval(data, limits) == x))
#Contains some unicode magic so that we can get the infinity symbol and <= to print nicely.
Line 2,090 ⟶ 2,187:
createBin(limits2, data2)
printBin(limits2, createBin(limits2, data2))
oneLine(limits2, c(data1, data2))#Not needed.</
{{out}}
<pre>> createBin(limits1, data1)
Line 2,132 ⟶ 2,229:
Bin 9 covers the range: 634 ≤ x < 720 and contains 16 elements.
Bin 10 covers the range: 720 ≤ x < ∞ and contains 59 elements.</pre>
=={{header|Racket}}==
<
(define (find-bin-index limits v)
Line 2,176 ⟶ 2,272:
(module+ main
(Bin-given-limits))</
{{out}}
Line 2,198 ⟶ 2,294:
634 <= v < 720 : 16
720 <= v < +inf.0 : 59</pre>
=={{header|Raku}}==
<syntaxhighlight lang="raku" line>
sub bin_it ( @limits, @data ) {
my @ranges = ( -Inf, |@limits, Inf ).rotor( 2 => -1 ).map: { .[0] ..^ .[1] };
Line 2,232 ⟶ 2,327:
say '';
}
</syntaxhighlight>
{{out}}
<pre>
Line 2,255 ⟶ 2,350:
720..^Inf => 59
</pre>
=={{header|REXX}}==
REXX programming note: since the sets of numbers defined don't have any leading signs, no quotes ('''<big>"</big>''') are needed.
<
lims= 23 37 43 53 67 83
data= 95 21 94 12 99 4 70 75 83 93 52 80 57 5 53 86 65 17 92 83 71 61 54 58 47
16 8 9 32 84 7 87 46 19 30 37 96 6 98 40 79 97 45 64 60 29 49 36 43 55
call
call bins data
call show 'the 1st set of bin counts for the specified data:'
Do 3; say''; End
lims= 14 18 249 312 389 392 513 591 634 720
data= 445 814 519 697 700 130 255 889 481 122 932 77 323 525 570 219 367 523 442 933
416 589 930 373 202 253 775 47 731 685 293 126 133 450 545 100 741 583 763 306
655 267 248 477 549 238 62 678 98 534 622 907 406 714 184 391 913 42 560 247
346 860 56 138 546 38 985 948 58 213 799 319 390 634 458 945 733 507 916 123
345 110 720 917 313 845 426 9 457 628 410 723 354 895 881 953 677 137 397 97
854 740 83 216 421 94 517 479 292 963 376 981 480 39 257 272 157 5 316 395
787 942 456 242 759 898 576 67 298 425 894 435 831 241 989 614 987 770 384 692
698 765 331 487 251 600 879 342 982 527 736 795 585 40 54 901 408 359 577 237
605 847 353 968 832 205 838 427 876 959 686 646 835 127 621 892 443 198 988 791
466 23 707 467 33 670 921 180 991 396 160 436 717 918 8 374 101 684 727 749
call
call bins data
call show 'the 2nd set of bin counts for the specified data:'
exit 0
/*------------------------------------------------------------------------------*/
bins:
Parse Arg numbers
count.=0
Do j=1 To words(numbers)
x=word(numbers,j)
Do k=1 To bins-1 Until
End
count.k=count.k+1 /* increment count for this bin */
End
count_length=0
Do k=0 To bins
count_length=max(count_length,length(count.k))
End
Return
/*------------------------------------------------------------------------------*/
limits:
Parse Arg limlist
limit_length=0
bins=words(limlist)+1 /* number of bins */
limit.=''
Do j=1 To bins-1
limit.j=word(limlist,j) /* lower limit of bin j */
limit_length=max(limit_length,length(limit.j)) /* length of largest limit */
End
Return
/*------------------------------------------------------------------------------*/
show:
Say arg(1)
Say copies('-',51)
ll=limit_length
do k=1 To bins
km1=k-1
Select
When k=1 Then
range=' ' right('' ,ll) ' <' right(limit.k,ll)
When k<bins Then
range='>=' right(limit.km1,ll) '.. <' right(limit.k,ll)
Otherwise
range='>=' right(limit.km1,ll) ' ' right('' ,ll)
End
Say ' 'range ' count=' right(count.k,count_length)
End
Return
</syntaxhighlight>
{{out|output|text= when using the internal default input:}}
<pre>
---------------------------------------------------
---------------------------------------------------
</pre>
=={{header|Ring}}==
<
limit = [0, 23, 37, 43, 53, 67, 83]
data = [95,21,94,12,99,4,70,75,83,93,52,80,57,5,53,86,65,17,92,83,71,61,54,58,47,
Line 2,372 ⟶ 2,492:
next
see ">= " + limit[n] + " := " + dn[n] + nl
</syntaxhighlight>
{{out}}
<pre>
Line 2,399 ⟶ 2,519:
>= 720 := 59
</pre>
=={{header|RPL}}==
{{works with|HP|49}}
« → limits data
« { } limits SIZE 1 + + 0 CON
1 data SIZE '''FOR''' j
limits data j GET OVER SIZE NDUPN →LIST - SIGN
'''CASE'''
DUP 0 POS '''THEN''' LASTARG 1 + NIP '''END'''
1 POS '''THEN''' LASTARG '''END'''
DUP SIZE
'''END'''
DUP2 GET 1 + PUT
'''NEXT'''
» » '<span style="color:blue">VBINS</span>' STO
« → limits bins
« 1 bins SIZE 1 GET '''FOR''' j
'''IFERR''' limits j 1 - GET '''THEN''' DROP2 "" '''ELSE''' "≤" + '''END'''
'''IFERR''' limits j GET '''THEN''' DROP2 "x" '''ELSE''' "x<" SWAP + '''END'''
+ ": " b j GET +
'''NEXT'''
» » '<span style="color:blue">SHOWBINS</span>' STO
{23, 37, 43, 53, 67, 83} DUP
{95,21,94,12,99,4,70,75,83,93,52,80,57,5,53,86,65,17,92,83,71,61,54,58,47,16,8,9,32,84,7,87,46,19,30,37,96,6,98,40,79,97,45,64,60,29,49,36,43,55} <span style="color:blue">VBINS SHOWBINS</span>
CLEAR {14,18,249,312,389,392,513,591,634,720} DUP
{445,814,519,697,700,130,255,889,481,122,932,77,323,525,570,219,367,523,442,933,416,589,930,373,202,253,775,47,731,685,293,126,133,450,545,100,741,583,763,306,655,267,248,477,549,238,62,678,98,534,622,907,406,714,184,391,913,42,560,247,346,860,56,138,546,38,985,948,58,213,799,319,390,634,458,945,733,507,916,123,345,110,720,917,313,845,426,9,457,628,410,723,354,895,881,953,677,137,397,97,854,740,83,216,421,94,517,479,292,963,376,981,480,39,257,272,157,5,316,395,787,942,456,242,759,898,576,67,298,425,894,435,831,241,989,614,987,770,384,692,698,765,331,487,251,600,879,342,982,527,736,795,585,40,54,901,408,359,577,237,605,847,353,968,832,205,838,427,876,959,686,646,835,127,621,892,443,198,988,791,466,23,707,467,33,670,921,180,991,396,160,436,717,918,8,374,101,684,727,749} <span style="color:blue">VBINS SHOWBINS</span>
{{out}}
<pre>
7: "x<23: 11"
6: "23≤x<37: 4"
5: "37≤x<43: 2"
4: "43≤x<53: 6"
3: "53≤x<67: 9"
2: "67≤x<83: 5"
1: "83≤x: 13"
11: "x<14: 3"
10: "14≤x<18: 0"
9: "18≤x<249: 44"
8: "249≤x<312: 10"
7: "312≤x<389: 16"
6: "389≤x<392: 2"
5: "392≤x<513: 28"
4: "513≤x<591: 16"
3: "591≤x<634: 6"
2: "634≤x<720: 16"
1: "720≤x: 59"
</pre>
=={{header|Ruby}}==
Perform a binary search on the data to select the limit and keep a tally on that. Uses Ruby 3.0 end-less and begin-less Ranges.
<
tests = Test.new( [23, 37, 43, 53, 67, 83],
[95,21,94,12,99,4,70,75,83,93,52,80,57,5,53,86,65,17,92,83,71,61,54,58,47,
Line 2,431 ⟶ 2,600:
puts
end
</syntaxhighlight>
{{out}}
<pre>...23 11
Line 2,459 ⟶ 2,628:
A very simple and naive algorithm that uses nested dynamic arrays.
<
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
Line 2,515 ⟶ 2,684:
print_bins(&limits2, &bins2);
}
</syntaxhighlight>
{{out}}
Line 2,540 ⟶ 2,709:
>= 634 .. < 720 := 16
>= 720 := 59
</pre>
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
object Bins extends App {
def bins[T](limits: List[T], data: Iterable[T])(implicit ord: Ordering[T]): Array[Int] = {
val result = new Array[Int](limits.size + 1)
for (n <- data) {
val i = limits.search(n)(ord) match {
case scala.collection.Searching.Found(i) => i + 1
case scala.collection.Searching.InsertionPoint(i) => i
}
result(i) += 1
}
result
}
def printBins(limits: List[_], bins: Array[Int]): Unit = {
val n = limits.size
if (n == 0) return
assert(n + 1 == bins.length)
println(f" < ${limits.head}%3s: ${bins(0)}%2d")
for (i <- 1 until n) {
println(f">= ${limits(i - 1)}%3s and < ${limits(i)}%3s: ${bins(i)}%2d")
}
println(f">= ${limits.last}%3s : ${bins(n)}%2d")
}
val limits1 = List(23, 37, 43, 53, 67, 83)
val data1 = List(95, 21, 94, 12, 99, 4, 70, 75, 83, 93, 52, 80, 57, 5, 53, 86, 65, 17, 92, 83, 71, 61, 54, 58, 47, 16, 8, 9, 32, 84, 7, 87, 46, 19, 30, 37, 96, 6, 98, 40, 79, 97, 45, 64, 60, 29, 49, 36, 43, 55)
println("Example 1:")
printBins(limits1, bins(limits1, data1))
val limits2 = List(14, 18, 249, 312, 389, 392, 513, 591, 634, 720)
val data2 = List(445, 814, 519, 697, 700, 130, 255, 889, 481, 122, 932, 77, 323, 525, 570, 219, 367, 523, 442, 933, 416, 589, 930, 373, 202, 253, 775, 47, 731, 685, 293, 126, 133, 450, 545, 100, 741, 583, 763, 306, 655, 267, 248, 477, 549, 238, 62, 678, 98, 534, 622, 907, 406, 714, 184, 391, 913, 42, 560, 247, 346, 860, 56, 138, 546, 38, 985, 948, 58, 213, 799, 319, 390, 634, 458, 945, 733, 507, 916, 123, 345, 110, 720, 917, 313, 845, 426, 9, 457, 628, 410, 723, 354, 895, 881, 953, 677, 137, 397, 97, 854, 740, 83, 216, 421, 94, 517, 479, 292, 963, 376, 981, 480, 39, 257, 272, 157, 5, 316, 395, 787, 942, 456, 242, 759, 898, 576, 67, 298, 425, 894, 435, 831, 241, 989, 614, 987, 770, 384, 692, 698, 765, 331, 487, 251, 600, 879, 342, 982, 527, 736, 795, 585, 40, 54, 901, 408, 359, 577, 237, 605, 847, 353, 968, 832, 205, 838, 427, 876, 959, 686, 646, 835, 127, 621, 892, 443, 198, 988, 791, 466, 23, 707, 467, 33, 670, 921, 180, 991, 396, 160, 436, 717, 918, 8, 374, 101, 684, 727, 749)
println()
println("Example 2:")
printBins(limits2, bins(limits2, data2))
}
</syntaxhighlight>
{{out}}
<pre>
Example 1:
< 23: 11
>= 23 and < 37: 4
>= 37 and < 43: 2
>= 43 and < 53: 6
>= 53 and < 67: 9
>= 67 and < 83: 5
>= 83 : 13
Example 2:
< 14: 3
>= 14 and < 18: 0
>= 18 and < 249: 44
>= 249 and < 312: 10
>= 312 and < 389: 16
>= 389 and < 392: 2
>= 392 and < 513: 28
>= 513 and < 591: 16
>= 591 and < 634: 6
>= 634 and < 720: 16
>= 720 : 59
</pre>
=={{header|Tcl}}==
For Tcl 8.6 (due to <code>lsearch -bisect</code>):
<
# Not necessary but useful helper
Line 2,584 ⟶ 2,823:
605 847 353 968 832 205 838 427 876 959 686 646 835 127 621 892 443 198 988 791
466 23 707 467 33 670 921 180 991 396 160 436 717 918 8 374 101 684 727 749}
print_bins $binlims [distribute_bins $binlims $data]</
{{out}}
Line 2,608 ⟶ 2,847:
720..∞: 59
</pre>
=={{header|Wren}}==
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
var getBins = Fn.new { |limits, data|
Line 2,669 ⟶ 2,907:
var bins = getBins.call(limitsList[i], dataList[i])
printBins.call(limitsList[i], bins)
}</
{{out}}
|