Skip to content

Instantly share code, notes, and snippets.

@cngdean
Last active July 16, 2018 13:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cngdean/3702b7be4b6d298dd69c to your computer and use it in GitHub Desktop.
Save cngdean/3702b7be4b6d298dd69c 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