Creating a specific pattern with two-dimensional array in scala

It should be like this:

  def createDiamond(size: Int) = {
    require(size % 2 != 0, "size must be odd")
    def innerIterator(limit: Int) = {
      (0 until size).map{ column =>
        if(column > ((size / 2) + limit) || column < ((size / 2) - limit)) "0"
        else "1"
      }.mkString(" ") + "\n"
    }

    val str = (0 until size).map{ row => if(row <= size / 2) innerIterator(row) else innerIterator(size - row - 1)}.mkString("")
    println(str)
  }

Browse More Popular Posts

Leave a Comment