스도쿠 생성기의 일부로 중첩 목록을 필터링하여 특정 인덱스의 내부 목록과 해당 목록 내의 특정 인덱스의 내부 목록 요소 만 반환하는 기능이 있습니다. 이 아이디어는 함수에 대한 인수로 제공되는 List [Int]로 표현되는 9x9 스도쿠 퍼즐에서 가져온 3x3 사각형의 값을 포함하는 List [Int]를 반환하는 것입니다.스칼라 스도쿠 생성기에서 중첩 목록 및 목록 항목 필터링
두 가지 방법을 시도했지만 일관되게 작동하지 못했습니다. 한 가지 방법은 목록에서 특정 하위 목록을 필터링 한 다음 나머지 목록에서 항목을 필터링합니다. 이 기능은 일부 지표 완전히 작동하지만, 다른 사람과는 모든 일부 하위 목록에서 너무 많거나 너무 적은 인덱스 값을 필터링하지만 것 : 여기
def getGroup(indexX: Int, indexY: Int, puzzle: List[List[Int]]): List[Int] = {
val groupX = { //determining row coordinate
if(indexX <= 2) 2
else if(indexX <= 5) 5
else 8
}
val groupY = { //determining column coordinate
if(indexY <= 2) 2
else if(indexY <= 5) 5
else 8
}
// Using filter
val subsection: List[List[Int]] = puzzle.filter(x => puzzle.indexOf(x) <= groupX && puzzle.indexOf(x) >= groupX - 2)
// This sometimes filters out too many or too few items
val group: List[List[Int]] = subsection.map(x => x.filter(y => x.indexOf(y) <= groupY && x.indexOf(y) >= groupY - 2))
val result = group.flatten
println("subsection " + subsection)
println("group " + group)
result
}
테스트 목록에서 일부 출력 [목록 [지능] 그 결과에 따라 결과를 보여줄 수있는 인쇄 기능이 제공됩니다. 잘못된 인덱스가 일부 하위 목록에서 필터링되지만 동일한 함수 호출에서 모두 필터링되지 않는 이유는 분명하지 않습니다. 나는 단순히 잘못된 인덱스를 선택했다고 생각하지 않는다. 그렇지 않으면 동일한 함수 호출에서 모든 하위 목록에 대해 동일한 방식으로 올바르지 않아야한다. 된,
def getGroup(indexX: Int, indexY: Int, puzzle: List[List[Int]]): List[Int] = {
//this portion is the same as above until the for expression:
val groupX = { //determining row coordinate
if(indexX <= 2) 2
else if(indexX <= 5) 5
else 8
}
val groupY = { //determining column coordinate
if(indexY <= 2) 2
else if(indexY <= 5) 5
else 8
}
// using for expression
val group = for(
outer <- puzzle if puzzle.indexOf(outer) <= groupX && puzzle.indexOf(outer) >= groupX - 2;
inner <- outer if outer.indexOf(inner) <= groupY && outer.indexOf(inner) >= groupY - 2)
yield inner
group
}
가 I이 목록 [리스트 [지능]에서는이 기능을 시험했다 :
test: List[List[Int]] = List(List(0, 2, 3, 4, 5, 6, 7, 8, 9), List(1, 2, 3, 4, 5, 6, 7, 8, 9), List(2, 2, 3, 4, 5, 6, 7, 8, 9), List(3, 2, 3, 4, 5, 6, 7, 8, 9), List(4, 2, 3, 4, 5, 6, 7, 8, 9), List(5, 2, 3, 4, 5, 6, 7, 8, 9), List(6, 2, 3, 4, 5, 6, 7, 8, 9), List(7, 2, 3, 4, 5, 6, 7, 8, 9), List(8, 2, 3, 4, 5, 6, 7, 8, 9))
scala> getGroup(2,2,test)
subsection: List(List(0, 2, 3, 4, 5, 6, 7, 8, 9), List(1, 2, 3, 4, 5, 6, 7, 8, 9), List(2, 2, 3, 4, 5, 6, 7, 8, 9))
group: List(List(0, 2, 3), List(1, 2, 3), List(2, 2, 3))
res12: List[Int] = List(0, 2, 3, 1, 2, 3, 2, 2, 3) //Correct
scala> getGroup(2,7,test)
subsection: List(List(0, 2, 3, 4, 5, 6, 7, 8, 9), List(1, 2, 3, 4, 5, 6, 7, 8, 9), List(2, 2, 3, 4, 5, 6, 7, 8, 9))
group: List(List(7, 8, 9), List(7, 8, 9), List(7, 8, 9))
res13: List[Int] = List(7, 8, 9, 7, 8, 9, 7, 8, 9) //Correct
scala> getGroup(7,7,test)
subsection: List(List(6, 2, 3, 4, 5, 6, 7, 8, 9), List(7, 2, 3, 4, 5, 6, 7, 8, 9), List(8, 2, 3, 4, 5, 6, 7, 8, 9))
group: List(List(7, 8, 9), List(8, 9), List(7, 9)) //Missing a 7 and an 8
res14: List[Int] = List(7, 8, 9, 8, 9, 7, 9)
scala> getGroup(4,0,test)
subsection: List(List(3, 2, 3, 4, 5, 6, 7, 8, 9), List(4, 2, 3, 4, 5, 6, 7, 8, 9), List(5, 2, 3, 4, 5, 6, 7, 8, 9))
group: List(List(3, 2, 3), List(4, 2, 3, 4), List(5, 2, 3, 5)) //Not enough values filtered out--unwanted 4 and 5
res32: List[Int] = List(3, 2, 3, 4, 2, 3, 4, 5, 2, 3, 5)
이 다른 접근법은 오직 만약 조건에 맞는 값을 산출 할 것이다 for 루프를 사용하여 각 목록은 인덱스 번호로 시작하고 단지 그 후 카운트 업 :
val testGrid = List(List(0,2,3,4,5,6,7,8,9),List(1,2,3,4,5,6,7,8,9),List(2,2,3,4,5,6,7,8,9),
List(3,2,3,4,5,6,7,8,9),List(4,2,3,4,5,6,7,8,9),List(5,2,3,4,5,6,7,8,9),
List(6,2,3,4,5,6,7,8,9),List(7,2,3,4,5,6,7,8,9),List(8,2,3,4,5,6,7,8,9))
그러나, 출력이 항상 정확하지 :
scala> getGroup(0, 0, testGrid)
res0: List[Int] = List(0, 2, 3, 1, 2, 3, 2, 2, 3) (correct)
scala> getGroup(3,3,testGrid)
res1: List[Int] = List(4, 5, 6, 5, 6, 4, 6) (too few)
scala> getGroup(3,7,testGrid)
res2: List[Int] = List(7, 8, 9, 7, 8, 9, 7, 8, 9) (correct)
scala> getGroup(7,7,testGrid)
res3: List[Int] = List(7, 8, 9, 8, 9, 7, 9) (too few)
scala> getGroup(7,0,testGrid)
res5: List[Int] = List(6, 2, 3, 6, 7, 2, 3, 7, 8, 2, 3, 8) (too many)
왜 내가이 출력을 얻었는지에 대한 분명한 대답이있을 수 있지만 그것은 나를 피했습니다. 나는 그것이 indexX에 대한 값에 관계없이 3보다 낮은 indexY를 가진 모든 테스트가 정확하기 때문에 내부 루프와 관련이 있다고 생각한다.
이러한 시도에 대한 의견을 보내 주시면 감사하겠습니다. 이 기능을보다 직관적으로 또는 간결하게 만들 수있는 대체 방법에 대한 의견을 보내 주시면 감사하겠습니다.