Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,11 @@
(defn combinations
"Generate the combinations of n elements from a list of [0..m)"
[m n]
(let [xs (range m)]
(loop [i (int 0) res #{#{}}]
(if (== i n)
res
(recur (+ 1 i)
(set (for [x xs r res
:when (not-any? #{x} r)]
(conj r x))))))))

View file

@ -1,71 +1,88 @@
class
COMBINATIONS
create
make
feature
make(n, k:INTEGER)
make (n, k: INTEGER)
require
n_positive: n>0
k_positive: k>0
k_smaller_equal: k<=n
n_positive: n > 0
k_positive: k > 0
k_smaller_equal: k <= n
do
create set.make
set.extend ("")
create sol.make
sol:=solve(set,k,n-k)
sol:= conv_sol(n, sol)
sol := solve (set, k, n - k)
sol := convert_solution (n, sol)
ensure
correct_num_of_sol: num_of_comb(n,k)= sol.count
correct_num_of_sol: num_of_comb (n, k) = sol.count
end
set: LINKED_LIST[STRING]
sol: LINKED_LIST[STRING]
conv_sol(n: INTEGER; solution: LINKED_LIST[STRING]):LINKED_LIST[STRING]
local
i,j: INTEGER
temp: STRING
do
create temp.make (n)
from
i:=1
until
i>solution.count
loop
from
j:= 1
until
j> n
loop
if solution[i].at (j)= '1' then
temp.append (j.out)
end
j:= j+1
end
solution[i].deep_copy( temp)
temp.wipe_out
i:= i+1
end
Result:= solution
end
sol: LINKED_LIST [STRING]
solve(seta: LINKED_LIST[STRING];one,zero: INTEGER): LINKED_LIST[STRING]
feature {None}
set: LINKED_LIST [STRING]
convert_solution (n: INTEGER; solution: LINKED_LIST [STRING]): LINKED_LIST [STRING]
-- strings of 'k' digits between 1 and 'n'
local
new_P1, new_P0: LINKED_LIST[STRING]
i, j: INTEGER
temp: STRING
do
create temp.make (n)
from
i := 1
until
i > solution.count
loop
from
j := 1
until
j > n
loop
if solution [i].at (j) = '1' then
temp.append (j.out)
end
j := j + 1
end
solution [i].deep_copy (temp)
temp.wipe_out
i := i + 1
end
Result := solution
end
solve (seta: LINKED_LIST [STRING]; one, zero: INTEGER): LINKED_LIST [STRING]
-- list of strings with a number of 'one' 1s and 'zero' 0, standig for wether the corresponing digit is taken or not.
local
new_P1, new_P0: LINKED_LIST [STRING]
do
create new_P1.make
create new_P0.make
if one > 0 then
new_P1.deep_copy(seta)
across new_P1 as P1 loop new_P1.item.append ("1") end
new_P1:=solve(new_P1, one-1, zero)
end
if zero > 0 then
new_P0.deep_copy(seta)
across new_P0 as P0 loop new_P0.item.append ("0") end
new_P0:=solve(new_P0, one, zero-1)
if one > 0 then
new_P1.deep_copy (seta)
across
new_P1 as P1
loop
new_P1.item.append ("1")
end
new_P1 := solve (new_P1, one - 1, zero)
end
if one=0 and zero= 0 then
Result:= seta
if zero > 0 then
new_P0.deep_copy (seta)
across
new_P0 as P0
loop
new_P0.item.append ("0")
end
new_P0 := solve (new_P0, one, zero - 1)
end
if one = 0 and zero = 0 then
Result := seta
else
create Result.make
Result.fill (new_p0)
@ -73,24 +90,25 @@ feature
end
end
num_of_comb (n,k:INTEGER):INTEGER
--- used for contracts
num_of_comb (n, k: INTEGER): INTEGER
-- number of 'k' sized combinations out of 'n'.
local
upper, lower, m, l: INTEGER
do
upper:= 1
lower:= 1
m:= n
l:= k
upper := 1
lower := 1
m := n
l := k
from
until
m<n-k+1
m < n - k + 1
loop
upper:= m*upper
lower:= l*lower
m:= m-1
l:= l-1
upper := m * upper
lower := l * lower
m := m - 1
l := l - 1
end
Result:= upper//lower
Result := upper // lower
end
end

View file

@ -1,14 +1,21 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature
make
do
create comb.make (5, 3)
across comb.sol as ar loop io.put_string (ar.item.out+"%T") end
end
feature
make
do
create comb.make (5, 3)
across
comb.sol as ar
loop
io.put_string (ar.item.out + "%T")
end
end
comb: COMBINATIONS
end

View file

@ -10,7 +10,7 @@
#symbol numbers = (:anN)
[
arrayControl new &length:(anN int) &each: anIndex [ Integer new:(anIndex + 1) ]
Array new &length:(anN int) set &every: (&index:n) [ Integer new &int:n ]
].
// --- Program ---
@ -18,8 +18,8 @@
#symbol program =
[
#var aNumbers := numbers:N.
control for:(Combinator new:(arrayControl new &length:M &each: i [ aNumbers ])) &do: aRow
Combinator new:M &of:aNumbers run &each: aRow
[
consoleEx writeLine:aRow.
console writeLine:aRow.
].
].

View file

@ -0,0 +1,11 @@
defmodule RC do
def comb(0, _), do: [[]]
def comb(_, []), do: []
def comb(m, [h|t]) do
(for l <- comb(m-1, t), do: [h|l]) ++ comb(m, t)
end
end
{m, n} = {3, 5}
list = for i <- 1..n, do: i
Enum.each(RC.comb(m, list), fn x -> IO.inspect x end)

View file

@ -1,5 +1 @@
comb1=: dyad define
c=. 1 {.~ - d=. 1+y-x
z=. i.1 0
for_j. (d-1+y)+/&i.d do. z=. (c#j) ,. z{~;(-c){.&.><i.{.c=. +/\.c end.
)
require'stats'

View file

@ -1,3 +1,11 @@
comb=: dyad define M.
if. (x>:y)+.0=x do. i.(x<:y),x else. (0,.x comb&.<: y),1+x comb y-1 end.
)
3 comb 5
0 1 2
0 1 3
0 1 4
0 2 3
0 2 4
0 3 4
1 2 3
1 2 4
1 3 4
2 3 4

View file

@ -1 +1,5 @@
combb=: (#~ ((-:/:~)>/:~-:\:~)"1)@(# #: [: i. ^~)
comb1=: dyad define
c=. 1 {.~ - d=. 1+y-x
z=. i.1 0
for_j. (d-1+y)+/&i.d do. z=. (c#j) ,. z{~;(-c){.&.><i.{.c=. +/\.c end.
)

View file

@ -0,0 +1,3 @@
combr=: dyad define M.
if. (x>:y)+.0=x do. i.(x<:y),x else. (0,.x combr&.<: y),1+x combr y-1 end.
)

View file

@ -0,0 +1 @@
combb=: (#~ ((-:/:~)>/:~-:\:~)"1)@(# #: [: i. ^~)

View file

@ -1,6 +1,4 @@
def comb(m, lst):
if m == 0:
return [[]]
else:
return [[x] + suffix for i, x in enumerate(lst)
for suffix in comb(m - 1, lst[i + 1:])]
if m == 0: return [[]]
return [[x] + suffix for i, x in enumerate(lst)
for suffix in comb(m - 1, lst[i + 1:])]

View file

@ -1,5 +1,4 @@
@(do
(defun comb-n-m (n m)
(comb (range* 0 n) m))
(defun comb-n-m (n m)
(comb (range* 0 n) m))
(put-line `3 comb 5 = @(comb-n-m 5 3)`))
(put-line `3 comb 5 = @(comb-n-m 5 3)`)

View file

@ -0,0 +1,40 @@
Function Dec2Bin(n)
q = n
Dec2Bin = ""
Do Until q = 0
Dec2Bin = CStr(q Mod 2) & Dec2Bin
q = Int(q / 2)
Loop
Dec2Bin = Right("00000" & Dec2Bin,6)
End Function
Sub Combination(n,k)
Dim arr()
ReDim arr(n-1)
For h = 0 To n-1
arr(h) = h + 1
Next
Set list = CreateObject("System.Collections.Arraylist")
For i = 1 To 2^n
bin = Dec2Bin(i)
c = 0
tmp_combo = ""
If Len(Replace(bin,"0","")) = k Then
For j = Len(bin) To 1 Step -1
If CInt(Mid(bin,j,1)) = 1 Then
tmp_combo = tmp_combo & arr(c) & ","
End If
c = c + 1
Next
list.Add Mid(tmp_combo,1,(k*2)-1)
End If
Next
list.Sort
For l = 0 To list.Count-1
WScript.StdOut.Write list(l)
WScript.StdOut.WriteLine
Next
End Sub
'Testing with n = 5 / k = 3
Call Combination(5,3)