Compare the Triplets

Sort by

recency

|

4309 Discussions

|

  • + 0 comments
    import java.io.*;
    import java.math.*;
    import java.security.*;
    import java.text.*;
    import java.util.*;
    import java.util.concurrent.*;
    import java.util.regex.*;
    
    class Result {
    
        /*
         * Complete the 'compareTriplets' function below.
         *
         * The function is expected to return an INTEGER_ARRAY.
         * The function accepts following parameters:
         *  1. INTEGER_ARRAY a
         *  2. INTEGER_ARRAY b
         */
    
        public static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) {
            List <Integer> respuesta = new ArrayList();
            respuesta.add(0);
            respuesta.add(0);
            
            for (int i = 0; i < a.size(); i++) {
                int comp = a.get(i).compareTo(b.get(i));
                if (comp > 0) {
                    respuesta.set(0, respuesta.get(0)+1);
                } else if(comp < 0){
                    respuesta.set(1, respuesta.get(1)+1);
                }
            }
            return respuesta;
        }
    
    }
    
  • + 0 comments
    function compareTriplets(a: number[], b: number[]): number[] {
        let res = [0, 0];
        for(let i = 0; i < 3; i++) {
            if(a[i] !== b[i]) {
                if(a[i] > b[i]) {
                    res[0]++;
                } else {
                   res[1]++; 
                }
            }
        }
        return res;
    

    }

  • + 0 comments

    My code in Rust

    fn compareTriplets(a: &[i32], b: &[i32]) -> Vec<i32> {
        let mut v : Vec<i32> = vec![0,0];
        for i in 0..a.len() {
            if a[i] > b[i]{
                v[0] = v[0]+1;
            }
            if a[i] < b[i]{
                v[1] = v[1]+1;
            }
        }
        v
    }
    
  • + 1 comment

    This is my code in Python.

    Code:
    def compareTriplets(a, b):
    count_a = 0
    count_b = 0
    for i in range(3):
    if a[i] < b[i]:
    count_b += 1
    elif a[i] == b[i]:
    pass
    else:
    count_a += 1

    return [count_a, count_b]
    

    x = [int(i) for i in input().split(' ')] y = [int(i) for i in input().split(' ')]

    result = compareTriplets(x, y) print(' '.join(str(num) for num in result))

  • + 0 comments

    This is my code in JavaScript language

    function compareTriplets(a, b) {
        const result = [0,0];
        for(let i = 0; i < a.length; i++){
            if(a[i] > b[i]){
                result[0]++;
            }
            else if(b[i] > a[i]) {
                result[1]++;
            }
        }
        return result;
    }