Skip to content

Commit 34b2bcc

Browse files
committed
-
1 parent 893ef46 commit 34b2bcc

File tree

3 files changed

+50
-48
lines changed

3 files changed

+50
-48
lines changed

source_py3/python_toolbox/binary_search/binary_search_profile.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
See its documentation for more info.
88
'''
99

10+
from python_toolbox import misc_tools
1011

1112
from .roundings import (Rounding, roundings, LOW, LOW_IF_BOTH,
1213
LOW_OTHERWISE_HIGH, HIGH, HIGH_IF_BOTH,
1314
HIGH_OTHERWISE_LOW, EXACT, CLOSEST, CLOSEST_IF_BOTH,
1415
BOTH)
15-
1616
from .functions import (binary_search, binary_search_by_index,
1717
make_both_data_into_preferred_rounding,
1818
_binary_search_both)
@@ -27,7 +27,8 @@ class BinarySearchProfile:
2727
than one time.
2828
'''
2929

30-
def __init__(self, sequence, function, value, both=None):
30+
def __init__(self, sequence, value, function=misc_tools.identity_function,
31+
both=None):
3132
'''
3233
Construct a `BinarySearchProfile`.
3334
@@ -42,21 +43,18 @@ def __init__(self, sequence, function, value, both=None):
4243
'''
4344

4445
if both is None:
45-
both = _binary_search_both(sequence, function, value)
46+
both = _binary_search_both(sequence, value, function=function)
4647

47-
if function is None:
48-
function = lambda x: x
49-
5048
self.results = {}
5149
'''
5250
`results` is a dict from rounding options to results that were obtained
5351
using each function.
5452
'''
5553

5654
for rounding in roundings:
57-
self.results[rounding] = \
58-
make_both_data_into_preferred_rounding(both, function, value,
59-
rounding)
55+
self.results[rounding] = make_both_data_into_preferred_rounding(
56+
both, value, function=function, rounding=rounding
57+
)
6058
none_count = list(both).count(None)
6159

6260
self.all_empty = (none_count == 2)

source_py3/python_toolbox/binary_search/functions.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
# todo: ensure there are no `if variable` checks where we're thinking of None
1515
# but the variable might be False
1616

17+
from python_toolbox import misc_tools
18+
1719
from .roundings import (Rounding, roundings, LOW, LOW_IF_BOTH,
1820
LOW_OTHERWISE_HIGH, HIGH, HIGH_IF_BOTH,
1921
HIGH_OTHERWISE_LOW, EXACT, CLOSEST, CLOSEST_IF_BOTH,
2022
BOTH)
2123

22-
def binary_search_by_index(sequence, function, value, rounding=CLOSEST):
24+
def binary_search_by_index(sequence, value,
25+
function=misc_tools.identity_function,
26+
rounding=CLOSEST):
2327
'''
2428
Do a binary search, returning answer as index number.
2529
@@ -29,15 +33,15 @@ def binary_search_by_index(sequence, function, value, rounding=CLOSEST):
2933
3034
For documentation of rounding options, check `binary_search.roundings`.
3135
'''
32-
if function is None:
33-
function = lambda x: x
3436
my_range = range(len(sequence))
3537
fixed_function = lambda index: function(sequence[index])
36-
result = binary_search(my_range, fixed_function, value, rounding)
38+
result = binary_search(my_range, value, function=fixed_function,
39+
rounding=rounding)
3740
return result
3841

3942

40-
def _binary_search_both(sequence, function, value):
43+
def _binary_search_both(sequence, value,
44+
function=misc_tools.identity_function):
4145
'''
4246
Do a binary search through a sequence with the `BOTH` rounding.
4347
@@ -52,9 +56,6 @@ def _binary_search_both(sequence, function, value):
5256

5357
### Preparing: ############################################################
5458
# #
55-
if function is None:
56-
function = lambda x: x
57-
5859
get = lambda number: function(sequence[number])
5960

6061
low = 0
@@ -100,7 +101,8 @@ def _binary_search_both(sequence, function, value):
100101

101102

102103

103-
def binary_search(sequence, function, value, rounding=CLOSEST):
104+
def binary_search(sequence, value, function=misc_tools.identity_function,
105+
rounding=CLOSEST):
104106
'''
105107
Do a binary search through a sequence.
106108
@@ -120,11 +122,13 @@ def binary_search(sequence, function, value, rounding=CLOSEST):
120122

121123
from .binary_search_profile import BinarySearchProfile
122124

123-
binary_search_profile = BinarySearchProfile(sequence, function, value)
125+
binary_search_profile = BinarySearchProfile(sequence, value,
126+
function=function)
124127
return binary_search_profile.results[rounding]
125128

126129

127-
def make_both_data_into_preferred_rounding(both, function, value, rounding):
130+
def make_both_data_into_preferred_rounding(
131+
both, value, function=misc_tools.identity_function, rounding=BOTH):
128132
'''
129133
Convert results gotten using `BOTH` to a different rounding option.
130134

source_py3/test_python_toolbox/test_binary_search/test.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,199 +14,199 @@ def test():
1414

1515
assert binary_search.binary_search(
1616
my_list,
17-
misc_tools.identity_function,
1817
3,
18+
misc_tools.identity_function,
1919
binary_search.EXACT
2020
) == 3
2121

2222
assert binary_search.binary_search(
2323
my_list,
24-
misc_tools.identity_function,
2524
3.2,
25+
misc_tools.identity_function,
2626
binary_search.CLOSEST
2727
) == 3
2828

2929
assert binary_search.binary_search(
3030
my_list,
31-
misc_tools.identity_function,
3231
3.2,
32+
misc_tools.identity_function,
3333
binary_search.LOW
3434
) == 3
3535

3636
assert binary_search.binary_search(
3737
my_list,
38-
misc_tools.identity_function,
3938
3.2,
39+
misc_tools.identity_function,
4040
binary_search.HIGH
4141
) == 4
4242

4343
assert binary_search.binary_search(
4444
my_list,
45-
misc_tools.identity_function,
4645
3.2,
46+
misc_tools.identity_function,
4747
binary_search.BOTH
4848
) == (3, 4)
4949

5050
assert binary_search.binary_search(
5151
my_list,
52-
misc_tools.identity_function,
5352
-5,
53+
misc_tools.identity_function,
5454
binary_search.BOTH
5555
) == (None, 0)
5656

5757
assert binary_search.binary_search(
5858
my_list,
59-
misc_tools.identity_function,
6059
-5,
60+
misc_tools.identity_function,
6161
binary_search.LOW
6262
) == None
6363

6464
assert binary_search.binary_search(
6565
my_list,
66-
misc_tools.identity_function,
6766
-5,
67+
misc_tools.identity_function,
6868
binary_search.HIGH
6969
) == 0
7070

7171
assert binary_search.binary_search(
7272
my_list,
73-
misc_tools.identity_function,
7473
-5,
74+
misc_tools.identity_function,
7575
binary_search.HIGH_OTHERWISE_LOW
7676
) == 0
7777

7878
assert binary_search.binary_search(
7979
my_list,
80-
misc_tools.identity_function,
8180
-5,
81+
misc_tools.identity_function,
8282
binary_search.LOW_OTHERWISE_HIGH
8383
) == 0
8484

8585
assert binary_search.binary_search(
8686
my_list,
87-
misc_tools.identity_function,
8887
100,
88+
misc_tools.identity_function,
8989
binary_search.BOTH
9090
) == (4, None)
9191

9292
assert binary_search.binary_search(
9393
my_list,
94-
misc_tools.identity_function,
9594
100,
95+
misc_tools.identity_function,
9696
binary_search.LOW
9797
) == 4
9898

9999
assert binary_search.binary_search(
100100
my_list,
101-
misc_tools.identity_function,
102101
100,
102+
misc_tools.identity_function,
103103
binary_search.HIGH
104104
) == None
105105

106106
assert binary_search.binary_search(
107107
my_list,
108-
misc_tools.identity_function,
109108
100,
109+
misc_tools.identity_function,
110110
binary_search.LOW_OTHERWISE_HIGH
111111
) == 4
112112

113113
assert binary_search.binary_search(
114114
my_list,
115-
misc_tools.identity_function,
116115
100,
116+
misc_tools.identity_function,
117117
binary_search.HIGH_OTHERWISE_LOW
118118
) == 4
119119

120120
assert binary_search.binary_search_by_index(
121121
[(number * 10) for number in my_list],
122-
misc_tools.identity_function,
123122
32,
123+
misc_tools.identity_function,
124124
binary_search.BOTH
125125
) == (3, 4)
126126

127127
assert binary_search.binary_search(
128128
[],
129-
misc_tools.identity_function,
130129
32,
130+
misc_tools.identity_function,
131131
binary_search.BOTH
132132
) == (None, None)
133133

134134
assert binary_search.binary_search(
135135
[],
136-
misc_tools.identity_function,
137136
32,
137+
misc_tools.identity_function,
138138
) == None
139139

140140

141141
def test_single_member():
142142

143143
assert binary_search.binary_search(
144144
[7],
145-
misc_tools.identity_function,
146145
7,
146+
misc_tools.identity_function,
147147
binary_search.LOW
148148
) == 7
149149

150150
assert binary_search.binary_search(
151151
[7],
152-
misc_tools.identity_function,
153152
7,
153+
misc_tools.identity_function,
154154
binary_search.HIGH
155155
) == 7
156156

157157
assert binary_search.binary_search(
158158
[7],
159-
misc_tools.identity_function,
160159
7,
160+
misc_tools.identity_function,
161161
binary_search.HIGH_IF_BOTH
162162
) == 7
163163

164164
assert binary_search.binary_search(
165165
[7],
166-
misc_tools.identity_function,
167166
7,
167+
misc_tools.identity_function,
168168
binary_search.LOW_IF_BOTH
169169
) == 7
170170

171171
assert binary_search.binary_search(
172172
[7],
173-
misc_tools.identity_function,
174173
7,
174+
misc_tools.identity_function,
175175
binary_search.EXACT
176176
) == 7
177177

178178
assert binary_search.binary_search(
179179
[7],
180-
misc_tools.identity_function,
181180
7,
181+
misc_tools.identity_function,
182182
binary_search.BOTH
183183
) == (7, 7)
184184

185185
assert binary_search.binary_search(
186186
[7],
187-
misc_tools.identity_function,
188187
7,
188+
misc_tools.identity_function,
189189
binary_search.CLOSEST
190190
) == 7
191191

192192
assert binary_search.binary_search(
193193
[7],
194-
misc_tools.identity_function,
195194
7,
195+
misc_tools.identity_function,
196196
binary_search.CLOSEST_IF_BOTH
197197
) == 7
198198

199199
assert binary_search.binary_search(
200200
[7],
201-
misc_tools.identity_function,
202201
7,
202+
misc_tools.identity_function,
203203
binary_search.LOW_OTHERWISE_HIGH
204204
) == 7
205205

206206
assert binary_search.binary_search(
207207
[7],
208-
misc_tools.identity_function,
209208
7,
209+
misc_tools.identity_function,
210210
binary_search.HIGH_OTHERWISE_LOW
211211
) == 7
212212

0 commit comments

Comments
 (0)