September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -1,22 +1,22 @@
import system'routines.
import extensions.
import extensions'routines.
import system'routines;
import extensions;
import extensions'routines;
const int M = 3.
const int N = 5.
const int M = 3;
const int N = 5;
numbers = (:anN)
[
^ Array new(anN); populate(:n)<int>( n )
].
Numbers(n)
{
^ Array.allocate(n).populate:(int n => n)
}
public program =
[
var aNumbers := numbers(N).
Combinator new:M of:aNumbers; forEach(:aRow)
[
console printLine(aRow toLiteral)
].
public program()
{
var numbers := Numbers(N);
Combinator.new(M, numbers).forEach:(row)
{
console.printLine(row.toString())
};
console readChar.
].
console.readChar()
}

View file

@ -0,0 +1,16 @@
iterator Combinations(m: int, n: int): seq[int] =
var result = newSeq[int](m)
var stack = initDeque[int]()
stack.addLast 0
while len(stack) > 0:
var index = len(stack) - 1
var value = stack.popLast()
while value < n:
value = value + 1
result[index] = value
index = index + 1
stack.addLast value
if index == m:
yield result
break

View file

@ -0,0 +1,15 @@
let combinations m n =
let rec c = function
| (0,_) -> [[]]
| (_,0) -> []
| (p,q) -> List.append
(List.map (List.cons (n-q)) (c (p-1, q-1)))
(c (p , q-1))
in c (m , n)
let () =
let rec print_list = function
| [] -> print_newline ()
| hd :: tl -> print_int hd ; print_string " "; print_list tl
in List.iter print_list (combinations 3 5)

View file

@ -0,0 +1,19 @@
define variable r as integer no-undo extent 3.
define variable m as integer no-undo initial 5.
define variable n as integer no-undo initial 3.
define variable max_n as integer no-undo.
max_n = m - n.
function combinations returns logical (input pos as integer, input val as integer):
define variable i as integer no-undo.
do i = val to max_n:
r[pos] = pos + i.
if pos lt n then
combinations(pos + 1, i).
else
message r[1] - 1 r[2] - 1 r[3] - 1.
end.
end function.
combinations(1, 0).

View file

@ -0,0 +1,62 @@
struct Combo<T> {
data_len: usize,
chunk_len: usize,
min: usize,
mask: usize,
data: Vec<T>,
}
impl<T: Clone> Combo<T> {
fn new(chunk_len: i32, data: Vec<T>) -> Self {
let d_len = data.len();
let min = 2usize.pow(chunk_len as u32) - 1;
let max = 2usize.pow(d_len as u32) - 2usize.pow((d_len - chunk_len as usize) as u32);
Combo {
data_len: d_len,
chunk_len: chunk_len as usize,
min: min,
mask: max,
data: data,
}
}
fn get_chunk(&self) -> Vec<T> {
let b = format!("{:01$b}", self.mask, self.data_len);
b
.chars()
.enumerate()
.filter(|&(_, e)| e == '1')
.map(|(i, _)| self.data[i].clone())
.collect()
}
}
impl<T: Clone> Iterator for Combo<T> {
type Item = Vec<T>;
fn next(&mut self) -> Option<Self::Item> {
while self.mask >= self.min {
if self.mask.count_ones() == self.chunk_len as u32 {
let res = self.get_chunk();
self.mask -= 1;
return Some(res);
}
self.mask -= 1;
}
None
}
}
fn main() {
let v1 = vec![1, 2, 3, 4, 5];
let combo = Combo::new(3, v1);
for c in combo.into_iter() {
println!("{:?}", c);
}
let v2 = vec!("A", "B", "C", "D", "E");
let combo = Combo::new(3, v2);
for c in combo.into_iter() {
println!("{:?}", c);
}
}

View file

@ -0,0 +1,24 @@
Private Sub comb(ByVal pool As Integer, ByVal needed As Integer, Optional ByVal done As Integer = 0, Optional ByVal chosen As Variant)
If needed = 0 Then '-- got a full set
For Each x In chosen: Debug.Print x;: Next x
Debug.Print
Exit Sub
End If
If done + needed > pool Then Exit Sub '-- cannot fulfil
'-- get all combinations with and without the next item:
done = done + 1
Dim tmp As Variant
tmp = chosen
If IsMissing(chosen) Then
ReDim tmp(1)
Else
ReDim Preserve tmp(UBound(chosen) + 1)
End If
tmp(UBound(tmp)) = done
comb pool, needed - 1, done, tmp
comb pool, needed, done, chosen
End Sub
Public Sub main()
comb 5, 3
End Sub