Scala returning coordinate as tuple

℡╲_俬逩灬. 提交于 2019-12-11 12:09:17

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/36266982/scala-returning-coordinate-as-tuple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!