Python Challenge - Find Xth Number In Order

I attempted to do it without using .sort() or the sorted function.
I used a simple sorting algorithm that’s not in-place (and is not efficient):
first create a list of boolean values to keep track of whether a given index has been used yet
in a loop
find the smallest number in the list that for the indices that are marked as not used yet
when that smallest number is found, increment the counter, and mark the index of that number as used
stop when the counter reaches n (if we’re trying to find the nth number).

code for the function
def getX(x, nums):
  used = [False for n in nums]
  #ordered = [None for n in nums]
  length = len(nums)
  if length is 0 or x > length or x < 1:
    return 0
  min_so_far = nums[0]
  index_of_min = 0
  for i in range(x): # i counts how many numbers so far
    started = False
    for j in range(length):
      n = nums[j]
      if used[j] is False and (n < min_so_far or not started):
        min_so_far = n
        index_of_min = j
        started = True
      #endfor
    #ordered[i] = min_so_far
    used[index_of_min] = True
    #endfor
  return min_so_far

This is very similar to an earlier post by @lucemile.
I also see that others used bubble sort or sorting algorithms similar to that, for example the post by @digital9933512222

def getX(x, nums): # write your code here for num in nums: if x in range(1, len(nums) + 1): return sorted(nums)[x-1] else: return 0 if nums == []: return 0 print(getX(2, [6, 3, -1, 5])) print(getX(4, [6, 3, -1, 5])) print(getX(5, [6, 3, -1, 5])) print(getX(5, [6, 3, -1, 5, -1])) print(getX(2, []))

Here is a quick solution to this problem:

def getX(x, nums):
nums.sort()
return nums[x - 1]

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

def getX(x, nums):
  nums_sorted = sorted(nums)
  if not nums or x > len(nums):
    return 0
  else:
    return nums_sorted[x - 1]
def getX(x, nums):
  # write your code here
  if len(nums) != 0 and x <= len(nums):
    nums.sort()
    return nums[x-1]
  return 0

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

def getX(x, nums): # write your code here nums.sort() if x > len(nums) or nums == []: return 0 else: return nums[x-1] print(getX(2, [6, 3, -1, 5])) print(getX(4, [5, 10, -3 , -3, 7, 9])) print(getX(2, [5, 10, -3, -3, 7, 9])) print(getX(10, [5, 10, -3, -3, 7, 9, 2]))
def getX(x, nums): # write your code here if x > len(nums): return 0 if sum(nums) ==0: return 0 sorted_nums = sorted(nums) return sorted_nums[x-1] print(getX(2, [6, 3, -1, 5]))
def getX(x, nums): # write your code here nums.sort() print("sorted nums is " + str(nums)) print("x is " + str(x)) print("length of nums is: " + str(len(nums))) if x > len(nums): print("x is higher than length of nums") return 0 elif x == 0: print("x is 0") return 0 elif len(nums) == 0: print("nums is empty") return 0 else: return nums[x-1] #print(getX(0, [6, 3, -1, 5])) #print(getX(0, [])) #print(getX(1, [])) #print(getX(2, [6, 3, -1, 5])) print(getX(10, [6, 3, -1, 5]))
def getX(x, nums): # write your code here nums.sort() len_of_a_list = len(nums) if len_of_a_list >= x and x > 0 and len_of_a_list != 0: return nums[x-1] else: return 0 print(getX(2, [5, 10, -3, -3, 7, 9]))

Will this work if a negative integer is passed as ‘x’?

Works with positive and non-positive input values

def getX(x, nums):
    if abs(x) <= len(nums) and nums:
        nums.sort()
        if x > 0:
            return nums[x-1]
        else:
            return nums[x]
    else:
        return 0
def getX(x, nums): if x > len(nums) or len(nums) == 0: return 0 else: nums.sort() for i in range(len(nums)): if i == x-1: return nums[i] print(getX(2, [6, 3, -1, 5]))

‘’’ python
def getX(x, nums):

write your code here

if x > len(nums) or nums == :
return 0
else:
sorted_list = sorted(nums)
x_number = sorted_list[x - 1]
return x_number

print(getX(7, [1, 5, 6, 8, 9, 10, 15, 25, -97]))

‘’’

def getX(x, nums):
  # write your code here
  if x > len(nums) or nums == []:
    return 0
  else:
    sorted_list = sorted(nums)
    x_number = sorted_list[x - 1]
    return x_number

print(getX(7, [1, 5, 6, 8, 9, 10, 15, 25, -97]))

Please ignore my first answer. It is not properly indented

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

def getX(x, nums):

write your code here

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

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

On looking over your code, I don’t think it works as to its intended purpose. We want the nth number of a sequence, correct? Where does sorting come into play? That will jumble the sequence, won’t it?

There are a number of ways to validate a sequence. We’re going direct to the object to obtain the vital information:

y = hasattr(nums, '__iter__')

That tells us whether nums is iterable, or not. Now we’ve validated it for that feature, we next can cache the length;

n = len(nums)

As much as we would like to write,

if y and n:

we cannot. Non-sequential values don’t have a length. So we have to put the latter assignment inside an if.

if y:
    n = ...
else:
    raise TypeError

What happens next is still to be resolved. We need to be sure that n is non-zero. Allowing for that, we now need to validate the index value in the arguments. Is x in range?

k = x in range(1, n + 1)

Those are all the test conditions. Did you catch the sneaky two birds with one stone? Now we only have to return the value at the requested position.

if k:
    return nums[x-1]
else:
    raise IndexError  #  this happens if n is zero or x is out of range
def getX(x, nums): # write your code here # first we sort the list s_nums = sorted(nums) # we need to go one index back, because the list starts with index 0 x = x - 1 # condition: if x is equal or smaller than the last index and equal or higher than the first index if (x <= len(s_nums) - 1 and x >= 0): return s_nums[x] # if above argument is not true return 0 else: return 0 print(getX(2, [6, 3, -1, 5]))

Hey all, this is my first post in the forum - I am happy to get some feedback on my solution of the challenge :slight_smile: !
Thanks in advance!

def getX(x, nums): length = len(sorted(nums)) num_list = sorted(nums) if x > length or length == 0: value = 0 else: value = num_list[(x-1)] return value print(getX(2, [6, 3, -1, 5]))