Bin given limits: Difference between revisions

→‎{{header|REXX}}: minor formatting correction
(→‎{{header|Ruby}}: Added intro text)
(→‎{{header|REXX}}: minor formatting correction)
 
(45 intermediate revisions by 24 users not shown)
Line 42:
 
Show output here, on this page.
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F bisect_right(a, x)
V lo = 0
V hi = a.len
L lo < hi
V mid = (lo + hi) I/ 2
I x < a[mid]
hi = mid
E
lo = mid + 1
R lo
 
F bin_it(limits, data)
‘Bin data according to (ascending) limits.’
V bins = [0] * (limits.len + 1)
L(d) data
bins[bisect_right(limits, d)]++
R bins
 
F bin_print(limits, bins)
print(‘ < #3 := #3’.format(limits[0], bins[0]))
L(lo, hi, count) zip(limits, limits[1..], bins[1..])
print(‘>= #3 .. < #3 := #3’.format(lo, hi, count))
print(‘>= #3 := #3’.format(limits.last, bins.last))
 
print("RC FIRST EXAMPLE\n")
V limits = [23, 37, 43, 53, 67, 83]
V 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]
V bins = bin_it(limits, data)
bin_print(limits, bins)
 
print("\nRC SECOND EXAMPLE\n")
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]
bins = bin_it(limits, data)
bin_print(limits, bins)</syntaxhighlight>
 
{{out}}
<pre>
RC FIRST EXAMPLE
 
< 23 := 11
>= 23 .. < 37 := 4
>= 37 .. < 43 := 2
>= 43 .. < 53 := 6
>= 53 .. < 67 := 9
>= 67 .. < 83 := 5
>= 83 := 13
 
RC SECOND EXAMPLE
 
< 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|Action!}}==
<syntaxhighlight lang="action!">DEFINE MAX_BINS="20"
 
PROC Count(INT ARRAY limits INT nLimits INT ARRAY data INT nData INT ARRAY bins)
INT i,j,v
BYTE found
 
FOR i=0 TO nLimits
DO
bins(i)=0
OD
FOR j=0 TO nData-1
DO
v=data(j) found=0
FOR i=0 TO nLimits-1
DO
IF v<limits(i) THEN
bins(i)==+1
found=1
EXIT
FI
OD
IF found=0 THEN
bins(nLimits)==+1
FI
OD
RETURN
 
PROC Test(INT ARRAY limits INT nLimits INT ARRAY data INT nData)
INT ARRAY bins(MAX_BINS)
INT i
 
Count(limits,nLimits,data,nData,bins)
FOR i=0 TO nLimits
DO
IF i=0 THEN
PrintF("<%I",limits(i))
ELSEIF i=nLimits THEN
PrintF(">=%I",limits(i-1))
ELSE
PrintF("%I..%I",limits(i-1),limits(i)-1)
FI
PrintF(": %I%E",bins(i))
OD
RETURN
 
PROC Main()
INT ARRAY
limits1(6)=[23 37 43 53 67 83],
data1(50)=[
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],
limits2(10)=[14 18 249 312 389 392 513 591 634 720],
data2(200)=[
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]
Test(limits1,6,data1,50) PutE()
Test(limits2,10,data2,200)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bin_given_limits.png Screenshot from Atari 8-bit computer]
<pre>
<23: 11
23..36: 4
37..42: 2
43..52: 6
53..66: 9
67..82: 5
>=83: 13
 
<14: 3
14..17: 0
18..248: 44
249..311: 10
312..388: 16
389..391: 2
392..512: 28
513..590: 16
591..633: 6
634..719: 16
>=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:'''
<langsyntaxhighlight Adalang="ada">package binning is
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
Line 55 ⟶ 220:
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array);
end binning;
</syntaxhighlight>
</lang>
'''package body:'''
<langsyntaxhighlight Adalang="ada">pragma Ada_2012;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Line 126 ⟶ 291:
 
end binning;
</syntaxhighlight>
</lang>
'''main procedure:'''
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with binning; use binning;
 
Line 162 ⟶ 327:
Print (Limits => Limits_2, Bin_Result => Bin_2);
end Main;
</syntaxhighlight>
</lang>
{output}
<pre>
Line 187 ⟶ 352:
>= 720 : 59
</pre>
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">BEGIN # count the number pf items that fall into "bins" given he limits #
# returns an array of "bins" containing the counts of the data items #
# that fall into the bins given the limits #
PRIO INTOBINS = 1;
OP INTOBINS = ( []INT data, []INT limits )[]INT:
BEGIN
[ LWB limits : UPB limits + 1 ]INT bins;
FOR bin number FROM LWB bins TO UPB bins DO bins[ bin number ] := 0 OD;
FOR d pos FROM LWB data TO UPB data DO
INT bin number := LWB bins;
INT item = data[ d pos ];
FOR b pos FROM LWB bins TO UPB bins - 1 WHILE item >= limits[ b pos ] DO
bin number +:= 1
OD;
bins[ bin number ] +:= 1
OD;
bins
END # INTOBINS # ;
# shows the limits of the bins and the number of items in each #
PROC show bins = ( []INT limits, []INT bins )VOID:
BEGIN
print( ( " < ", whole( limits[ LWB limits ], -4 )
, ": ", whole( bins[ LWB bins ], -4 )
, newline
)
);
INT bin number := LWB bins + 1;
FOR l pos FROM LWB limits + 1 TO UPB limits DO
print( ( ">= ", whole( limits[ l pos - 1 ], -4 )
, " and < ", whole( limits[ l pos ], -4 )
, ": ", whole( bins[ bin number ], -4 )
, newline
)
);
bin number +:= 1
OD;
print( ( " > ", whole( limits[ UPB limits ], -4 )
, ": ", whole( bins[ UPB bins ], -4 )
, newline
)
)
END # show bins # ;
 
# task test cases #
BEGIN
print( ( "data set 1", newline ) );
[]INT limits =
( 23, 37, 43, 53, 67, 83 );
[]INT 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
);
show bins( limits, data INTOBINS limits )
END;
print( ( newline ) );
BEGIN
print( ( "data set 2", newline ) );
[]INT limits =
( 14, 18, 249, 312, 389, 392, 513, 591, 634, 720 );
[]INT 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
);
show bins( limits, data INTOBINS limits )
END
END</syntaxhighlight>
{{out}}
<pre>
data set 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
 
data set 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|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Bin_given_limits(limits, data){
bin := [], counter := 0
for i, val in data {
Line 205 ⟶ 468:
}
return output .= (prevlimit ? prevlimit : "-∞") ", ∞ : " ((x:=bin["∞"].Count())?x:0) "`n"
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">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]
Line 223 ⟶ 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</langsyntaxhighlight>
{{out}}
<pre>
Line 246 ⟶ 509:
720, ∞ : 59
</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 330 ⟶ 592:
 
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 356 ⟶ 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++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <cassert>
#include <iomanip>
Line 420 ⟶ 749:
std::cout << "\nExample 2:\n";
print_bins(limits2, bins(limits2, data2));
}</langsyntaxhighlight>
 
{{out}}
Line 446 ⟶ 775:
>= 720 : 59
</pre>
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Bin the given data, return an array of counts.
% CLU allows arrays to start at any index; the result array
% will have the same lower bound as the limit array.
 
% The datatype for the limits and data may be any type
=={{header|C sharp}}==
% that allows the < comparator.
<lang csharp>using System;
bin_count = proc [T: type] (limits, data: array[T]) returns (array[int])
where T has lt: proctype (T,T) returns (bool)
ad = array[T] % abbreviations for array types
ai = array[int]
lowbin: int := ad$low(limits)
bins: ai := ai$fill(lowbin, ad$size(limits)+1, 0)
for item: T in ad$elements(data) do
bin: int := lowbin
while bin <= ad$high(limits) do
if item < limits[bin] then break end
bin := bin + 1
end
bins[bin] := bins[bin] + 1
end
return(bins)
end bin_count
 
% Display the bins and the amount of items in each bin.
public class Program
% This imposes the further restriction on the datatype
{
% that it allows `unparse' (may be turned into a string).
static void Main()
display_bins = proc [T: type] (limits, data: array[T])
{
where T has unparse: proctype (T) returns (string),
PrintBins(new [] { 23, 37, 43, 53, 67, 83 },
T has lt: proctype (T,T) returns (bool)
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,
ad = array[T]
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
ai = );array[int]
Console.WriteLine();
po: stream := stream$primary_output()
bins: ai := bin_count[T](limits, data)
for i: int in int$from_to(ad$low(limits), ad$high(limits)+1) do
lo, hi: string
if i-1 < ad$low(limits)
then lo := "-inf"
else lo := T$unparse(limits[i-1])
end
if i > ad$high(limits)
then hi := "inf"
else hi := T$unparse(limits[i])
end
stream$putright(po, lo, 5)
stream$puts(po, " - ")
stream$putright(po, hi, 5)
stream$puts(po, " : ")
stream$putright(po, int$unparse(bins[i]), 5)
stream$putl(po, "")
end
stream$putl(po, "------------------------------------------\n")
end display_bins
 
% Try both example inputs
PrintBins(new [] { 14, 18, 249, 312, 389, 392, 513, 591, 634, 720 },
start_up = proc ()
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,
ai = array[int]
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,
limits1: ai := ai$[23, 37, 43, 53, 67, 83]
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,
data1: ai := ai$
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[95,57621, 6794,29812,42599,8944,43570,83175,24183,98993,61452,98780,77057,3845,69253,69886,76565,33117,48792,25183,60071,87961,34254,98258,52747,
736,795,58516, 408, 549,90132,40884,3597,57787,23746,60519,84730,35337,96896,8326,20598,40,83879,42797,87645,95964,68660,64629,83549,12736,62143,55]
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);
limits2: ai := ai$[14, 18, 249, 312, 389, 392, 513, 591, 634, 720]
}
data2: ai := ai$
[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]
 
display_bins[int](limits1, data1)
static void PrintBins(int[] limits, params int[] data)
display_bins[int](limits2, data2)
{
end start_up</syntaxhighlight>
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;
}
}</lang>
{{out}}
<pre> -inf - 23 : 11
<pre>
-∞ .. 23 =>- 37 : 114
23 .. 37 =>- 43 : 42
37 .. 43 =>- 53 : 26
43 .. 53 =>- 67 : 69
53 .. 67 =>- 83 : 95
67 .. 83 =>- inf : 513
------------------------------------------
83 .. ∞ => 13
 
-inf - .. 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 ..- inf : => 59
------------------------------------------</pre>
</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].
<langsyntaxhighlight lang="factor">USING: assocs formatting grouping io kernel math math.parser
math.statistics sequences sequences.extras sorting.extras ;
 
Line 565 ⟶ 962:
160 436 717 918 8 374 101 684 727 749
}
{ 14 18 249 312 389 392 513 591 634 720 } .bins</langsyntaxhighlight>
{{out}}
<pre>
Line 591 ⟶ 988:
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">sub binlims( dat() as integer, limits() as integer, bins() as uinteger )
dim as uinteger n = ubound(limits), j, i
for i = 0 to ubound(dat)
if dat(i)<limits(0) then
bins(0) += 1
elseif dat(i) >= limits(n) then
bins(n+1) += 1
else
for j = 1 to n
if dat(i)<limits(j) then
bins(j) += 1
exit for
end if
next j
end if
next i
end sub
'example 1
dim as integer limits1(0 to ...) = {23, 37, 43, 53, 67, 83}
dim as integer dat1(0 to ...) = {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}
dim as uinteger bins1(0 to ubound(limits1)+1)
binlims( dat1(), limits1(), bins1() )
print "=====EXAMPLE ONE====="
print "< ";limits1(0);": ";bins1(0)
for i as uinteger = 1 to ubound(limits1)
print ">= ";limits1(i-1);" and < ";limits1(i);": ";bins1(i)
next i
print ">= ";limits1(ubound(limits1));": ";bins1(ubound(bins1))
print
 
'example 2
dim as integer limits2(0 to ...) = {14, 18, 249, 312, 389, 392, 513, 591, 634, 720}
dim as integer dat2(0 to ...) = {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}
redim as uinteger bins2(0 to ubound(limits2)+1)
 
binlims( dat2(), limits2(), bins2() )
print "=====EXAMPLE TWO====="
print "< ";limits2(0);": ";bins2(0)
for i as uinteger = 1 to ubound(limits2)
print ">= ";limits2(i-1);" and < ";limits2(i);": ";bins2(i)
next i
print ">= ";limits2(ubound(limits2));": ";bins2(ubound(bins2))</syntaxhighlight>
{{out}}
<pre>=====EXAMPLE ONE=====
< 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 TWO=====
< 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|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 652 ⟶ 1,124:
printBins(limitsList[i], bins)
}
}</langsyntaxhighlight>
 
{{out}}
Line 680 ⟶ 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.
 
<syntaxhighlight lang="haskell">import Control.Monad (foldM)
import Data.List (partition)
 
binSplit :: Ord a => [a] -> [a] -> [[a]]
binSplit lims ns = counts ++ [rest]
where
(counts, rest) = foldM split ns lims
split l i = let (a, b) = partition (< i) l in ([a], b)
 
binCounts :: Ord a => [a] -> [a] -> [Int]
binCounts b = fmap length . binSplit b</syntaxhighlight>
 
<pre>λ> binSplit [2,4,7] [1,4,2,6,3,8,9,4,1,2,7,4,1,5,1]
[[1,1,1,1],[2,3,2],[4,6,4,4,5],[8,9,7]]
 
λ> binCounts [2,4,7] [1,4,2,6,3,8,9,4,1,2,7,4,1,5,1]
[4,3,5,3]</pre>
 
More efficient binning procedure exploits the binary search tree.
<syntaxhighlight lang="haskell">{-# language DeriveFoldable #-}
 
import Data.Foldable (toList)
 
data BTree a b = Node a (BTree a b) (BTree a b)
| Val b
deriving Foldable
 
-- assuming list is sorted.
mkTree :: [a] -> BTree a [a]
mkTree [] = Val []
mkTree [x] = Node x (Val []) (Val [])
mkTree lst = Node x (mkTree l) (mkTree r)
where (l, x:r) = splitAt (length lst `div` 2) lst
 
binSplit :: Ord a => [a] -> [a] -> [[a]]
binSplit lims = toList . foldr add (mkTree lims)
where
add x (Val v) = Val (x:v)
add x (Node y l r) = if x < y
then Node y (add x l) r
else Node y l (add x r)</syntaxhighlight>
 
Tasks examples
 
<syntaxhighlight lang="haskell">import Text.Printf
 
task bs ns = mapM_ putStrLn
$ zipWith mkLine (binCounts bs ns) bins
where
bins :: [String]
bins = [printf "(-∞, %v)" $ head bs] <>
zipWith mkInterval bs (tail bs) <>
[printf "[%v, ∞)" $ last bs]
 
mkLine = printf "%v\t in %s"
mkInterval = printf "[%v, %v)"
 
bins1 = [23, 37, 43, 53, 67, 83]
data1 = [ 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]
 
bins2 = [14, 18, 249, 312, 389, 392, 513, 591, 634, 720]
data2 = [ 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]</syntaxhighlight>
 
<pre>λ> task bins1 data1
11 in (-∞, 23)
4 in [23, 37)
2 in [37, 43)
6 in [43, 53)
9 in [53, 67)
5 in [67, 83)
13 in [83, ∞)
 
λ> task bins2 data2
3 in (-∞, 14)
0 in [14, 18)
44 in [18, 249)
10 in [249, 312)
16 in [312, 389)
2 in [389, 392)
28 in [392, 513)
16 in [513, 591)
6 in [591, 634)
16 in [634, 720)
59 in [720, ∞)</pre>
=={{header|J}}==
'''Solution:'''
Using <code>Idotr</code> from [[j:User:Brian_Schott/Histogram|this JWiki page]]
<lang j>binData=: |.@((] (I. <@}./. ]) [ ,~ ] , <:@:<./@]) |.)~
<syntaxhighlight lang="j">Idotr=: |.@[ (#@[ - I.) ] NB. reverses order of limits to obtain intervals closed on left, open on right (>= y <)
countBins=: #&>
binnedData=: adverb define
bidx=. i.@>:@# x NB. indicies of bins
x (Idotr (u@}./.)&(bidx&,) ]) y NB. apply u to data in each bin after dropping first value
)
 
require 'format/printf'
printBinCounts=: verbdyad define
counts =. countBins y
'%2d in [ <-∞, %3d = %2d)' printf ({. xcounts) , {. countsx
'>= %3d2d and < in [%3d =, %2d3d)' printf (2 ]\}.}: xcounts) ,. }.}:2 counts]\ x
'>= %3d2d in [%3d, = %2d∞]' printf ({: xcounts) , {: countsx
)</syntaxhighlight>
)
 
NB. equivalent explicit version of binData above.
binDataX =: verb define
lim =. |. x NB. reverse order of limits if < >= (not required if <= >)
lastbinval=. <: <./lim NB. >: >./ lim (if <= >)
limdat =. lim , lastbinval , y NB. prepend data with a value for each bin. Orders the bins and ensures all are represented in result
|. lim (I. <@}./. ]) limdat NB. box data in bins, dropping first value from each bin, reverse order of bins
)</lang>
 
'''Required Examples:'''
<langsyntaxhighlight lang="j">limits1=: 23 37 43 53 67 83
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 722 ⟶ 1,294:
466 23 707 467 33 670 921 180 991 396 160 436 717 918 8 374 101 684 727 749
)
limits1 binData< binnedData data1 NB. box/group binned data
┌──────────────────────────┬───────────┬─────┬─────────────────┬──────────────────────────┬──────────────┬──────────────────────────────────────┐
│21 12 4 5 17 16 8 9 7 19 6│32 30 29 36│37 40│52 47 46 45 49 43│57 53 65 61 54 58 64 60 55│70 75 80 71 79│95 94 99 83 93 86 92 83 84 87 96 98 97│
└──────────────────────────┴───────────┴─────┴─────────────────┴──────────────────────────┴──────────────┴──────────────────────────────────────┘
countBins limits1 binData# binnedData data1 NB. tally binned data
11 4 2 6 9 5 13
limits2 printBinCounts limits2 binData# binnedData data2
3 in [ <-∞, 14 = 3)
>= 0 14 andin <[ 14, 18 = 0)
>=44 18 andin <[ 18, 249 = 44)
>=10 249 and <in [249, 312 = 10)
>=16 312 and <in [312, 389 = 16)
>= 2 389 and <in [389, 392 = 2)
>=28 392 and <in [392, 513 = 28)
>=16 513 and <in [513, 591 = 16)
>= 6 591 and <in [591, 634 = 6)
>=16 634 and <in [634, 720 = 16)
59 in [720, ∞]</syntaxhighlight>
>= 720 = 59</lang>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Line 810 ⟶ 1,382:
printBins(limits, bins(limits, data));
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 835 ⟶ 1,407:
>= 720 : 59
</pre>
=={{header|jq}}==
The following takes advantage of jq's built-in filter for conducting a binary
search, `bsearch/1`, which returns a negative value giving the insertion point
if the item is not already in the input array.
 
The "data" is assumed to be a stream of values (rather than an array), thus allowing an
indefinitely large number of items to be processed. These items
could, but need not, be presented one line at a time.
<syntaxhighlight lang="jq"># input and output: {limits, count} where
# .limits holds an array defining the limits, and
# .count[$i] holds the count of bin $i, where bin[0] is the left-most bin
def bin($x):
(.limits | bsearch($x)) as $ix
| (if $ix > -1 then $ix + 1 else -1 - $ix end) as $i
| .count[$i] += 1;
 
# pretty-print for the structure defined at bin/1
def pp:
(.limits|length) as $length
| (range(0;$length) as $i
| "< \(.limits[$i]) => \(.count[$i] // 0)" ),
">= \(.limits[$length-1] ) => \(.count[$length] // 0)" ;
 
# Main program
reduce inputs as $x ({$limits, count: []}; bin($x))
| pp</syntaxhighlight>
{{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
< 53 => 6
< 67 => 9
< 83 => 5
>= 83 => 13</syntaxhighlight>
 
Example 2:
<syntaxhighlight lang="text">< 14 => 3
< 18 => 0
< 249 => 44
< 312 => 10
< 389 => 16
< 392 => 2
< 513 => 28
< 591 => 16
< 634 => 6
< 720 => 16
>= 720 => 59</syntaxhighlight>
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang="julia">"""Add the function Python has in its bisect library"""
function bisect_right(array, x, low = 1, high = length(array) + 1)
while low < high
Line 891 ⟶ 1,515:
 
testbins()
</langsyntaxhighlight>{{out}}
<pre>
RC FIRST EXAMPLE:
Line 915 ⟶ 1,539:
>= 720 := 59
</pre>
=={{header|Lua}}==
Array indexing is 1-based, as is customary for Lua:
<syntaxhighlight lang="lua">
function binner(limits, data)
local bins = setmetatable({}, {__index=function() return 0 end})
local n, flr = #limits+1, math.floor
for _, x in ipairs(data) do
local lo, hi = 1, n
while lo < hi do
local mid = flr((lo + hi) / 2)
if not limits[mid] or x < limits[mid] then hi=mid else lo=mid+1 end
end
bins[lo] = bins[lo] + 1
end
return bins
end
 
function printer(limits, bins)
for i = 1, #limits+1 do
print(string.format("[%3s,%3s) : %d", limits[i-1] or " -∞", limits[i] or " +∞", bins[i]))
end
end
 
print("PART 1:")
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}
bins = binner(limits, data)
printer(limits, bins)
 
print("\nPART 2:")
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}
bins = binner(limits, data)
printer(limits, bins)
</syntaxhighlight>
{{out}}
<pre>PART 1:
[ -∞, 23) : 11
[ 23, 37) : 4
[ 37, 43) : 2
[ 43, 53) : 6
[ 53, 67) : 9
[ 67, 83) : 5
[ 83, +∞) : 13
 
PART 2:
[ -∞, 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|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">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};
limits = {{-\[Infinity]}~Join~limits~Join~{\[Infinity]}};
BinCounts[data, limits]
MapThread[{#2, #1} &, {%, Partition[First[limits], 2, 1]}] // Grid
 
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};
limits = {{-\[Infinity]}~Join~limits~Join~{\[Infinity]}};
BinCounts[data, limits]
MapThread[{#2, #1} &, {%, Partition[First[limits], 2, 1]}] // Grid</syntaxhighlight>
{{out}}
<pre>{11, 4, 2, 6, 9, 5, 13}
{-\[Infinity],23} 11
{23,37} 4
{37,43} 2
{43,53} 6
{53,67} 9
{67,83} 5
{83,\[Infinity]} 13
 
{3, 0, 44, 10, 16, 2, 28, 16, 6, 16, 59}
{-\[Infinity],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,\[Infinity]} 59</pre>
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight Nimlang="nim">import algorithm, strformat
 
func binIt(limits, data: openArray[int]): seq[Natural] =
Line 970 ⟶ 1,711:
160, 436, 717, 918, 8, 374, 101, 684, 727, 749]
let bins2 = binIt(Limits2, Data2)
binPrint(Limits2, bins2)</langsyntaxhighlight>
 
{{out}}
Line 994 ⟶ 1,735:
>= 634 .. < 720 := 16
>= 720 := 59</pre>
 
=={{header|Objective-C}}==
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
NSArray<NSNumber *> *bins(NSArray<NSNumber *> *limits, NSArray<NSNumber *> *data) {
Line 1,059 ⟶ 1,799:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,084 ⟶ 1,824:
>= 720 : 59
</pre>
 
=={{header|Perl}}==
Borrowed <tt>bisect_right</tt> from Julia entry.
<langsyntaxhighlight lang="perl">use strict;
use warnings; no warnings 'uninitialized';
use feature 'say';
Line 1,143 ⟶ 1,882:
my @limits = (0, @{$tests[$_]{limits}}, Inf);
say bin_format \@limits, bin_it(\@limits,\@{$tests[$_]{data}});
}</langsyntaxhighlight>
{{out}}
<pre>[ 0, 23) => 11
Line 1,166 ⟶ 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).
<langsyntaxhighlight lang="perl">use Math::SimpleHisto::XS;
 
for (@tests) {
Line 1,175 ⟶ 1,914:
printf "[%3d, %3d) => %3d\n", $lim[$_], ($lim[$_+1] == Inf ? 'Inf' : $lim[$_+1]), $$data_bins[$_] for 0..@lim-2;
print "\n";
}</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function bin_it(sequence limits, data)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
-- Bin data according to (ascending) limits.
<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>
sequence bins = repeat(0,length(limits)+1) -- adds under/over range bins too
<span style="color: #000080;font-style:italic;">-- Bin data according to (ascending) limits.</span>
for i=1 to length(data) do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">bins</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- adds under/over range bins too</span>
integer bdx = binary_search(data[i],limits)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
bins[abs(bdx)+(bdx>0)] += 1
<span style="color: #004080;">integer</span> <span style="color: #000000;">bdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">binary_search</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #000000;">bdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bdx</span><span style="color: #0000FF;">)+(</span><span style="color: #000000;">bdx</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
return bins
<span style="color: #000000;">bins</span><span style="color: #0000FF;">[</span><span style="color: #000000;">bdx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">bins</span>
procedure bin_print(sequence limits, bins)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
printf(1," < %3d := %3d\n",{limits[1],bins[1]})
for i=2 to length(limits) do
<span style="color: #008080;">procedure</span> <span style="color: #000000;">bin_print</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;">bins</span><span style="color: #0000FF;">)</span>
printf(1,">= %3d and < %3d := %3d\n",{limits[i-1],limits[i],bins[i]})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" &lt; %3d := %3d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">bins</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]})</span>
end for
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
printf(1,">= %3d := %3d\n\n",{limits[$],bins[$]})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&gt;= %3d and &lt; %3d := %3d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">bins</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]})</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&gt;= %3d := %3d\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">[$],</span><span style="color: #000000;">bins</span><span style="color: #0000FF;">[$]})</span>
sequence limits, data
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
printf(1,"Example 1:\n")
limits = {23, 37, 43, 53, 67, 83}
<span style="color: #004080;">sequence</span> <span style="color: #000000;">limits</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">data</span>
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,
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Example 1:\n"</span><span style="color: #0000FF;">)</span>
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: #000000;">limits</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">23</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">37</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">43</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">53</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">67</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">83</span><span style="color: #0000FF;">}</span>
bin_print(limits, bin_it(limits, data))
<span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">95</span><span style="color: #0000FF;">,</span><span style="color: #000000;">21</span><span style="color: #0000FF;">,</span><span style="color: #000000;">94</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">99</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">70</span><span style="color: #0000FF;">,</span><span style="color: #000000;">75</span><span style="color: #0000FF;">,</span><span style="color: #000000;">83</span><span style="color: #0000FF;">,</span><span style="color: #000000;">93</span><span style="color: #0000FF;">,</span><span style="color: #000000;">52</span><span style="color: #0000FF;">,</span><span style="color: #000000;">80</span><span style="color: #0000FF;">,</span><span style="color: #000000;">57</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">53</span><span style="color: #0000FF;">,</span><span style="color: #000000;">86</span><span style="color: #0000FF;">,</span><span style="color: #000000;">65</span><span style="color: #0000FF;">,</span><span style="color: #000000;">17</span><span style="color: #0000FF;">,</span><span style="color: #000000;">92</span><span style="color: #0000FF;">,</span><span style="color: #000000;">83</span><span style="color: #0000FF;">,</span><span style="color: #000000;">71</span><span style="color: #0000FF;">,</span><span style="color: #000000;">61</span><span style="color: #0000FF;">,</span><span style="color: #000000;">54</span><span style="color: #0000FF;">,</span><span style="color: #000000;">58</span><span style="color: #0000FF;">,</span><span style="color: #000000;">47</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">16</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">32</span><span style="color: #0000FF;">,</span><span style="color: #000000;">84</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">87</span><span style="color: #0000FF;">,</span><span style="color: #000000;">46</span><span style="color: #0000FF;">,</span><span style="color: #000000;">19</span><span style="color: #0000FF;">,</span><span style="color: #000000;">30</span><span style="color: #0000FF;">,</span><span style="color: #000000;">37</span><span style="color: #0000FF;">,</span><span style="color: #000000;">96</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">98</span><span style="color: #0000FF;">,</span><span style="color: #000000;">40</span><span style="color: #0000FF;">,</span><span style="color: #000000;">79</span><span style="color: #0000FF;">,</span><span style="color: #000000;">97</span><span style="color: #0000FF;">,</span><span style="color: #000000;">45</span><span style="color: #0000FF;">,</span><span style="color: #000000;">64</span><span style="color: #0000FF;">,</span><span style="color: #000000;">60</span><span style="color: #0000FF;">,</span><span style="color: #000000;">29</span><span style="color: #0000FF;">,</span><span style="color: #000000;">49</span><span style="color: #0000FF;">,</span><span style="color: #000000;">36</span><span style="color: #0000FF;">,</span><span style="color: #000000;">43</span><span style="color: #0000FF;">,</span><span style="color: #000000;">55</span><span style="color: #0000FF;">}</span>
printf(1,"Example 2:\n")
<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>
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,
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Example 2:\n"</span><span style="color: #0000FF;">)</span>
416,589,930,373,202,253,775, 47,731,685,293,126,133,450,545,100,741,583,763,306,
<span style="color: #000000;">limits</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">14</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">18</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">249</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">312</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">389</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">392</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">513</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">591</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">634</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">720</span><span style="color: #0000FF;">}</span>
655,267,248,477,549,238, 62,678, 98,534,622,907,406,714,184,391,913, 42,560,247,
<span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">445</span><span style="color: #0000FF;">,</span><span style="color: #000000;">814</span><span style="color: #0000FF;">,</span><span style="color: #000000;">519</span><span style="color: #0000FF;">,</span><span style="color: #000000;">697</span><span style="color: #0000FF;">,</span><span style="color: #000000;">700</span><span style="color: #0000FF;">,</span><span style="color: #000000;">130</span><span style="color: #0000FF;">,</span><span style="color: #000000;">255</span><span style="color: #0000FF;">,</span><span style="color: #000000;">889</span><span style="color: #0000FF;">,</span><span style="color: #000000;">481</span><span style="color: #0000FF;">,</span><span style="color: #000000;">122</span><span style="color: #0000FF;">,</span><span style="color: #000000;">932</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">77</span><span style="color: #0000FF;">,</span><span style="color: #000000;">323</span><span style="color: #0000FF;">,</span><span style="color: #000000;">525</span><span style="color: #0000FF;">,</span><span style="color: #000000;">570</span><span style="color: #0000FF;">,</span><span style="color: #000000;">219</span><span style="color: #0000FF;">,</span><span style="color: #000000;">367</span><span style="color: #0000FF;">,</span><span style="color: #000000;">523</span><span style="color: #0000FF;">,</span><span style="color: #000000;">442</span><span style="color: #0000FF;">,</span><span style="color: #000000;">933</span><span style="color: #0000FF;">,</span>
346,860, 56,138,546, 38,985,948, 58,213,799,319,390,634,458,945,733,507,916,123,
<span style="color: #000000;">416</span><span style="color: #0000FF;">,</span><span style="color: #000000;">589</span><span style="color: #0000FF;">,</span><span style="color: #000000;">930</span><span style="color: #0000FF;">,</span><span style="color: #000000;">373</span><span style="color: #0000FF;">,</span><span style="color: #000000;">202</span><span style="color: #0000FF;">,</span><span style="color: #000000;">253</span><span style="color: #0000FF;">,</span><span style="color: #000000;">775</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">47</span><span style="color: #0000FF;">,</span><span style="color: #000000;">731</span><span style="color: #0000FF;">,</span><span style="color: #000000;">685</span><span style="color: #0000FF;">,</span><span style="color: #000000;">293</span><span style="color: #0000FF;">,</span><span style="color: #000000;">126</span><span style="color: #0000FF;">,</span><span style="color: #000000;">133</span><span style="color: #0000FF;">,</span><span style="color: #000000;">450</span><span style="color: #0000FF;">,</span><span style="color: #000000;">545</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">741</span><span style="color: #0000FF;">,</span><span style="color: #000000;">583</span><span style="color: #0000FF;">,</span><span style="color: #000000;">763</span><span style="color: #0000FF;">,</span><span style="color: #000000;">306</span><span style="color: #0000FF;">,</span>
345,110,720,917,313,845,426, 9,457,628,410,723,354,895,881,953,677,137,397, 97,
<span style="color: #000000;">655</span><span style="color: #0000FF;">,</span><span style="color: #000000;">267</span><span style="color: #0000FF;">,</span><span style="color: #000000;">248</span><span style="color: #0000FF;">,</span><span style="color: #000000;">477</span><span style="color: #0000FF;">,</span><span style="color: #000000;">549</span><span style="color: #0000FF;">,</span><span style="color: #000000;">238</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">62</span><span style="color: #0000FF;">,</span><span style="color: #000000;">678</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">98</span><span style="color: #0000FF;">,</span><span style="color: #000000;">534</span><span style="color: #0000FF;">,</span><span style="color: #000000;">622</span><span style="color: #0000FF;">,</span><span style="color: #000000;">907</span><span style="color: #0000FF;">,</span><span style="color: #000000;">406</span><span style="color: #0000FF;">,</span><span style="color: #000000;">714</span><span style="color: #0000FF;">,</span><span style="color: #000000;">184</span><span style="color: #0000FF;">,</span><span style="color: #000000;">391</span><span style="color: #0000FF;">,</span><span style="color: #000000;">913</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">42</span><span style="color: #0000FF;">,</span><span style="color: #000000;">560</span><span style="color: #0000FF;">,</span><span style="color: #000000;">247</span><span style="color: #0000FF;">,</span>
854,740, 83,216,421, 94,517,479,292,963,376,981,480, 39,257,272,157, 5,316,395,
<span style="color: #000000;">346</span><span style="color: #0000FF;">,</span><span style="color: #000000;">860</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">56</span><span style="color: #0000FF;">,</span><span style="color: #000000;">138</span><span style="color: #0000FF;">,</span><span style="color: #000000;">546</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">38</span><span style="color: #0000FF;">,</span><span style="color: #000000;">985</span><span style="color: #0000FF;">,</span><span style="color: #000000;">948</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">58</span><span style="color: #0000FF;">,</span><span style="color: #000000;">213</span><span style="color: #0000FF;">,</span><span style="color: #000000;">799</span><span style="color: #0000FF;">,</span><span style="color: #000000;">319</span><span style="color: #0000FF;">,</span><span style="color: #000000;">390</span><span style="color: #0000FF;">,</span><span style="color: #000000;">634</span><span style="color: #0000FF;">,</span><span style="color: #000000;">458</span><span style="color: #0000FF;">,</span><span style="color: #000000;">945</span><span style="color: #0000FF;">,</span><span style="color: #000000;">733</span><span style="color: #0000FF;">,</span><span style="color: #000000;">507</span><span style="color: #0000FF;">,</span><span style="color: #000000;">916</span><span style="color: #0000FF;">,</span><span style="color: #000000;">123</span><span style="color: #0000FF;">,</span>
787,942,456,242,759,898,576, 67,298,425,894,435,831,241,989,614,987,770,384,692,
<span style="color: #000000;">345</span><span style="color: #0000FF;">,</span><span style="color: #000000;">110</span><span style="color: #0000FF;">,</span><span style="color: #000000;">720</span><span style="color: #0000FF;">,</span><span style="color: #000000;">917</span><span style="color: #0000FF;">,</span><span style="color: #000000;">313</span><span style="color: #0000FF;">,</span><span style="color: #000000;">845</span><span style="color: #0000FF;">,</span><span style="color: #000000;">426</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">457</span><span style="color: #0000FF;">,</span><span style="color: #000000;">628</span><span style="color: #0000FF;">,</span><span style="color: #000000;">410</span><span style="color: #0000FF;">,</span><span style="color: #000000;">723</span><span style="color: #0000FF;">,</span><span style="color: #000000;">354</span><span style="color: #0000FF;">,</span><span style="color: #000000;">895</span><span style="color: #0000FF;">,</span><span style="color: #000000;">881</span><span style="color: #0000FF;">,</span><span style="color: #000000;">953</span><span style="color: #0000FF;">,</span><span style="color: #000000;">677</span><span style="color: #0000FF;">,</span><span style="color: #000000;">137</span><span style="color: #0000FF;">,</span><span style="color: #000000;">397</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">97</span><span style="color: #0000FF;">,</span>
698,765,331,487,251,600,879,342,982,527,736,795,585, 40, 54,901,408,359,577,237,
<span style="color: #000000;">854</span><span style="color: #0000FF;">,</span><span style="color: #000000;">740</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">83</span><span style="color: #0000FF;">,</span><span style="color: #000000;">216</span><span style="color: #0000FF;">,</span><span style="color: #000000;">421</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">94</span><span style="color: #0000FF;">,</span><span style="color: #000000;">517</span><span style="color: #0000FF;">,</span><span style="color: #000000;">479</span><span style="color: #0000FF;">,</span><span style="color: #000000;">292</span><span style="color: #0000FF;">,</span><span style="color: #000000;">963</span><span style="color: #0000FF;">,</span><span style="color: #000000;">376</span><span style="color: #0000FF;">,</span><span style="color: #000000;">981</span><span style="color: #0000FF;">,</span><span style="color: #000000;">480</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">39</span><span style="color: #0000FF;">,</span><span style="color: #000000;">257</span><span style="color: #0000FF;">,</span><span style="color: #000000;">272</span><span style="color: #0000FF;">,</span><span style="color: #000000;">157</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">316</span><span style="color: #0000FF;">,</span><span style="color: #000000;">395</span><span style="color: #0000FF;">,</span>
605,847,353,968,832,205,838,427,876,959,686,646,835,127,621,892,443,198,988,791,
<span style="color: #000000;">787</span><span style="color: #0000FF;">,</span><span style="color: #000000;">942</span><span style="color: #0000FF;">,</span><span style="color: #000000;">456</span><span style="color: #0000FF;">,</span><span style="color: #000000;">242</span><span style="color: #0000FF;">,</span><span style="color: #000000;">759</span><span style="color: #0000FF;">,</span><span style="color: #000000;">898</span><span style="color: #0000FF;">,</span><span style="color: #000000;">576</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">67</span><span style="color: #0000FF;">,</span><span style="color: #000000;">298</span><span style="color: #0000FF;">,</span><span style="color: #000000;">425</span><span style="color: #0000FF;">,</span><span style="color: #000000;">894</span><span style="color: #0000FF;">,</span><span style="color: #000000;">435</span><span style="color: #0000FF;">,</span><span style="color: #000000;">831</span><span style="color: #0000FF;">,</span><span style="color: #000000;">241</span><span style="color: #0000FF;">,</span><span style="color: #000000;">989</span><span style="color: #0000FF;">,</span><span style="color: #000000;">614</span><span style="color: #0000FF;">,</span><span style="color: #000000;">987</span><span style="color: #0000FF;">,</span><span style="color: #000000;">770</span><span style="color: #0000FF;">,</span><span style="color: #000000;">384</span><span style="color: #0000FF;">,</span><span style="color: #000000;">692</span><span style="color: #0000FF;">,</span>
466, 23,707,467, 33,670,921,180,991,396,160,436,717,918, 8,374,101,684,727,749}
<span style="color: #000000;">698</span><span style="color: #0000FF;">,</span><span style="color: #000000;">765</span><span style="color: #0000FF;">,</span><span style="color: #000000;">331</span><span style="color: #0000FF;">,</span><span style="color: #000000;">487</span><span style="color: #0000FF;">,</span><span style="color: #000000;">251</span><span style="color: #0000FF;">,</span><span style="color: #000000;">600</span><span style="color: #0000FF;">,</span><span style="color: #000000;">879</span><span style="color: #0000FF;">,</span><span style="color: #000000;">342</span><span style="color: #0000FF;">,</span><span style="color: #000000;">982</span><span style="color: #0000FF;">,</span><span style="color: #000000;">527</span><span style="color: #0000FF;">,</span><span style="color: #000000;">736</span><span style="color: #0000FF;">,</span><span style="color: #000000;">795</span><span style="color: #0000FF;">,</span><span style="color: #000000;">585</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">40</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">54</span><span style="color: #0000FF;">,</span><span style="color: #000000;">901</span><span style="color: #0000FF;">,</span><span style="color: #000000;">408</span><span style="color: #0000FF;">,</span><span style="color: #000000;">359</span><span style="color: #0000FF;">,</span><span style="color: #000000;">577</span><span style="color: #0000FF;">,</span><span style="color: #000000;">237</span><span style="color: #0000FF;">,</span>
bin_print(limits, bin_it(limits, data))</lang>
<span style="color: #000000;">605</span><span style="color: #0000FF;">,</span><span style="color: #000000;">847</span><span style="color: #0000FF;">,</span><span style="color: #000000;">353</span><span style="color: #0000FF;">,</span><span style="color: #000000;">968</span><span style="color: #0000FF;">,</span><span style="color: #000000;">832</span><span style="color: #0000FF;">,</span><span style="color: #000000;">205</span><span style="color: #0000FF;">,</span><span style="color: #000000;">838</span><span style="color: #0000FF;">,</span><span style="color: #000000;">427</span><span style="color: #0000FF;">,</span><span style="color: #000000;">876</span><span style="color: #0000FF;">,</span><span style="color: #000000;">959</span><span style="color: #0000FF;">,</span><span style="color: #000000;">686</span><span style="color: #0000FF;">,</span><span style="color: #000000;">646</span><span style="color: #0000FF;">,</span><span style="color: #000000;">835</span><span style="color: #0000FF;">,</span><span style="color: #000000;">127</span><span style="color: #0000FF;">,</span><span style="color: #000000;">621</span><span style="color: #0000FF;">,</span><span style="color: #000000;">892</span><span style="color: #0000FF;">,</span><span style="color: #000000;">443</span><span style="color: #0000FF;">,</span><span style="color: #000000;">198</span><span style="color: #0000FF;">,</span><span style="color: #000000;">988</span><span style="color: #0000FF;">,</span><span style="color: #000000;">791</span><span style="color: #0000FF;">,</span>
<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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,240 ⟶ 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.
 
<langsyntaxhighlight lang="python">from bisect import bisect_right
 
def bin_it(limits: list, data: list) -> list:
Line 1,282 ⟶ 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)</langsyntaxhighlight>
 
{{out}}
Line 1,308 ⟶ 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!
<langsyntaxhighlight rlang="rsplus">limits1 <- c(23, 37, 43, 53, 67, 83)
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)
limits2 <- c(14, 18, 249, 312, 389, 392, 513, 591, 634, 720)
data2 <- c(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)
 
createBin <- function(limits, data) sapply(0:length(limits), function(x) sum(findInterval(data, limits) == x))
{
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.
#Half of the battle here is making sure that we're not being thrown by R being 1-indexed.
#The other half is avoiding the mathematical sin of saying that anything can be >=infinity.
printBin <- function(limits, bin)
{
invisible(sapply(0:length(limits), function(x) cat("Bin", x, "covers the range:",
if(x == 0){ "-\U221E < x <"} else( paste(limits[x], "\U2264 x <")),
if(x == length(limits)){ "\U221E"} else( limits[x + 1]),
"and contains", bin[x + 1], "elements.\n")))
}
 
#Showing off a one-line solution. Admittedly, calling the massive anonymous function "one-line" is generous.
oneLine <- function(limits, data)
{
invisible(sapply(0:length(limits), function(x) cat("Bin", x, "covers the range:",
if(x == 0){ "-\U221E < x <"} else( paste(limits[x], "\U2264 x <")),
if(x == length(limits)){ "\U221E"} else( limits[x + 1]),
"and contains", sum(findInterval(data, limits) == x),
"elements.\n")))
}
 
createBin(limits1, data1)
printBin(limits1, createBin(limits1, data1))
createBin(limits2, data2)
printBin(limits2, createBin(limits2, data2))
oneLine(limits2, c(data1, data2))#Not needed.</langsyntaxhighlight>
{{out}}
<pre>> createBin(limits1, data1)
[1] 11 4 2 6 9 5 13
 
> printBin(limits1, createBin(limits1, data1))
Bin 0 covers the range: -∞ < x < 23 and contains 11 elements.
Bin 1 covers the range: 23 ≤ x < 37 and contains 4 elements.
Line 1,371 ⟶ 2,201:
Bin 6 covers the range: 83 ≤ x < ∞ and contains 13 elements.
 
> createBin(limits2, data2)
[1] 3 0 44 10 16 2 28 16 6 16 59
 
> printBin(limits2, createBin(limits2, data2))
Bin 0 covers the range: -∞ < x < 14 and contains 3 elements.
Bin 1 covers the range: 14 ≤ x < 18 and contains 0 elements.
Line 1,387 ⟶ 2,217:
Bin 10 covers the range: 720 ≤ x < ∞ and contains 59 elements.
 
> oneLine(limits2, c(data1, data2))#Not needed.
Bin 0 covers the range: -∞ < x < 14 and contains 10 elements.
Bin 1 covers the range: 14 ≤ x < 18 and contains 2 elements.
Line 1,399 ⟶ 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}}==
 
<syntaxhighlight lang="racket">#lang racket
 
(define (find-bin-index limits v)
(let inner ((l 0) (r (vector-length limits)))
(let ((m (quotient (+ l r) 2)))
(if (< v (vector-ref limits m))
(if (= m l) l (inner l m))
(if (= m (sub1 r)) r (inner m r))))))
 
(define ((bin-given-limits! limits) data (bins (make-vector (add1 (vector-length limits)) 0)))
(for ((d data))
(let ((i (find-bin-index limits d)))
(vector-set! bins i (add1 (vector-ref bins i)))))
bins)
 
(define (report-bins-given-limits limits data)
(for ((b ((bin-given-limits! limits) data))
(ge (in-sequences (in-value -Inf.0) limits))
(lt (in-sequences limits (in-value +Inf.0))))
(printf "~a <= v < ~a : ~a~%" ge lt b)))
 
(define (Bin-given-limits)
(report-bins-given-limits
#[23 37 43 53 67 83]
(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))
(newline)
(report-bins-given-limits
#[14 18 249 312 389 392 513 591 634 720]
(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)))
 
(module+ main
(Bin-given-limits))</syntaxhighlight>
 
{{out}}
<pre>-inf.0 <= v < 23 : 11
23 <= v < 37 : 4
37 <= v < 43 : 2
43 <= v < 53 : 6
53 <= v < 67 : 9
67 <= v < 83 : 5
83 <= v < +inf.0 : 13
 
-inf.0 <= v < 14 : 3
14 <= v < 18 : 0
18 <= v < 249 : 44
249 <= v < 312 : 10
312 <= v < 389 : 16
389 <= v < 392 : 2
392 <= v < 513 : 28
513 <= v < 591 : 16
591 <= v < 634 : 6
634 <= v < 720 : 16
720 <= v < +inf.0 : 59</pre>
=={{header|Raku}}==
<syntaxhighlight lang="raku" line>
<lang perl6>
sub bin_it ( @limits, @data ) {
my @ranges = ( -Inf, |@limits, Inf ).rotor( 2 => -1 ).map: { .[0] ..^ .[1] };
Line 1,432 ⟶ 2,327:
say '';
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,455 ⟶ 2,350:
720..^Inf => 59
</pre>
 
=={{header|REXX}}==
REXX programming note: &nbsp; since the sets of numbers defined don't have any leading signs, no quotes ('''<big>"</big>''') are needed.
<lang rexx>/*REXX program counts how many numbers of a set that fall in the range of each bin. */
<syntaxhighlight lang="rexx">/*REXX program counts how many numbers of a set that fall in the range of each bin. */
lims= 23 37 43 53 67 83 /* ◄■■■■■■1st set of bin limits & data.*/
datalims= 9523 2137 9443 1253 9967 483 70 75 83 93 52 80 57 5 53 86 65 17 92 83 71 61 54 58 47 , /* <----- 1st set of bin limits & data.*/
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 limslimits lims; call bins data
call bins data
call show 'the 1st set of bin counts for the specified data:'
Do 3; say''; End
say; say; say
lims= 14 18 249 312 389 392 513 591 634 720 /* ◄■■■■■■2nd<-----2nd set of bin limits & data.*/
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 limslimits lims; call bins data
call bins data
call show 'the 2nd set of bin counts for the specified data:'
exit 0
exit 0 /*stick a fork in it, we're all done. */
/*------------------------------------------------------------------------------*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
bins:
bins: parse arg nums; !.= 0; datum= words(nums); wc= length(datum) /*max width count.*/
Parse Arg numbers
do j=1 for datum; x= word(nums, j)
count.=0
do k=0 for # /*find the bin that this number is in. */
Do j=1 To words(numbers)
if x < @.k then do; !.k= !.k + 1; iterate j; end /*bump a bin count*/
x=word(numbers,j)
end /*k*/
Do k=1 To bins-1 Until !x<limit.k= !.k +/* 1determine which bin is to /*numberbe used is > the highest bin specified*/
End
end /*j*/; return
count.k=count.k+1 /* increment count for this bin */
/*──────────────────────────────────────────────────────────────────────────────────────*/
End
lims: parse arg limList; #= words(limList); wb= 0 /*max width binLim*/
count_length=0 do j=1 for #; _= j - 1; @._= word(limList, j);/* compute the wb=length max(wb,of the largest count length(@._) )*/
Do k=0 To bins
end /*j*/; return
count_length=max(count_length,length(count.k))
/*──────────────────────────────────────────────────────────────────────────────────────*/
End
show: parse arg t; say center(t, 51 ); $= left('', 9) /*$: for indentation*/
Return
say center('', 51, "═") /*show title separator.*/
/*------------------------------------------------------------------------------*/
jp= # - 1; ge= '≥'; le='<'; eq= ' count='
limits:
do j=0 for #; jm= j - 1; bin= right(@.j, wb)
Parse Arg limlist
if j==0 then say $ left('', length(ge) +3+wb+length(..) )le bin eq right(!.j, wc)
limit_length=0
else say $ ge right(@.jm, wb) .. le bin eq right(!.j, wc)
bins=words(limlist)+1 /* number of bins */
if j==jp then say $ ge right(@.jp,wb) left('', 3+length(..)+wb) eq right(!.#, wc)
limit.=''
end /*j*/; return</lang>
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=&nbsp; when using the internal default input:}}
<pre>
the 1st set of bin counts for the specified data:
---------------------------------------------------
═══════════════════════════════════════════════════
< 23 count= 11
>= 23 .. < 37 count= 4
>= 37 .. < 43 count= 2
>= 43 .. < 53 count= 6
>= 53 .. < 67 count= 9
>= 67 .. < 83 count= 5
>= 83 count= 13
 
 
 
the 2nd set of bin counts for the specified data:
---------------------------------------------------
═══════════════════════════════════════════════════
< 14 count= 3
>= 14 .. < 18 count= 0
>= 18 .. < 249 count= 44
>= 249 .. < 312 count= 10
>= 312 .. < 389 count= 16
>= 389 .. < 392 count= 2
>= 392 .. < 513 count= 28
>= 513 .. < 591 count= 16
>= 591 .. < 634 count= 6
>= 634 .. < 720 count= 16
>= 720 count= 59
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="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,
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]
data = sort(data)
dn = list(len(limit))
see "Example 1:" + nl + nl
limits(limit,data,dn)
see nl
 
limit = [0, 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]
data = sort(data)
dn = list(len(limit))
see "Example 2:" + nl + nl
limits(limit,data,dn)
 
func limits(limit,data,dn)
for n = 1 to len(data)
for m = 1 to len(limit)-1
if data[n] >= limit[m] and data[n] < limit[m+1]
dn[m] += 1
ok
next
if data[n] >= limit[len(limit)]
dn[len(limit)] += 1
ok
next
 
for n = 1 to len(limit)-1
see ">= " + limit[n] + " and < " + limit[n+1] + " := " + dn[n] + nl
next
see ">= " + limit[n] + " := " + dn[n] + nl
</syntaxhighlight>
{{out}}
<pre>
Example 1:
 
>= 0 and < 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:
 
>= 0 and < 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|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. Makes use ofUses Ruby 3.0 end-less and begin-less Ranges.
<langsyntaxhighlight lang="ruby">Test = Struct.new(:limits, :data)
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 1,559 ⟶ 2,600:
puts
end
</syntaxhighlight>
</lang>
{{out}}
<pre>23...3723 411
23...37 4
37...43 2
43...53 6
Line 1,586 ⟶ 2,628:
A very simple and naive algorithm that uses nested dynamic arrays.
 
<langsyntaxhighlight lang="rust">fn make_bins(limits: &Vec<usize>, data: &Vec<usize>) -> Vec<Vec<usize>> {
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
Line 1,642 ⟶ 2,684:
print_bins(&limits2, &bins2);
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,669 ⟶ 2,711:
</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>):
<syntaxhighlight lang="tcl">namespace path {::tcl::mathop ::tcl::mathfunc}
 
# Not necessary but useful helper
proc lincr {_list index} {
upvar $_list list
lset list $index [+ [lindex $list $index] 1]
}
 
proc distribute_bins {binlims data} {
set bins [lrepeat [+ [llength $binlims] 1] 0]
foreach val $data {
lincr bins [+ [lsearch -exact -integer -sorted -bisect $binlims $val] 1]
}
return $bins
}
 
proc print_bins {binlims bins} {
set binlims [list -∞ {*}$binlims ∞]
for {set i 0} {$i < [llength $bins]} {incr i} {
puts "[lindex $binlims $i]..[lindex $binlims [+ $i 1]]: [lindex $bins $i]"
}
}
 
set binlims {23 37 43 53 67 83}
set 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}
print_bins $binlims [distribute_bins $binlims $data]
puts ""
 
set binlims {14 18 249 312 389 392 513 591 634 720}
set 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}
print_bins $binlims [distribute_bins $binlims $data]</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|Wren}}==
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./sort" for Find
import "./fmt" for Fmt
 
var getBins = Fn.new { |limits, data|
Line 1,729 ⟶ 2,907:
var bins = getBins.call(limitsList[i], dataList[i])
printBins.call(limitsList[i], bins)
}</langsyntaxhighlight>
 
{{out}}
2,289

edits