Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
25
Task/Binary-search/AppleScript/binary-search.applescript
Normal file
25
Task/Binary-search/AppleScript/binary-search.applescript
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
on binarySearch(n, theList, l, r)
|
||||
repeat until (l = r)
|
||||
set m to (l + r) div 2
|
||||
if (item m of theList < n) then
|
||||
set l to m + 1
|
||||
else
|
||||
set r to m
|
||||
end if
|
||||
end repeat
|
||||
|
||||
if (item l of theList is n) then return l
|
||||
return missing value
|
||||
end binarySearch
|
||||
|
||||
on test(n, theList, l, r)
|
||||
set |result| to binarySearch(n, theList, l, r)
|
||||
if (|result| is missing value) then
|
||||
return (n as text) & " is not in range " & l & " thru " & r & " of the list"
|
||||
else
|
||||
return "The first occurrence of " & n & " in range " & l & " thru " & r & " of the list is at index " & |result|
|
||||
end if
|
||||
end test
|
||||
|
||||
set theList to {1, 2, 3, 3, 5, 7, 7, 8, 9, 10, 11, 12}
|
||||
return test(7, theList, 4, 11) & linefeed & test(7, theList, 7, 12) & linefeed & test(7, theList, 1, 5)
|
||||
Loading…
Add table
Add a link
Reference in a new issue