Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,2 @@
---
from: http://rosettacode.org/wiki/Bin_given_limits

View file

@ -0,0 +1,42 @@
You are given a list of n ascending, unique numbers which are to form limits
for n+1 bins which count how many of a large set of input numbers fall in the
range of each bin.
(Assuming zero-based indexing)
bin[0] counts how many inputs are < limit[0]
bin[1] counts how many inputs are >= limit[0] and < limit[1]
..''
bin[n-1] counts how many inputs are >= limit[n-2] and < limit[n-1]
bin[n] counts how many inputs are >= limit[n-1]
;Task:
The task is to create a function that given the ascending limits and a stream/
list of numbers, will return the bins; together with another function that
given the same list of limits and the binning will ''print the limit of each bin
together with the count of items that fell in the range''.
Assume the numbers to bin are too large to practically sort.
;Task examples:
Part 1: Bin using the following limits the given input data
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]
Part 2: Bin using the following limits the given input data
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]
Show output here, on this page.

View file

@ -0,0 +1,45 @@
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)

View file

@ -0,0 +1,74 @@
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

View file

@ -0,0 +1,67 @@
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

View file

@ -0,0 +1,8 @@
package binning is
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
subtype Limits_Array is Nums_Array with
Dynamic_Predicate => Is_Sorted (Limits_Array);
function Bins (Limits : Limits_Array; Data : Nums_Array) return Nums_Array;
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array);
end binning;

View file

@ -0,0 +1,69 @@
pragma Ada_2012;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
package body binning is
---------------
-- Is_Sorted --
---------------
function Is_Sorted (Item : Nums_Array) return Boolean is
begin
return
(for all i in Item'First .. Item'Last - 1 => Item (i) < Item (i + 1));
end Is_Sorted;
----------
-- Bins --
----------
function Bins (Limits : Limits_Array; Data : Nums_Array) return Nums_Array
is
Result : Nums_Array (Limits'First .. Limits'Last + 1) := (others => 0);
Bin_Index : Natural;
begin
for value of Data loop
Bin_Index := Result'First;
for I in reverse Limits'Range loop
if value >= Limits (I) then
Bin_Index := I + 1;
exit;
end if;
end loop;
Result (Bin_Index) := Result (Bin_Index) + 1;
end loop;
return Result;
end Bins;
-----------
-- Print --
-----------
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array) is
begin
if Limits'Length = 0 then
return;
end if;
Put (" < ");
Put (Item => Limits (Limits'First), Width => 3);
Put (": ");
Put (Item => Bin_Result (Bin_Result'First), Width => 2);
New_Line;
for i in Limits'First + 1 .. Limits'Last loop
Put (">= ");
Put (Item => Limits (i - 1), Width => 3);
Put (" and < ");
Put (Item => Limits (i), Width => 3);
Put (": ");
Put (Item => Bin_Result (i), Width => 2);
New_Line;
end loop;
Put (">= ");
Put (Item => Limits (Limits'Last), Width => 3);
Put (" : ");
Put (Item => Bin_Result (Bin_Result'Last), Width => 2);
New_Line;
end Print;
end binning;

View file

@ -0,0 +1,34 @@
with Ada.Text_IO; use Ada.Text_IO;
with binning; use binning;
procedure Main is
Limits_1 : Limits_Array := (23, 37, 43, 53, 67, 83);
Data_1 : Nums_Array :=
(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_2 : Limits_Array := (14, 18, 249, 312, 389, 392, 513, 591, 634, 720);
Data_2 : Nums_Array :=
(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);
Bin_1 : Nums_Array := Bins (Limits => Limits_1, Data => Data_1);
Bin_2 : Nums_Array := Bins (Limits => Limits_2, Data => Data_2);
begin
Put_Line ("Example 1:");
Print (Limits => Limits_1, Bin_Result => Bin_1);
New_Line;
Put_Line ("Example 2:");
Print (Limits => Limits_2, Bin_Result => Bin_2);
end Main;

View file

@ -0,0 +1,17 @@
Bin_given_limits(limits, data){
bin := [], counter := 0
for i, val in data {
if (limits[limits.count()] <= val)
bin["", ++counter] := val
else for j, limit in limits
if (limits[j-1] <= val && val < limits[j])
bin[limit, ++counter] := val
}
for j, limit in limits {
output .= (prevlimit ? prevlimit : "-∞") ", " limit " : " ((x:=bin[limit].Count())?x:0) "`n"
prevlimit := limit
}
return output .= (prevlimit ? prevlimit : "-∞") ", ∞ : " ((x:=bin[""].Count())?x:0) "`n"
}

View file

@ -0,0 +1,18 @@
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]
MsgBox, 262144, , % Bin_given_limits(limits, data)
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]
MsgBox, 262144, , % Bin_given_limits(limits, data)
return

View file

@ -0,0 +1,63 @@
#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <vector>
std::vector<int> bins(const std::vector<int>& limits,
const std::vector<int>& data) {
std::vector<int> result(limits.size() + 1, 0);
for (int n : data) {
auto i = std::upper_bound(limits.begin(), limits.end(), n);
++result[i - limits.begin()];
}
return result;
}
void print_bins(const std::vector<int>& limits, const std::vector<int>& bins) {
size_t n = limits.size();
if (n == 0)
return;
assert(n + 1 == bins.size());
std::cout << " < " << std::setw(3) << limits[0] << ": "
<< std::setw(2) << bins[0] << '\n';
for (size_t i = 1; i < n; ++i)
std::cout << ">= " << std::setw(3) << limits[i - 1] << " and < "
<< std::setw(3) << limits[i] << ": " << std::setw(2)
<< bins[i] << '\n';
std::cout << ">= " << std::setw(3) << limits[n - 1] << " : "
<< std::setw(2) << bins[n] << '\n';
}
int main() {
const std::vector<int> limits1{23, 37, 43, 53, 67, 83};
const std::vector<int> 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};
std::cout << "Example 1:\n";
print_bins(limits1, bins(limits1, data1));
const std::vector<int> limits2{14, 18, 249, 312, 389,
392, 513, 591, 634, 720};
const std::vector<int> 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};
std::cout << "\nExample 2:\n";
print_bins(limits2, bins(limits2, data2));
}

View file

@ -0,0 +1,45 @@
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;
}
}

View file

@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
size_t upper_bound(const int* array, size_t n, int value) {
size_t start = 0;
while (n > 0) {
size_t step = n / 2;
size_t index = start + step;
if (value >= array[index]) {
start = index + 1;
n -= step + 1;
} else {
n = step;
}
}
return start;
}
int* bins(const int* limits, size_t nlimits, const int* data, size_t ndata) {
int* result = calloc(nlimits + 1, sizeof(int));
if (result == NULL)
return NULL;
for (size_t i = 0; i < ndata; ++i)
++result[upper_bound(limits, nlimits, data[i])];
return result;
}
void print_bins(const int* limits, size_t n, const int* bins) {
if (n == 0)
return;
printf(" < %3d: %2d\n", limits[0], bins[0]);
for (size_t i = 1; i < n; ++i)
printf(">= %3d and < %3d: %2d\n", limits[i - 1], limits[i], bins[i]);
printf(">= %3d : %2d\n", limits[n - 1], bins[n]);
}
int main() {
const int limits1[] = {23, 37, 43, 53, 67, 83};
const int 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};
printf("Example 1:\n");
size_t n = sizeof(limits1) / sizeof(int);
int* b = bins(limits1, n, data1, sizeof(data1) / sizeof(int));
if (b == NULL) {
fprintf(stderr, "Out of memory\n");
return EXIT_FAILURE;
}
print_bins(limits1, n, b);
free(b);
const int limits2[] = {14, 18, 249, 312, 389, 392, 513, 591, 634, 720};
const int 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};
printf("\nExample 2:\n");
n = sizeof(limits2) / sizeof(int);
b = bins(limits2, n, data2, sizeof(data2) / sizeof(int));
if (b == NULL) {
fprintf(stderr, "Out of memory\n");
return EXIT_FAILURE;
}
print_bins(limits2, n, b);
free(b);
return EXIT_SUCCESS;
}

View file

@ -0,0 +1,85 @@
% 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
% that allows the < comparator.
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.
% This imposes the further restriction on the datatype
% that it allows `unparse' (may be turned into a string).
display_bins = proc [T: type] (limits, data: array[T])
where T has unparse: proctype (T) returns (string),
T has lt: proctype (T,T) returns (bool)
ad = array[T]
ai = array[int]
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
start_up = proc ()
ai = array[int]
limits1: ai := ai$[23, 37, 43, 53, 67, 83]
data1: ai := ai$
[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: 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)
display_bins[int](limits2, data2)
end start_up

View file

@ -0,0 +1,48 @@
USING: assocs formatting grouping io kernel math math.parser
math.statistics sequences sequences.extras sorting.extras ;
: bin ( data limits -- seq )
dup length 1 + [ 0 ] replicate -rot
[ bisect-right over [ 1 + ] change-nth ] curry each ;
: .bin ( {lo,hi} n i -- )
swap "%3d members in " printf zero? "(" "[" ? write
"%s, %s)\n" vprintf ;
: .bins ( data limits -- )
dup [ number>string ] map "-∞" prefix "∞" suffix 2 clump
-rot bin [ .bin ] 2each-index ;
"First example:" print
{
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 } .bins nl
"Second example:" print
{
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 } .bins

View file

@ -0,0 +1,52 @@
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))

View file

@ -0,0 +1,61 @@
package main
import (
"fmt"
"sort"
)
func getBins(limits, data []int) []int {
n := len(limits)
bins := make([]int, n+1)
for _, d := range data {
index := sort.SearchInts(limits, d) // uses binary search
if index < len(limits) && d == limits[index] {
index++
}
bins[index]++
}
return bins
}
func printBins(limits, bins []int) {
n := len(limits)
fmt.Printf(" < %3d = %2d\n", limits[0], bins[0])
for i := 1; i < n; i++ {
fmt.Printf(">= %3d and < %3d = %2d\n", limits[i-1], limits[i], bins[i])
}
fmt.Printf(">= %3d = %2d\n", limits[n-1], bins[n])
fmt.Println()
}
func main() {
limitsList := [][]int{
{23, 37, 43, 53, 67, 83},
{14, 18, 249, 312, 389, 392, 513, 591, 634, 720},
}
dataList := [][]int{
{
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,
},
{
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,
},
}
for i := 0; i < len(limitsList); i++ {
fmt.Println("Example", i+1, "\b\n")
bins := getBins(limitsList[i], dataList[i])
printBins(limitsList[i], bins)
}
}

View file

@ -0,0 +1,11 @@
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

View file

@ -0,0 +1,22 @@
{-# 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)

View file

@ -0,0 +1,35 @@
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]

View file

@ -0,0 +1,13 @@
Idotr=: |.@[ (#@[ - I.) ] NB. reverses order of limits to obtain intervals closed on left, open on right (>= y <)
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=: dyad define
counts =. y
'%2d in [ -∞, %3d)' printf ({. counts) , {. x
'%2d in [%3d, %3d)' printf (}.}: counts) ,. 2 ]\ x
'%2d in [%3d, ∞]' printf ({: counts) , {: x
)

View file

@ -0,0 +1,37 @@
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
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=: 14 18 249 312 389 392 513 591 634 720
data2=: , 0&".;._2 noun define
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
)
limits1 < 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│
└──────────────────────────┴───────────┴─────┴─────────────────┴──────────────────────────┴──────────────┴──────────────────────────────────────┘
limits1 # binnedData data1 NB. tally binned data
11 4 2 6 9 5 13
limits2 printBinCounts limits2 # binnedData 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, ∞]

View file

@ -0,0 +1,69 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Bins {
public static <T extends Comparable<? super T>> int[] bins(
List<? extends T> limits, Iterable<? extends T> data) {
int[] result = new int[limits.size() + 1];
for (T n : data) {
int i = Collections.binarySearch(limits, n);
if (i >= 0) {
// n == limits[i]; we put it in right-side bin (i+1)
i = i+1;
} else {
// n is not in limits and i is ~(insertion point)
i = ~i;
}
result[i]++;
}
return result;
}
public static void printBins(List<?> limits, int[] bins) {
int n = limits.size();
if (n == 0) {
return;
}
assert n+1 == bins.length;
System.out.printf(" < %3s: %2d\n", limits.get(0), bins[0]);
for (int i = 1; i < n; i++) {
System.out.printf(">= %3s and < %3s: %2d\n", limits.get(i-1), limits.get(i), bins[i]);
}
System.out.printf(">= %3s : %2d\n", limits.get(n-1), bins[n]);
}
public static void main(String[] args) {
List<Integer> limits = Arrays.asList(23, 37, 43, 53, 67, 83);
List<Integer> data = Arrays.asList(
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);
System.out.println("Example 1:");
printBins(limits, bins(limits, data));
limits = Arrays.asList(14, 18, 249, 312, 389,
392, 513, 591, 634, 720);
data = Arrays.asList(
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);
System.out.println();
System.out.println("Example 2:");
printBins(limits, bins(limits, data));
}
}

View file

@ -0,0 +1,18 @@
# 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

View file

@ -0,0 +1 @@
< data.json jq -rn --argfile limits limits.json -f program.jq

View file

@ -0,0 +1,7 @@
< 23 => 11
< 37 => 4
< 43 => 2
< 53 => 6
< 67 => 9
< 83 => 5
>= 83 => 13

View file

@ -0,0 +1,11 @@
< 14 => 3
< 18 => 0
< 249 => 44
< 312 => 10
< 389 => 16
< 392 => 2
< 513 => 28
< 591 => 16
< 634 => 6
< 720 => 16
>= 720 => 59

View file

@ -0,0 +1,53 @@
"""Add the function Python has in its bisect library"""
function bisect_right(array, x, low = 1, high = length(array) + 1)
while low < high
middle = (low + high) ÷ 2
x < array[middle] ? (high = middle) : (low = middle + 1)
end
return low
end
""" Bin data according to (ascending) limits """
function bin_it(limits, data)
bins = zeros(Int, length(limits) + 1) # adds under/over range bins too
for d in data
bins[bisect_right(limits, d)] += 1
end
return bins
end
""" Pretty print the resulting bins and counts """
function bin_print(limits, bins)
println(" < $(lpad(limits[1], 3)) := $(lpad(bins[1], 3))")
for (lo, hi, count) in zip(limits, limits[2:end], bins[2:end])
println(">= $(lpad(lo, 3)) .. < $(lpad(hi, 3)) := $(lpad(count, 3))")
end
println(">= $(lpad(limits[end], 3)) := $(lpad(bins[end], 3))")
end
""" Test on data provided """
function testbins()
println("RC FIRST EXAMPLE:")
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 = bin_it(limits, data)
bin_print(limits, bins)
println("\nRC SECOND EXAMPLE:")
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)
end
testbins()

View file

@ -0,0 +1,41 @@
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)

View file

@ -0,0 +1,28 @@
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

View file

@ -0,0 +1,53 @@
import algorithm, strformat
func binIt(limits, data: openArray[int]): seq[Natural] =
result.setLen(limits.len + 1)
for d in data:
inc result[limits.upperBound(d)]
proc binPrint(limits: openArray[int]; bins: seq[Natural]) =
echo &" < {limits[0]:3} := {bins[0]:3}"
for i in 1..limits.high:
echo &">= {limits[i-1]:3} .. < {limits[i]:3} := {bins[i]:3}"
echo &">= {limits[^1]:3} := {bins[^1]:3}"
when isMainModule:
echo "Example 1:"
const
Limits1 = [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]
let bins1 = binIt(Limits1, Data1)
binPrint(Limits1, bins1)
echo ""
echo "Example 2:"
const
Limits2 = [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]
let bins2 = binIt(Limits2, Data2)
binPrint(Limits2, bins2)

View file

@ -0,0 +1,64 @@
#import <Foundation/Foundation.h>
NSArray<NSNumber *> *bins(NSArray<NSNumber *> *limits, NSArray<NSNumber *> *data) {
NSMutableArray<NSNumber *> *result = [[NSMutableArray alloc] initWithCapacity:[limits count] + 1];
for (NSInteger i = 0; i <= [limits count]; i++) {
[result addObject:@0];
}
for (NSNumber *n in data) {
NSUInteger i = [limits indexOfObject:n
inSortedRange:NSMakeRange(0, [limits count])
options:NSBinarySearchingInsertionIndex|NSBinarySearchingLastEqual
usingComparator:^(NSNumber *x, NSNumber *y){ return [x compare: y]; }];
result[i] = @(result[i].integerValue + 1);
}
return result;
}
void printBins(NSArray<NSNumber *> *limits, NSArray<NSNumber *> *bins) {
NSUInteger n = [limits count];
if (n == 0)
return;
NSCAssert(n + 1 == [bins count], @"Wrong size of bins");
NSLog(@" < %3@: %2@", limits[0], bins[0]);
for (NSInteger i = 1; i < n; i++) {
NSLog(@">= %3@ and < %3@: %2@", limits[i-1], limits[i], bins[i]);
}
NSLog(@">= %3@ : %2@", limits[n-1], bins[n]);
}
int main(void) {
@autoreleasepool {
NSArray<NSNumber *> *limits = @[@23, @37, @43, @53, @67, @83];
NSArray<NSNumber *> *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];
NSLog(@"Example 1:");
printBins(limits, bins(limits, data));
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];
NSLog(@"");
NSLog(@"Example 2:");
printBins(limits, bins(limits, data));
}
return 0;
}

View file

@ -0,0 +1,57 @@
use strict;
use warnings; no warnings 'uninitialized';
use feature 'say';
use experimental 'signatures';
use constant Inf => 1e10;
my @tests = (
{
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 => [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
]
}
);
sub bisect_right ($x, $low, $high, @array) {
my ($middle);
while ($low < $high) {
$middle = ($low + $high) / 2;
$x < $array[$middle] ? $high = $middle : ($low = $middle + 1)
}
$low-1
}
sub bin_it ($limits, $data) {
my @bins;
++$bins[ bisect_right($_, 0, @$limits-1, @$limits) ] for @$data;
@bins
}
sub bin_format ($limits, @bins) {
my @lim = @$limits;
my(@formatted);
push @formatted, sprintf "[%3d, %3d) => %3d\n", $lim[$_], ($lim[$_+1] == Inf ? 'Inf' : $lim[$_+1]), $bins[$_] for 0..@lim-2;
@formatted
}
for (0..$#tests) {
my @limits = (0, @{$tests[$_]{limits}}, Inf);
say bin_format \@limits, bin_it(\@limits,\@{$tests[$_]{data}});
}

View file

@ -0,0 +1,10 @@
use Math::SimpleHisto::XS;
for (@tests) {
my @lim = (0, @{$$_{limits}}, Inf);
my $hist = Math::SimpleHisto::XS->new( bins => \@lim );
$hist->fill( \$$_{data}->@* );
my $data_bins = $hist->all_bin_contents;
printf "[%3d, %3d) => %3d\n", $lim[$_], ($lim[$_+1] == Inf ? 'Inf' : $lim[$_+1]), $$data_bins[$_] for 0..@lim-2;
print "\n";
}

View file

@ -0,0 +1,42 @@
(phixonline)-->
<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>
<span style="color: #000080;font-style:italic;">-- Bin data according to (ascending) limits.</span>
<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>
<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>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">bins</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<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>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</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: #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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<!--

View file

@ -0,0 +1,38 @@
from bisect import bisect_right
def bin_it(limits: list, data: list) -> list:
"Bin data according to (ascending) limits."
bins = [0] * (len(limits) + 1) # adds under/over range bins too
for d in data:
bins[bisect_right(limits, d)] += 1
return bins
def bin_print(limits: list, bins: list) -> list:
print(f" < {limits[0]:3} := {bins[0]:3}")
for lo, hi, count in zip(limits, limits[1:], bins[1:]):
print(f">= {lo:3} .. < {hi:3} := {count:3}")
print(f">= {limits[-1]:3} := {bins[-1]:3}")
if __name__ == "__main__":
print("RC FIRST EXAMPLE\n")
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 = 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)

View file

@ -0,0 +1,43 @@
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))
#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.

View file

@ -0,0 +1,42 @@
/*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.*/
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 lims lims; call bins data
call show 'the 1st set of bin counts for the specified data:'
say; say; say
lims= 14 18 249 312 389 392 513 591 634 720 /* ◄■■■■■■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 lims lims; call bins data
call show 'the 2nd set of bin counts for the specified data:'
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
bins: parse arg nums; !.= 0; datum= words(nums); wc= length(datum) /*max width count.*/
do j=1 for datum; x= word(nums, j)
do k=0 for # /*find the bin that this number is in. */
if x < @.k then do; !.k= !.k + 1; iterate j; end /*bump a bin count*/
end /*k*/
!.k= !.k + 1 /*number is > the highest bin specified*/
end /*j*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
lims: parse arg limList; #= words(limList); wb= 0 /*max width binLim*/
do j=1 for #; _= j - 1; @._= word(limList, j); wb= max(wb, length(@._) )
end /*j*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: parse arg t; say center(t, 51 ); $= left('', 9) /*$: for indentation*/
say center('', 51, "") /*show title separator.*/
jp= # - 1; ge= ''; le='<'; eq= ' count='
do j=0 for #; jm= j - 1; bin= right(@.j, wb)
if j==0 then say $ left('', length(ge) +3+wb+length(..) )le bin eq right(!.j, wc)
else say $ ge right(@.jm, wb) .. le bin eq right(!.j, wc)
if j==jp then say $ ge right(@.jp,wb) left('', 3+length(..)+wb) eq right(!.#, wc)
end /*j*/; return

View file

@ -0,0 +1,42 @@
#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))

View file

@ -0,0 +1,31 @@
sub bin_it ( @limits, @data ) {
my @ranges = ( -Inf, |@limits, Inf ).rotor( 2 => -1 ).map: { .[0] ..^ .[1] };
my @binned = @data.classify(-> $d { @ranges.grep(-> $r { $d ~~ $r }) });
my %counts = @binned.map: { .key => .value.elems };
return @ranges.map: { $_ => ( %counts{$_} // 0 ) };
}
sub bin_format ( @bins ) {
return @bins.map: { .key.gist.fmt('%9s => ') ~ .value.fmt('%2d') };
}
my @tests =
{
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 => (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
),
},
;
for @tests -> ( :@limits, :@data ) {
my @bins = bin_it( @limits, @data );
.say for bin_format(@bins);
say '';
}

View file

@ -0,0 +1,41 @@
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

View file

@ -0,0 +1,29 @@
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,
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]),
Test.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])
def bin(limits, data)
data.map{|d| limits.bsearch{|limit| limit > d} }.tally
end
def present_bins(limits, bins)
ranges = ([nil]+limits+[nil]).each_cons(2).map{|low, high| Range.new(low, high, true) }
ranges.each{|range| puts "#{range.to_s.ljust(12)} #{bins[range.end].to_i}"}
end
tests.each do |test|
present_bins(test.limits, bin(test.limits, test.data))
puts
end

View file

@ -0,0 +1,56 @@
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());}
limits.iter().enumerate().for_each(|(idx, limit)| {
data.iter().for_each(|elem| {
if idx == 0 && elem < limit { bins[0].push(*elem); } // smaller than the smallest limit
else if idx == limits.len()-1 && elem >= limit { bins[limits.len()].push(*elem); } // larger than the largest limit
else if elem < limit && elem >= &limits[idx-1] { bins[idx].push(*elem); } // otherwise
});
});
bins
}
fn print_bins(limits: &Vec<usize>, bins: &Vec<Vec<usize>>) {
for (idx, bin) in bins.iter().enumerate() {
if idx == 0 {
println!(" < {:3} := {:3}", limits[idx], bin.len());
} else if idx == limits.len() {
println!(">= {:3} := {:3}", limits[idx-1], bin.len());
}else {
println!(">= {:3} .. < {:3} := {:3}", limits[idx-1], limits[idx], bin.len());
}
};
}
fn main() {
let limits1 = vec![23, 37, 43, 53, 67, 83];
let data1 = vec![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];
let limits2 = vec![14, 18, 249, 312, 389, 392, 513, 591, 634, 720];
let data2 = vec![
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
];
// Why are we calling it RC anyways???
println!("RC FIRST EXAMPLE");
let bins1 = make_bins(&limits1, &data1);
print_bins(&limits1, &bins1);
println!("\nRC SECOND EXAMPLE");
let bins2 = make_bins(&limits2, &data2);
print_bins(&limits2, &bins2);
}

View file

@ -0,0 +1,41 @@
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]

View file

@ -0,0 +1,58 @@
import "/sort" for Find
import "/fmt" for Fmt
var getBins = Fn.new { |limits, data|
var n = limits.count
var bins = List.filled(n+1, 0)
for (d in data) {
var res = Find.all(limits, d) // uses binary search
var found = res[0]
var index = res[2].from
if (found) index = index + 1
bins[index] = bins[index] + 1
}
return bins
}
var printBins = Fn.new { |limits, bins|
for (i in 0..limits.count) {
if (i == 0) {
Fmt.print(" < $3d = $2d", limits[0], bins[0])
} else if (i == limits.count) {
Fmt.print(">= $3d = $2d", limits[i-1], bins[i])
} else {
Fmt.print(">= $3d and < $3d = $2d", limits[i-1], limits[i], bins[i])
}
}
System.print()
}
var limitsList = [
[23, 37, 43, 53, 67, 83],
[14, 18, 249, 312, 389, 392, 513, 591, 634, 720]
]
var dataList = [
[
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
],
[
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
]
]
for (i in 0...limitsList.count) {
System.print("Example %(i+1):\n")
var bins = getBins.call(limitsList[i], dataList[i])
printBins.call(limitsList[i], bins)
}