간단한 테일 재귀를 사용하여리스트의 모든 순열을 검색하려고합니다. 모듈은 다음과 같습니다 재귀가 순열의 배열이 중첩 풀릴 불행하게도 때엘릭서에서 2 차원리스트의 순열
defmodule PermutationsSpec do
use ESpec
alias WaffleEventImporter.Permutations
describe "of/2" do
subject do: Permutations.of(list_list, [])
let :list_list, do: [[1,2,3],[1,2,3],[1,2,3]]
let :expected, do: [
[1,1,1],[1,1,2],[1,1,3],[1,2,1],[1,2,2],[1,2,3],[1,3,1],[1,3,2],[1,3,3],
[2,1,1],[2,1,2],[2,1,3],[2,2,1],[2,2,2],[2,2,3],[2,3,1],[2,3,2],[2,3,3],
[3,1,1],[3,1,2],[3,1,3],[3,2,1],[3,2,2],[3,2,3],[3,3,1],[3,3,2],[3,3,3],
]
it "returns all permutations for the 2 diensional array provided" do
expect(subject) |> to(eq expected)
end
end
end
: 이것에 대한
defmodule Permutations do
def of([], accumulator) do
accumulator
end
def of([head | tail], accumulator) do
for item <- head, do: of(tail, accumulator ++ [item])
end
end
내 사양은 같다. 해당 사양의 결과는 다음과 같습니다.
Expected `[[[[1, 1, 1], [1, 1, 2], [1, 1, 3]], [[1, 2, 1], [1, 2, 2],
[1, 2, 3]], [[1, 3, 1], [1, 3, 2], [1, 3, 3]]], [[[2, 1, 1], [2, 1, 2],
[2, 1, 3]], [[2, 2, 1], [2, 2, 2], [2, 2, 3]], [[2, 3, 1], [2, 3, 2],
[2, 3, 3]]], [[[3, 1, 1], [3, 1, 2], [3, 1, 3]], [[3, 2, 1], [3, 2, 2],
[3, 2, 3]], [[3, 3, 1], [3, 3, 2], [3, 3, 3]]]]` to equals (==)
`[[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 1], [1, 2, 2], [1, 2, 3],
[1, 3, 1], [1, 3, 2], [1, 3, 3], [2, 1, 1], [2, 1, 2], [2, 1, 3],
[2, 2, 1], [2, 2, 2], [2, 2, 3], [2, 3, 1], [2, 3, 2], [2, 3, 3],
[3, 1, 1], [3, 1, 2], [3, 1, 3], [3, 2, 1], [3, 2, 2], [3, 2, 3],
[3, 3, 1], [3, 3, 2], [3, 3, 3]]`, but it doesn't.
중첩 방지 방법에 대한 모든 정보를 제공해 주시면 감사하겠습니다. 불행히도 결과를 평평하게하는 것은 조합의 첫 번째 순서 그룹을 제거했습니다.
'process' 함수를 공유하십시오. – mudasobwa