RosettaCodeData/Task/Ordered-partitions/11l/ordered-partitions.11l

48 lines
1007 B
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

F partitions(lengths)
[[[Int]]] r
[(Int, Int)] slices
V delta = -1
V idx = 0
L(length) lengths
assert(length >= 0, lengths must not be negative.)
delta += length
slices.append((idx, delta))
idx += length
V n = sum(lengths)
V perm = Array(1 .. n)
L
[[Int]] part
L(start, end) slices
V s = perm[start .. end]
I !s.is_sorted()
L.break
part.append(s)
L.was_no_break
r.append(part)
I !perm.next_permutation()
L.break
R r
F toString(part)
V result = (
L(s) part
I result.len > 1
result = ,
result = {s.join(, )}
R result)
F displayPermutations(lengths)
print(Ordered permutations for (lengths.join(, )):)
L(part) partitions(lengths)
print(toString(part))
:start:
I :argv.len > 1
displayPermutations(:argv[1..].map(Int))
E
displayPermutations([2, 0, 2])