Python Challenge - Find Xth Number In Order

On your line 19 at

x = x - (len(smaller) + 1)

I removed the +1 at the end, and it no longer gave out of range errors. It still only passed some of the tests.

You wrote that the O(n) time complexity would perform better than Python’s built in sort function. However, when I searched the time complexity of the .sort() function, it came up to be O(n log n) in time and O(n) in space. It also brought me to a much simpler answer to write, taking only 4 body lines of the function.

I hope this helps! Happy coding!

def getX(x, nums):
if x > len(nums) or nums == :
return 0
nums.sort()
return nums[x - 1]

print(getX(2, [6, 3, -1, 5]))

Thank you for your reply.

def getX(x, nums): nums.sort() n = len(nums) if n == 0: return 0 else: if x <= n: x_number = nums[x-1] return x_number else: return 0 x_number = getX(2, [5, 10, -3, -3, 7, 9]) print(str(x_number))
def getX(x, nums): nums.sort() if x > len(nums) or nums == []: return 0 else: for i in nums: i = nums[:x] final = i[-1] return final print(getX(2, [6, 3, -1, 5])) print(getX(2, [5, 10, -3, -3, 7, 9])) print(getX(4, [5, 10, -3 , -3, 7, 9]))
def getX(x, nums): if len(nums) < x or len(nums)==0: return 0 numbers = nums print(len(numbers)-2) print(len(numbers)-1) for i in range(0, len(numbers)): for j in range(0, len(numbers)): if numbers[i]<=numbers[j]: temp = numbers [i] numbers[i] = numbers[j] numbers[j] = temp print(numbers) return numbers[x-1] getX(4, [5, 10, -3 , -3, 7, 9])

I reinvented the wheel, not once, but twice:

from datetime import datetime def getX1(x, nums): nums_length = len(nums) if x <= nums_length and x > 0: nums2 = nums[0:1] for num in nums[1:]: nums2_length = len(nums2) index = 0 while index < nums2_length: if num > nums2[index]: index += 1 else: nums2.insert(index, num) break if index == nums2_length: nums2.insert(index, num) return nums2[x-1] else: return 0 start1=datetime.now() print(getX1(7, [9139,7272,549,2999,3015,1632,9913,3341,6141,2378])) print('First version: ' + str(datetime.now() - start1)) def getX(x, nums): nums_length = len(nums) if x > nums_length / 2 and x <= nums_length: nums2 = [] index = len(nums) while index >= x: nums2.insert(0,max(nums)) nums.remove(max(nums)) index -= 1 return nums2[0] if x <= nums_length / 2 and x > 0: nums2 = [] index = 0 while index < x: nums2.append(min(nums)) nums.remove(min(nums)) index +=1 return nums2[-1] else: return 0 start2=datetime.now() print(getX(7, [9139,7272,549,2999,3015,1632,9913,3341,6141,2378])) print('Second version: ' + str(datetime.now()-start2)) def getXog(x, nums): if not nums or len(nums) < x: return 0 nums.sort() return nums[x-1] start3=datetime.now() print(getXog(7, [9139,7272,549,2999,3015,1632,9913,3341,6141,2378])) print('Original: ' + str(datetime.now()-start3))

My wheels are inferior to the original. It’d probably be helpful to see the source code for the sort method and I haven’t been successful searching for that source code thus far. If there’s an easy way to direct me to it, I’d appreciate a reply that did so.

My code passes 8 of the 10 tests. It won’t tell me which test it fails and I cannot figure out a situation in which it does not work. Can anyone help???

def getX(x, nums):
  newlist = sorted(nums)
  if len(nums) < x+1 or len(nums) == 0:
    return 0
  else:
    return newlist[x-1]

You likely need to account for the extreme edge cases. What if x is zero or a negative number? In your code, what if the list, nums has 5 elements, and x is 5?

1 Like

I very well could be wrong but I feel like those scenarios are all covered in the code. The if statement checks all of those things. I agree it has to be an edge case but I cannot figure out what that edge case would be.

Your code: (Be sure to read the added comments.)

def getX(x, nums): newlist = sorted(nums) if len(nums) < x+1 or len(nums) == 0: return 0 else: return newlist[x-1] # Test edge cases: print(getX(5, [4, 2, 5, 1, 3])) #Should return 5. Does it? print(getX(0, [4, 2, 5, 1, 3])) #Should return 0. Does it? # The line below should return 0. Un-comment it to see the result. # print(getX(-25, [4, 2, 5, 1, 3]))
1 Like

Very much appreciated. Those test cases were helpful and got me to where I could solve it!

1 Like
def getX(x, nums): nums.sort() if len(nums) <= x-1 or len(nums) == 0: return 0 else: return nums[x-1] print(getX(2, [6, 3, -1, 5]))

def getX(x, nums):
if len(nums) == 0 or x>len(nums):
return 0
nums.sort()
return nums[x-1]

print(getX(2, [6, 3, -1, 5]))

Solution without using the sort function:

def getX(x, nums):
  # Check if array is empty or x > length of array
  if not nums or x>len(nums):
    return 0
  # Find the smallest number x times
  for k in range(1,x+1):
    min_num = nums[0]
    for n in range(1,len(nums)):
      if nums[n] < min_num:
        min_num = nums[n]
    if k == x:
      # If checked x times already, return the current smallest number
      return min_num
    else:
      # Else, remove smallest number and check again
      nums.remove(min_num)

Your original code was fine except line 3, where

len(nums) < x+1

should be

len(nums) < x

instead.
That’s it. In the description you don’t need to cater for negative numbers, only for empty array and if x is greater than the length of the array.

If you make this correction, it will pass all the test cases.

def getX(x, nums): # write your code here if len(nums) == 0: return 0 elif len(nums) < x: return 0 else: sorted_list = sorted(nums) return sorted_list[x-1] print(getX(2, [6, 3, -1, 5]))

I’m beff jezos

def getX(x, nums): nums.sort() if x> len(nums) or x <= 0: return 0 else: return nums[x-1] //I am the beff jezos print(getX(0, [5, 10, -3, -3, 7, 9]))
def getX(x, nums): # write your code here answer = 0 if x >= 1 and x <= len(nums): answer = sorted(nums)[x-1] return answer print(getX(4, [1,2,1,1]))

def getX(x, nums):

sorts the code

nums.sort()

if x is equal to length of the list and is not zero, return the number in position, else, return 0

if x<len(nums)+1 and x != 0:
return nums[x-1]
else:
return 0

print(getX(100, [6, 3, -1, 5]))

def getX(x, nums):
  # write your code here
  if nums == []:
    return 0
  if x > len(nums):
    return 0
  if x <= 0:
    return 0
  nums.sort()
  return nums[x-1]


print(getX(4, [6, 3, -1, 5]))