Skip to content

Instantly share code, notes, and snippets.

@codeforkjeff
Created July 19, 2017 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeforkjeff/bee28e89ad43d9c4188aa2a3b18e7264 to your computer and use it in GitHub Desktop.
Save codeforkjeff/bee28e89ad43d9c4188aa2a3b18e7264 to your computer and use it in GitHub Desktop.
// https://www.codewars.com/kata/complete-the-pattern-number-13/python
object Pattern {
def spaces(size: Int) = Seq.fill(size)(" ").mkString
// line is 0-indexed
def pattern(n: Int, xArg: Int = 1, line: Int = 0, acc: Seq[String] = Seq[String]()): String = {
val x = if(xArg <= 1) 1 else xArg
if(line < n) {
val numToPrint = (line + 1) % 10
// spacing 'inside' and 'outside' each V
val innerSpacing = 1 + ((n - line - 2) * 2)
val outerSpacing = spaces(line * 2 - 1)
val lineOutput = if(line == 0) {
Seq.fill(x + 1)(numToPrint).mkString(spaces(innerSpacing))
} else {
val vContent = if(line == n - 1) numToPrint else numToPrint.toString + spaces(innerSpacing) + numToPrint.toString
spaces(line) + Seq.fill(x)(vContent).mkString(outerSpacing)
}
pattern(n, x, line + 1, acc :+ lineOutput)
} else {
// mirror image of V's creates the X's
(acc ++ acc.dropRight(1).reverse).mkString("\n")
}
}
def main(args: Array[String]) = {
println(pattern(0))
println(pattern(4, 3))
println(pattern(10, 2))
println(pattern(25))
println(pattern(3, -1))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment