Bin given limits: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 42:
 
Show output here, on this page.
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F bisect_right(a, x)
V lo = 0
V hi = a.len
Line 118 ⟶ 117:
>= 720 := 59
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang=Action"action!">DEFINE MAX_BINS="20"
 
PROC Count(INT ARRAY limits INT nLimits INT ARRAY data INT nData INT ARRAY bins)
Line 210 ⟶ 208:
>=720: 59
</pre>
 
=={{header|Ada}}==
This example works with Ada 2012. The definition of the subtype Limits_Array employs a dynamic predicate to ensure that the limits array is sorted. The solution defines the binning types and operations within an Ada package, providing modularity and simplifying the code in the main procedure.
 
'''package specification:'''
<syntaxhighlight lang=Ada"ada">package binning is
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
Line 225 ⟶ 222:
</syntaxhighlight>
'''package body:'''
<syntaxhighlight lang=Ada"ada">pragma Ada_2012;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Line 296 ⟶ 293:
</syntaxhighlight>
'''main procedure:'''
<syntaxhighlight lang=Ada"ada">with Ada.Text_IO; use Ada.Text_IO;
with binning; use binning;
 
Line 355 ⟶ 352:
>= 720 : 59
</pre>
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">BEGIN # count the number pf items that fall into "bins" given he limits #
# returns an array of "bins" containing the counts of the data items #
# that fall into the bins given the limits #
Line 455 ⟶ 451:
> 720: 59
</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang=AutoHotkey"autohotkey">Bin_given_limits(limits, data){
bin := [], counter := 0
for i, val in data {
Line 474 ⟶ 469:
return output .= (prevlimit ? prevlimit : "-∞") ", ∞ : " ((x:=bin["∞"].Count())?x:0) "`n"
}</syntaxhighlight>
Examples:<syntaxhighlight lang=AutoHotkey"autohotkey">limits := [23, 37, 43, 53, 67, 83]
data := [95,21,94,12,99,4,70,75,83,93,52,80,57,5,53,86,65,17,92,83,71,61,54,58,47,16
, 8, 9,32,84,7,87,46,19,30,37,96,6,98,40,79,97,45,64,60,29,49,36,43,55]
Line 514 ⟶ 509:
720, ∞ : 59
</pre>
 
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 624 ⟶ 618:
>= 720 : 59
</pre>
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System;
 
public class Program
{
static void Main()
{
PrintBins(new [] { 23, 37, 43, 53, 67, 83 },
95,21,94,12,99,4,70,75,83,93,52,80,57,5,53,86,65,17,92,83,71,61,54,58,47,
16, 8, 9,32,84,7,87,46,19,30,37,96,6,98,40,79,97,45,64,60,29,49,36,43,55
);
Console.WriteLine();
 
PrintBins(new [] { 14, 18, 249, 312, 389, 392, 513, 591, 634, 720 },
445,814,519,697,700,130,255,889,481,122,932, 77,323,525,570,219,367,523,442,933,416,589,930,373,202,
253,775, 47,731,685,293,126,133,450,545,100,741,583,763,306,655,267,248,477,549,238, 62,678, 98,534,
622,907,406,714,184,391,913, 42,560,247,346,860, 56,138,546, 38,985,948, 58,213,799,319,390,634,458,
945,733,507,916,123,345,110,720,917,313,845,426, 9,457,628,410,723,354,895,881,953,677,137,397, 97,
854,740, 83,216,421, 94,517,479,292,963,376,981,480, 39,257,272,157, 5,316,395,787,942,456,242,759,
898,576, 67,298,425,894,435,831,241,989,614,987,770,384,692,698,765,331,487,251,600,879,342,982,527,
736,795,585, 40, 54,901,408,359,577,237,605,847,353,968,832,205,838,427,876,959,686,646,835,127,621,
892,443,198,988,791,466, 23,707,467, 33,670,921,180,991,396,160,436,717,918, 8,374,101,684,727,749);
}
 
static void PrintBins(int[] limits, params int[] data)
{
int[] bins = Bins(limits, data);
Console.WriteLine($"-∞ .. {limits[0]} => {bins[0]}");
for (int i = 0; i < limits.Length-1; i++) {
Console.WriteLine($"{limits[i]} .. {limits[i+1]} => {bins[i+1]}");
}
Console.WriteLine($"{limits[^1]} .. ∞ => {bins[^1]}");
}
 
static int[] Bins(int[] limits, params int[] data)
{
Array.Sort(limits);
int[] bins = new int[limits.Length + 1];
foreach (int n in data) {
int i = Array.BinarySearch(limits, n);
i = i < 0 ? ~i : i+1;
bins[i]++;
}
return bins;
}
}</syntaxhighlight>
{{out}}
<pre>
-∞ .. 23 => 11
23 .. 37 => 4
37 .. 43 => 2
43 .. 53 => 6
53 .. 67 => 9
67 .. 83 => 5
83 .. ∞ => 13
 
-∞ .. 14 => 3
14 .. 18 => 0
18 .. 249 => 44
249 .. 312 => 10
312 .. 389 => 16
389 .. 392 => 2
392 .. 513 => 28
513 .. 591 => 16
591 .. 634 => 6
634 .. 720 => 16
720 .. ∞ => 59
</pre>
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <algorithm>
#include <cassert>
#include <iomanip>
Line 714 ⟶ 775:
>= 720 : 59
</pre>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang=csharp>using System;
 
public class Program
{
static void Main()
{
PrintBins(new [] { 23, 37, 43, 53, 67, 83 },
95,21,94,12,99,4,70,75,83,93,52,80,57,5,53,86,65,17,92,83,71,61,54,58,47,
16, 8, 9,32,84,7,87,46,19,30,37,96,6,98,40,79,97,45,64,60,29,49,36,43,55
);
Console.WriteLine();
 
PrintBins(new [] { 14, 18, 249, 312, 389, 392, 513, 591, 634, 720 },
445,814,519,697,700,130,255,889,481,122,932, 77,323,525,570,219,367,523,442,933,416,589,930,373,202,
253,775, 47,731,685,293,126,133,450,545,100,741,583,763,306,655,267,248,477,549,238, 62,678, 98,534,
622,907,406,714,184,391,913, 42,560,247,346,860, 56,138,546, 38,985,948, 58,213,799,319,390,634,458,
945,733,507,916,123,345,110,720,917,313,845,426, 9,457,628,410,723,354,895,881,953,677,137,397, 97,
854,740, 83,216,421, 94,517,479,292,963,376,981,480, 39,257,272,157, 5,316,395,787,942,456,242,759,
898,576, 67,298,425,894,435,831,241,989,614,987,770,384,692,698,765,331,487,251,600,879,342,982,527,
736,795,585, 40, 54,901,408,359,577,237,605,847,353,968,832,205,838,427,876,959,686,646,835,127,621,
892,443,198,988,791,466, 23,707,467, 33,670,921,180,991,396,160,436,717,918, 8,374,101,684,727,749);
}
 
static void PrintBins(int[] limits, params int[] data)
{
int[] bins = Bins(limits, data);
Console.WriteLine($"-∞ .. {limits[0]} => {bins[0]}");
for (int i = 0; i < limits.Length-1; i++) {
Console.WriteLine($"{limits[i]} .. {limits[i+1]} => {bins[i+1]}");
}
Console.WriteLine($"{limits[^1]} .. ∞ => {bins[^1]}");
}
 
static int[] Bins(int[] limits, params int[] data)
{
Array.Sort(limits);
int[] bins = new int[limits.Length + 1];
foreach (int n in data) {
int i = Array.BinarySearch(limits, n);
i = i < 0 ? ~i : i+1;
bins[i]++;
}
return bins;
}
}</syntaxhighlight>
{{out}}
<pre>
-∞ .. 23 => 11
23 .. 37 => 4
37 .. 43 => 2
43 .. 53 => 6
53 .. 67 => 9
67 .. 83 => 5
83 .. ∞ => 13
 
-∞ .. 14 => 3
14 .. 18 => 0
18 .. 249 => 44
249 .. 312 => 10
312 .. 389 => 16
389 .. 392 => 2
392 .. 513 => 28
513 .. 591 => 16
591 .. 634 => 6
634 .. 720 => 16
720 .. ∞ => 59
</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Bin the given data, return an array of counts.
% CLU allows arrays to start at any index; the result array
% will have the same lower bound as the limit array.
Line 892 ⟶ 883:
720 - inf : 59
------------------------------------------</pre>
 
=={{header|Factor}}==
Factor provides the <code>bisect-right</code> word in the <code>sorting.extras</code> vocabulary. See the implementation [https://docs.factorcode.org/content/word-bisect-right%2Csorting.extras.html here].
<syntaxhighlight lang="factor">USING: assocs formatting grouping io kernel math math.parser
math.statistics sequences sequences.extras sorting.extras ;
 
Line 967 ⟶ 957:
59 members in [720, ∞)
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">sub binlims( dat() as integer, limits() as integer, bins() as uinteger )
dim as uinteger n = ubound(limits), j, i
for i = 0 to ubound(dat)
Line 1,043 ⟶ 1,032:
>= 634 and < 720: 16
>= 720: 59</pre>
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import (
Line 1,133 ⟶ 1,121:
>= 720 = 59
</pre>
 
=={{header|Haskell}}==
 
Splitting the data into bins may be done using the monadic nature of a tuple. Here tuple plays role of the Writer monad, so that sequential partitioning by each bin boundary adds new bin contents.
 
<syntaxhighlight lang="haskell">import Control.Monad (foldM)
import Data.List (partition)
 
Line 1,157 ⟶ 1,144:
 
More efficient binning procedure exploits the binary search tree.
<syntaxhighlight lang="haskell">{-# language DeriveFoldable #-}
 
import Data.Foldable (toList)
Line 1,182 ⟶ 1,169:
Tasks examples
 
<syntaxhighlight lang="haskell">import Text.Printf
 
task bs ns = mapM_ putStrLn
Line 1,239 ⟶ 1,226:
16 in [634, 720)
59 in [720, ∞)</pre>
 
=={{header|J}}==
'''Solution:'''
Using <code>Idotr</code> from [[j:User:Brian_Schott/Histogram|this JWiki page]]
<syntaxhighlight lang="j">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
Line 1,258 ⟶ 1,244:
 
'''Required Examples:'''
<syntaxhighlight lang="j">limits1=: 23 37 43 53 67 83
data1=: , 0&".;._2 noun define
95 21 94 12 99 4 70 75 83 93 52 80 57 5 53 86 65 17 92 83 71 61 54 58 47
Line 1,295 ⟶ 1,281:
16 in [634, 720)
59 in [720, ∞]</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Line 1,390 ⟶ 1,375:
>= 720 : 59
</pre>
 
=={{header|jq}}==
The following takes advantage of jq's built-in filter for conducting a binary
Line 1,399 ⟶ 1,383:
indefinitely large number of items to be processed. These items
could, but need not, be presented one line at a time.
<syntaxhighlight lang="jq"># input and output: {limits, count} where
# .limits holds an array defining the limits, and
# .count[$i] holds the count of bin $i, where bin[0] is the left-most bin
Line 1,419 ⟶ 1,403:
{{out}}
Invocation:
<syntaxhighlight lang="sh">
< data.json jq -rn --argfile limits limits.json -f program.jq
</syntaxhighlight>
 
Example 1:
<syntaxhighlight lang="text">< 23 => 11
< 37 => 4
< 43 => 2
Line 1,433 ⟶ 1,417:
 
Example 2:
<syntaxhighlight lang="text">< 14 => 3
< 18 => 0
< 249 => 44
Line 1,444 ⟶ 1,428:
< 720 => 16
>= 720 => 59</syntaxhighlight>
 
=={{header|Julia}}==
{{trans|Python}}
<syntaxhighlight lang="julia">"""Add the function Python has in its bisect library"""
function bisect_right(array, x, low = 1, high = length(array) + 1)
while low < high
Line 1,524 ⟶ 1,507:
>= 720 := 59
</pre>
 
=={{header|Lua}}==
Array indexing is 1-based, as is customary for Lua:
<syntaxhighlight lang="lua">
function binner(limits, data)
local bins = setmetatable({}, {__index=function() return 0 end})
Line 1,592 ⟶ 1,574:
[634,720) : 16
[720, +∞) : 59</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">limits = {23, 37, 43, 53, 67, 83};
data = {95, 21, 94, 12, 99, 4, 70, 75, 83, 93, 52, 80, 57, 5, 53, 86,
65, 17, 92, 83, 71, 61, 54, 58, 47, 16, 8, 9, 32, 84, 7, 87, 46,
Line 1,644 ⟶ 1,625:
{634,720} 16
{720,\[Infinity]} 59</pre>
 
=={{header|Nim}}==
{{trans|Python}}
<syntaxhighlight lang=Nim"nim">import algorithm, strformat
 
func binIt(limits, data: openArray[int]): seq[Natural] =
Line 1,723 ⟶ 1,703:
>= 634 .. < 720 := 16
>= 720 := 59</pre>
 
=={{header|Objective-C}}==
<syntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
NSArray<NSNumber *> *bins(NSArray<NSNumber *> *limits, NSArray<NSNumber *> *data) {
Line 1,813 ⟶ 1,792:
>= 720 : 59
</pre>
 
=={{header|Perl}}==
Borrowed <tt>bisect_right</tt> from Julia entry.
<syntaxhighlight lang="perl">use strict;
use warnings; no warnings 'uninitialized';
use feature 'say';
Line 1,895 ⟶ 1,873:
 
But if we were to take to heart the warning that the input data was scary-big, then perhaps using a more efficient routine to classify the data into bins would be prudent (boilerplate/input/output same as above).
<syntaxhighlight lang="perl">use Math::SimpleHisto::XS;
 
for (@tests) {
Line 1,905 ⟶ 1,883:
print "\n";
}</syntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix"phix">(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>
Line 1,973 ⟶ 1,950:
>= 720 := 59
</pre>
 
=={{header|Python}}==
This example uses binary search through the limits to assign each number to its bin, via standard module bisect.bisect_right.<br>
The Counter module is ''not'' used as the number of bins is known allowing faster array access for incrementing bin counts versus dict lookup.
 
<syntaxhighlight lang="python">from bisect import bisect_right
 
def bin_it(limits: list, data: list) -> list:
Line 2,045 ⟶ 2,021:
 
Code such as 0:length(limits) is generally considered bad practice, but it didn't cause any problems here. To my amazement, this code works even if limits is of size 0 or 1. Even the <= printing doesn't break!
<syntaxhighlight lang="rsplus">limits1 <- c(23, 37, 43, 53, 67, 83)
data1 <- c(95,21,94,12,99,4,70,75,83,93,52,80,57,5,53,86,65,17,92,83,71,61,54,58,47,
16,8,9,32,84,7,87,46,19,30,37,96,6,98,40,79,97,45,64,60,29,49,36,43,55)
Line 2,129 ⟶ 2,105:
Bin 9 covers the range: 634 ≤ x < 720 and contains 16 elements.
Bin 10 covers the range: 720 ≤ x < ∞ and contains 59 elements.</pre>
 
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">#lang racket
 
(define (find-bin-index limits v)
Line 2,195 ⟶ 2,170:
634 <= v < 720 : 16
720 <= v < +inf.0 : 59</pre>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" line>
sub bin_it ( @limits, @data ) {
my @ranges = ( -Inf, |@limits, Inf ).rotor( 2 => -1 ).map: { .[0] ..^ .[1] };
Line 2,252 ⟶ 2,226:
720..^Inf => 59
</pre>
 
=={{header|REXX}}==
REXX programming note: &nbsp; since the sets of numbers defined don't have any leading signs, no quotes ('''<big>"</big>''') are needed.
<syntaxhighlight lang="rexx">/*REXX program counts how many numbers of a set that fall in the range of each bin. */
lims= 23 37 43 53 67 83 /* ◄■■■■■■1st set of bin limits & data.*/
data= 95 21 94 12 99 4 70 75 83 93 52 80 57 5 53 86 65 17 92 83 71 61 54 58 47 ,
Line 2,325 ⟶ 2,298:
≥ 720 count= 59
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
limit = [0, 23, 37, 43, 53, 67, 83]
data = [95,21,94,12,99,4,70,75,83,93,52,80,57,5,53,86,65,17,92,83,71,61,54,58,47,
Line 2,396 ⟶ 2,368:
>= 720 := 59
</pre>
 
=={{header|Ruby}}==
Perform a binary search on the data to select the limit and keep a tally on that. Uses Ruby 3.0 end-less and begin-less Ranges.
<syntaxhighlight lang="ruby">Test = Struct.new(:limits, :data)
tests = Test.new( [23, 37, 43, 53, 67, 83],
[95,21,94,12,99,4,70,75,83,93,52,80,57,5,53,86,65,17,92,83,71,61,54,58,47,
Line 2,450 ⟶ 2,421:
720... 59
</pre>
 
=={{header|Rust}}==
{{works with|rustc|1.49.0}}
Line 2,456 ⟶ 2,426:
A very simple and naive algorithm that uses nested dynamic arrays.
 
<syntaxhighlight lang="rust">fn make_bins(limits: &Vec<usize>, data: &Vec<usize>) -> Vec<Vec<usize>> {
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
Line 2,538 ⟶ 2,508:
>= 720 := 59
</pre>
 
=={{header|Tcl}}==
For Tcl 8.6 (due to <code>lsearch -bisect</code>):
<syntaxhighlight lang="tcl">namespace path {::tcl::mathop ::tcl::mathfunc}
 
# Not necessary but useful helper
Line 2,605 ⟶ 2,574:
720..∞: 59
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascript">import "/sort" for Find
import "/fmt" for Fmt
 
10,327

edits