Hackerrank – Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Read all ratings and compare it according to the defined conditions:
- If
ai > bi
, then Alice is awarded 1 point - If
ai < bi
, then Bob is awarded 1 point - If
ai = bi
, then neither person receives a point
I created solution in:
All solutions are also available on my GitHub.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import java.util.*; public class CompareTheTriplets { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a0 = in.nextInt(); int a1 = in.nextInt(); int a2 = in.nextInt(); int b0 = in.nextInt(); int b1 = in.nextInt(); int b2 = in.nextInt(); int aliceScore = 0; int bobScore = 0; if(a0 > b0) { aliceScore++; } if(a1 > b1) { aliceScore++; } if(a2 > b2) { aliceScore++; } if(a0 < b0) { bobScore++; } if(a1 < b1) { bobScore++; } if(a2 < b2) { bobScore++; } System.out.println(aliceScore + " " + bobScore); in.close(); } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
'use strict'; const processData = input => { const lines = input.split('\n'); const alicesRating = lines[0].split(' ').map(i => parseInt(i)); const bobRating = lines[1].split(' ').map(i => parseInt(i)); let aliceScore = 0; let bobScore = 0; for(let i = 0; i < alicesRating.length; i++) { if (alicesRating[i] > bobRating[i]) { aliceScore++; } else if (alicesRating[i] < bobRating[i]) { bobScore++; } } console.log(aliceScore + ' ' + bobScore); }; process.stdin.resume(); process.stdin.setEncoding("ascii"); let _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Scala
1 2 3 4 5 6 7 8 9 10 11 |
object CompareTheTriplets extends App { val lines = Source.stdin.getLines().toList val alicesRating = lines(0).split(" ").map(_.toInt) val bobsRating = lines(1).split(" ").map(_.toInt) val differentRatings = alicesRating.zip(bobsRating).filter(pair => pair._1 != pair._2) val aliceScore = differentRatings.count(pair => pair._1 > pair._2) val bobScore = differentRatings.length - aliceScore println(aliceScore + " " + bobScore) } |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
a0, a1, a2 = gets.strip.split(' ') a0 = a0.to_i a1 = a1.to_i a2 = a2.to_i b0, b1, b2 = gets.strip.split(' ') b0 = b0.to_i b1 = b1.to_i b2 = b2.to_i alice_score = 0 bob_score = 0 alice_score = alice_score + 1 if a0 > b0 alice_score = alice_score + 1 if a1 > b1 alice_score = alice_score + 1 if a2 > b2 bob_score = bob_score + 1 if a0 < b0 bob_score = bob_score + 1 if a1 < b1 bob_score = bob_score + 1 if a2 < b2 puts "#{alice_score} #{bob_score}" |