DEV Community

loading...

Discussion on: Devmates.co [Array Search]

Collapse
camicode profile image
CamCode
list_nums = [3,4,7,7,9,9,9,10]
new_list_nums = []

first_element = -1 #0 
last_element = -1 #len(new_list_nums)-1 

while list_nums:
  min = list_nums[0]
  for num in list_nums:
    if num < min:
      min = num 
  new_list_nums.append(min)
  list_nums.remove(min)
print(new_list_nums)
#how to find index in a list sorted 
index_of_list = [i for i in enumerate(new_list_nums)]

target = 8
n = len(new_list_nums)
def findFirstAndLast(new_list_nums, n, target) : 
  first_element = -1 #0 
  last_element = -1 #len(new_list_nums)-1 

  for i in range(0,n) : 
        if (target != new_list_nums[i]) : 
            continue
        if (first_element == -1) : 
            first_element = i 
        last_element = i 

  if (first_element != -1): 
      print( first_element ,last_element) 
  else : 
        print([-1 , -1]) 


print(findFirstAndLast(new_list_nums,n,target))