Bin given limits: Difference between revisions

→‎{{header|REXX}}: minor formatting correction
m (→‎{{header|Wren}}: Minor tidy)
(→‎{{header|REXX}}: minor formatting correction)
 
(5 intermediate revisions by 5 users not shown)
Line 883:
720 - inf : 59
------------------------------------------</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
global limits[] data[] .
#
proc count . .
len cnt[] len limits[] + 1
#
for e in data[]
for i to len limits[]
if e < limits[i]
break 1
.
.
cnt[i] += 1
.
for i to len limits[]
print "< " & limits[i] & " : " & cnt[i]
.
print "Rest: " & cnt[i]
print ""
.
limits[] = [ 23 37 43 53 67 83 ]
data[] = [ 95 21 94 12 99 4 70 75 83 93 52 80 57 5 53 86 65 17 92 83 71 61 54 58 47 16 8 9 32 84 7 87 46 19 30 37 96 6 98 40 79 97 45 64 60 29 49 36 43 55 ]
count
#
limits[] = [ 14 18 249 312 389 392 513 591 634 720 ]
data[] = [ 445 814 519 697 700 130 255 889 481 122 932 77 323 525 570 219 367 523 442 933 416 589 930 373 202 253 775 47 731 685 293 126 133 450 545 100 741 583 763 306 655 267 248 477 549 238 62 678 98 534 622 907 406 714 184 391 913 42 560 247 346 860 56 138 546 38 985 948 58 213 799 319 390 634 458 945 733 507 916 123 345 110 720 917 313 845 426 9 457 628 410 723 354 895 881 953 677 137 397 97 854 740 83 216 421 94 517 479 292 963 376 981 480 39 257 272 157 5 316 395 787 942 456 242 759 898 576 67 298 425 894 435 831 241 989 614 987 770 384 692 698 765 331 487 251 600 879 342 982 527 736 795 585 40 54 901 408 359 577 237 605 847 353 968 832 205 838 427 876 959 686 646 835 127 621 892 443 198 988 791 466 23 707 467 33 670 921 180 991 396 160 436 717 918 8 374 101 684 727 749 ]
count
</syntaxhighlight>
 
=={{header|Factor}}==
Factor provides the <code>bisect-right</code> word in the <code>sorting.extras</code> vocabulary. See the implementation [https://docs.factorcode.org/content/word-bisect-right%2Csorting.extras.html here].
Line 957 ⟶ 987:
59 members in [720, ∞)
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">sub binlims( dat() as integer, limits() as integer, bins() as uinteger )
Line 2,018 ⟶ 2,049:
>= 634 .. < 720 := 16
>= 720 := 59</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ 2dup peek 1+ unrot poke ] is inc ( [ n --> [ )
 
[ dup size 1+ 0 swap of
swap rot witheach
[ over
findwith [ over > ] drop
swap dip inc ]
drop ] is ->bins ( [ [ --> [ )
 
[ tuck ->bins
say "Less than "
over 0 peek echo
say ": " behead echo cr
dup size 1 - times
[ over i^ peek echo
say " or more, "
say "but less than "
over i^ 1+ peek echo
say ": "
dup i^ peek echo cr ]
swap
-1 peek echo
say " or more: "
-1 peek echo cr ] is report ( [ [ --> )
 
' [ 95 21 94 12 99 4 70 75 83 93
52 80 57 5 53 86 65 17 92 83
71 61 54 58 47 16 8 9 32 84
7 87 46 19 30 37 96 6 98 40
79 97 45 64 60 29 49 36 43 55 ]
 
' [ 23 37 43 53 67 83 ]
 
say "Part 1" cr cr
report cr cr
 
' [ 445 814 519 697 700 130 255 889 481 122
932 77 323 525 570 219 367 523 442 933
416 589 930 373 202 253 775 47 731 685
293 126 133 450 545 100 741 583 763 306
655 267 248 477 549 238 62 678 98 534
622 907 406 714 184 391 913 42 560 247
346 860 56 138 546 38 985 948 58 213
799 319 390 634 458 945 733 507 916 123
345 110 720 917 313 845 426 9 457 628
410 723 354 895 881 953 677 137 397 97
854 740 83 216 421 94 517 479 292 963
376 981 480 39 257 272 157 5 316 395
787 942 456 242 759 898 576 67 298 425
894 435 831 241 989 614 987 770 384 692
698 765 331 487 251 600 879 342 982 527
736 795 585 40 54 901 408 359 577 237
605 847 353 968 832 205 838 427 876 959
686 646 835 127 621 892 443 198 988 791
466 23 707 467 33 670 921 180 991 396
160 436 717 918 8 374 101 684 727 749 ]
 
' [ 14 18 249 312 389 392 513 591 634 720 ]
 
say "Part 2" cr cr
report</syntaxhighlight>
 
{{out}}
 
<pre>Part 1
 
Less than 23: 11
23 or more, but less than 37: 4
37 or more, but less than 43: 2
43 or more, but less than 53: 6
53 or more, but less than 67: 9
67 or more, but less than 83: 5
83 or more: 13
 
 
Part 2
 
Less than 14: 3
14 or more, but less than 18: 0
18 or more, but less than 249: 44
249 or more, but less than 312: 10
312 or more, but less than 389: 16
389 or more, but less than 392: 2
392 or more, but less than 513: 28
513 or more, but less than 591: 16
591 or more, but less than 634: 6
634 or more, but less than 720: 16
720 or more: 59</pre>
 
=={{header|R}}==
This is R's bread and butter. Even with only the base library, the only thing stopping us from giving a one-line solution is the task's requirement of using two functions.
Line 2,229 ⟶ 2,352:
=={{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<----- 1st set of bin limits & data.*/
data= 95 21 94 12 99 4 70 75 83 93 52 80 57 5 53 86 65 17 92 83 71 61 54 58 47 ,
16 8 9 32 84 7 87 46 19 30 37 96 6 98 40 79 97 45 64 60 29 49 36 43 55
call limslimits lims; call bins data
call bins data
call show 'the 1st set of bin counts for the specified data:'
Do 3; say''; End
say; say; say
lims= 14 18 249 312 389 392 513 591 634 720 /* ◄■■■■■■2nd<-----2nd set of bin limits & data.*/
data= 445 814 519 697 700 130 255 889 481 122 932 77 323 525 570 219 367 523 442 933 ,
416 589 930 373 202 253 775 47 731 685 293 126 133 450 545 100 741 583 763 306 ,
655 267 248 477 549 238 62 678 98 534 622 907 406 714 184 391 913 42 560 247 ,
346 860 56 138 546 38 985 948 58 213 799 319 390 634 458 945 733 507 916 123 ,
345 110 720 917 313 845 426 9 457 628 410 723 354 895 881 953 677 137 397 97 ,
854 740 83 216 421 94 517 479 292 963 376 981 480 39 257 272 157 5 316 395 ,
787 942 456 242 759 898 576 67 298 425 894 435 831 241 989 614 987 770 384 692 ,
698 765 331 487 251 600 879 342 982 527 736 795 585 40 54 901 408 359 577 237 ,
605 847 353 968 832 205 838 427 876 959 686 646 835 127 621 892 443 198 988 791 ,
466 23 707 467 33 670 921 180 991 396 160 436 717 918 8 374 101 684 727 749
call limslimits lims; call bins data
call bins data
call show 'the 2nd set of bin counts for the specified data:'
exit 0
exit 0 /*stick a fork in it, we're all done. */
/*------------------------------------------------------------------------------*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
bins:
bins: parse arg nums; !.= 0; datum= words(nums); wc= length(datum) /*max width count.*/
Parse Arg numbers
do j=1 for datum; x= word(nums, j)
count.=0
do k=0 for # /*find the bin that this number is in. */
Do j=1 To words(numbers)
if x < @.k then do; !.k= !.k + 1; iterate j; end /*bump a bin count*/
x=word(numbers,j)
end /*k*/
Do k=1 To bins-1 Until !x<limit.k= !.k +/* 1determine which bin is to /*numberbe used is > the highest bin specified*/
End
end /*j*/; return
count.k=count.k+1 /* increment count for this bin */
/*──────────────────────────────────────────────────────────────────────────────────────*/
End
lims: parse arg limList; #= words(limList); wb= 0 /*max width binLim*/
count_length=0 do j=1 for #; _= j - 1; @._= word(limList, j);/* compute the wb=length max(wb,of the largest count length(@._) )*/
Do k=0 To bins
end /*j*/; return
count_length=max(count_length,length(count.k))
/*──────────────────────────────────────────────────────────────────────────────────────*/
End
show: parse arg t; say center(t, 51 ); $= left('', 9) /*$: for indentation*/
Return
say center('', 51, "═") /*show title separator.*/
/*------------------------------------------------------------------------------*/
jp= # - 1; ge= '≥'; le='<'; eq= ' count='
limits:
do j=0 for #; jm= j - 1; bin= right(@.j, wb)
Parse Arg limlist
if j==0 then say $ left('', length(ge) +3+wb+length(..) )le bin eq right(!.j, wc)
limit_length=0
else say $ ge right(@.jm, wb) .. le bin eq right(!.j, wc)
bins=words(limlist)+1 /* number of bins */
if j==jp then say $ ge right(@.jp,wb) left('', 3+length(..)+wb) eq right(!.#, wc)
limit.=''
end /*j*/; return</syntaxhighlight>
Do j=1 To bins-1
limit.j=word(limlist,j) /* lower limit of bin j */
limit_length=max(limit_length,length(limit.j)) /* length of largest limit */
End
Return
/*------------------------------------------------------------------------------*/
show:
Say arg(1)
Say copies('-',51)
ll=limit_length
do k=1 To bins
km1=k-1
Select
When k=1 Then
range=' ' right('' ,ll) ' <' right(limit.k,ll)
When k<bins Then
range='>=' right(limit.km1,ll) '.. <' right(limit.k,ll)
Otherwise
range='>=' right(limit.km1,ll) ' ' right('' ,ll)
End
Say ' 'range ' count=' right(count.k,count_length)
End
Return
</syntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
the 1st set of bin counts for the specified data:
---------------------------------------------------
═══════════════════════════════════════════════════
< 23 count= 11
>= 23 .. < 37 count= 4
>= 37 .. < 43 count= 2
>= 43 .. < 53 count= 6
>= 53 .. < 67 count= 9
>= 67 .. < 83 count= 5
>= 83 count= 13
 
 
 
the 2nd set of bin counts for the specified data:
---------------------------------------------------
═══════════════════════════════════════════════════
< 14 count= 3
>= 14 .. < 18 count= 0
>= 18 .. < 249 count= 44
>= 249 .. < 312 count= 10
>= 312 .. < 389 count= 16
>= 389 .. < 392 count= 2
>= 392 .. < 513 count= 28
>= 513 .. < 591 count= 16
>= 591 .. < 634 count= 6
>= 634 .. < 720 count= 16
>= 720 count= 59
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
Line 2,368 ⟶ 2,518:
>= 634 and < 720 := 16
>= 720 := 59
</pre>
=={{header|RPL}}==
{{works with|HP|49}}
« → limits data
« { } limits SIZE 1 + + 0 CON
1 data SIZE '''FOR''' j
limits data j GET OVER SIZE NDUPN →LIST - SIGN
'''CASE'''
DUP 0 POS '''THEN''' LASTARG 1 + NIP '''END'''
1 POS '''THEN''' LASTARG '''END'''
DUP SIZE
'''END'''
DUP2 GET 1 + PUT
'''NEXT'''
» » '<span style="color:blue">VBINS</span>' STO
« → limits bins
« 1 bins SIZE 1 GET '''FOR''' j
'''IFERR''' limits j 1 - GET '''THEN''' DROP2 "" '''ELSE''' "≤" + '''END'''
'''IFERR''' limits j GET '''THEN''' DROP2 "x" '''ELSE''' "x<" SWAP + '''END'''
+ ": " b j GET +
'''NEXT'''
» » '<span style="color:blue">SHOWBINS</span>' STO
 
{23, 37, 43, 53, 67, 83} DUP
{95,21,94,12,99,4,70,75,83,93,52,80,57,5,53,86,65,17,92,83,71,61,54,58,47,16,8,9,32,84,7,87,46,19,30,37,96,6,98,40,79,97,45,64,60,29,49,36,43,55} <span style="color:blue">VBINS SHOWBINS</span>
 
CLEAR {14,18,249,312,389,392,513,591,634,720} DUP
{445,814,519,697,700,130,255,889,481,122,932,77,323,525,570,219,367,523,442,933,416,589,930,373,202,253,775,47,731,685,293,126,133,450,545,100,741,583,763,306,655,267,248,477,549,238,62,678,98,534,622,907,406,714,184,391,913,42,560,247,346,860,56,138,546,38,985,948,58,213,799,319,390,634,458,945,733,507,916,123,345,110,720,917,313,845,426,9,457,628,410,723,354,895,881,953,677,137,397,97,854,740,83,216,421,94,517,479,292,963,376,981,480,39,257,272,157,5,316,395,787,942,456,242,759,898,576,67,298,425,894,435,831,241,989,614,987,770,384,692,698,765,331,487,251,600,879,342,982,527,736,795,585,40,54,901,408,359,577,237,605,847,353,968,832,205,838,427,876,959,686,646,835,127,621,892,443,198,988,791,466,23,707,467,33,670,921,180,991,396,160,436,717,918,8,374,101,684,727,749} <span style="color:blue">VBINS SHOWBINS</span>
{{out}}
<pre>
7: "x<23: 11"
6: "23≤x<37: 4"
5: "37≤x<43: 2"
4: "43≤x<53: 6"
3: "53≤x<67: 9"
2: "67≤x<83: 5"
1: "83≤x: 13"
 
11: "x<14: 3"
10: "14≤x<18: 0"
9: "18≤x<249: 44"
8: "249≤x<312: 10"
7: "312≤x<389: 16"
6: "389≤x<392: 2"
5: "392≤x<513: 28"
4: "513≤x<591: 16"
3: "591≤x<634: 6"
2: "634≤x<720: 16"
1: "720≤x: 59"
</pre>
=={{header|Ruby}}==
Line 2,422 ⟶ 2,622:
720... 59
</pre>
 
=={{header|Rust}}==
{{works with|rustc|1.49.0}}
Line 2,509 ⟶ 2,710:
>= 720 := 59
</pre>
 
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
object Bins extends App {
 
def bins[T](limits: List[T], data: Iterable[T])(implicit ord: Ordering[T]): Array[Int] = {
val result = new Array[Int](limits.size + 1)
for (n <- data) {
val i = limits.search(n)(ord) match {
case scala.collection.Searching.Found(i) => i + 1
case scala.collection.Searching.InsertionPoint(i) => i
}
result(i) += 1
}
result
}
 
def printBins(limits: List[_], bins: Array[Int]): Unit = {
val n = limits.size
if (n == 0) return
 
assert(n + 1 == bins.length)
 
println(f" < ${limits.head}%3s: ${bins(0)}%2d")
for (i <- 1 until n) {
println(f">= ${limits(i - 1)}%3s and < ${limits(i)}%3s: ${bins(i)}%2d")
}
println(f">= ${limits.last}%3s : ${bins(n)}%2d")
}
 
val limits1 = List(23, 37, 43, 53, 67, 83)
val data1 = List(95, 21, 94, 12, 99, 4, 70, 75, 83, 93, 52, 80, 57, 5, 53, 86, 65, 17, 92, 83, 71, 61, 54, 58, 47, 16, 8, 9, 32, 84, 7, 87, 46, 19, 30, 37, 96, 6, 98, 40, 79, 97, 45, 64, 60, 29, 49, 36, 43, 55)
 
println("Example 1:")
printBins(limits1, bins(limits1, data1))
 
val limits2 = List(14, 18, 249, 312, 389, 392, 513, 591, 634, 720)
val data2 = List(445, 814, 519, 697, 700, 130, 255, 889, 481, 122, 932, 77, 323, 525, 570, 219, 367, 523, 442, 933, 416, 589, 930, 373, 202, 253, 775, 47, 731, 685, 293, 126, 133, 450, 545, 100, 741, 583, 763, 306, 655, 267, 248, 477, 549, 238, 62, 678, 98, 534, 622, 907, 406, 714, 184, 391, 913, 42, 560, 247, 346, 860, 56, 138, 546, 38, 985, 948, 58, 213, 799, 319, 390, 634, 458, 945, 733, 507, 916, 123, 345, 110, 720, 917, 313, 845, 426, 9, 457, 628, 410, 723, 354, 895, 881, 953, 677, 137, 397, 97, 854, 740, 83, 216, 421, 94, 517, 479, 292, 963, 376, 981, 480, 39, 257, 272, 157, 5, 316, 395, 787, 942, 456, 242, 759, 898, 576, 67, 298, 425, 894, 435, 831, 241, 989, 614, 987, 770, 384, 692, 698, 765, 331, 487, 251, 600, 879, 342, 982, 527, 736, 795, 585, 40, 54, 901, 408, 359, 577, 237, 605, 847, 353, 968, 832, 205, 838, 427, 876, 959, 686, 646, 835, 127, 621, 892, 443, 198, 988, 791, 466, 23, 707, 467, 33, 670, 921, 180, 991, 396, 160, 436, 717, 918, 8, 374, 101, 684, 727, 749)
 
println()
println("Example 2:")
printBins(limits2, bins(limits2, data2))
}
</syntaxhighlight>
{{out}}
<pre>
Example 1:
< 23: 11
>= 23 and < 37: 4
>= 37 and < 43: 2
>= 43 and < 53: 6
>= 53 and < 67: 9
>= 67 and < 83: 5
>= 83 : 13
 
Example 2:
< 14: 3
>= 14 and < 18: 0
>= 18 and < 249: 44
>= 249 and < 312: 10
>= 312 and < 389: 16
>= 389 and < 392: 2
>= 392 and < 513: 28
>= 513 and < 591: 16
>= 591 and < 634: 6
>= 634 and < 720: 16
>= 720 : 59
 
</pre>
 
=={{header|Tcl}}==
For Tcl 8.6 (due to <code>lsearch -bisect</code>):
2,295

edits