This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize
for the AI-speak, we are working on rewriting it!
The program’s map: every function and which calls which.
There’s no recorded run of this program yet.
Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.
[1]lines 1–36
# Generated by tools/algo/legacy.py from the corpus: do not edit.# TheAlgorithms' all_permutations.py backtracking (backtracking.viv).# Element comparisons are routed through lib/algo/legacy/order.viv; nothing else changes.import algo.legacy.order
# from study/idioms/thealgorithms-python/backtracking.viv: create_state_space_tree generate_all_permutations#| all_permutations.py create_state_space_tree: extend the current sequence by each#| unused item, recurse, then undo. Python appends to and pops a list; here position#| `index` of a fixed-length array is overwritten. Returns how many it printed.defcreate_state_space_tree(sequence:Arr, current:Arr, index, used:Arr, as_text:Bool) {
if index ==len(sequence) {
if as_text {
text(current)
}
else {
show current
}
return1.0
}
count =0.0for i tolen(sequence) {
if used[i] ==0.0 {
current[index] = sequence[i]
used[i] =1.0
count +=create_state_space_tree(sequence, current, index +1.0, used, as_text)
used[i] =0.0
}
}
return count
}
defgenerate_all_permutations(sequence:Arr, as_text:Bool) {
n =len(sequence)
returncreate_state_space_tree(sequence, zeros(n), 0.0, zeros(n), as_text)
}