Created
October 22, 2013 00:19
-
-
Save john9631/7093202 to your computer and use it in GitHub Desktop.
Rosetta Code Middle Three Digits
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
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