Skip to content

Instantly share code, notes, and snippets.

@Ismael-VC
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ismael-VC/9127541 to your computer and use it in GitHub Desktop.
Save Ismael-VC/9127541 to your computer and use it in GitHub Desktop.
Example of 1 vs 0 based indexing.
#!/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
#!/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()
@Ismael-VC
Copy link
Author

Example:

ismaelvc@toybox ~> julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "help()" to list help topics
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.3.0-prerelease+1629 (2014-02-18 19:07 UTC)
 _/ |\__'_|_|_|\__'_|  |  Commit 7d997ed* (2 days old master)
|__/                   |  i686-pc-linux-gnu

julia> include("guess_number.jl");

julia> guess()
50

julia> bigger()
75

julia> smaller()
62

julia> smaller()
56

julia>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment