아주 확실하지 나는 문제가 완전히 형성,하지만 난 할 노력하고있어 기본적으로 이것이다 :모든 가능한 값 조합의 배열로 배열을 결합하는 단일 라이너 알고리즘?
# where the indices correspond to attributes fore example:
# [type, status]
x = %w(a b)
y = %w(c d)
combine(x, y) #=> [["a", "b"], ["a", "d"], ["c", "a"], ["c", "b"]]
배열의 순서는 항상 동일하므로 각각의 역 같은 [b, a]
을 등 , 결과에 포함되지 않습니다.
이 내용은 무엇이며이를 구현하는 효율적인 방법은 무엇입니까? combine(*arrays)
감사 :
이 희망 배열과 값의 수를 작동합니다 ...
나는 Array#permutation를 볼 수 있지만, 그게 아주 아니에요! 다음
이 (x | y).combination(x.length).to_a
생산 :
x = ["front_door", "open"]
y = ["back_door", "closed"]
(x | y).combination(x.length).to_a
=> [["front_door", "open"], ["front_door", "back_door"], ["front_door", "closed"], ["open", "back_door"], ["open", "closed"], ["back_door", "closed"]]
내가 찾고 실제 결과가 이것이다 업데이트
여기에 내가 무엇을 찾고 더 나은 예제 :
=> [["front_door", "open"], ["front_door", "closed"], ["back_door", "open"], ["back_door", "closed"]]
또는 더 긴 배열 인 경우 :
x = ["house", "front_door", "open"]
y = ["building", "back_door", "closed"]
compute(x, y)
=> ["house", "front_door", "open"], ["house", "back_door", "open"], ["house", "front_door", "closed"], ["house", "back_door", "closed"], ["building", "front_door", "open"], ["building", "back_door", "open"], ["building", "front_door", "closed"], ["building", "back_door", "closed"]
아이디어가 있으십니까?
내가 첫 번째 예제에서 추측을 [ "C", "A"] [ "C"를 찾아야한다 , "d"] – tokland