NicolaBernini / CodingChallengesSolutions

CPP Code Snippets with comments
4 stars 4 forks source link

Hackerrank - Apple and Orange #33

Open NicolaBernini opened 4 years ago

NicolaBernini commented 4 years ago

Overview

All about Apple and Orange appunto

Appunto

NicolaBernini commented 4 years ago

Scala

import java.io._
import java.math._
import java.security._
import java.text._
import java.util._
import java.util.concurrent._
import java.util.function._
import java.util.regex._
import java.util.stream._

object Solution {

    def f(s: Int, t: Int, tree_pos: Int, fruits_pos: Array[Int]) : Int = {
        (for(a <- fruits_pos) yield {
        a match {
        case x if((x+tree_pos)>=s && (x+tree_pos)<=t) => 1
        case _ => 0 
        }
        }).sum 
    }

    // Complete the countApplesAndOranges function below.
    def countApplesAndOranges(s: Int, t: Int, a: Int, b: Int, apples: Array[Int], oranges: Array[Int]) {
    val tot_a = f(s,t,a,apples)
    val tot_b = f(s,t,b,oranges)
    println(tot_a)
    println(tot_b)
    }

    def main(args: Array[String]) {
        val stdin = scala.io.StdIn

        val st = stdin.readLine.split(" ")

        val s = st(0).trim.toInt

        val t = st(1).trim.toInt

        val ab = stdin.readLine.split(" ")

        val a = ab(0).trim.toInt

        val b = ab(1).trim.toInt

        val mn = stdin.readLine.split(" ")

        val m = mn(0).trim.toInt

        val n = mn(1).trim.toInt

        val apples = stdin.readLine.split(" ").map(_.trim.toInt)

        val oranges = stdin.readLine.split(" ").map(_.trim.toInt)
        countApplesAndOranges(s, t, a, b, apples, oranges)
    }
}

Appunto