Last active
July 16, 2018 13:17
-
-
Save cngdean/3702b7be4b6d298dd69c to your computer and use it in GitHub Desktop.
scala hackerrank i/o stdin stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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