Bin given limits: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (→{{header|C sharp}}: Regularize header markup to recommended on category page) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 46:
{{trans|Python}}
<
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)</
{{out}}
Line 120:
=={{header|Action!}}==
<
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</
{{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:'''
<
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>
'''package body:'''
<
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Line 294:
end binning;
</syntaxhighlight>
'''main procedure:'''
<
with binning; use binning;
Line 330:
Print (Limits => Limits_2, Bin_Result => Bin_2);
end Main;
</syntaxhighlight>
{output}
<pre>
Line 357:
=={{header|ALGOL 68}}==
<
# 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</
{{out}}
<pre>
Line 457:
=={{header|AutoHotkey}}==
<
bin := [], counter := 0
for i, val in data {
Line 473:
}
return output .= (prevlimit ? prevlimit : "-∞") ", ∞ : " ((x:=bin["∞"].Count())?x:0) "`n"
}</
Examples:<
data := [95,21,94,12,99,4,70,75,83,93,52,80,57,5,53,86,65,17,92,83,71,61,54,58,47,16
, 8, 9,32,84,7,87,46,19,30,37,96,6,98,40,79,97,45,64,60,29,49,36,43,55]
Line 491:
,466, 23,707,467, 33,670,921,180,991,396,160,436,717,918, 8,374,101,684,727,749]
MsgBox, 262144, , % Bin_given_limits(limits, data)
return</
{{out}}
<pre>
Line 516:
=={{header|C}}==
<
#include <stdlib.h>
Line 598:
return EXIT_SUCCESS;
}</
{{out}}
Line 626:
=={{header|C++}}==
<
#include <cassert>
#include <iomanip>
Line 688:
std::cout << "\nExample 2:\n";
print_bins(limits2, bins(limits2, data2));
}</
{{out}}
Line 716:
=={{header|C sharp|C#}}==
<
public class Program
Line 760:
return bins;
}
}</
{{out}}
<pre>
Line 785:
=={{header|CLU}}==
<
% CLU allows arrays to start at any index; the result array
% will have the same lower bound as the limit array.
Line 869:
display_bins[int](limits1, data1)
display_bins[int](limits2, data2)
end start_up</
{{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].
<
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</
{{out}}
<pre>
Line 969:
=={{header|FreeBASIC}}==
<
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))</
{{out}}
<pre>=====EXAMPLE ONE=====
Line 1,045:
=={{header|Go}}==
<
import (
Line 1,105:
printBins(limitsList[i], bins)
}
}</
{{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.
<
import Data.List (partition)
Line 1,148:
binCounts :: Ord a => [a] -> [a] -> [Int]
binCounts b = fmap length . binSplit b</
<pre>λ> binSplit [2,4,7] [1,4,2,6,3,8,9,4,1,2,7,4,1,5,1]
Line 1,157:
More efficient binning procedure exploits the binary search tree.
<
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)</
Tasks examples
<
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]</
<pre>λ> task bins1 data1
Line 1,243:
'''Solution:'''
Using <code>Idotr</code> from [[j:User:Brian_Schott/Histogram|this JWiki page]]
<
binnedData=: adverb define
bidx=. i.@>:@# x NB. indicies of bins
Line 1,255:
'%2d in [%3d, %3d)' printf (}.}: counts) ,. 2 ]\ x
'%2d in [%3d, ∞]' printf ({: counts) , {: x
)</
'''Required Examples:'''
<
data1=: , 0&".;._2 noun define
95 21 94 12 99 4 70 75 83 93 52 80 57 5 53 86 65 17 92 83 71 61 54 58 47
Line 1,294:
6 in [591, 634)
16 in [634, 720)
59 in [720, ∞]</
=={{header|Java}}==
<
import java.util.Collections;
import java.util.List;
Line 1,365:
printBins(limits, bins(limits, data));
}
}</
{{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.
<
# .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</
{{out}}
Invocation:
<syntaxhighlight lang=sh>
< data.json jq -rn --argfile limits limits.json -f program.jq
</syntaxhighlight>
Example 1:
<syntaxhighlight lang=text>< 23 => 11
< 37 => 4
< 43 => 2
Line 1,430:
< 67 => 9
< 83 => 5
>= 83 => 13</
Example 2:
<syntaxhighlight lang=text>< 14 => 3
< 18 => 0
< 249 => 44
Line 1,443:
< 634 => 6
< 720 => 16
>= 720 => 59</
=={{header|Julia}}==
{{trans|Python}}
<
function bisect_right(array, x, low = 1, high = length(array) + 1)
while low < high
Line 1,500:
testbins()
</
<pre>
RC FIRST EXAMPLE:
Line 1,527:
=={{header|Lua}}==
Array indexing is 1-based, as is customary for Lua:
<
function binner(limits, data)
local bins = setmetatable({}, {__index=function() return 0 end})
Line 1,569:
bins = binner(limits, data)
printer(limits, bins)
</syntaxhighlight>
{{out}}
<pre>PART 1:
Line 1,594:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
data = {95, 21, 94, 12, 99, 4, 70, 75, 83, 93, 52, 80, 57, 5, 53, 86,
65, 17, 92, 83, 71, 61, 54, 58, 47, 16, 8, 9, 32, 84, 7, 87, 46,
Line 1,621:
limits = {{-\[Infinity]}~Join~limits~Join~{\[Infinity]}};
BinCounts[data, limits]
MapThread[{#2, #1} &, {%, Partition[First[limits], 2, 1]}] // Grid</
{{out}}
<pre>{11, 4, 2, 6, 9, 5, 13}
Line 1,647:
=={{header|Nim}}==
{{trans|Python}}
<
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)</
{{out}}
Line 1,725:
=={{header|Objective-C}}==
<
NSArray<NSNumber *> *bins(NSArray<NSNumber *> *limits, NSArray<NSNumber *> *data) {
Line 1,788:
}
return 0;
}</
{{out}}
<pre>
Line 1,816:
=={{header|Perl}}==
Borrowed <tt>bisect_right</tt> from Julia entry.
<
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}});
}</
{{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).
<
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";
}</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">bin_it</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">limits</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">)</span>
Line 1,948:
<span style="color: #000000;">466</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">23</span><span style="color: #0000FF;">,</span><span style="color: #000000;">707</span><span style="color: #0000FF;">,</span><span style="color: #000000;">467</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">33</span><span style="color: #0000FF;">,</span><span style="color: #000000;">670</span><span style="color: #0000FF;">,</span><span style="color: #000000;">921</span><span style="color: #0000FF;">,</span><span style="color: #000000;">180</span><span style="color: #0000FF;">,</span><span style="color: #000000;">991</span><span style="color: #0000FF;">,</span><span style="color: #000000;">396</span><span style="color: #0000FF;">,</span><span style="color: #000000;">160</span><span style="color: #0000FF;">,</span><span style="color: #000000;">436</span><span style="color: #0000FF;">,</span><span style="color: #000000;">717</span><span style="color: #0000FF;">,</span><span style="color: #000000;">918</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">374</span><span style="color: #0000FF;">,</span><span style="color: #000000;">101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">684</span><span style="color: #0000FF;">,</span><span style="color: #000000;">727</span><span style="color: #0000FF;">,</span><span style="color: #000000;">749</span><span style="color: #0000FF;">}</span>
<span style="color: #000000;">bin_print</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bin_it</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">))</span>
<!--</
{{out}}
<pre>
Line 1,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.
<
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)</
{{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!
<
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.</
{{out}}
<pre>> createBin(limits1, data1)
Line 2,132:
=={{header|Racket}}==
<
(define (find-bin-index limits v)
Line 2,173:
(module+ main
(Bin-given-limits))</
{{out}}
Line 2,197:
=={{header|Raku}}==
<syntaxhighlight lang=raku
sub bin_it ( @limits, @data ) {
my @ranges = ( -Inf, |@limits, Inf ).rotor( 2 => -1 ).map: { .[0] ..^ .[1] };
Line 2,229:
say '';
}
</syntaxhighlight>
{{out}}
<pre>
Line 2,255:
=={{header|REXX}}==
REXX programming note: since the sets of numbers defined don't have any leading signs, no quotes ('''<big>"</big>''') are needed.
<
lims= 23 37 43 53 67 83 /* ◄■■■■■■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</
{{out|output|text= when using the internal default input:}}
<pre>
Line 2,327:
=={{header|Ring}}==
<
limit = [0, 23, 37, 43, 53, 67, 83]
data = [95,21,94,12,99,4,70,75,83,93,52,80,57,5,53,86,65,17,92,83,71,61,54,58,47,
Line 2,369:
next
see ">= " + limit[n] + " := " + dn[n] + nl
</syntaxhighlight>
{{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.
<
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>
{{out}}
<pre>...23 11
Line 2,456:
A very simple and naive algorithm that uses nested dynamic arrays.
<
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
Line 2,512:
print_bins(&limits2, &bins2);
}
</syntaxhighlight>
{{out}}
Line 2,541:
=={{header|Tcl}}==
For Tcl 8.6 (due to <code>lsearch -bisect</code>):
<
# 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]</
{{out}}
Line 2,609:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 2,666:
var bins = getBins.call(limitsList[i], dataList[i])
printBins.call(limitsList[i], bins)
}</
{{out}}
|