Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,28 @@
|
|||
-- Permutations by swapping
|
||||
-- J. Carter 2024 Jun
|
||||
-- Uses the PragmAda Reusable Components (https://github.com/jrcarter/PragmARC)
|
||||
|
||||
with Ada.Text_IO;
|
||||
with PragmARC.Permutations;
|
||||
|
||||
procedure Permutations_By_Swapping is
|
||||
package Permutations is new PragmARC.Permutations (Element => Positive);
|
||||
|
||||
Permutation : Permutations.Sequence_Lists.Vector;
|
||||
begin -- Permutations_By_Swapping
|
||||
Permutations.Generate (Initial => (1, 2, 3), Result => Permutation);
|
||||
|
||||
Output : for I in 1 .. Permutation.Last_Index loop
|
||||
One_Permutation : declare
|
||||
Sequence : Permutations.Sequence renames Permutation.Element (I);
|
||||
begin -- One_Permutation
|
||||
Ada.Text_IO.Put (Item => '(');
|
||||
|
||||
Print : for J in Sequence'Range loop
|
||||
Ada.Text_IO.Put (Item => (if J > 1 then "," else "") & Sequence (J)'Image);
|
||||
end loop Print;
|
||||
|
||||
Ada.Text_IO.Put_Line (Item => ") " & (if I rem 2 = 0 then '-' else '+') & '1');
|
||||
end One_Permutation;
|
||||
end loop Output;
|
||||
end Permutations_By_Swapping;
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
function output([Object[]]$A, [Int]$k, [ref]$sign)
|
||||
{
|
||||
"Perm: [$([String]::Join(', ', $A))] Sign: $($sign.Value)"
|
||||
}
|
||||
|
||||
function permutation([Object[]]$array)
|
||||
{
|
||||
function generate([Object[]]$A, [Int]$k, [ref]$sign)
|
||||
{
|
||||
if($k -eq 1)
|
||||
{
|
||||
output $A $k $sign
|
||||
$sign.Value = -$sign.Value
|
||||
}
|
||||
else
|
||||
{
|
||||
$k -= 1
|
||||
generate $A $k $sign
|
||||
for([Int]$i = 0; $i -lt $k; $i += 1)
|
||||
{
|
||||
if($i % 2 -eq 0)
|
||||
{
|
||||
$A[$i], $A[$k] = $A[$k], $A[$i]
|
||||
}
|
||||
else
|
||||
{
|
||||
$A[0], $A[$k] = $A[$k], $A[0]
|
||||
}
|
||||
generate $A $k $sign
|
||||
}
|
||||
}
|
||||
}
|
||||
generate $array $array.Count ([ref]1)
|
||||
}
|
||||
permutation @(0, 1, 2)
|
||||
""
|
||||
permutation @(0, 1, 2, 3)
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
struct HeapsAlgorithm {}
|
||||
|
||||
fn (mut ha HeapsAlgorithm) recursive(mut array []int) {
|
||||
ha.is_recursive(mut array, array.len, true)
|
||||
}
|
||||
|
||||
fn (mut ha HeapsAlgorithm) is_recursive(mut array []int, nir int, plus bool) {
|
||||
if nir == 1 { ha.is_output(array, plus) }
|
||||
else {
|
||||
for ial := 0; ial < nir; ial++ {
|
||||
ha.is_recursive(mut array, nir - 1, ial == 0)
|
||||
if nir % 2 == 0 { ha.is_swap(mut array, ial, nir - 1) }
|
||||
else { ha.is_swap(mut array, 0, nir - 1) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut ha HeapsAlgorithm) is_output(array []int, plus bool) {
|
||||
mut sign := " -1"
|
||||
if plus { sign = " +1" }
|
||||
println("${array} $sign")
|
||||
}
|
||||
|
||||
fn (mut ha HeapsAlgorithm) is_swap(mut array []int, air int, bir int) {
|
||||
temp := array[air]
|
||||
array[air] = array[bir]
|
||||
array[bir] = temp
|
||||
}
|
||||
|
||||
fn (mut ha HeapsAlgorithm) loop(mut array []int) {
|
||||
ha.is_loop(mut array, array.len)
|
||||
}
|
||||
|
||||
fn (mut ha HeapsAlgorithm) is_loop(mut array []int, nir int) {
|
||||
mut cay := []int{len: nir, init: 0}
|
||||
mut plus := false
|
||||
mut ir := 0
|
||||
ha.is_output(array, true)
|
||||
for ir < nir {
|
||||
if cay[ir] < ir {
|
||||
if ir % 2 == 0 { ha.is_swap(mut array, 0, ir) }
|
||||
else { ha.is_swap(mut array, cay[ir], ir) }
|
||||
ha.is_output(array, plus)
|
||||
plus = !plus
|
||||
cay[ir]++
|
||||
ir = 0
|
||||
}
|
||||
else {
|
||||
cay[ir] = 0
|
||||
ir++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut array := [0, 1, 2, 3]
|
||||
mut algorithm := HeapsAlgorithm{}
|
||||
algorithm.recursive(mut array)
|
||||
println("")
|
||||
algorithm.loop(mut array)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue