Bin given limits: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|C sharp}}: Regularize header markup to recommended on category page)
m (syntax highlighting fixup automation)
Line 46:
{{trans|Python}}
 
<langsyntaxhighlight lang=11l>F bisect_right(a, x)
V lo = 0
V hi = a.len
Line 90:
466, 23,707,467, 33,670,921,180,991,396,160,436,717,918, 8,374,101,684,727,749]
bins = bin_it(limits, data)
bin_print(limits, bins)</langsyntaxhighlight>
 
{{out}}
Line 120:
 
=={{header|Action!}}==
<langsyntaxhighlight lang=Action!>DEFINE MAX_BINS="20"
 
PROC Count(INT ARRAY limits INT nLimits INT ARRAY data INT nData INT ARRAY bins)
Line 186:
Test(limits1,6,data1,50) PutE()
Test(limits2,10,data2,200)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bin_given_limits.png Screenshot from Atari 8-bit computer]
Line 215:
 
'''package specification:'''
<langsyntaxhighlight lang=Ada>package binning is
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
Line 223:
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array);
end binning;
</syntaxhighlight>
</lang>
'''package body:'''
<langsyntaxhighlight lang=Ada>pragma Ada_2012;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Line 294:
 
end binning;
</syntaxhighlight>
</lang>
'''main procedure:'''
<langsyntaxhighlight lang=Ada>with Ada.Text_IO; use Ada.Text_IO;
with binning; use binning;
 
Line 330:
Print (Limits => Limits_2, Bin_Result => Bin_2);
end Main;
</syntaxhighlight>
</lang>
{output}
<pre>
Line 357:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight 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 430:
show bins( limits, data INTOBINS limits )
END
END</langsyntaxhighlight>
{{out}}
<pre>
Line 457:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang=AutoHotkey>Bin_given_limits(limits, data){
bin := [], counter := 0
for i, val in data {
Line 473:
}
return output .= (prevlimit ? prevlimit : "-∞") ", ∞ : " ((x:=bin["∞"].Count())?x:0) "`n"
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight lang=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 491:
,466, 23,707,467, 33,670,921,180,991,396,160,436,717,918, 8,374,101,684,727,749]
MsgBox, 262144, , % Bin_given_limits(limits, data)
return</langsyntaxhighlight>
{{out}}
<pre>
Line 516:
 
=={{header|C}}==
<langsyntaxhighlight lang=c>#include <stdio.h>
#include <stdlib.h>
 
Line 598:
 
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 626:
 
=={{header|C++}}==
<langsyntaxhighlight lang=cpp>#include <algorithm>
#include <cassert>
#include <iomanip>
Line 688:
std::cout << "\nExample 2:\n";
print_bins(limits2, bins(limits2, data2));
}</langsyntaxhighlight>
 
{{out}}
Line 716:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang=csharp>using System;
 
public class Program
Line 760:
return bins;
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 785:
 
=={{header|CLU}}==
<langsyntaxhighlight 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 869:
display_bins[int](limits1, data1)
display_bins[int](limits2, data2)
end start_up</langsyntaxhighlight>
{{out}}
<pre> -inf - 23 : 11
Line 895:
=={{header|Factor}}==
Factor provides the <code>bisect-right</code> word in the <code>sorting.extras</code> vocabulary. See the implementation [https://docs.factorcode.org/content/word-bisect-right%2Csorting.extras.html here].
<langsyntaxhighlight lang=factor>USING: assocs formatting grouping io kernel math math.parser
math.statistics sequences sequences.extras sorting.extras ;
 
Line 942:
160 436 717 918 8 374 101 684 727 749
}
{ 14 18 249 312 389 392 513 591 634 720 } .bins</langsyntaxhighlight>
{{out}}
<pre>
Line 969:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight 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,020:
print ">= ";limits2(i-1);" and < ";limits2(i);": ";bins2(i)
next i
print ">= ";limits2(ubound(limits2));": ";bins2(ubound(bins2))</langsyntaxhighlight>
{{out}}
<pre>=====EXAMPLE ONE=====
Line 1,045:
 
=={{header|Go}}==
<langsyntaxhighlight lang=go>package main
 
import (
Line 1,105:
printBins(limitsList[i], bins)
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,138:
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.
 
<langsyntaxhighlight lang=haskell>import Control.Monad (foldM)
import Data.List (partition)
 
Line 1,148:
 
binCounts :: Ord a => [a] -> [a] -> [Int]
binCounts b = fmap length . binSplit b</langsyntaxhighlight>
 
<pre>λ> binSplit [2,4,7] [1,4,2,6,3,8,9,4,1,2,7,4,1,5,1]
Line 1,157:
 
More efficient binning procedure exploits the binary search tree.
<langsyntaxhighlight lang=haskell>{-# language DeriveFoldable #-}
 
import Data.Foldable (toList)
Line 1,178:
add x (Node y l r) = if x < y
then Node y (add x l) r
else Node y l (add x r)</langsyntaxhighlight>
 
Tasks examples
 
<langsyntaxhighlight lang=haskell>import Text.Printf
 
task bs ns = mapM_ putStrLn
Line 1,216:
, 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]</langsyntaxhighlight>
 
<pre>λ> task bins1 data1
Line 1,243:
'''Solution:'''
Using <code>Idotr</code> from [[j:User:Brian_Schott/Histogram|this JWiki page]]
<langsyntaxhighlight 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,255:
'%2d in [%3d, %3d)' printf (}.}: counts) ,. 2 ]\ x
'%2d in [%3d, ∞]' printf ({: counts) , {: x
)</langsyntaxhighlight>
 
'''Required Examples:'''
<langsyntaxhighlight lang=j>limits1=: 23 37 43 53 67 83
data1=: , 0&".;._2 noun define
95 21 94 12 99 4 70 75 83 93 52 80 57 5 53 86 65 17 92 83 71 61 54 58 47
Line 1,294:
6 in [591, 634)
16 in [634, 720)
59 in [720, ∞]</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang=java>import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Line 1,365:
printBins(limits, bins(limits, data));
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,399:
indefinitely large number of items to be processed. These items
could, but need not, be presented one line at a time.
<langsyntaxhighlight 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,416:
# Main program
reduce inputs as $x ({$limits, count: []}; bin($x))
| pp</langsyntaxhighlight>
{{out}}
Invocation:
<syntaxhighlight lang=sh>
<lang sh>
< data.json jq -rn --argfile limits limits.json -f program.jq
</syntaxhighlight>
</lang>
 
Example 1:
<syntaxhighlight lang=text>< 23 => 11
< 37 => 4
< 43 => 2
Line 1,430:
< 67 => 9
< 83 => 5
>= 83 => 13</langsyntaxhighlight>
 
Example 2:
<syntaxhighlight lang=text>< 14 => 3
< 18 => 0
< 249 => 44
Line 1,443:
< 634 => 6
< 720 => 16
>= 720 => 59</langsyntaxhighlight>
 
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang=julia>"""Add the function Python has in its bisect library"""
function bisect_right(array, x, low = 1, high = length(array) + 1)
while low < high
Line 1,500:
 
testbins()
</langsyntaxhighlight>{{out}}
<pre>
RC FIRST EXAMPLE:
Line 1,527:
=={{header|Lua}}==
Array indexing is 1-based, as is customary for Lua:
<langsyntaxhighlight lang=lua>
function binner(limits, data)
local bins = setmetatable({}, {__index=function() return 0 end})
Line 1,569:
bins = binner(limits, data)
printer(limits, bins)
</syntaxhighlight>
</lang>
{{out}}
<pre>PART 1:
Line 1,594:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>limits = {23, 37, 43, 53, 67, 83};
data = {95, 21, 94, 12, 99, 4, 70, 75, 83, 93, 52, 80, 57, 5, 53, 86,
65, 17, 92, 83, 71, 61, 54, 58, 47, 16, 8, 9, 32, 84, 7, 87, 46,
Line 1,621:
limits = {{-\[Infinity]}~Join~limits~Join~{\[Infinity]}};
BinCounts[data, limits]
MapThread[{#2, #1} &, {%, Partition[First[limits], 2, 1]}] // Grid</langsyntaxhighlight>
{{out}}
<pre>{11, 4, 2, 6, 9, 5, 13}
Line 1,647:
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang=Nim>import algorithm, strformat
 
func binIt(limits, data: openArray[int]): seq[Natural] =
Line 1,699:
160, 436, 717, 918, 8, 374, 101, 684, 727, 749]
let bins2 = binIt(Limits2, Data2)
binPrint(Limits2, bins2)</langsyntaxhighlight>
 
{{out}}
Line 1,725:
 
=={{header|Objective-C}}==
<langsyntaxhighlight lang=objc>#import <Foundation/Foundation.h>
 
NSArray<NSNumber *> *bins(NSArray<NSNumber *> *limits, NSArray<NSNumber *> *data) {
Line 1,788:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,816:
=={{header|Perl}}==
Borrowed <tt>bisect_right</tt> from Julia entry.
<langsyntaxhighlight lang=perl>use strict;
use warnings; no warnings 'uninitialized';
use feature 'say';
Line 1,872:
my @limits = (0, @{$tests[$_]{limits}}, Inf);
say bin_format \@limits, bin_it(\@limits,\@{$tests[$_]{data}});
}</langsyntaxhighlight>
{{out}}
<pre>[ 0, 23) => 11
Line 1,895:
 
But if we were to take to heart the warning that the input data was scary-big, then perhaps using a more efficient routine to classify the data into bins would be prudent (boilerplate/input/output same as above).
<langsyntaxhighlight lang=perl>use Math::SimpleHisto::XS;
 
for (@tests) {
Line 1,904:
printf "[%3d, %3d) => %3d\n", $lim[$_], ($lim[$_+1] == Inf ? 'Inf' : $lim[$_+1]), $$data_bins[$_] for 0..@lim-2;
print "\n";
}</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight lang=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,948:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,978:
The Counter module is ''not'' used as the number of bins is known allowing faster array access for incrementing bin counts versus dict lookup.
 
<langsyntaxhighlight lang=python>from bisect import bisect_right
 
def bin_it(limits: list, data: list) -> list:
Line 2,015:
466, 23,707,467, 33,670,921,180,991,396,160,436,717,918, 8,374,101,684,727,749]
bins = bin_it(limits, data)
bin_print(limits, bins)</langsyntaxhighlight>
 
{{out}}
Line 2,045:
 
Code such as 0:length(limits) is generally considered bad practice, but it didn't cause any problems here. To my amazement, this code works even if limits is of size 0 or 1. Even the <= printing doesn't break!
<langsyntaxhighlight 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,087:
createBin(limits2, data2)
printBin(limits2, createBin(limits2, data2))
oneLine(limits2, c(data1, data2))#Not needed.</langsyntaxhighlight>
{{out}}
<pre>> createBin(limits1, data1)
Line 2,132:
=={{header|Racket}}==
 
<langsyntaxhighlight lang=racket>#lang racket
 
(define (find-bin-index limits v)
Line 2,173:
 
(module+ main
(Bin-given-limits))</langsyntaxhighlight>
 
{{out}}
Line 2,197:
 
=={{header|Raku}}==
<syntaxhighlight lang=raku perl6line>
sub bin_it ( @limits, @data ) {
my @ranges = ( -Inf, |@limits, Inf ).rotor( 2 => -1 ).map: { .[0] ..^ .[1] };
Line 2,229:
say '';
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,255:
=={{header|REXX}}==
REXX programming note: &nbsp; since the sets of numbers defined don't have any leading signs, no quotes ('''<big>"</big>''') are needed.
<langsyntaxhighlight 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,296:
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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
Line 2,327:
 
=={{header|Ring}}==
<langsyntaxhighlight 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,369:
next
see ">= " + limit[n] + " := " + dn[n] + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,399:
=={{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.
<langsyntaxhighlight lang=ruby>Test = Struct.new(:limits, :data)
tests = Test.new( [23, 37, 43, 53, 67, 83],
[95,21,94,12,99,4,70,75,83,93,52,80,57,5,53,86,65,17,92,83,71,61,54,58,47,
Line 2,428:
puts
end
</syntaxhighlight>
</lang>
{{out}}
<pre>...23 11
Line 2,456:
A very simple and naive algorithm that uses nested dynamic arrays.
 
<langsyntaxhighlight lang=rust>fn make_bins(limits: &Vec<usize>, data: &Vec<usize>) -> Vec<Vec<usize>> {
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
Line 2,512:
print_bins(&limits2, &bins2);
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,541:
=={{header|Tcl}}==
For Tcl 8.6 (due to <code>lsearch -bisect</code>):
<langsyntaxhighlight lang=tcl>namespace path {::tcl::mathop ::tcl::mathfunc}
 
# Not necessary but useful helper
Line 2,581:
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]</langsyntaxhighlight>
 
{{out}}
Line 2,609:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang=ecmascript>import "/sort" for Find
import "/fmt" for Fmt
 
Line 2,666:
var bins = getBins.call(limitsList[i], dataList[i])
printBins.call(limitsList[i], bins)
}</langsyntaxhighlight>
 
{{out}}
10,333

edits