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 () 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 () below!
You can also find further discussion and get answers to your questions over in #get-help.
Agree with a comment or answer? Like () to up-vote the contribution!
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]))
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):
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]
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 = []
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]