HackerRank 'Compare Triplets' Solution

Martin Kysel · July 29, 2016

Short Problem Definition:

Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from 1 to 100 for three categories: problem clarity, originality, and difficulty.

Compare Triplets

Complexity:

time complexity is O(1)

space complexity is O(1)

Execution:

This is a warmup. Follow specification.

Solution:

use std::io;
use std::cmp::Ordering;

fn get_numbers() -> Vec<u32> {
    let mut line = String::new();
    io::stdin().read_line(&mut line).ok().expect("Failed to read line");
    line.split_whitespace().map(|s| s.parse::<u32>().unwrap()).collect()
}

fn main() {
    let a = get_numbers();
    let b = get_numbers();
    
    let mut alice = 0;
    let mut bob = 0;
    
    for idx in 0..3 {
        match a[idx].cmp(&b[idx]) {
            Ordering::Less      => bob += 1,
            Ordering::Greater   => alice += 1,
            Ordering::Equal     => {},
        }
    }
 
    println!("{} {}", alice, bob);
}

Twitter, Facebook

To learn more about solving Coding Challenges in Python, I recommend these courses: Educative.io Python Algorithms, Educative.io Python Coding Interview.