Today we will see the HackerRank Day 27 Solution in Python. The problem is named Testing which is part of 30 Days of code on HackerRank. Let’s get started!
Day 27: Testing Problem statement
The problem statement given is about unit testing. We need to write a function that meets the following requirements:
- For a given array of n integers, the function returns the index of the element with the minimum value in the array. If there is more than one element with the minimum value, it returns the smallest one.
- If an empty array is passed to the function, it raises an exception.
We have to finish the implementation of 3 classes to provide data and expected results for the tests.
In the class TestDataEmptyArray
:
get_array()
returns an empty array
In the class TestDataUniqueValues
:
get_array()
returns an array of size at least 2 with all unique elementsget_expected_result()
returns the expected minimum value index for this array
In the class TestDataExactlyTwoDifferentMinimums
:
get_array()
returns an array where the minimum value occurs at exactly 2 indicesget_expected_result()
returns the expected index
You can solve the problem here.
HackerRank Day 27 Solution in Python
def minimum_index(seq): if len(seq) == 0: raise ValueError("Cannot get the minimum value index from an empty sequence") min_idx = 0 for i in range(1, len(seq)): if seq[i] < seq[min_idx]: min_idx = i return min_idx class TestDataEmptyArray(): #Function to return an empty array def get_array(): return [] class TestDataUniqueValues(): #Function to return an array of size at least 2 with all unique elements def get_array(): uniq_array = [1,2,3,4] return uniq_array #Function to return the expected minimum value index for this array def get_expected_result(): return 0 class TestDataExactlyTwoDifferentMinimums: #Function to return an array where the minimum value occurs at exactly 2 indices def get_array(): rtn_list = [0,2,0,3] return rtn_list #Function to return the expected index def get_expected_result(): return 0 def TestWithEmptyArray(): try: seq = TestDataEmptyArray.get_array() result = minimum_index(seq) except ValueError as e: pass else: assert False def TestWithUniqueValues(): seq = TestDataUniqueValues.get_array() assert len(seq) >= 2 assert len(list(set(seq))) == len(seq) expected_result = TestDataUniqueValues.get_expected_result() result = minimum_index(seq) assert result == expected_result def TestiWithExactyTwoDifferentMinimums(): seq = TestDataExactlyTwoDifferentMinimums.get_array() assert len(seq) >= 2 tmp = sorted(seq) assert tmp[0] == tmp[1] and (len(tmp) == 2 or tmp[1] < tmp[2]) expected_result = TestDataExactlyTwoDifferentMinimums.get_expected_result() result = minimum_index(seq) assert result == expected_result TestWithEmptyArray() TestWithUniqueValues() TestiWithExactyTwoDifferentMinimums() print("OK")
Code Explanation
- First, we created the class
TestDataEmptyArray
, in which the methodget_array()
returns an empty array - Then we created the class
TestDataUniqueValues
in whichget_array()
returns an array of size at least 2 with all unique elements. Theget_expected_result()
method returns the expected minimum value index for this array - Then we created the class, in which
get_array()
returns an array where the minimum value occurs at exactly 2 indices. Theget_expected_result()
method returns the expected index
Also Read:
- HackerRank Day 8 Solution in Python: Dictionaries and Maps
- HackerRank Day 7 Solution in Python: Arrays
- HackerRank Day 6 Solution in Python: Let’s review
- HackerRank Day 5 Solution in Python: Loops
- HackerRank Day 4 Solution in Python: Class vs Instance
- HackerRank Day 3 Solution in Python: Intro to Conditional Statements
- HackerRank Day 2 Solution in Python: Operators
- HackerRank Day 1 Solution in Python: Data Types
- HackerRank Day 0 Solution in Python: Hello World
- HackerRank Day 29 Solution in Python: Bitwise AND
- HackerRank Day 28 Solution in Python: RegEx, Patterns, and Intro to databases
- HackerRank Day 27 Solution in Python: Testing
- HackerRank Day 26 Solution in Python: Nested Logic
- HackerRank Day 25 Solution in Python: Running Time and Complexity
- HackerRank Day 24 Solution in Python: More Linked Lists
- HackerRank Day 23 Solution in Python: BST Level Order Traversal
- HackerRank Day 22 Solution in Python: Binary Search Trees
- HackerRank Day 20 Solution in Python: Sorting
- HackerRank Day 19 Solution in Python: Interfaces
- HackerRank Day 18 Solution in Python: Queues and Stacks
- HackerRank Day 17 Solution in Python: More Exceptions
- HackerRank Day 16 Solution: Exceptions – String to Integer
- HackerRank Day 15 Solution in Python: Linked List
- HackerRank Day 13 Solution in Python: Abstract Classes
- HackerRank Day 14 Solution in Python: Scope
- HackerRank Day 12 Solution in Python: Inheritance
- HackerRank Day 11 Solution in Python: 2D Arrays
- HackerRank Day 10 Solution in Python: Binary Numbers