Skip to content

Instantly share code, notes, and snippets.

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 laxmanjangley/90551e657fca6e2cf596157d6a2bb5b3 to your computer and use it in GitHub Desktop.
Save laxmanjangley/90551e657fca6e2cf596157d6a2bb5b3 to your computer and use it in GitHub Desktop.
scala hackerrank i/o stdin stdout
object hackerrank {
object Solution {
def main(args: Array[String]) {
def fact(n: Int): Int = if (n == 1) 1 else n * fact(n - 1)
for (n <- io.Source.stdin.getLines) {
print(fact(Integer.parseInt(n.toString)) + "\n")
}
}
}
val stdinString = "2\n3\n5"
System.setIn(new java.io.ByteArrayInputStream(stdinString.getBytes("UTF-8")))
Solution.main(null)
}
// this one reads in "n" test cases then does a function for each one
object hackerrank{
def main(args: Array[String]) {
def permute(s: String): String = if (s.isEmpty) "" else s.tail.head + s.head.toString + permute(s.tail.tail)
val stdin = io.Source.stdin.getLines().toList
val n = stdin(0).toInt
for (i <- 1 to n) {
println(permute(stdin(i)))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment