Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
27
Task/Permutations/UNIX-Shell/permutations-1.sh
Normal file
27
Task/Permutations/UNIX-Shell/permutations-1.sh
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
function permute {
|
||||
if (( $# == 1 )); then
|
||||
set -- $(seq $1)
|
||||
fi
|
||||
local A=("$@")
|
||||
permuteAn "$#"
|
||||
}
|
||||
|
||||
function permuteAn {
|
||||
# print all permutations of first n elements of the array A, with remaining
|
||||
# elements unchanged.
|
||||
local -i n=$1 i
|
||||
shift
|
||||
if (( n == 1 )); then
|
||||
printf '%s\n' "${A[*]}"
|
||||
else
|
||||
permuteAn $(( n-1 ))
|
||||
for (( i=0; i<n-1; ++i )); do
|
||||
local -i k
|
||||
(( k=n%2 ? 0: i ))
|
||||
local t=${A[k]}
|
||||
A[k]=${A[n-1]}
|
||||
A[n-1]=$t
|
||||
permuteAn $(( n-1 ))
|
||||
done
|
||||
fi
|
||||
}
|
||||
19
Task/Permutations/UNIX-Shell/permutations-2.sh
Normal file
19
Task/Permutations/UNIX-Shell/permutations-2.sh
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
function permuteAn {
|
||||
# print all permutations of first n elements of the array A, with remaining
|
||||
# elements unchanged.
|
||||
local -i n=$1 i
|
||||
shift
|
||||
if (( n == 1 )); then
|
||||
printf '%s\n' "${A[*]}"
|
||||
else
|
||||
permuteAn $(( n-1 ))
|
||||
for (( i=1; i<n; ++i )); do
|
||||
local -i k
|
||||
(( k=n%2 ? 1 : i ))
|
||||
local t=$A[k]
|
||||
A[k]=$A[n]
|
||||
A[n]=$t
|
||||
permuteAn $(( n-1 ))
|
||||
done
|
||||
fi
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue