Sort by

recency

|

226 Discussions

|

  • + 0 comments

    import java.util.*;

    public class Solution {

    public static int minimum_index(int[] seq) {
        if (seq.length == 0) {
            throw new IllegalArgumentException("Cannot get the minimum value index from an empty sequence");
        }
        int min_idx = 0;
        for (int i = 1; i < seq.length; ++i) {
            if (seq[i] < seq[min_idx]) {
                min_idx = i;
            }
        }
        return min_idx;
    }
    
    
    public static void TestWithEmptyArray() {
        try {
            int[] seq = TestDataEmptyArray.get_array();
            int result = minimum_index(seq);
        } catch (IllegalArgumentException e) {
            return;
        }
        throw new AssertionError("Exception wasn't thrown as expected");
    }
    
    public static void TestWithUniqueValues() {
        int[] seq = TestDataUniqueValues.get_array();
        if (seq.length < 2) {
            throw new AssertionError("less than 2 elements in the array");
        }
    
        Integer[] tmp = new Integer[seq.length];
        for (int i = 0; i < seq.length; ++i) {
            tmp[i] = Integer.valueOf(seq[i]);
        }
        if (!((new LinkedHashSet<Integer>(Arrays.asList(tmp))).size() == seq.length)) {
            throw new AssertionError("not all values are unique");
        }
    
        int expected_result = TestDataUniqueValues.get_expected_result();
        int result = minimum_index(seq);
        if (result != expected_result) {
            throw new AssertionError("result is different than the expected result");
        }
    }
    
    public static void TestWithExactlyTwoDifferentMinimums() {
        int[] seq = TestDataExactlyTwoDifferentMinimums.get_array();
        if (seq.length < 2) {
            throw new AssertionError("less than 2 elements in the array");
        }
    
        int[] tmp = seq.clone();
        Arrays.sort(tmp);
        if (!(tmp[0] == tmp[1] && (tmp.length == 2 || tmp[1] < tmp[2]))) {
            throw new AssertionError("there are not exactly two minimums in the array");
        }
    
        int expected_result = TestDataExactlyTwoDifferentMinimums.get_expected_result();
        int result = minimum_index(seq);
        if (result != expected_result) {
            throw new AssertionError("result is different than the expected result");
        }
    }
    
    public static void main(String[] args) {
        TestWithEmptyArray();
        TestWithUniqueValues();
        TestWithExactlyTwoDifferentMinimums();
        System.out.println("OK");
    }
    

    } **

  • + 0 comments

    'use strict';

    process.stdin.resume(); process.stdin.setEncoding('utf-8');

    let inputString: string = ''; let inputLines: string[] = []; let currentLine: number = 0;

    process.stdin.on('data', function(inputStdin: string): void { inputString += inputStdin; });

    process.stdin.on('end', function(): void { inputLines = inputString.split('\n'); inputString = '';

    main();
    

    });

    function readLine(): string { return inputLines[currentLine++]; }

    class TestDataEmptyArray { static get_array(): number[] { return []; } }

    class TestDataUniqueValues { static get_array(): number[] { return [3, 1, 2]; }

    static get_expected_result(): number {
        return 1;
    }
    

    }

    class TestDataExactlyTwoDifferentMinimums { static get_array(): number[] { return [2, 1, 3, 1]; }

    static get_expected_result(): number {
        return 1;
    }
    

    }

    function minimum_index(seq: number[]): number { if (seq.length === 0) { throw new Error("Cannot get the minimum value index from an empty sequence"); } let min_idx: number = 0; for (let i: number = 1; i < seq.length; i++) { if (seq[i] < seq[min_idx]) { min_idx = i; } } return min_idx; }

    function TestWithEmptyArray(): void { try { const seq: number[] = TestDataEmptyArray.get_array(); const result: number = minimum_index(seq); } catch (e) { return; } throw new Error("Exception wasn't thrown as expected"); }

    function TestWithUniqueValues(): void { const seq: number[] = TestDataUniqueValues.get_array(); const result: number = minimum_index(seq); if (result !== TestDataUniqueValues.get_expected_result()) { throw new Error("Result doesn't match the expected value for unique values array"); } }

    function TestWithExactlyTwoDifferentMinimums(): void { const seq: number[] = TestDataExactlyTwoDifferentMinimums.get_array(); const result: number = minimum_index(seq); if (result !== TestDataExactlyTwoDifferentMinimums.get_expected_result()) { throw new Error("Result doesn't match the expected value for array with two minimums"); } }

    function main(): void { try { TestWithEmptyArray(); TestWithUniqueValues(); TestWithExactlyTwoDifferentMinimums(); console.log("OK"); } catch (e) { console.log((e as Error).message); } }

  • + 0 comments
    class TestDataEmptyArray():
        def get_array():
            return []
    
    class TestDataUniqueValues():
        def get_array():
            return [11, 7]
        def get_expected_result():
            return 1
    
    class TestDataExactlyTwoDifferentMinimums():
        def get_array():
            return [5, 5]
        def get_expected_result():
            return 0
    
  • + 0 comments

    Java

    public class Solution { public static void main(String[] args) throws Exception {

        TestDataEmptyArray tea=new TestDataEmptyArray();
    
        try
        {
            if(tea.get_array().length==0)
            {
                throw new Exception("Cannot get the minimum value index from an empty sequence");
            }
        }
        catch(Exception ex)
        {
            if(ex.getMessage() == "Cannot get the minimum value index from an empty sequence")
            {
                //System.out.print("OK");
            }
        }
    
        TestDataUniqueValues tduv = new TestDataUniqueValues();
    
        var temp=tduv.get_array();
    
        int minidx=0;
    
        for (int i=0; i<temp.length;i++) 
        {
            if(temp[i]<temp[minidx])
            {
                minidx=i;
            }
        }
    
        if(minidx == tduv.get_expected_result())
        {
            //System.out.print("OK");
        }
    
        TestDataExactlyTwoDifferentMinimums tdtdm=new TestDataExactlyTwoDifferentMinimums();
    
        temp=tdtdm.get_array();
    
        var res= -1;
    
        int minItem=999999999;
    
        for (int i=0; i<temp.length;i++) 
        {
            if(temp[i]<minItem)
            {
                minItem=temp[i];
                res=i;
            }
        }
    
        var tres = tdtdm.get_expected_result();
    
        if(tres==res)
        {
            System.out.print("OK");
        }
    }
    

    }

    class TestDataEmptyArray { public int[] get_array() { return new int[0]; } }

    class TestDataUniqueValues { public int[] get_array() { return new int[]{3,2,6,9,6}; }

    public int get_expected_result()
    {
        return 1;
    }
    

    }

    class TestDataExactlyTwoDifferentMinimums { public int[] get_array() { return new int[]{3,2,6,9,2,6}; }

    public int get_expected_result()
    {
        return 1;
    }
    

    }

  • + 0 comments

    python

        class TestDataEmptyArray:
                def get_array():
                        return []
    
    class TestDataUniqueValues:
            def get_array():
                    return [9, 1]
    
            def get_expected_result():
                    return 1
    
    class TestDataExactlyTwoDifferentMinimums():
            def get_array():
                    return[1,1,2]
    
            def get_expected_result():
                    return 0