Data update

This commit is contained in:
Ingy döt Net 2024-10-16 18:07:41 -07:00
parent 81fd053722
commit 52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions

View file

@ -0,0 +1,32 @@
;;
;; Before the permutation is completely formed,
;; check for validity.
;;
;; n at a time.
(define (permute n__permute seq__permute func__permute
(chk__permute or) (built__permute '()))
(when (or (null? built__permute) (chk__permute built__permute))
(if (or (zero? n__permute) (null? seq__permute))
(func__permute built__permute)
(dotimes (i__permute (length seq__permute))
(unless (zero? i__permute) (rotate seq__permute -1))
(permute (- n__permute 1)
(rest seq__permute)
func__permute
chk__permute
(append built__permute (0 1 seq__permute)))))))
(time
(permute -1 '(4 6 8 0 5 9 2 -1 7 1 3) println (fn(xs) (= xs (sort (copy xs)))))
)
(-1 0 1 2 3 4 5 6 7 8 9)
78.125 [ milliseconds ]
(time
(permute -1 '(4 6 8 0 5 9 2 7 1 3) println (fn(xs) (= xs (sort (copy xs)))))
)
(0 1 2 3 4 5 6 7 8 9)
15.625 [ milliseconds ]

View file

@ -0,0 +1,21 @@
use itertools::Itertools;
use num::Num;
fn is_sorted<T>(data: &Vec<&T>) -> bool where T: Num + Ord {
return data.windows(2).all(|pair| pair[0] <= pair[1]);
}
fn permsort<T>(arr: Vec<T>) -> Vec<T> where T: Num + Ord + Clone {
for perm in arr.iter().permutations(arr.len()) {
if is_sorted(&perm) {
return perm.iter().cloned().cloned().collect();
}
}
unreachable!("No ordered permutation found");
}
fn main() {
let x = [4, 6, 5, 3, 1, 8, 7, 9, 10, 2].to_vec();
println!("{:?}", permsort(x)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
return ();
}

View file

@ -0,0 +1,29 @@
int A, Len;
func IsSorted;
int I;
[for I:= 0 to Len-2 do
if A(I) > A(I+1) then return false;
return true;
];
func PermSort(Last);
int Last;
int I, T;
[if Last <= 0 then return IsSorted;
for I:= 0 to Last do
[T:= A(I); A(I):= A(Last); A(Last):= T;
if PermSort(Last-1) then return true;
T:= A(I); A(I):= A(Last); A(Last):= T;
];
return false;
];
int I;
[A:= [170, 45, 75, -90, -802, 24, 2, 66];
Len:= 8;
PermSort(Len-1);
for I:= 0 to Len-1 do
[IntOut(0, A(I)); if I < Len-1 then Text(0, ", ")];
CrLf(0);
]