``````

In [49]:

def quick_sort(mylist):
if len(mylist) <= 1:
return mylist

pivot = mylist[0]

less = [x for x in mylist if x < pivot]
more = [x for x in mylist if x > pivot]
pivotList = [x for x in mylist if x == pivot]

less = quickSort(less)
more = quickSort(more)

return less + pivotList + more

a = [4, 65, 2, -31, 0, 99, 83, 782, 1]
print(a, " -> ", quick_sort(a))

``````
``````

[4, 65, 2, -31, 0, 99, 83, 782, 1]  ->  [-31, 0, 1, 2, 4, 65, 83, 99, 782]

``````
``````

In [52]:

def find(data, key, l, r, default):
if r < 1:
return default

midpoint = 1 + (r - l) // 2

if key < data[midpoint]:
return find(data, key, 1, midpoint - 1, default)
if key > data[midpoint]:
return find(data, key, midpoint + 1, r, default)
else:
return midpoint

a = quick_sort([4, 65, 2, -31, 0, 99, 83, 782, 1])
print(a)

print(find(a, -31, 0, len(a)-1, -999))

``````
``````

[-31, 0, 1, 2, 4, 65, 83, 99, 782]
-999

``````
``````

In [78]:

def first_and_last(l, item):
first, last = -1, -1

for i in range(len(l)):
if l[i] == item:
if first == -1:
first = i
last = i

return first, last

print(first_and_last([1, 3, 6, 7, 7, 7 ,88, 103, 426], 7))

``````
``````

(3, 5)

``````
``````

In [101]:

def find(data, key, l, r, default):
if r < 1:
return default

midpoint = 1 + (r - l) // 2

if key < data[midpoint]:
return find(data, key, 1, midpoint - 1, default)
if key > data[midpoint]:
return find(data, key, midpoint + 1, r, default)
else:
return midpoint

def binary_first_and_last(data, key):
first = last = find(data, key, 0, len(data)-1, -1)

while data[first-1] == key:
first = first - 1

while data[last+1] == key:
last = last + 1

return first, last

print(binary_first_and_last([1, 3, 6, 7, 7, 7 ,88, 103, 426], 7))

``````
``````

(3, 5)

``````
``````

In [ ]:

``````