Sort by

recency

|

224 Discussions

|

  • + 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
    
  • + 0 comments

    JAVA 8

    static Random rand = new Random();
    
    public static class TestDataEmptyArray
    {
        public static int[] get_array()
        {
            return new int[0];
        }
    
    }
    
    public static class TestDataUniqueValues
    {
        static int[] arr;
    
        public static int[] get_array()
        {
            int bound = rand.nextInt(100) + 2;
            arr = new int[bound];
            Set<Integer> set = new HashSet<>();
    
            while (set.size() < bound)
            {
                set.add(rand.nextInt(100000));
            }
    
            arr = set.stream().mapToInt(Integer::intValue).toArray();
    
            return arr;
        }
    
        public static int get_expected_result()
        {
            int min_index = 0;
    
            for (int i = 1; i < arr.length; i++)
            {
                if (arr[i] < arr[min_index])
                {
                    min_index = i;
                }
            }
    
            return min_index;
        }
    }
    
    public static class TestDataExactlyTwoDifferentMinimums
    {
        static int[] arr;
    
        public static int[] get_array()
        {
            int bound = rand.nextInt(100) + 2;
            arr = new int[bound];
            Set<Integer> set = new HashSet<>();
    
            while (set.size() < bound - 1)
            {
                set.add(rand.nextInt(100000));
            }
    
            arr = set.stream().mapToInt(Integer::intValue).toArray();
    
            Arrays.sort(arr);
            arr[arr.length - 1] = arr[0];
    
            return arr;
        }
    
        public static int get_expected_result()
        {
            int min_index = 0;
    
            for (int i = 1; i < arr.length; i++)
            {
                if (arr[i] < arr[min_index])
                {
                    min_index = i;
                }
            }
    

    I wanted to go off track and use abstract classes, but everything ended up being more straightforward than I expected, haha.

            return min_index;
        }
    }
    
  • + 0 comments

    Where is C# for this problem?