Skip to content

Commit bc442a0

Browse files
author
Mari Wahl
committed
more typos! 😓
1 parent 56ad845 commit bc442a0

File tree

4 files changed

+47
-68
lines changed

4 files changed

+47
-68
lines changed

book/book_second_edition.pdf

-1.29 KB
Binary file not shown.

src/searching_and_sorting/searching/binary_search.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,63 @@
33
__author__ = "bt3"
44

55

6-
7-
def binary_search_rec(array, item, lo=0, hi = None):
6+
def binary_search(array, item, hi=None, lo=0):
87
'''
9-
>>> binary_search_rec([2,3,5,6,8,10,15,23], 15)
10-
(True, 6)
11-
>>> binary_search_rec([2,3,5,6,8,10,15,23], 4)
8+
>>> binary_search([2,3,5,6,8,10,15,23], 15)
9+
6
10+
>>> binary_search([2,3,5,6,8,10,15,23], 4)
11+
False
12+
>>> binary_search([1,3,4,5,7,8 ,10,12,23], 10)
13+
6
14+
>>> binary_search([1,3,4,5,7,8 ,10,12,23], 22)
1215
False
1316
'''
17+
1418
hi = hi or len(array)
15-
if hi < lo :
19+
if hi < lo:
1620
return False
1721

1822
mid = (hi + lo)//2
19-
20-
if array[mid] == item:
21-
return True, mid
22-
elif array[mid] < item:
23-
return binary_search_rec(array, item, mid + 1, hi)
23+
if item == array[mid]:
24+
return mid
25+
elif item < array[mid]:
26+
return binary_search(array, item, hi=mid-1, lo=lo)
2427
else:
25-
return binary_search_rec(array[:mid], item, lo, mid -1)
28+
return binary_search(array, item, hi=hi, lo=mid+1)
29+
2630

2731

2832

2933
def binary_search_iter(array, item):
3034
'''
3135
>>> binary_search_iter([2,3,5,6,8,10,15,23], 15)
32-
(True, 6)
36+
6
3337
>>> binary_search_iter([2,3,5,6,8,10,15,23], 4)
3438
False
39+
>>> binary_search_iter([1,3,4,5,7,8 ,10,12,23], 10)
40+
6
41+
>>> binary_search_iter([1,3,4,5,7,8 ,10,12,23], 22)
42+
False
3543
'''
36-
hi = len(array)
37-
lo = 0
44+
lo, hi = 0, len(array)
3845

3946
while lo < hi:
4047
mid = (hi+lo)//2
4148
if array[mid] == item:
42-
return True, mid
49+
return mid
4350
elif array[mid] > item:
4451
hi = mid
4552
else:
46-
lo = mid + 1
53+
lo=mid+1
4754
return False
4855

4956

5057

5158

5259

60+
61+
62+
5363
if __name__ == '__main__':
5464
import doctest
5565
doctest.testmod()

src/searching_and_sorting/sorting/merge_sort.py

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,34 @@
33
__author__ = "bt3"
44

55

6-
def merge_sort(seq):
6+
def merge_sort(array):
77
'''
8-
>>> seq = [3, 5, 2, 6, 8, 1, 0, 3, 5, 6, 2]
9-
>>> merge_sort(seq)
10-
[0, 1, 2, 2, 3, 3, 5, 5, 6, 6, 8]
8+
>>> merge_sort([3 ,5, 1, 2, 10, 6])
9+
[1, 2, 3, 5, 6, 10]
1110
'''
12-
if len(seq) < 2:
13-
return seq
14-
mid = len(seq)//2
15-
lft, rgt = seq[:mid], seq[mid:]
16-
if len(lft)>1:
17-
lft = merge_sort(lft)
18-
if len(rgt)>1:
19-
rgt = merge_sort(rgt)
20-
21-
res = []
22-
while lft and rgt:
23-
if lft [-1]>= rgt[-1]:
24-
res.append(lft.pop())
25-
else:
26-
res.append(rgt.pop())
27-
res.reverse()
28-
return(lft or rgt) + res
29-
30-
31-
32-
# separating the merge part in another function
33-
def merge_sort_sep(seq):
34-
'''
35-
>>> seq = [3, 5, 2, 6, 8, 1, 0, 3, 5, 6, 2]
36-
>>> merge_sort_sep(seq)
37-
[0, 1, 2, 2, 3, 3, 5, 5, 6, 6, 8]
38-
'''
39-
if len(seq) < 2 :
40-
return seq
41-
mid = len(seq)//2
42-
left = merge_sort(seq[:mid])
43-
right = merge_sort(seq[mid:]) # notice that mid is included!
44-
return merge(left, right) # merge iteratively
11+
if len(array) < 2:
12+
return array
4513

14+
mid = len(array)//2
15+
left = merge_sort(array[:mid])
16+
right = merge_sort(array[mid:])
4617

47-
def merge(left, right):
48-
if not left or not right:
49-
return left or right # nothing to be merged
50-
result = []
18+
res = []
5119
i, j = 0, 0
5220
while i < len(left) and j < len(right):
5321
if left[i] <= right[j]:
54-
result.append(left[i])
22+
res.append(left[i])
5523
i += 1
5624
else:
57-
result.append(right[j])
25+
res.append(right[j])
5826
j += 1
59-
if left[i:] : result.extend(left[i:]) # REMEMBER TO EXTEND, NOT APPEND
60-
if right[j:] : result.extend(right[j:])
61-
return result
27+
28+
if left[i:]:
29+
res.extend(left[i:])
30+
if right[j:]:
31+
res.extend(right[j:])
32+
return res
33+
6234

6335

6436
''' Merge sort for files '''

src/searching_and_sorting/sorting/quick_sort.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ def qs(array):
1818
left = [a for a in new_array if a <= piv_element]
1919
right = [a for a in new_array if a > piv_element]
2020

21-
2221
return qs(left) + [array[piv]] + qs(right)
2322

2423

25-
""" we can also divide them into two functions """
24+
25+
# we can also divide them into two functions
2626
def partition(seq):
2727
pi,seq = seq[0],seq[1:]
2828
lo = [x for x in seq if x <= pi]
@@ -41,9 +41,6 @@ def quick_sort_divided(seq):
4141

4242

4343

44-
45-
46-
4744
if __name__ == '__main__':
4845
import doctest
4946
doctest.testmod()

0 commit comments

Comments
 (0)