Last active
August 29, 2015 13:56
-
-
Save Ismael-VC/9127541 to your computer and use it in GitHub Desktop.
Example of 1 vs 0 based indexing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env julia | |
range = 1:100 | |
big = range[end] | |
small = range[1] | |
function guess() | |
(small + big) >> 1 | |
end | |
function bigger() | |
global small = guess() + 1 | |
guess() | |
end | |
function smaller() | |
global big = guess() - 1 | |
guess() | |
end | |
function start_over() | |
global big = range[end] | |
global small = range[1] | |
guess() | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
my_range = range(1, 101) | |
big = my_range[-1] | |
small = my_range[0] | |
def guess(): | |
return (small + big) >> 1 | |
def bigger(): | |
global small | |
small = guess() + 1 | |
return guess() | |
def smaller(): | |
global big | |
big = guess() -1 | |
return guess() | |
def start_over(): | |
global big, small | |
big = my_range[-1] | |
small = my_range[0] | |
return guess() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: