Scala返回坐标作为元组 [英] Scala returning coordinate as tuple
问题描述
Map [(Int,Int),List [Int])]
以下是我到目前为止:
<$ p [p(int,Int),List [Int]]):Map [(Int,Int),int(int)列表[Int]] = {
str match {
case=> lst
case _ => if(x == 6){
if(str.charAt(0).equals('。')){
parseHelper(str.substring(1),0,y + 1,lst + ((x,y) - > List(-1)))
} else {
print(str =+ str +int =+ str(0)+list = + lst.head +\\\
)
parseHelper(str.substring(1),0,y + 1,lst +((x,y) - > List(str(0)))
}
} else {
if(str.charAt(0).equals('!')){
parseHelper(str.substring(1),x + y,lst +((x,y) - > List(-1)))
} else {
print(str =+ str +int =+ str(0)+ list =+ lst.head +\\\
)
parseHelper(str.substring(1),x + 1,y,lst +((x,y) - > List(str(0) ))
}
}
}
}
!
代表一个空白点,所以我们把 -1
代表。
示例字符串: !!!! 8!3 !!! 6 !! 7 !! 84!3!5 !! 2!9 !!! 1!54!8 !!!!!!!!! 4!27!6 !!! 3!1 !! 7!4!72 !! 4 !! 6 !!! 4!1! 3
我用于调试的打印语句。问题是当我输入不是-1的int时,输入一个从不在字符串中的数字。下面我有输出:
str = 8.3 ... 6..7..84.3.5..2.9 .. .1.54.8 ......... 4.27.6 ... 3.1..7.4.72..4..6 ... 4.1 ... 3 int = 8 list =((0,0) ,列表(-1))
str = 3 ... 6..7..84.3.5..2.9 ... 1.54.8 ......... 4.27.6 ... 3.1..7.4.72..4..6 ... 4.1 ... 3 int = 3 list =((5,0),List(-1))
str = 6..7 .. 84.3.5..2.9 ... 1.54.8 ......... 4.27.6 ... 3.1..7.4.72..4..6 ... 4.1 ... 3 int = 6 list =((5,0),List(-1))
str = 7..84.3.5..2.9 ... 1.54.8 ......... 4.27.6 ..列表=((5,0),列表(-1))
str = 84.3.5 .. 2.9 ... 1.54.8 ......... 4.27.6 ... 3.1..7.4.72..4..6 ... 4.1 ... 3 int = 8 list =((5 ,0),List(-1))
str = 4.3.5..2.9 ... 1.54.8 ......... 4.27.6 ... 3.1..7.4.72。 .4..6 ... 4.1 ... 3 int = 4 list =((7,1),List(56))
str = 3.5..2.9 ... 1.54.8 .... ..... 4.27.6 ... 3.1..7.4.72..4..6 ... 4.1 ... 3 int = 3 list =((7,1),List(56))
str = 5..2.9 ... 1.54.8 ......... 4.27.6 ... 3.1..7.4.72..4..6 ... 4.1 ... 3 int = 5 list =((7,1),List(56))
str = 2.9 ... 1.54.8 ......... 4.27.6 ... 3.1..7.4.72 ..4..6。 ..4.1 ... 3 int = 2 list =((7,1),List(56))
str = 9 ... 1.54.8 ......... 4.27.6。 .3.1..7.4.72..4..6 ... 4.1 ... 3 int = 9 list =((7,1),List(56))
str = 1.54.8 .. ....... 4.27.6 ... 3.1..7.4.72..4..6 ... 4.1 ... 3 int = 1 list =((7,1),List(56))
str = 54.8 ......... 4.27.6 ... 3.1..7.4.72..4..6 ... 4.1 ... 3 int = 5 list =((7 ,1),List(56))
str = 4.8 ......... 4.27.6 ... 3.1..7.4.72..4..6 ... 4.1 ... 3 int = 4 list =((7,1),List(56))
str = 8 ......... 4.27.6 ... 3.1..7.4.72..4。 .6 ... 4.1 ... 3 int = 8 list =((7,1),List(56))
str = 4.27.6 ... 3.1..7.4.72..4 .. 6 ... 4.1 ... 3 int = 4 list =((7,1),List(56))
str = 27.6 ... 3.1..7.4.72..4..6 .. .4.1 ... 3 int = 2 list =((7,1),List(56))
str = 7.6 ... 3.1..7.4.72..4..6 ... 4.1。 .3 int = 7 list =((7,1),List(56))
str = 6 ... 3.1..7.4.72..4..6 ... 4.1 ... 3 int = 6 list =((7,1),List(56))
str = 3.1..7.4.72..4..6 ... 4.1 ... 3 int = 3 list =(( 7,1),List(56))
str = 1..7.4.72..4..6 ... 4.1 ... 3 int = 1 list =((7,1),List 56))
str = 7.4.72..4..6 ... 4.1 ... 3 int = 7 list =((7,1),List(56))
str = 4.72..4..6 ... 4.1 ... 3 int = 4 list =((7,1),List 56))
str = 72..4..6 ... 4.1 ... 3 int = 7 list =((7,1),List(56))
str = 2 .. 4 ... 6 ... 4.1 ... 3 int = 2 list =((7,1),List(56))
str = 4..6 ... 4.1 ... 3 int = 4 list =((7,1),List(56))
str = 6 ... 4.1 ... 3 int = 6 list =((7,1),List(56))
str = 4.1 ... 3 int = 4 list =((7,1),List(56))
str = 1 ... 3 int = 1 list =((7,1),List ))
str = 3 int = 3 list =((7,1),List(56))
由于某些原因,字符串中的56字符不等于数字 56
被输入映射。
感谢您的帮助!
您正在使用ASCII代码字符
。你想要的是使用 str(0).asDigit
,将其转换为数位。话虽如此,你这样做太复杂了。你可以得到你想要的东西:
str.zipWithIndex.map {case(digit,index)=> ((index / 7,index%7),digit.asDigit)} toMap
I have a 7 by 7 grid, and I have to parse a string that fills in the grid. So if I have a string with 49 elements, I need to convert the string into a usable grid. The grid is represented with a map that looks like this:
Map[(Int, Int), List[Int])]
Here's what I have so far:
def parseHelper(str: String, x: Int, y: Int, lst: Map[(Int, Int), List[Int]]): Map[(Int,Int), List[Int]] = {
str match{
case "" => lst
case _ => if (x == 6){
if (str.charAt(0).equals('.')){
parseHelper(str.substring(1), 0, y+1, lst + ((x, y) -> List(-1)))
} else {
print("str = " + str + " int = " + str(0) + " list = " + lst.head + "\n")
parseHelper(str.substring(1), 0, y+1, lst + ((x,y) -> List(str(0))))
}
} else {
if (str.charAt(0).equals('!')){
parseHelper(str.substring(1), x+1, y, lst+((x, y) -> List(-1)))
} else {
print("str = " + str + " int = " + str(0) + " list = " + lst.head + "\n")
parseHelper(str.substring(1), x+1, y, lst+((x,y) -> List(str(0))))
}
}
}
}
The !
represents a blank spot, so we put -1
to represent that.
Example String: !!!!8!3!!!6!!7!!84!3!5!!2!9!!!1!54!8!!!!!!!!!4!27!6!!!3!1!!7!4!72!!4!!6!!!4!1!!!3
The print statements I have for debugging. The issue is that when I enter an int that isn't -1, a number that was never in the string is entered. Below I have the output:
str = 8.3...6..7..84.3.5..2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 8 list = ((0,0),List(-1))
str = 3...6..7..84.3.5..2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 3 list = ((5,0),List(-1))
str = 6..7..84.3.5..2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 6 list = ((5,0),List(-1))
str = 7..84.3.5..2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 7 list = ((5,0),List(-1))
str = 84.3.5..2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 8 list = ((5,0),List(-1))
str = 4.3.5..2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 4 list = ((7,1),List(56))
str = 3.5..2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 3 list = ((7,1),List(56))
str = 5..2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 5 list = ((7,1),List(56))
str = 2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 2 list = ((7,1),List(56))
str = 9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 9 list = ((7,1),List(56))
str = 1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 1 list = ((7,1),List(56))
str = 54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 5 list = ((7,1),List(56))
str = 4.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 4 list = ((7,1),List(56))
str = 8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 8 list = ((7,1),List(56))
str = 4.27.6...3.1..7.4.72..4..6...4.1...3 int = 4 list = ((7,1),List(56))
str = 27.6...3.1..7.4.72..4..6...4.1...3 int = 2 list = ((7,1),List(56))
str = 7.6...3.1..7.4.72..4..6...4.1...3 int = 7 list = ((7,1),List(56))
str = 6...3.1..7.4.72..4..6...4.1...3 int = 6 list = ((7,1),List(56))
str = 3.1..7.4.72..4..6...4.1...3 int = 3 list = ((7,1),List(56))
str = 1..7.4.72..4..6...4.1...3 int = 1 list = ((7,1),List(56))
str = 7.4.72..4..6...4.1...3 int = 7 list = ((7,1),List(56))
str = 4.72..4..6...4.1...3 int = 4 list = ((7,1),List(56))
str = 72..4..6...4.1...3 int = 7 list = ((7,1),List(56))
str = 2..4..6...4.1...3 int = 2 list = ((7,1),List(56))
str = 4..6...4.1...3 int = 4 list = ((7,1),List(56))
str = 6...4.1...3 int = 6 list = ((7,1),List(56))
str = 4.1...3 int = 4 list = ((7,1),List(56))
str = 1...3 int = 1 list = ((7,1),List(56))
str = 3 int = 3 list = ((7,1),List(56))
For some reason, the number 56
is entered into the mapping, when 56 isn't even in the string.
Thanks for the help!
You're using the ASCII code for the Char
. What you want is to use str(0).asDigit
, to convert it to a digit. That being said, you're making this way too complicated. You can get what you want with something like:
str.zipWithIndex.map{case (digit, index) => ((index / 7, index % 7), digit.asDigit)}.toMap
这篇关于Scala返回坐标作为元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!