Skip to content

Instantly share code, notes, and snippets.

@GlulkAlex
Created July 10, 2016 05:58
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 GlulkAlex/791f14184d954e2427ba1ddc630e11d8 to your computer and use it in GitHub Desktop.
Save GlulkAlex/791f14184d954e2427ba1ddc630e11d8 to your computer and use it in GitHub Desktop.
handles IO operations for hackerRank challenges in Scala
object Solution extends App{
import scala.annotation.tailrec
import scala.io.StdIn
import scala.util.{Try, Success, Failure}
import scala.math.pow//{abs, pow, ceil, floor}
// as replacement of this Java styled code
/*
def main(args: Array[String]) {
val sc = new java.util.Scanner (System.in);
var t = sc.nextInt();
int a0 = 0;
while(a0 < t){
var n = sc.nextInt();
var k = sc.nextInt();
var a = new Array[Int](n);
for(a_i <- 0 to n-1) {
a(a_i) = sc.nextInt();
}
a0+=1;
}
}
*/
/*
Task:
===
*Given* the `arrival time` of _each_ `student`,
*determine*
if the `class` is *canceled*.
Note:
===
_Non-positive_ `arrival times` (`arrival time` <= 0) *indicate*
the `student` *arrived*
_early_ or
_*on time*_;
_positive_ `arrival times` (`arrival time` > 0) *indicate*
the `student` *arrived* (`arrival time`) `minutes` _*late*_.
If a `student` *arrives* _exactly_ _*on time*_ (`arrival time` == 0),
the `student` is *considered* to
have *entered* _*before*_ the `class` *started*.
Constrains:
===
-100 <= `arrival time` <= 100
*/
val is_Debug_Mode: Boolean = (
true
//false
)
/*
condition:
`cancelation_Threshold` of `arrival_Times` in `arrival_Times_Vector` are
<= 0
*/
val YES: String = "YES"
val NO: String = "NO"
val rand = new scala.util.Random(seed = 100)
//println(NO)
// Range from -128 to 127
val test_Cases_Total: Byte/*Int*/ = Try(
StdIn
//.readInt()
.readByte()
) match {
case Success(f) => f
case Failure(e) => /*println(e.getMessage)*/0
}
assert(test_Cases_Total >= 1)
assert(test_Cases_Total <= 10)
if (is_Debug_Mode) {Console.withOut(Console.err) {println(s"""test_Cases_Total: ${test_Cases_Total}""")}}
//for (test <- 0 until test_Cases_Total by 1) {
for (test <- 1 to test_Cases_Total by 1) {
//val students_In_Class: Int = 0
//val cancelation_Threshold: Int = 0
val Array(students_In_Class, cancelation_Threshold): Array[Int] = Try(
StdIn.readLine()
) match {
case Success(str) => {
str
.split(" ")
///> one iteration
.map(_.toInt)
//.map(BigInt(_))
//.toSet
}
case Failure(e) => {
//println(e.getMessage)
// <- default
Array.empty[Int]
}
}
if (is_Debug_Mode) {
Console.withOut(Console.err) {
println(s"""students_In_Class: ${students_In_Class}, cancelation_Threshold: ${cancelation_Threshold}""")}}
//val arrival_Times: Array[Int] = Array.empty[Int]
//val b_Str: String = Try(
//val b_Map: Map[Int, Int] = Try(
val arrival_Times_Vector: /*Vector*/Array[Int] = Try(
//val b_Set: Set[Int] = Try(
StdIn.readLine()
) match {
case Success(str) => {
//elems_Count(
str
///> one iteration
.split(" ")
///> one iteration
.map(_.toInt)
//.map(BigInt(_))
//.toSet
//)
}
case Failure(e) => {
//println(e.getMessage)
// <- default
//false
//Vector(1)
///> not neutral to summation
//Array(0)
Array.empty[Int]
//List("-1")
//Array(BigInt(1))
//Set.empty[Int]
//Map.empty[Int, Int]
}
}
if (is_Debug_Mode) {
Console.withOut(Console.err) {
println(
s"""arrival_Times_Vector(${arrival_Times_Vector.size}):
|${arrival_Times_Vector.mkString("[", ", ", "]")}""".stripMargin.replaceAll("\n", " "))}}
assert(students_In_Class >= 1)
assert(students_In_Class <= 1000)//pow(10, 3)
assert(cancelation_Threshold >= 1)
assert(cancelation_Threshold <= students_In_Class)//pow(10, 3)
assert(arrival_Times_Vector.length == students_In_Class)
val is_In_Time: boolean = rand.nextBoolean()
if (is_Debug_Mode) {
Console.withOut(Console.err) {
println(
s"""is_In_Time:${is_In_Time} ?
|${cancelation_Threshold}:cancelation_Threshold""".stripMargin.replaceAll("\n", " "))}}
if (is_In_Time) {
println(NO)
} else {
println(YES)
}
}
//Console.withOut(Console.err) { println("This goes to default _error_") }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment