# Python Challenge - Find Xth Number In Order

def getX(x, nums): sortedNums = sorted(nums) if x <= len(sortedNums) and x > 0: return sortedNums[x-1] else: return 0 print(getX(2, [6, 3, -1, 5]))
getX = lambda x,nums: sorted(nums)[abs(x)-1] if nums and x <= len(nums) else 0

Check that nums is not an empty list, x is a nonnegative integer, and x is not greater than the length of nums.

``````def getX(x, nums):
if not nums or x < 1 or x > len(nums):
return 0
return sorted(nums)[x-1]
print(getX(2, [6, 3, -1, 5]))
``````
def getX(x, nums): # write your code here if (x > len(nums)) or (len(nums) == 0): return 0 else: return sorted(nums)[x-1] print(getX(2, [6, 3, -1, 5]))
def getX(x, nums): # write your code here if x > len(nums) or len(nums) == 0: print('Error') return 0 else: nums.sort() print(nums) return nums[x-1] print(getX(2, [6, 3, -1, 5])) print(getX(2,[1,2])) print(getX(7, [-3, -2, -1, 0, 1, 2, 3]))

This was my solution:

Pd. I added a third conditional to test if x equals to 0 because otherwise if the user introduces 0 by mistake, the function would return the last element of the list (-1)

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

def getX(x, nums):

# write your code here

nums_sorted = nums

# If the input x is greater than the length of the list, or nums is empty, return 0.

if (nums == ) or (x > len(nums)):
xth_number = 0
else:
step = 1
while step <= x:
xth_number = nums_sorted
for number in nums_sorted:
if xth_number > number:
xth_number = number
step += 1
nums_sorted.remove(xth_number)
return xth_number

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

def getX(x, nums): # write your code here if not x > len(nums) and len(nums): return sorted(nums)[x-1] return 0 print(getX(2, [6, 3, -1, 5]))
1 Like

def getX(x, nums):

# write your code here

n = len(nums)

for i in range(n): #sorts loop in ascending order
for j in range(i + 1, n):
if nums[i] > nums[j]:
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp

print(nums) #prints out the sorted list

if x > n or n == 0 or x < 0:
return 0
if x <= n:
return nums[x - 1]
#if x == n:
#return nums[n - 1]

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

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

Short and elegant, Great job!

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

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

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

print(getX(4, [5, 10, -3 , -3, 7, 9]))
``````
def getX(x, nums): if nums == [] or x > len(nums): return 0 else: nums.sort() return nums[x-1] print(getX(2, [6, 3, -1, 5]))
def getX(x, nums): if x > len(nums) or len(nums) == 0: return 0 sorty = sorted(nums) return sorty[x-1] # write your code here print(getX(5, [6, 3, -1, 5]))