Skip to content

Instantly share code, notes, and snippets.

@Fredpwol
Created April 23, 2021 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fredpwol/61fea0b58c5e31f4702a4b3c7bbc887e to your computer and use it in GitHub Desktop.
Save Fredpwol/61fea0b58c5e31f4702a4b3c7bbc887e to your computer and use it in GitHub Desktop.
Find num start and end position in a list after sorting
def findSortedPosition(nums, val):
if nums == None: return [-1, -1]
if val == None: return [-1, -1]
prev_num = None
nums_less = 0
num_val = 0
for num in nums:
if num < val:
nums_less += 1
elif num == val:
num_val += 1
if num_val > 0:
return [nums_less, (nums_less+num_val)-1] #subtract one here because of 0 based indexing
return [-1, -1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment