Combinations with repetitions: Difference between revisions

→‎Concise recursive: Changed to Wren S/H
(Add Factor)
(→‎Concise recursive: Changed to Wren S/H)
 
(12 intermediate revisions by 8 users not shown)
Line 156:
cwr( 3, 2)= 6
cwr(10, 3)= 220
</pre>
 
=={{header|IS-BASICAcornsoft Lisp}}==
{{trans|Scheme}}
 
<syntaxhighlight lang="is-basiclisp">100 PROGRAM "Combinat.bas"
(defun samples (k items)
(cond
((zerop k) '(()))
((null items) '())
(t (append
(mapc '(lambda (c) (cons (car items) c))
(samples (sub1 k) items))
(samples k (cdr items))))))
 
(defun append (a b)
(cond ((null a) b)
(t (cons (car a) (append (cdr a) b)))))
 
(defun length (list (len . 0))
(map '(lambda (e) (setq len (add1 len)))
list)
len)
rcomb=: (combr #) { ]</syntaxhighlight>
 
{{Out}}
 
<pre>
Evaluate : (samples 2 '(iced jam plain))
 
Value is : ((iced iced) (iced jam) (iced plain) (jam jam)
(jam plain) (plain plain))
 
Evaluate : (length (samples 3 '(1 2 3 4 5 6 7 8 9 10)))
 
Value is : 220
</pre>
 
Line 552 ⟶ 588:
{{Out}}
<pre>{220, {{"iced", "iced"}, {"jam", "iced"}, {"jam", "jam"}, {"plain", "iced"}, {"plain", "jam"}, {"plain", "plain"}}}</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">print combine.repeated.by:2 ["iced" "jam" "plain"]
 
print combine.count.repeated.by:3 @1..10</syntaxhighlight>
 
{{out}}
 
<pre>[iced iced] [iced jam] [iced plain] [jam jam] [jam plain] [plain plain]
220</pre>
 
=={{header|AutoHotkey}}==
Line 627 ⟶ 673:
</pre>
 
=={{header|BASIC}}==
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">arraybase 0
print "Enter n comb m. "
Line 654 ⟶ 700:
end subroutine</syntaxhighlight>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> DIM list$(2), chosen%(2)
Line 697 ⟶ 743:
Total choices = 220
</pre>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "Combinat.bas"
110 READ N
120 STRING D$(1 TO N)*5
130 FOR I=1 TO N
140 READ D$(I)
150 NEXT
160 FOR I=1 TO N
170 FOR J=I TO N
180 PRINT D$(I);" ";D$(J)
190 NEXT
200 NEXT
210 DATA 3,iced,jam,plain</syntaxhighlight>
 
=={{header|Bracmat}}==
Line 1,165 ⟶ 1,225:
=={{header|EasyLang}}==
 
<syntaxhighlight lang="text">items$[] = [ "iced" "jam" "plain" ]
items$[] = [ "iced" "jam" "plain" ]
n = len items$[]
k = 2
Line 1,171 ⟶ 1,232:
n_results = 0
#
funcproc output . .
n_results += 1
if len items$[] > 0
for r ins$ result[]= ""
writefor items$[r]i &= "1 "to k
s$ &= items$[result[i]] & " "
.
print "" .
print s$
.
.
.
funcproc combine pos val . .
if pos => k
call output
else
for i = val to n - 1
result[pos] = i
call combine pos + 1 i
.
.
.
call combine 01 01
#
n = 10
Line 1,197 ⟶ 1,259:
items$[] = [ ]
n_results = 0
call combine 01 01
print ""
print n_results & " results with 10 donuts"</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 1,801 ⟶ 1,864:
There are 220 possible combinations of 3 from 10
</pre>
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">100 PROGRAM "Combinat.bas"
110 READ N
120 STRING D$(1 TO N)*5
130 FOR I=1 TO N
140 READ D$(I)
150 NEXT
160 FOR I=1 TO N
170 FOR J=I TO N
180 PRINT D$(I);" ";D$(J)
190 NEXT
200 NEXT
210 DATA 3,iced,jam,plain</syntaxhighlight>
 
=={{header|J}}==
Line 1,845 ⟶ 1,894:
 
<syntaxhighlight lang="j">require 'stats'
rcomb=: (combrep #) { ]</syntaxhighlight>
combr=: i.@[ -"1~ [ comb + - 1:
rcomb=: (combr #) { ]</syntaxhighlight>
 
This definition of <code>rcomb</code> functionsbehaves identically to the previous one, and <code>combrcombrep</code> calculates indices:
 
<syntaxhighlight lang="j"> 2 combrcombrep 3
0 0
0 1
Line 1,858 ⟶ 1,906:
2 2</syntaxhighlight>
 
In other words: we<code>combrep</code> computecomputes <code>2 comb 4 </code> (note that 4 = (2 + 3)-1) and then subtractsubtracts from each column the minimum value in each column (<code>i. 2</code>).
 
=={{header|Java}}==
Line 2,739 ⟶ 2,787:
{{out}}
<pre>
220
</pre>
As of 1.0.2 there is a builtin combinations_with_repetitions() function. Using a string here for simplicity and neater output, but it works with any sequence:
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">combinations_with_repetitions</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"ijp"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #008000;">','</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">combinations_with_repetitions</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">),</span><span style="color: #000000;">3</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
"ii,ij,ip,jj,jp,pp"
220
</pre>
Line 3,979 ⟶ 4,037:
{{trans|Go}}
Produces results in no particular order.
<syntaxhighlight lang="ecmascriptwren">var combrepCombrep //= recursiveFn.new { |n, lst|
combrep = Fn.new { |n, lst|
if (n == 0 ) return [[]]
if (lst.count == 0) return []
var r = combrepCombrep.call(n, lst[1..-1])
for (x in combrepCombrep.call(n-1, lst)) {
var y = x.toList
y.add(lst[0])
Line 3,992 ⟶ 4,049:
}
 
System.print(combrepCombrep.call(2, ["iced", "jam", "plain"]))
System.print(combrepCombrep.call(3, (1..10).toList).count)</syntaxhighlight>
 
{{out}}
Line 4,000 ⟶ 4,057:
220
</pre>
 
===Library based===
{{libheader|Wren-seq}}
{{libheader|Wren-perm}}
Produces results in lexicographic order.
<syntaxhighlight lang="ecmascriptwren">import "./seq" for Lst
import "./perm" for Comb
 
9,482

edits