-
Notifications
You must be signed in to change notification settings - Fork 0
/
disorganized_handyman.py
62 lines (52 loc) · 1.62 KB
/
disorganized_handyman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
A disorganized handyman has mixed up his nuts and bolts in a bag and your task is to match them as
quickly as possible.
"""
import random
n = int(input("Number of nut and bolts in total"))
nuts = [0]*n
bolts = [0]*n
types = int(input("The different categories of nuts and bolts available:"))
for i in range (n):
number_generated = random.randint(1, types)
nuts[i] = bolts[i] = number_generated
random.shuffle(bolts)
"""
Now to solve this naively would be take one nut and try it with n bolts. But that will be an
O(n^2) solution.
So, we can should look to sort the lists and then each nut will be matched to its bolt.
We will use quicksort and that too inplace so that we can work with small memory.
"""
def pivot_partition_inplace(lst, start, end):
pivot = lst[end]
bottom = start - 1
top = end
done = False
while not done:
while not done:
bottom += 1
if (bottom == top):
done = True
break
if (lst[bottom] > pivot):
lst[top] = lst[bottom]
break
while not done:
top -= 1
if (top == bottom):
done = True
break
if (lst[top] < pivot):
lst[bottom] = lst[top]
break
lst[top] = pivot
return top
def quicksort(lst, start, end):
if start < end:
split = pivot_partition_inplace(lst, start, end)
quicksort(lst, start, split - 1)
quicksort(lst, split + 1, end)
return
quicksort(nuts, 0, n - 1)
quicksort(bolts, 0, n - 1)
print(nuts, bolts)