catalins.tech with Gatsby

[Coderbyte] – Second GreatLow [Easy]

2019 Feb 1st

The challenge I have solved recently is called “Second GreatLow”.  The full challenge description, taken from the Coderbyte website, is below:

Have the function SecondGreatLow(arr) take the array of numbers stored in arr and return the second lowest and second greatest numbers, respectively, separated by a space. For example: if arr contains [7, 7, 12, 98, 106] the output should be 12 98. The array will not be empty and will contain at least 2 numbers. It can get tricky if there’s just two numbers! 

Use the Parameter Testing feature in the box below to test your code with different arguments.

One important thing to consider is the duplicate numbers in the array. Also, it is important to think whether to sort the array or not beforehand. If there are no duplicate numbers and if the array is sorted, then the second lowest and second highest numbers are always at position 1 (array[1]) and the penultimate position (array[len(array)-2]), respectively.

The problem asks only for those two numbers so I have assumed that the duplicates can be removed. Therefore, I have decided to convert the array to a set. The reason for doing so is because sets do not contain the same number multiple times. Also, it is more convenient if the array is sorted, because the two numbers can be accessed without looping over the array and checking each number individually.

The line below does that – it converts the array to a set and orders it. An array such as [7, 7, 12, 12, 7, 98, 106] becomes [7, 12, 98, 106].

arr = sorted(set(arr))

However, when there are only two numbers in the array the position of those numbers are 0 and 1. That is the first and last positions. So it just returns arr[1] and arr[0]. The code below checks the length of the array, and if it only has two numbers, it returns those.

def SecondGreatLow(arr): 
    if len(arr) == 2:
        return str(arr[1]) + " " + str(arr[0])

Another important scenario is when there is only one number in the array. If there is only one number in the array, it returns the same number twice. The solution did not pass the test case if it did not return the same number twice. The line below does just that.

    if len(arr) == 1:
        return str(arr[0]) + " " + str(arr[0])

The last thing left is to check if the array is empty. If the array is empty, then return a message saying that the array is empty. Once again, the second lowest number is in the second position and the second most significant number is in the penultimate place in a sorted set. The full solution to this challenge is below:

def SecondGreatLow(arr): 
    
    # convert the array to set - no duplicates
    # sort the array
    arr = sorted(set(arr))

    if len(arr) == 2:
        return str(arr[1]) + " " + str(arr[0])
        
    if len(arr) == 1:
        return str(arr[0]) + " " + str(arr[0])
        
    if len(arr) < 1:
        return "Empty array"
        
    return str(arr[1]) + " " + str(arr[len(arr)-2])

As it happened on a previous occasion, while writing this blog article I came up with a better solution than the one I have initially submitted on Coderbyte.

The Coderbyte’s solution is as follows (the same idea):

def SecondGreatLow(arr): 

  # first we create a unique list by 
  # using the Python set function
  unique = set(arr)
   
  # sort the new list
  sortedList = sorted(unique)
 
  # return the second smallest and largest elements
  # but also check to make sure there is more than 1
  # element in the list
  if len(sortedList) < 2:
    return str(sortedList[0]) + " " + str(sortedList[0])
  else:
    return str(sortedList[1]) + " " + str(sortedList[len(sortedList)-2])