Skip to content

Instantly share code, notes, and snippets.

@john9631
Created October 22, 2013 00:19
Show Gist options
  • Save john9631/7093202 to your computer and use it in GitHub Desktop.
Save john9631/7093202 to your computer and use it in GitHub Desktop.
Rosetta Code Middle Three Digits
function middle(i)
let s = string(abs(i)) , l = length(s) , mid = int((l+1)/2)
l < 3 ?
"error: not enough digits" :
iseven(l) ?
"error: number of digits is even" :
join((s[mid-1],s[mid],s[mid+1]))
end
end
for n = [123, 12345, 1234567, 987654321, 10001, -10001, -123,
-100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0]
println (@sprintf("%10d : ",n), middle(n))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment