Python Challenge - Find Xth Number In Order

This community-built FAQ covers the “Find Xth Number In Order” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the Python challenge Find Xth Number In Order

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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

all u gotta do is first check if there is nothing in the list or if the x is greater than the list if so then return 0
if not then just sort the list and return the the number in the list index - 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])) print(getX(4, [5, 10, -3 , -3, 7, 9]))
1 Like
def getX(x, nums): if x > len(nums) or len(nums) < 1: return 0 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 #sorting list nums.sort() for count, element in enumerate(nums,1) : if x == count : return element print(getX(2, [6, 3, -1, 5]))

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

def getX(x, nums): nums.sort() if len(nums) <= 0 or abs(x) > len(nums) or type(x) != int: return 0 else: return 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 nums = sorted(nums) 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]))

def getX(x, nums):
nums.sort()
try:
return (nums[x-1])
except:
return 0
print(getX(5, [6, 3, -1, 5]))

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

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

def getX(x, nums):
return sorted(nums)[x-1] if x in range(1,len(nums)+1) else 0

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

Here’s how I thought of doing it:
Is it better to have more readable code or more concise code?

def getX(x, nums):
snums = sorted(nums)
try:
return snums[x-1]
except:
return 0

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

def getX(x, nums):
  try:
    return sorted(nums)[x-1]
  except:
    return 0

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

def getX(x, nums):
count = int(len(nums))
if x > count or nums == :
return 0
else:
for i in range(count):
for j in range(count):
if nums[i] < nums[j]:
nums[i],nums[j]=nums[j],nums[i]
return nums[x-1]

print(getX(7, [6, 3, -1, 5,8,9,-10,7,8,45,34,13,34]))

I used Error Checking to figure out if the position was invalid

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

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

Hey there seems to be a problem with my code but I can’t find it since all the tests I did went without any problems. But when I press the Test Code Button in the challenge it always gives me the following error message: ‘Tests failed to run due to an error: “list index out of range”. Check your code and try again.’.
If anyone can find the problem I would be very thankful if you’d share it with me.

Also I did some calculations and the runtime should be on average at least 2O(n) = O(n). Which should in theory be a better runtime than the sort function. But I’m also not quite sure about that so if someone wants to also check that I would be even more thankful.

def getX(x, nums):
  smaller = []
  bigger = []
  for j in range(len(nums)):
#split in two arrays one with smaller numbers than nums[0] one with bigger
    for i in range(1,len(nums)):
      if nums[0] >= nums[i]:
        smaller.append(nums[i])
      elif nums[0] < nums[i]:
        bigger.append(nums[i])
# if len(smaller) == x-1 we found our number otherwise replace nums with either smaller 
#or bigger depending on the size of them and x. 
    if len(smaller) == x - 1:
      return nums[0]
    elif len(smaller) > x - 1:
      nums = smaller
    elif len(smaller) < x:
      nums = bigger
      x = x - (len(smaller) + 1)
    smaller = []
    bigger = []

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

This solution uses Python’s built in sort() function for lists, whose time complexity is about n log n. The space complexity, however, is n. Given that the problem emphasizes time over space, this would be the ideal solution for time and simplicity, in my opinion.

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