Combinations with repetitions: Difference between revisions

→‎Concise recursive: Changed to Wren S/H
(→‎Concise recursive: Changed to Wren S/H)
 
(9 intermediate revisions by 7 users not shown)
Line 156:
cwr( 3, 2)= 6
cwr(10, 3)= 220
</pre>
 
=={{header|Acornsoft Lisp}}==
{{trans|Scheme}}
 
<syntaxhighlight lang="lisp">
(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)
</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,166 ⟶ 1,226:
 
<syntaxhighlight lang="text">
items$[] = [ "iced" "jam" "plain" ]
func combine pos val . .
n = len items$[]
if pos > k
k = 2
call output
len result[] k
else
n_results = 0
for i = val to n
#
result[pos] = i
proc output . .
call combine pos + 1 i
n_results .+= 1
if len items$[] > 0
.
s$ = ""
for i = 1 to k
s$ &= items$[result[i]] & " "
.
print s$
.
.
proc combine pos val . .
if pos > k
output
else
for i = val to n
result[pos] = i
combine pos + 1 i
.
.
.
call combine 1 1
#
n = 10
Line 1,183 ⟶ 1,259:
items$[] = [ ]
n_results = 0
call combine 1 1
print ""
print n_results & " results with 10 donuts"
Line 1,788 ⟶ 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 2,725 ⟶ 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,965 ⟶ 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,978 ⟶ 4,049:
}
 
System.print(combrepCombrep.call(2, ["iced", "jam", "plain"]))
System.print(combrepCombrep.call(3, (1..10).toList).count)</syntaxhighlight>
 
{{out}}
Line 3,986 ⟶ 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,476

edits