File tree Expand file tree Collapse file tree 2 files changed +8
-6
lines changed Expand file tree Collapse file tree 2 files changed +8
-6
lines changed Original file line number Diff line number Diff line change @@ -224,10 +224,10 @@ def tostream:
224
224
# the index of the target if the target is in the input array; and otherwise
225
225
# (-1 - ix), where ix is the insertion point that would leave the array sorted.
226
226
# If the input is not sorted, bsearch will terminate but with irrelevant results.
227
- def bsearch (target ):
227
+ def bsearch ($ target ):
228
228
if length == 0 then - 1
229
229
elif length == 1 then
230
- if target == . [0 ] then 0 elif target < . [0 ] then - 1 else - 2 end
230
+ if $ target == . [0 ] then 0 elif $ target < . [0 ] then - 1 else - 2 end
231
231
else . as $in
232
232
# state variable: [start, end, answer]
233
233
# where start and end are the upper and lower offsets to use.
@@ -237,14 +237,14 @@ def bsearch(target):
237
237
else
238
238
( ( (. [1 ] + . [0 ]) / 2 ) | floor ) as $mid
239
239
| $in [$mid ] as $monkey
240
- | if $monkey == target then (. [2 ] = $mid ) # success
240
+ | if $monkey == $ target then (. [2 ] = $mid ) # success
241
241
elif . [0 ] == . [1 ] then (. [1 ] = - 1 ) # failure
242
- elif $monkey < target then (. [0 ] = ($mid + 1 ))
242
+ elif $monkey < $ target then (. [0 ] = ($mid + 1 ))
243
243
else (. [1 ] = ($mid - 1 ))
244
244
end
245
245
end )
246
246
| if . [2 ] == null then # compute the insertion point
247
- if $in [ . [0 ] ] < target then (- 2 -. [0 ])
247
+ if $in [ . [0 ] ] < $ target then (- 2 -. [0 ])
248
248
else (- 1 -. [0 ])
249
249
end
250
250
else . [2 ]
Original file line number Diff line number Diff line change @@ -1427,8 +1427,10 @@ ascii_upcase
1427
1427
"useful but not for é"
1428
1428
"USEFUL BUT NOT FOR é"
1429
1429
1430
- bsearch(4)
1430
+ bsearch(0,2, 4)
1431
1431
[1,2,3]
1432
+ -1
1433
+ 1
1432
1434
-4
1433
1435
1434
1436
# strptime tests are in optional.test
You can’t perform that action at this time.
0 commit comments