Combinations with repetitions: Difference between revisions

→‎Concise recursive: Changed to Wren S/H
(→‎Concise recursive: Changed to Wren S/H)
 
(5 intermediate revisions by 4 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 1,190 ⟶ 1,226:
 
<syntaxhighlight lang="text">
items$[] = [ "iced" "jam" "plain" ]
n = len items$[]
k = 2
len result[] k
n_results = 0
#
proc output . .
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
call output
else
for i = val to n
result[pos] = i
call combine pos + 1 i
.
.
.
call combine 1 1
#
n = 10
Line 1,207 ⟶ 1,259:
items$[] = [ ]
n_results = 0
call combine 1 1
print ""
print n_results & " results with 10 donuts"
Line 2,735 ⟶ 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,975 ⟶ 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,988 ⟶ 4,049:
}
 
System.print(combrepCombrep.call(2, ["iced", "jam", "plain"]))
System.print(combrepCombrep.call(3, (1..10).toList).count)</syntaxhighlight>
 
{{out}}
Line 3,996 ⟶ 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