Middle three digits: Difference between revisions
Content added Content deleted
m (→{{header|J}}) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 17: | Line 17: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F middle_three_digits(i) |
||
V s = String(abs(i)) |
V s = String(abs(i)) |
||
assert(s.len >= 3 & s.len % 2 == 1, ‘Need odd and >= 3 digits’) |
assert(s.len >= 3 & s.len % 2 == 1, ‘Need odd and >= 3 digits’) |
||
Line 29: | Line 29: | ||
print(‘middle_three_digits(#.) returned: #.’.format(x, answer)) |
print(‘middle_three_digits(#.) returned: #.’.format(x, answer)) |
||
X.catch AssertionError error |
X.catch AssertionError error |
||
print(‘middle_three_digits(#.) returned error: ’.format(x)‘’String(error))</ |
print(‘middle_three_digits(#.) returned error: ’.format(x)‘’String(error))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 55: | Line 55: | ||
{{libheader|Action! Tool Kit}} |
{{libheader|Action! Tool Kit}} |
||
{{libheader|Action! Real Math}} |
{{libheader|Action! Real Math}} |
||
< |
<syntaxhighlight lang="action!">INCLUDE "H6:REALMATH.ACT" |
||
INCLUDE "D2:PRINTF.ACT" ;from the Action! Tool Kit |
INCLUDE "D2:PRINTF.ACT" ;from the Action! Tool Kit |
||
Line 99: | Line 99: | ||
Test("2002") Test("-2002") |
Test("2002") Test("-2002") |
||
Test("0") |
Test("0") |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Middle_three_digits.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Middle_three_digits.png Screenshot from Atari 8-bit computer] |
||
Line 124: | Line 124: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
procedure Middle_Three_Digits is |
procedure Middle_Three_Digits is |
||
Line 166: | Line 166: | ||
end loop; |
end loop; |
||
end Middle_Three_Digits;</ |
end Middle_Three_Digits;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 188: | Line 188: | ||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
< |
<syntaxhighlight lang="aime">void |
||
m3(integer i) |
m3(integer i) |
||
{ |
{ |
||
Line 228: | Line 228: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 123: 123 |
<pre> 123: 123 |
||
Line 250: | Line 250: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{works with|ALGOL 68G|Any - tested with release 2.6.win32}} |
{{works with|ALGOL 68G|Any - tested with release 2.6.win32}} |
||
< |
<syntaxhighlight lang="algol68"># we define a UNION MODE so that our middle 3 digits PROC can # |
||
# return either an integer on success or a error message if # |
# return either an integer on success or a error message if # |
||
# the middle 3 digits couldn't be extracted # |
# the middle 3 digits couldn't be extracted # |
||
Line 312: | Line 312: | ||
print( ( newline ) ) |
print( ( newline ) ) |
||
OD |
OD |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 335: | Line 335: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
< |
<syntaxhighlight lang="algolw">begin |
||
% record structure that will be used to return the middle 3 digits of a number % |
% record structure that will be used to return the middle 3 digits of a number % |
||
% if the middle three digits can't be determined, isOk will be false and message % |
% if the middle three digits can't be determined, isOk will be false and message % |
||
Line 382: | Line 382: | ||
end for_n |
end for_n |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 408: | Line 408: | ||
987654321 is too large to be represented as an AppleScript integer, so "integer value" is taken here to refer to the numeric value rather than to the language class. AppleScript automatically coerces numeric text and single-item lists to appropriate number classes where necessary and possible, so these are acceptable as parameters too. |
987654321 is too large to be represented as an AppleScript integer, so "integer value" is taken here to refer to the numeric value rather than to the language class. AppleScript automatically coerces numeric text and single-item lists to appropriate number classes where necessary and possible, so these are acceptable as parameters too. |
||
< |
<syntaxhighlight lang="applescript">on middle3Digits(n) |
||
try |
try |
||
n as number -- Errors if n isn't a number or coercible thereto. |
n as number -- Errors if n isn't a number or coercible thereto. |
||
Line 442: | Line 442: | ||
set output to output as text |
set output to output as text |
||
set AppleScript's text item delimiters to astid |
set AppleScript's text item delimiters to astid |
||
return output</ |
return output</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
< |
<syntaxhighlight lang="applescript">"The middle three digits of 123 are 123. |
||
The middle three digits of 12345 are 234. |
The middle three digits of 12345 are 234. |
||
The middle three digits of 1234567 are 345. |
The middle three digits of 1234567 are 345. |
||
Line 462: | Line 462: | ||
middle3Digits handler got an error: 2002 has an even number of digits. |
middle3Digits handler got an error: 2002 has an even number of digits. |
||
middle3Digits handler got an error: -2002 has an even number of digits. |
middle3Digits handler got an error: -2002 has an even number of digits. |
||
middle3Digits handler got an error: 0 has fewer than three digits."</ |
middle3Digits handler got an error: 0 has fewer than three digits."</syntaxhighlight> |
||
===Functional=== |
===Functional=== |
||
< |
<syntaxhighlight lang="applescript">-------------------- MID THREE DIGITS --------------------- |
||
-- mid3digits :: Int -> Either String String |
-- mid3digits :: Int -> Either String String |
||
Line 755: | Line 755: | ||
end tell |
end tell |
||
end if |
end if |
||
end zipWith</ |
end zipWith</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Mid three digits: |
<pre>Mid three digits: |
||
Line 778: | Line 778: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">middleThree: function [num][ |
||
n: to :string abs num |
n: to :string abs num |
||
if 3 > size n -> return "Number must have at least three digits" |
if 3 > size n -> return "Number must have at least three digits" |
||
Line 794: | Line 794: | ||
loop samples 's [ |
loop samples 's [ |
||
print [pad to :string s 10 ":" middleThree s] |
print [pad to :string s 10 ":" middleThree s] |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 817: | Line 817: | ||
=={{header|ATS}}== |
=={{header|ATS}}== |
||
<syntaxhighlight lang="ats"> |
|||
<lang ATS> |
|||
(* ****** ****** *) |
(* ****** ****** *) |
||
// |
// |
||
Line 938: | Line 938: | ||
(* ****** ****** *) |
(* ****** ****** *) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Middle-three-digits(123): 123 |
<pre>Middle-three-digits(123): 123 |
||
Line 959: | Line 959: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">Numbers:="123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,1,2,-1,-10,2002,-2002,0" |
||
Loop, parse, Numbers, `, |
Loop, parse, Numbers, `, |
||
{ |
{ |
||
Line 971: | Line 971: | ||
log := log . A_LoopField . "`t: " . SubStr(n,((d-3)//2)+1,3) . "`n" |
log := log . A_LoopField . "`t: " . SubStr(n,((d-3)//2)+1,3) . "`n" |
||
} |
} |
||
MsgBox % log</ |
MsgBox % log</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>123 : 123 |
<pre>123 : 123 |
||
Line 992: | Line 992: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang="awk">#!/bin/awk -f |
||
# use as: awk -f middle_three_digits.awk |
# use as: awk -f middle_three_digits.awk |
||
Line 1,020: | Line 1,020: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,044: | Line 1,044: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
< |
<syntaxhighlight lang="applesoftbasic">100 DEF FN L(N) = LEN(STR$(INT(ABS(N)))) |
||
110 DEF FN N(N) = VAL(MID$(STR$(INT(ABS(N))),(FN L(N)-1)/2,3)) |
110 DEF FN N(N) = VAL(MID$(STR$(INT(ABS(N))),(FN L(N)-1)/2,3)) |
||
120 DEF FN EVEN(N) = INT(N/2) = N/2 |
120 DEF FN EVEN(N) = INT(N/2) = N/2 |
||
Line 1,066: | Line 1,066: | ||
300 DATA123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345 |
300 DATA123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345 |
||
310 DATA1,2,-1,-10,2002,-2002,0 |
310 DATA1,2,-1,-10,2002,-2002,0 |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,089: | Line 1,089: | ||
==={{header|BBC BASIC}}=== |
==={{header|BBC BASIC}}=== |
||
< |
<syntaxhighlight lang="bbcbasic">REM >midthree |
||
FOR i% = 1 TO 17 |
FOR i% = 1 TO 17 |
||
READ test% |
READ test% |
||
Line 1,109: | Line 1,109: | ||
OTHERWISE |
OTHERWISE |
||
= MID$(n$, LEN n$ / 2, 3) |
= MID$(n$, LEN n$ / 2, 3) |
||
ENDCASE</ |
ENDCASE</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 123 -> 123 |
<pre> 123 -> 123 |
||
Line 1,130: | Line 1,130: | ||
==={{header|FBSL}}=== |
==={{header|FBSL}}=== |
||
< |
<syntaxhighlight lang="qbasic">#APPTYPE CONSOLE |
||
DIM numbers AS STRING = "123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,1,2,-1,-10,2002,-2002,0" |
DIM numbers AS STRING = "123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,1,2,-1,-10,2002,-2002,0" |
||
Line 1,174: | Line 1,174: | ||
RETURN m |
RETURN m |
||
END IF |
END IF |
||
END FUNCTION</ |
END FUNCTION</syntaxhighlight> |
||
Output |
Output |
||
<pre> 123 --> 123 |
<pre> 123 --> 123 |
||
Line 1,197: | Line 1,197: | ||
==={{header|IS-BASIC}}=== |
==={{header|IS-BASIC}}=== |
||
< |
<syntaxhighlight lang="is-basic">100 INPUT PROMPT "Number: ":N |
||
120 PRINT MIDDLE$(N) |
120 PRINT MIDDLE$(N) |
||
130 DEF MIDDLE$(N) |
130 DEF MIDDLE$(N) |
||
Line 1,205: | Line 1,205: | ||
170 LET P=(LEN(N$)-3)/2 |
170 LET P=(LEN(N$)-3)/2 |
||
180 LET MIDDLE$=N$(P+1:P+3) |
180 LET MIDDLE$=N$(P+1:P+3) |
||
190 END DEF</ |
190 END DEF</syntaxhighlight> |
||
==={{header|PureBasic}}=== |
==={{header|PureBasic}}=== |
||
< |
<syntaxhighlight lang="purebasic">Procedure.s middleThreeDigits(x.q) |
||
Protected x$, digitCount |
Protected x$, digitCount |
||
Line 1,236: | Line 1,236: | ||
Print(#crlf$ + #crlf$ + "Press ENTER to exit"): Input() |
Print(#crlf$ + #crlf$ + "Press ENTER to exit"): Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
<pre> 123 : 123 |
<pre> 123 : 123 |
||
Line 1,257: | Line 1,257: | ||
==={{header|Run BASIC}}=== |
==={{header|Run BASIC}}=== |
||
< |
<syntaxhighlight lang="runbasic">x$ = "123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0" |
||
while word$(x$,i+1,",") <> "" |
while word$(x$,i+1,",") <> "" |
||
Line 1,269: | Line 1,269: | ||
end if |
end if |
||
wend |
wend |
||
end</ |
end</syntaxhighlight> |
||
<pre>123 123 |
<pre>123 123 |
||
234 12345 |
234 12345 |
||
Line 1,289: | Line 1,289: | ||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
< |
<syntaxhighlight lang="dos">@echo off |
||
setlocal enabledelayedexpansion |
setlocal enabledelayedexpansion |
||
Line 1,334: | Line 1,334: | ||
) |
) |
||
goto :EOF |
goto :EOF |
||
%==/The Procedure ==%</ |
%==/The Procedure ==%</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>123: 123 |
<pre>123: 123 |
||
Line 1,359: | Line 1,359: | ||
Reads the integer value from stdin and writes the middle three digits (or an error message) to stdout. |
Reads the integer value from stdin and writes the middle three digits (or an error message) to stdout. |
||
< |
<syntaxhighlight lang="befunge">>&>:0`2*1-*0>v |
||
v+*86%+55:p00< |
v+*86%+55:p00< |
||
>\55+/:00g1+\| |
>\55+/:00g1+\| |
||
Line 1,366: | Line 1,366: | ||
2^,+55_,#!>#:< |
2^,+55_,#!>#:< |
||
>/>\#<$#-:#1_v |
>/>\#<$#-:#1_v |
||
>_@#<,+55,,,$<</ |
>_@#<,+55,,,$<</syntaxhighlight> |
||
{{out}} (multiple runs) |
{{out}} (multiple runs) |
||
Line 1,405: | Line 1,405: | ||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
< |
<syntaxhighlight lang="bracmat">( ( middle3 |
||
= x p |
= x p |
||
. @(!arg:? [?p:? [(1/2*!p+-3/2) %?x [(1/2*!p+3/2) ?) |
. @(!arg:? [?p:? [(1/2*!p+-3/2) %?x [(1/2*!p+3/2) ?) |
||
Line 1,420: | Line 1,420: | ||
& |
& |
||
); |
); |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre>123 |
<pre>123 |
||
Line 1,442: | Line 1,442: | ||
=={{header|Burlesque}}== |
=={{header|Burlesque}}== |
||
< |
<syntaxhighlight lang="blsq"> |
||
blsq ) {123 12345 1234567 987654321 -10001 -123}{XX{~-}{L[3.>}w!m]\[}m[uN |
blsq ) {123 12345 1234567 987654321 -10001 -123}{XX{~-}{L[3.>}w!m]\[}m[uN |
||
123 |
123 |
||
Line 1,450: | Line 1,450: | ||
000 |
000 |
||
123 |
123 |
||
</syntaxhighlight> |
|||
</lang> |
|||
<tt>m]\[</tt> and <tt>uN</tt> are just for displaying it nicely. |
<tt>m]\[</tt> and <tt>uN</tt> are just for displaying it nicely. |
||
Line 1,456: | Line 1,456: | ||
=={{header|C}}== |
=={{header|C}}== |
||
This code is followed by its output. |
This code is followed by its output. |
||
<syntaxhighlight lang="c"> |
|||
<lang c> |
|||
#include <stdio.h> |
#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 1,488: | Line 1,488: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
<pre> |
<pre> |
||
Line 1,512: | Line 1,512: | ||
===Alternative Version=== |
===Alternative Version=== |
||
This code has been extensively rewritten. The original was purely interactive and had to be invoked each time from the console. It also did not produce the correct answer. |
This code has been extensively rewritten. The original was purely interactive and had to be invoked each time from the console. It also did not produce the correct answer. |
||
<syntaxhighlight lang="c"> |
|||
<lang c> |
|||
#include <stdio.h> |
#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 1,568: | Line 1,568: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,595: | Line 1,595: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
std::string middleThreeDigits(int n) |
std::string middleThreeDigits(int n) |
||
Line 1,622: | Line 1,622: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>middleThreeDigits(123): 123 |
<pre>middleThreeDigits(123): 123 |
||
Line 1,643: | Line 1,643: | ||
=={{header|C_sharp|C#}}== |
=={{header|C_sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
namespace RosettaCode |
namespace RosettaCode |
||
Line 1,655: | Line 1,655: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>123:123 |
<pre>123:123 |
||
Line 1,678: | Line 1,678: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
<syntaxhighlight lang="clojure"> |
|||
<lang Clojure> |
|||
(defn middle3 [v] |
(defn middle3 [v] |
||
(let [no (Math/abs v) |
(let [no (Math/abs v) |
||
Line 1,695: | Line 1,695: | ||
(def fails '(1 2 -1 -10 2002 -2002 0)) |
(def fails '(1 2 -1 -10 2002 -2002 0)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,706: | Line 1,706: | ||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">middle_three_digits = proc (n: int) returns (string) |
||
signals (too_small, even_length) |
signals (too_small, even_length) |
||
s: string := int$unparse(int$abs(n)) |
s: string := int$unparse(int$abs(n)) |
||
Line 1,729: | Line 1,729: | ||
end |
end |
||
end |
end |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 123: 123 |
<pre> 123: 123 |
||
Line 1,750: | Line 1,750: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
< |
<syntaxhighlight lang="cobol">identification division. |
||
program-id. middle3. |
program-id. middle3. |
||
environment division. |
environment division. |
||
Line 1,840: | Line 1,840: | ||
add 1 to digit-counter |
add 1 to digit-counter |
||
end-perform. |
end-perform. |
||
exit paragraph.</ |
exit paragraph.</syntaxhighlight> |
||
Output |
Output |
||
<pre> 123 --> 123 |
<pre> 123 --> 123 |
||
Line 1,860: | Line 1,860: | ||
0 --> Number too small</pre> |
0 --> Number too small</pre> |
||
Optimised version |
Optimised version |
||
< |
<syntaxhighlight lang="cobol">identification division. |
||
program-id. middle3. |
program-id. middle3. |
||
environment division. |
environment division. |
||
Line 1,932: | Line 1,932: | ||
end-if. |
end-if. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output |
Output |
||
<pre> 123 --> 123 |
<pre> 123 --> 123 |
||
Line 1,954: | Line 1,954: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp"> |
||
(defun mid3 (n) |
(defun mid3 (n) |
||
(let ((a (abs n)) |
(let ((a (abs n)) |
||
Line 1,967: | Line 1,967: | ||
((evenp hmd) (need "odd number of")) |
((evenp hmd) (need "odd number of")) |
||
(t (nbr (mod (truncate a (expt 10 (/ (- hmd 3) 2))) 1000))))))) |
(t (nbr (mod (truncate a (expt 10 (/ (- hmd 3) 2))) 1000))))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Test code: |
Test code: |
||
< |
<syntaxhighlight lang="lisp"> |
||
(loop as n in '(123 12345 1234567 987654321 |
(loop as n in '(123 12345 1234567 987654321 |
||
10001 -10001 -123 -100 100 -12345 |
10001 -10001 -123 -100 100 -12345 |
||
1 2 -1 -10 2002 -2002 0) |
1 2 -1 -10 2002 -2002 0) |
||
do (format t "~d:~12t~a~%" n (mid3 n))) |
do (format t "~d:~12t~a~%" n (mid3 n))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>123: 123 |
<pre>123: 123 |
||
Line 1,997: | Line 1,997: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.traits, std.conv; |
||
string middleThreeDigits(T)(in T n) pure nothrow if (isIntegral!T) { |
string middleThreeDigits(T)(in T n) pure nothrow if (isIntegral!T) { |
||
Line 2,017: | Line 2,017: | ||
foreach (immutable n; failing) |
foreach (immutable n; failing) |
||
writefln("middleThreeDigits(%s): %s", n, middleThreeDigits(n)); |
writefln("middleThreeDigits(%s): %s", n, middleThreeDigits(n)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>middleThreeDigits(123): 123 |
<pre>middleThreeDigits(123): 123 |
||
Line 2,043: | Line 2,043: | ||
===Alternative Version=== |
===Alternative Version=== |
||
This longer version gives a stronger typed output, and it tries to be faster avoiding conversions to string. |
This longer version gives a stronger typed output, and it tries to be faster avoiding conversions to string. |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.traits, std.math, std.variant; |
||
/// Returns a string with the error, or the three digits. |
/// Returns a string with the error, or the three digits. |
||
Line 2,129: | Line 2,129: | ||
writefln("middleThreeDigits(cast(short)%d): %s", n, mtd); |
writefln("middleThreeDigits(cast(short)%d): %s", n, mtd); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>middleThreeDigits(123): 123 |
<pre>middleThreeDigits(123): 123 |
||
Line 2,180: | Line 2,180: | ||
=={{header|Dart}}== |
=={{header|Dart}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
import'dart:math'; |
import'dart:math'; |
||
int length(int x) |
int length(int x) |
||
Line 2,227: | Line 2,227: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|DCL}}== |
=={{header|DCL}}== |
||
<lang>$ list = "123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,1,2,-1,-10,2002,-2002,0" |
<syntaxhighlight lang="text">$ list = "123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,1,2,-1,-10,2002,-2002,0" |
||
$ i = 0 |
$ i = 0 |
||
$ loop: |
$ loop: |
||
Line 2,244: | Line 2,244: | ||
$ endif |
$ endif |
||
$ i = i + 1 |
$ i = i + 1 |
||
$ goto loop</ |
$ goto loop</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>$ @middle_three_digits |
<pre>$ @middle_three_digits |
||
Line 2,269: | Line 2,269: | ||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
<syntaxhighlight lang="eiffel"> |
|||
<lang Eiffel> |
|||
class |
class |
||
APPLICATION |
APPLICATION |
||
Line 2,331: | Line 2,331: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,356: | Line 2,356: | ||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
< |
<syntaxhighlight lang="elena">import system'routines; |
||
import extensions; |
import extensions; |
||
Line 2,384: | Line 2,384: | ||
console.printLine("middleThreeDigits(",n,"):",middleThreeDigits(n) | on:(e => e.Message)) |
console.printLine("middleThreeDigits(",n,"):",middleThreeDigits(n) | on:(e => e.Message)) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,407: | Line 2,407: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">defmodule Middle do |
||
def three(num) do |
def three(num) do |
||
n = num |> abs |> to_string |
n = num |> abs |> to_string |
||
Line 2,436: | Line 2,436: | ||
e -> IO.puts e.message |
e -> IO.puts e.message |
||
end |
end |
||
end)</ |
end)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,460: | Line 2,460: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
< |
<syntaxhighlight lang="erlang">% |
||
-module(middle_three_digits). |
-module(middle_three_digits). |
||
-export([main/0]). |
-export([main/0]). |
||
Line 2,499: | Line 2,499: | ||
loop(X,N) when X>0 -> |
loop(X,N) when X>0 -> |
||
loop(X-1, N div 10). |
loop(X-1, N div 10). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>123 |
<pre>123 |
||
Line 2,522: | Line 2,522: | ||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang="erre"> |
|||
<lang ERRE> |
|||
PROGRAM MIDDLE |
PROGRAM MIDDLE |
||
Line 2,567: | Line 2,567: | ||
END FOR |
END FOR |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,590: | Line 2,590: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<lang>USING: combinators formatting io kernel math math.parser |
<syntaxhighlight lang="text">USING: combinators formatting io kernel math math.parser |
||
sequences ; |
sequences ; |
||
IN: rosetta-code.middle-three-digits |
IN: rosetta-code.middle-three-digits |
||
Line 2,618: | Line 2,618: | ||
test-values [ dup "%9d : " printf middle-three ] each ; |
test-values [ dup "%9d : " printf middle-three ] each ; |
||
MAIN: main</ |
MAIN: main</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,642: | Line 2,642: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
This is a problem, which is easily solved in Forth. It converts the number to a string and checks it length. If the number does not represent a printable string, it returns an empty string. |
This is a problem, which is easily solved in Forth. It converts the number to a string and checks it length. If the number does not represent a printable string, it returns an empty string. |
||
< |
<syntaxhighlight lang="forth">: middle3 ( n1 -- a n2) |
||
abs s>d <# #s #> dup 2/ 0<> over 1 and 0<> and |
abs s>d <# #s #> dup 2/ 0<> over 1 and 0<> and |
||
if 2/ 1- chars + 3 else drop 0 then |
if 2/ 1- chars + 3 else drop 0 then |
||
;</ |
;</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Please find compilation instructions along with the output for the examples in the comments at the beginning of the file. This program was produced in an Ubuntu distribution of the GNU/linux system. |
Please find compilation instructions along with the output for the examples in the comments at the beginning of the file. This program was produced in an Ubuntu distribution of the GNU/linux system. |
||
<syntaxhighlight lang="fortran"> |
|||
<lang FORTRAN> |
|||
!-*- mode: compilation; default-directory: "/tmp/" -*- |
!-*- mode: compilation; default-directory: "/tmp/" -*- |
||
!Compilation started at Sat Jun 1 14:48:41 |
!Compilation started at Sat Jun 1 14:48:41 |
||
Line 2,723: | Line 2,723: | ||
end program MiddleMuddle |
end program MiddleMuddle |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Function middleThreeDigits (n As Integer) As String |
Function middleThreeDigits (n As Integer) As String |
||
Line 2,757: | Line 2,757: | ||
Print |
Print |
||
Print "Press any key to quit" |
Print "Press any key to quit" |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,784: | Line 2,784: | ||
=={{header|FutureBasic}}== |
=={{header|FutureBasic}}== |
||
< |
<syntaxhighlight lang="futurebasic"> |
||
local fn MiddleThreeDigits( n as NSInteger ) as CFStringRef |
local fn MiddleThreeDigits( n as NSInteger ) as CFStringRef |
||
CFStringRef testStr, resultStr |
CFStringRef testStr, resultStr |
||
Line 2,818: | Line 2,818: | ||
HandleEvents |
HandleEvents |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{output}} |
{{output}} |
||
<pre style="font-size: 13px"> |
<pre style="font-size: 13px"> |
||
Line 2,848: | Line 2,848: | ||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
'''[https://gambas-playground.proko.eu/?gist=ed25e84978446d65895ccd909fc092fa Click this link to run this code]''' |
'''[https://gambas-playground.proko.eu/?gist=ed25e84978446d65895ccd909fc092fa Click this link to run this code]''' |
||
< |
<syntaxhighlight lang="gambas">Public Sub Main() |
||
Dim iList As Integer[] = [123, 12345, 1234567, 987654321, 10001, |
Dim iList As Integer[] = [123, 12345, 1234567, 987654321, 10001, |
||
-10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0] 'Array of integers to process |
-10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0] 'Array of integers to process |
||
Line 2,871: | Line 2,871: | ||
Next |
Next |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,896: | Line 2,896: | ||
File m3.go: |
File m3.go: |
||
< |
<syntaxhighlight lang="go">package m3 |
||
import ( |
import ( |
||
Line 2,921: | Line 2,921: | ||
m := len(s) / 2 |
m := len(s) / 2 |
||
return s[m-1 : m+2], nil |
return s[m-1 : m+2], nil |
||
}</ |
}</syntaxhighlight> |
||
File m3_test.go: |
File m3_test.go: |
||
< |
<syntaxhighlight lang="go">package m3_test |
||
import ( |
import ( |
||
Line 2,981: | Line 2,981: | ||
t.Logf("d(%d) returns %q", tc.i, err) |
t.Logf("d(%d) returns %q", tc.i, err) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Output of go test is normally terse: |
Output of go test is normally terse: |
||
Line 3,014: | Line 3,014: | ||
=={{header|Gosu}}== |
=={{header|Gosu}}== |
||
< |
<syntaxhighlight lang="gosu">var valid = {123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345} |
||
valid.each(\ num ->print(middleThree(num))) |
valid.each(\ num ->print(middleThree(num))) |
||
Line 3,026: | Line 3,026: | ||
var start = (s.length / 2) - 1 |
var start = (s.length / 2) - 1 |
||
return s.substring(start, start + 3) |
return s.substring(start, start + 3) |
||
}</ |
}</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
Line 3,049: | Line 3,049: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy">def middleThree(Number number) { |
||
def text = Math.abs(number) as String |
def text = Math.abs(number) as String |
||
assert text.size() >= 3 : "'$number' must be more than 3 numeric digits" |
assert text.size() >= 3 : "'$number' must be more than 3 numeric digits" |
||
Line 3,056: | Line 3,056: | ||
int start = text.size() / 2 - 1 |
int start = text.size() / 2 - 1 |
||
text[start..(start+2)] |
text[start..(start+2)] |
||
}</ |
}</syntaxhighlight> |
||
Test Code: |
Test Code: |
||
< |
<syntaxhighlight lang="groovy">[123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0].each { number -> |
||
def text = (number as String).padLeft(10) |
def text = (number as String).padLeft(10) |
||
try { |
try { |
||
Line 3,065: | Line 3,065: | ||
println "$text cannot be converted: $error.message" |
println "$text cannot be converted: $error.message" |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> 123: 123 |
<pre> 123: 123 |
||
Line 3,086: | Line 3,086: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">mid3 :: Int -> Either String String |
||
mid3 n |
mid3 n |
||
| m < 100 = Left "is too small" |
| m < 100 = Left "is too small" |
||
Line 3,126: | Line 3,126: | ||
where |
where |
||
justifyRight :: Int -> Char -> String -> String |
justifyRight :: Int -> Char -> String -> String |
||
justifyRight n c s = drop (length s) (replicate n c ++ s)</ |
justifyRight n c s = drop (length s) (replicate n c ++ s)</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> 123 -> 123 |
<pre> 123 -> 123 |
||
Line 3,150: | Line 3,150: | ||
The following solution works in both languages. |
The following solution works in both languages. |
||
< |
<syntaxhighlight lang="unicon">procedure main(a) |
||
every n := !a do write(right(n,15)," -> ",midM(n)) |
every n := !a do write(right(n,15)," -> ",midM(n)) |
||
end |
end |
||
Line 3,161: | Line 3,161: | ||
else "wrong number of digits" |
else "wrong number of digits" |
||
else "too short" |
else "too short" |
||
end</ |
end</syntaxhighlight> |
||
with output: |
with output: |
||
Line 3,187: | Line 3,187: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution:''' |
'''Solution:''' |
||
< |
<syntaxhighlight lang="j">asString=: ":"0 NB. convert vals to strings |
||
getPfxSize=: [: -:@| 3 -~ # NB. get size of prefix to drop before the 3 middle digits |
getPfxSize=: [: -:@| 3 -~ # NB. get size of prefix to drop before the 3 middle digits |
||
getMid3=: (3 {. getPfxSize }. ,&'err') :: ('err'"_) NB. get 3 middle digits or return 'err' |
getMid3=: (3 {. getPfxSize }. ,&'err') :: ('err'"_) NB. get 3 middle digits or return 'err' |
||
getMiddle3=: getMid3@asString@:|</ |
getMiddle3=: getMid3@asString@:|</syntaxhighlight> |
||
'''Example:''' |
'''Example:''' |
||
< |
<syntaxhighlight lang="j"> vals=: 123 12345 1234567 987654321 10001 _10001 _123 _100 100 _12345 1 2 _1 _10 2002 _2002 0 |
||
getMiddle3 vals |
getMiddle3 vals |
||
123 |
123 |
||
Line 3,210: | Line 3,210: | ||
err |
err |
||
err |
err |
||
err</ |
err</syntaxhighlight> |
||
Or, expressed more concisely:< |
Or, expressed more concisely:<syntaxhighlight lang="j"> ({~ 2 1 0 -~ -:@>:@#) ::('err'"_)@":@| vals |
||
123 |
123 |
||
234 |
234 |
||
Line 3,229: | Line 3,229: | ||
err |
err |
||
err |
err |
||
err</ |
err</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">public class MiddleThreeDigits { |
||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
Line 3,258: | Line 3,258: | ||
return s.substring(mid - 1, mid + 2); |
return s.substring(mid - 1, mid + 2); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre>middleThreeDigits(123): 123 |
<pre>middleThreeDigits(123): 123 |
||
middleThreeDigits(12345): 234 |
middleThreeDigits(12345): 234 |
||
Line 3,282: | Line 3,282: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">function middleThree(x){ |
||
var n=''+Math.abs(x); var l=n.length-1; |
var n=''+Math.abs(x); var l=n.length-1; |
||
if(l<2||l%2) throw new Error(x+': Invalid length '+(l+1)); |
if(l<2||l%2) throw new Error(x+': Invalid length '+(l+1)); |
||
Line 3,291: | Line 3,291: | ||
1, 2, -1, -10, 2002, -2002, 0].forEach(function(n){ |
1, 2, -1, -10, 2002, -2002, 0].forEach(function(n){ |
||
try{console.log(n,middleThree(n))}catch(e){console.error(e.message)} |
try{console.log(n,middleThree(n))}catch(e){console.error(e.message)} |
||
});</ |
});</syntaxhighlight> |
||
<pre>123 "123" |
<pre>123 "123" |
||
Line 3,314: | Line 3,314: | ||
Or, using an option type, composing a solution from existing generic primitives, and formatting the output a little: |
Or, using an option type, composing a solution from existing generic primitives, and formatting the output a little: |
||
{{Trans|Python}} |
{{Trans|Python}} |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 3,439: | Line 3,439: | ||
// MAIN --- |
// MAIN --- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 3,461: | Line 3,461: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
< |
<syntaxhighlight lang="jq">def middle3: |
||
if . < 0 then -. else . end |
if . < 0 then -. else . end |
||
| tostring as $s |
| tostring as $s |
||
Line 3,470: | Line 3,470: | ||
(123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0) |
(123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0) |
||
| "\(.) => \( .|middle3 )"</ |
| "\(.) => \( .|middle3 )"</syntaxhighlight> |
||
Typescript:< |
Typescript:<syntaxhighlight lang="sh"> $ jq -r -n -f Middle_three_digits.jq |
||
123 => 123 |
123 => 123 |
||
12345 => 234 |
12345 => 234 |
||
Line 3,488: | Line 3,488: | ||
2002 => invalid length: 4 |
2002 => invalid length: 4 |
||
-2002 => invalid length: 4 |
-2002 => invalid length: 4 |
||
0 => invalid length: 1 </ |
0 => invalid length: 1 </syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{works with|Julia|1.0.3}} |
{{works with|Julia|1.0.3}} |
||
< |
<syntaxhighlight lang="julia">using Printf |
||
function middle3(n::Integer) |
function middle3(n::Integer) |
||
Line 3,506: | Line 3,506: | ||
-100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0] |
-100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0] |
||
@printf("%10d -> %s\n", n, try middle3(n) catch e e.msg end) |
@printf("%10d -> %s\n", n, try middle3(n) catch e e.msg end) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,528: | Line 3,528: | ||
=={{header|K}}== |
=={{header|K}}== |
||
<syntaxhighlight lang="k"> |
|||
<lang K> |
|||
/ Rosetta code - Middle three digits |
/ Rosetta code - Middle three digits |
||
/ mid3.k |
/ mid3.k |
||
mid3: {qs:$x;:[qs[0]="-";qs: 1 _ qs];:[(#qs)<3;:"small";(1+#qs)!2;:"even"];p:(-3+#qs)%2;:(|p _|p _ qs)} |
mid3: {qs:$x;:[qs[0]="-";qs: 1 _ qs];:[(#qs)<3;:"small";(1+#qs)!2;:"even"];p:(-3+#qs)%2;:(|p _|p _ qs)} |
||
</syntaxhighlight> |
|||
</lang> |
|||
The output of the session: |
The output of the session: |
||
{{out}} |
{{out}} |
||
Line 3,562: | Line 3,562: | ||
=={{header|Klingphix}}== |
=={{header|Klingphix}}== |
||
< |
<syntaxhighlight lang="klingphix">include ..\Utilitys.tlhy |
||
( 123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345 1 2 -1 -10 2002 -2002 0 ) |
( 123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345 1 2 -1 -10 2002 -2002 0 ) |
||
Line 3,576: | Line 3,576: | ||
" " input |
" " input |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>123 : 123 |
<pre>123 : 123 |
||
Line 3,597: | Line 3,597: | ||
=={{header|Klong}}== |
=={{header|Klong}}== |
||
< |
<syntaxhighlight lang="k">items::[123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345 1 2 -1 -10 2002 -2002 0] |
||
mid3::{[d k];:[3>k::#$#x;"small":|0=k!2;"even";(-d)_(d::_(k%2)-1)_$#x]} |
mid3::{[d k];:[3>k::#$#x;"small":|0=k!2;"even";(-d)_(d::_(k%2)-1)_$#x]} |
||
.p(mid3'items)</ |
.p(mid3'items)</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>[123 234 345 654 000 000 123 100 100 234 small small small small even even small]</pre> |
<pre>[123 234 345 654 000 000 123 100 100 234 small small small small even even small]</pre> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">fun middleThree(x: Int): Int? { |
||
val s = Math.abs(x).toString() |
val s = Math.abs(x).toString() |
||
return when { |
return when { |
||
Line 3,620: | Line 3,620: | ||
println(middleThree(123))// 123 |
println(middleThree(123))// 123 |
||
println(middleThree(123555)) //null |
println(middleThree(123555)) //null |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
{def S 123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345 |
{def S 123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345 |
||
1 2 -1 -10 2002 -2002 0} |
1 2 -1 -10 2002 -2002 0} |
||
Line 3,665: | Line 3,665: | ||
-2002: has an even number of digits |
-2002: has an even number of digits |
||
0: has not enough digits |
0: has not enough digits |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">define middlethree(value::integer) => { |
||
local( |
local( |
||
pos_value = math_abs(#value), |
pos_value = math_abs(#value), |
||
Line 3,689: | Line 3,689: | ||
'<br />' |
'<br />' |
||
^} |
^} |
||
'</pre>'</ |
'</pre>'</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
<pre> 123: 123 |
<pre> 123: 123 |
||
Line 3,710: | Line 3,710: | ||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
< |
<syntaxhighlight lang="logo">to middle3digits :n |
||
if [less? :n 0] [make "n minus :n] |
if [less? :n 0] [make "n minus :n] |
||
local "len make "len count :n |
local "len make "len count :n |
||
Line 3,728: | Line 3,728: | ||
] |
] |
||
bye</ |
bye</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
Line 3,750: | Line 3,750: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function middle_three(n) |
||
if n < 0 then |
if n < 0 then |
||
n = -n |
n = -n |
||
Line 3,775: | Line 3,775: | ||
print(n, middle_three(n)) |
print(n, middle_three(n)) |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,797: | Line 3,797: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">middleDigits := proc(n) |
||
local nList, start; |
local nList, start; |
||
nList := [seq(parse(i), i in convert (abs(n), string))]; |
nList := [seq(parse(i), i in convert (abs(n), string))]; |
||
Line 3,815: | Line 3,815: | ||
middleDigits(i); |
middleDigits(i); |
||
printf("\n"); |
printf("\n"); |
||
end do;</ |
end do;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,838: | Line 3,838: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">middleThree[n_Integer] := |
||
Block[{digits = IntegerDigits[n], len}, |
Block[{digits = IntegerDigits[n], len}, |
||
len = Length[digits]; |
len = Length[digits]; |
||
Line 3,848: | Line 3,848: | ||
100, -12345, 1, 2, -1, -10, 2002, -2002, 0}; |
100, -12345, 1, 2, -1, -10, 2002, -2002, 0}; |
||
Column[middleThree /@ testData]</ |
Column[middleThree /@ testData]</syntaxhighlight> |
||
{{out}}<pre>123 |
{{out}}<pre>123 |
||
234 |
234 |
||
Line 3,868: | Line 3,868: | ||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab">function s=middle_three_digits(a) |
||
% http://rosettacode.org/wiki/Middle_three_digits |
% http://rosettacode.org/wiki/Middle_three_digits |
||
Line 3,882: | Line 3,882: | ||
end; |
end; |
||
s = s((length(s)+1)/2+[-1:1]);</ |
s = s((length(s)+1)/2+[-1:1]);</syntaxhighlight> |
||
Test with |
Test with |
||
< |
<syntaxhighlight lang="matlab"> x=[123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0]; |
||
for k=1:length(x); fprintf(1,'%9i:%s\n',x(k),middle_three_digits(x(k)));end;</ |
for k=1:length(x); fprintf(1,'%9i:%s\n',x(k),middle_three_digits(x(k)));end;</syntaxhighlight> |
||
Result |
Result |
||
<pre> |
<pre> |
||
Line 3,908: | Line 3,908: | ||
=={{header|MiniScript}}== |
=={{header|MiniScript}}== |
||
< |
<syntaxhighlight lang="miniscript">middle3 = function(num) |
||
if num < 0 then num = -num |
if num < 0 then num = -num |
||
s = str(num) |
s = str(num) |
||
Line 3,920: | Line 3,920: | ||
100, -12345, 1, 2, -1, -10, 2002, -2002, 0] |
100, -12345, 1, 2, -1, -10, 2002, -2002, 0] |
||
print test + " --> " + middle3(test) |
print test + " --> " + middle3(test) |
||
end for</ |
end for</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>123 --> 123 |
<pre>123 --> 123 |
||
Line 3,941: | Line 3,941: | ||
=={{header|МК-61/52}}== |
=={{header|МК-61/52}}== |
||
<lang>П0 lg [x] 3 - x>=0 23 ИП0 1 0 |
<syntaxhighlight lang="text">П0 lg [x] 3 - x>=0 23 ИП0 1 0 |
||
/ [x] ^ lg [x] 10^x П1 / {x} ИП1 |
/ [x] ^ lg [x] 10^x П1 / {x} ИП1 |
||
* БП 00 1 + x=0 29 ИП0 С/П 0 |
* БП 00 1 + x=0 29 ИП0 С/П 0 |
||
/</ |
/</syntaxhighlight> |
||
''Instruction:'' enter the number in the РX (on display), the result after the execution of the same. In the case of an even or less than 3 number of digits the indicator displays an error message. |
''Instruction:'' enter the number in the РX (on display), the result after the execution of the same. In the case of an even or less than 3 number of digits the indicator displays an error message. |
||
Line 3,950: | Line 3,950: | ||
=={{header|ML}}== |
=={{header|ML}}== |
||
==={{header|mLite}}=== |
==={{header|mLite}}=== |
||
< |
<syntaxhighlight lang="ocaml"> |
||
val test_array = ["123","12345","1234567","987654321","10001","~10001","~123","~100","100","~12345","1","2","~1","~10","2002","~2002","0"]; |
val test_array = ["123","12345","1234567","987654321","10001","~10001","~123","~100","100","~12345","1","2","~1","~10","2002","~2002","0"]; |
||
Line 3,975: | Line 3,975: | ||
; |
; |
||
map (println o middleThreeDigits) test_array;</ |
map (println o middleThreeDigits) test_array;</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>123 --> 123 |
<pre>123 --> 123 |
||
Line 3,997: | Line 3,997: | ||
=={{header|MUMPS}}== |
=={{header|MUMPS}}== |
||
This sample shows the MUMPS code required to pass the specification. |
This sample shows the MUMPS code required to pass the specification. |
||
< |
<syntaxhighlight lang="mumps">/* MUMPS */ |
||
MID3(N) ; |
MID3(N) ; |
||
N LEN,N2 |
N LEN,N2 |
||
Line 4,007: | Line 4,007: | ||
F I=123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,1,2,-1,-10,2002,-2002,0 W !,$J(I,10),": ",$$MID3^MID3(I) |
F I=123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,1,2,-1,-10,2002,-2002,0 W !,$J(I,10),": ",$$MID3^MID3(I) |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Output:''' |
'''Output:''' |
||
<pre> |
<pre> |
||
Line 4,031: | Line 4,031: | ||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
This sample goes the extra mile and provides a method that can display the middle N digits from the input value. To satisfy the requirements of this task, a static invocation of this general method is also provided with the value '''<tt>3</tt>''' hard coded as the digit count. |
This sample goes the extra mile and provides a method that can display the middle N digits from the input value. To satisfy the requirements of this task, a static invocation of this general method is also provided with the value '''<tt>3</tt>''' hard coded as the digit count. |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 4,077: | Line 4,077: | ||
end |
end |
||
return text |
return text |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Output:''' |
'''Output:''' |
||
<pre> |
<pre> |
||
Line 4,107: | Line 4,107: | ||
=={{header|NewLISP}}== |
=={{header|NewLISP}}== |
||
<syntaxhighlight lang="newlisp"> |
|||
<lang NewLISP> |
|||
(define (middle3 x) |
(define (middle3 x) |
||
(if (even? (length x)) |
(if (even? (length x)) |
||
Line 4,119: | Line 4,119: | ||
(map middle3 lst) |
(map middle3 lst) |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Output:''' |
'''Output:''' |
||
<pre> |
<pre> |
||
Line 4,142: | Line 4,142: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">proc middleThreeDigits(i: int): string = |
||
var s = $abs(i) |
var s = $abs(i) |
||
if s.len < 3 or s.len mod 2 == 0: |
if s.len < 3 or s.len mod 2 == 0: |
||
Line 4,155: | Line 4,155: | ||
var answer = try: middleThreeDigits(i) |
var answer = try: middleThreeDigits(i) |
||
except ValueError: getCurrentExceptionMsg() |
except ValueError: getCurrentExceptionMsg() |
||
echo "middleThreeDigits(", i, ") returned: ", answer</ |
echo "middleThreeDigits(", i, ") returned: ", answer</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,177: | Line 4,177: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck"> |
||
class Test { |
class Test { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
Line 4,190: | Line 4,190: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre> |
<pre> |
||
Line 4,214: | Line 4,214: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let even x = (x land 1) <> 1 |
||
let middle_three_digits x = |
let middle_three_digits x = |
||
Line 4,238: | Line 4,238: | ||
print_endline "Should fail:"; |
print_endline "Should fail:"; |
||
List.iter print failing; |
List.iter print failing; |
||
;;</ |
;;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,266: | Line 4,266: | ||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">: middle3 |
||
| s sz | |
| s sz | |
||
abs asString dup ->s size ->sz |
abs asString dup ->s size ->sz |
||
sz 3 < ifTrue: [ "Too short" println return ] |
sz 3 < ifTrue: [ "Too short" println return ] |
||
sz isEven ifTrue: [ "Not odd number of digits" println return ] |
sz isEven ifTrue: [ "Not odd number of digits" println return ] |
||
sz 3 - 2 / 1+ dup 2 + s extract ;</ |
sz 3 - 2 / 1+ dup 2 + s extract ;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,290: | Line 4,290: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
{{works with|PARI/GP|2.6.0}} |
{{works with|PARI/GP|2.6.0}} |
||
< |
<syntaxhighlight lang="parigp">middle(n)=my(v=digits(n));if(#v>2&&#v%2,100*v[#v\2]+10*v[#v\2+1]+v[#v\2+2],"no middle 3 digits"); |
||
apply(middle,[123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0])</ |
apply(middle,[123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0])</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>%1 = [123, 234, 345, 654, 0, 0, 123, 100, 100, 234, "no middle 3 digits", "no middle 3 digits", "no middle 3 digits", "no middle 3 digits", "no middle 3 digits", "no middle 3 digits", "no middle 3 digits"]</pre> |
<pre>%1 = [123, 234, 345, 654, 0, 0, 123, 100, 100, 234, "no middle 3 digits", "no middle 3 digits", "no middle 3 digits", "no middle 3 digits", "no middle 3 digits", "no middle 3 digits", "no middle 3 digits"]</pre> |
||
If for some reason you want to see the leading digits, you can run |
If for some reason you want to see the leading digits, you can run |
||
< |
<syntaxhighlight lang="parigp">apply(n-> Strprintf("%03d", n), %)</syntaxhighlight> |
||
For earlier versions <code>digits</code> can be defined as |
For earlier versions <code>digits</code> can be defined as |
||
< |
<syntaxhighlight lang="parigp">digits(n)=eval(Vec(Str(n)))</syntaxhighlight> |
||
or more efficiently as |
or more efficiently as |
||
< |
<syntaxhighlight lang="parigp">digits(n)=Vec(apply(n->n-48,Vectorsmall(Str(n))))</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{works with|Free Pascal}} |
{{works with|Free Pascal}} |
||
< |
<syntaxhighlight lang="pascal">program Midl3dig; |
||
{$IFDEF FPC} |
{$IFDEF FPC} |
||
{$MODE Delphi} //result /integer => Int32 aka longInt etc.. |
{$MODE Delphi} //result /integer => Int32 aka longInt etc.. |
||
Line 4,341: | Line 4,341: | ||
writeln(n:9,': ',GetMid3dig(Test[i])); |
writeln(n:9,': ',GetMid3dig(Test[i])); |
||
end; |
end; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 123: 123 |
<pre> 123: 123 |
||
Line 4,363: | Line 4,363: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict ; |
use strict ; |
||
use warnings ; |
use warnings ; |
||
Line 4,388: | Line 4,388: | ||
1, 2, -1, -10, 2002, -2002, 0 ) ; |
1, 2, -1, -10, 2002, -2002, 0 ) ; |
||
map { middlethree( $_ ) } @numbers ; |
map { middlethree( $_ ) } @numbers ; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Middle 3 digits of 123 : 123 ! |
<pre>Middle 3 digits of 123 : 123 ! |
||
Line 4,410: | Line 4,410: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">mid3</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">procedure</span> <span style="color: #000000;">mid3</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">))</span> |
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">))</span> |
||
Line 4,422: | Line 4,422: | ||
<span style="color: #000000;">mid3</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span> |
<span style="color: #000000;">mid3</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,445: | Line 4,445: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php">// 32-bit builds of PHP: Integers can be from -2147483648 to 2147483647 |
||
// 64-bit builds of PHP: Integers can be from -9223372036854775808 to 9223372036854775807 |
// 64-bit builds of PHP: Integers can be from -9223372036854775808 to 9223372036854775807 |
||
Line 4,483: | Line 4,483: | ||
{ |
{ |
||
echo $nums.' : '.middlethree($nums). '<br>'; |
echo $nums.' : '.middlethree($nums). '<br>'; |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>123 : 123 |
<pre>123 : 123 |
||
Line 4,506: | Line 4,506: | ||
===Prolog-style=== |
===Prolog-style=== |
||
{{trans|Prolog}} |
{{trans|Prolog}} |
||
< |
<syntaxhighlight lang="picat">get_middle_f1(Str) = [X,Y,Z] => |
||
append(Pre,[X,Y,Z],Post,Str), |
append(Pre,[X,Y,Z],Post,Str), |
||
length(Pre) = length(Post).</ |
length(Pre) = length(Post).</syntaxhighlight> |
||
===List slice=== |
===List slice=== |
||
< |
<syntaxhighlight lang="picat">get_middle_f2(Str) = Str[Mid-1..Mid+1] => |
||
Mid = 1 + Str.len div 2.</ |
Mid = 1 + Str.len div 2.</syntaxhighlight> |
||
===Test=== |
===Test=== |
||
< |
<syntaxhighlight lang="picat">go => |
||
Success = [123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345], |
Success = [123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345], |
||
Fail = [1, 2, -1, -10, 2002, -2002, 0], |
Fail = [1, 2, -1, -10, 2002, -2002, 0], |
||
Line 4,540: | Line 4,540: | ||
if Len mod 2 = 0 then throw $not_odd_length(N) |
if Len mod 2 = 0 then throw $not_odd_length(N) |
||
elseif Len < 3 then throw $number_too_small(N) |
elseif Len < 3 then throw $number_too_small(N) |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,562: | Line 4,562: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de middle3digits (N) |
||
(let (Lst (chop (abs N)) Len (length Lst)) |
(let (Lst (chop (abs N)) Len (length Lst)) |
||
(tab (10 -2 -30) |
(tab (10 -2 -30) |
||
Line 4,571: | Line 4,571: | ||
((bit? 1 Len) |
((bit? 1 Len) |
||
(head 3 (nth Lst (/ Len 2))) ) |
(head 3 (nth Lst (/ Len 2))) ) |
||
(T "even number of digits") ) ) ) )</ |
(T "even number of digits") ) ) ) )</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="picolisp">(mapc middle3digits |
||
(123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345 |
(123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345 |
||
1 2 -1 -10 2002 -2002 0 ) )</ |
1 2 -1 -10 2002 -2002 0 ) )</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> 123: 123 |
<pre> 123: 123 |
||
Line 4,598: | Line 4,598: | ||
{{trans|PHP}} |
{{trans|PHP}} |
||
< |
<syntaxhighlight lang="pike">string middlethree(int i) { |
||
i = abs(i); |
i = abs(i); |
||
int length = sizeof((string)i); |
int length = sizeof((string)i); |
||
Line 4,623: | Line 4,623: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>123 : 123 |
<pre>123 : 123 |
||
Line 4,644: | Line 4,644: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
middle: procedure options (main); /* 29 October 2013 */ |
middle: procedure options (main); /* 29 October 2013 */ |
||
declare n fixed (15); |
declare n fixed (15); |
||
Line 4,669: | Line 4,669: | ||
end middle; |
end middle; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 4,691: | Line 4,691: | ||
=={{header|Plain English}}== |
=={{header|Plain English}}== |
||
< |
<syntaxhighlight lang="plainenglish">To run: |
||
Start up. |
Start up. |
||
Test 123. |
Test 123. |
||
Line 4,727: | Line 4,727: | ||
To test a number: |
To test a number: |
||
Get the middle three digits of the number giving a string. |
Get the middle three digits of the number giving a string. |
||
Write "" then the number then " -> " then the string on the console.</ |
Write "" then the number then " -> " then the string on the console.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,750: | Line 4,750: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
< |
<syntaxhighlight lang="powershell">function middle3($inp){ |
||
$str = [Math]::abs($inp) |
$str = [Math]::abs($inp) |
||
Line 4,773: | Line 4,773: | ||
$sample = 123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0 |
$sample = 123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0 |
||
foreach ($x in $sample){middle3 $x}</ |
foreach ($x in $sample){middle3 $x}</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>123: 123 |
<pre>123: 123 |
||
Line 4,796: | Line 4,796: | ||
{{works with|SWI-Prolog|6}} |
{{works with|SWI-Prolog|6}} |
||
< |
<syntaxhighlight lang="prolog"> |
||
middle_3_digits(Number, [D1,D2,D3]) :- |
middle_3_digits(Number, [D1,D2,D3]) :- |
||
verify_middle_3_able(Number, Digits), |
verify_middle_3_able(Number, Digits), |
||
Line 4,811: | Line 4,811: | ||
; true |
; true |
||
). |
). |
||
</syntaxhighlight> |
|||
</lang> |
|||
Test code: |
Test code: |
||
< |
<syntaxhighlight lang="prolog"> |
||
test_correct :- |
test_correct :- |
||
TestCases = [123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345], |
TestCases = [123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345], |
||
Line 4,823: | Line 4,823: | ||
format('Middle 3 digits of ~w ~30|: ~w~n', [TestCase, Result]) |
format('Middle 3 digits of ~w ~30|: ~w~n', [TestCase, Result]) |
||
). |
). |
||
</syntaxhighlight> |
|||
</lang> |
|||
Line 4,863: | Line 4,863: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Procedural=== |
===Procedural=== |
||
< |
<syntaxhighlight lang="python">>>> def middle_three_digits(i): |
||
s = str(abs(i)) |
s = str(abs(i)) |
||
length = len(s) |
length = len(s) |
||
Line 4,897: | Line 4,897: | ||
middle_three_digits(-2002) returned: AssertionError('Need odd and >= 3 digits',) |
middle_three_digits(-2002) returned: AssertionError('Need odd and >= 3 digits',) |
||
middle_three_digits(0) returned: AssertionError('Need odd and >= 3 digits',) |
middle_three_digits(0) returned: AssertionError('Need odd and >= 3 digits',) |
||
>>> </ |
>>> </syntaxhighlight> |
||
===Composition of pure functions=== |
===Composition of pure functions=== |
||
Using a composable option type as an alternative to error handling: |
Using a composable option type as an alternative to error handling: |
||
{{Trans|Haskell}} |
{{Trans|Haskell}} |
||
< |
<syntaxhighlight lang="python">'''Middle 3 digits''' |
||
Line 4,986: | Line 4,986: | ||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Middle three digits, where defined: |
<pre>Middle three digits, where defined: |
||
Line 5,011: | Line 5,011: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ 1 & not ] is even ( n --> b ) |
||
[ over size - |
[ over size - |
||
Line 5,038: | Line 5,038: | ||
[ dup number$ 9 justify echo$ |
[ dup number$ 9 justify echo$ |
||
say " --> " |
say " --> " |
||
middle3 echo$ cr ]</ |
middle3 echo$ cr ]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,061: | Line 5,061: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="scheme">#lang racket |
||
(define (middle x) |
(define (middle x) |
||
(cond |
(cond |
||
Line 5,074: | Line 5,074: | ||
(map middle (list 123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345)) |
(map middle (list 123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345)) |
||
(map middle (list 1 2 -1 -10 2002 -2002 0))</ |
(map middle (list 1 2 -1 -10 2002 -2002 0))</syntaxhighlight> |
||
The output: |
The output: |
||
< |
<syntaxhighlight lang="scheme">'("123" "234" "345" "654" "000" "000" "123" "100" "100" "234") |
||
'("error: number too small" "error: number too small" "error: number too small" "error: number too small" |
'("error: number too small" "error: number too small" "error: number too small" "error: number too small" |
||
"error: number has even length" "error: number has even length" "error: number too small")</ |
"error: number has even length" "error: number has even length" "error: number too small")</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang="raku" line>sub middle-three($n) { |
||
given $n.abs { |
given $n.abs { |
||
when .chars < 3 { "$n is too short" } |
when .chars < 3 { "$n is too short" } |
||
Line 5,093: | Line 5,093: | ||
123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345 |
123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345 |
||
1 2 -1 -10 2002 -2002 0 |
1 2 -1 -10 2002 -2002 0 |
||
>;</ |
>;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>The three middle digits of 123 are: 123 |
<pre>The three middle digits of 123 are: 123 |
||
Line 5,114: | Line 5,114: | ||
It is also possible to write a regular expression with a code assertion: |
It is also possible to write a regular expression with a code assertion: |
||
<lang |
<syntaxhighlight lang="raku" line>for [\~] ^10 { say "$_ => $()" if m/^^(\d+) <(\d**3)> (\d+) $$ <?{ $0.chars == $1.chars}> / }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>01234 => 123 |
<pre>01234 => 123 |
||
Line 5,122: | Line 5,122: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===version 1=== |
===version 1=== |
||
< |
<syntaxhighlight lang="rexx">/* REXX *************************************************************** |
||
* 03.02.2013 Walter Pachl |
* 03.02.2013 Walter Pachl |
||
* 19.04.2013 mid 3 is now a function returning the middle 3 digits |
* 19.04.2013 mid 3 is now a function returning the middle 3 digits |
||
Line 5,149: | Line 5,149: | ||
End |
End |
||
End |
End |
||
Return res</ |
Return res</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 5,178: | Line 5,178: | ||
<br><br>This REXX version is limited to numbers whose length is <big>≤</big> 100,000 decimal digits. |
<br><br>This REXX version is limited to numbers whose length is <big>≤</big> 100,000 decimal digits. |
||
<br>(The decimal digits limit is defined via the '''numeric digits''' statement in the first line of the subroutine/procedure.) |
<br>(The decimal digits limit is defined via the '''numeric digits''' statement in the first line of the subroutine/procedure.) |
||
< |
<syntaxhighlight lang="rexx">/*REXX program returns the three middle digits of a decimal number (or an error msg).*/ |
||
n= '123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345', |
n= '123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345', |
||
'+123 0123 2 -1 -10 2002 -2002 0 abc 1e3 -17e-3 1234567.', |
'+123 0123 2 -1 -10 2002 -2002 0 abc 1e3 -17e-3 1234567.', |
||
Line 5,193: | Line 5,193: | ||
if L<3 then return er "argument is less than three digits." |
if L<3 then return er "argument is less than three digits." |
||
if L//2==0 then return er "argument isn't an odd number of digits." |
if L//2==0 then return er "argument isn't an odd number of digits." |
||
return substr(x, (L-3)%2+1, 3)</ |
return substr(x, (L-3)%2+1, 3)</syntaxhighlight> |
||
'''output''' |
'''output''' |
||
<pre> |
<pre> |
||
Line 5,223: | Line 5,223: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
n = 1234567 |
n = 1234567 |
||
middle(n) |
middle(n) |
||
Line 5,234: | Line 5,234: | ||
but lennr%2=0 see "Number must have an odd number of digits" |
but lennr%2=0 see "Number must have an odd number of digits" |
||
else cnr = substr(string(nr),mnr,3) see cnr + nl ok |
else cnr = substr(string(nr),mnr,3) see cnr + nl ok |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">def middle_three_digits(num) |
||
# minus sign doesn't factor into digit count, |
# minus sign doesn't factor into digit count, |
||
# and calling #abs acts as a duck-type assertion |
# and calling #abs acts as a duck-type assertion |
||
Line 5,251: | Line 5,251: | ||
return str[length/2 - 1, 3].to_i |
return str[length/2 - 1, 3].to_i |
||
end</ |
end</syntaxhighlight> |
||
Testing program: |
Testing program: |
||
< |
<syntaxhighlight lang="ruby">samples = [ |
||
123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, |
123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, |
||
1, 2, -1, -10, 2002, -2002, 0 |
1, 2, -1, -10, 2002, -2002, 0 |
||
Line 5,268: | Line 5,268: | ||
puts e |
puts e |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 5,291: | Line 5,291: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn middle_three_digits(x: i32) -> Result<String, String> { |
||
let s: String = x.abs().to_string(); |
let s: String = x.abs().to_string(); |
||
let len = s.len(); |
let len = s.len(); |
||
Line 5,320: | Line 5,320: | ||
print_result(*i); |
print_result(*i); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 5,356: | Line 5,356: | ||
course just list the numbers backwards. |
course just list the numbers backwards. |
||
</pre> |
</pre> |
||
< |
<syntaxhighlight lang="s-lang"> |
||
define m3(i) |
define m3(i) |
||
{ |
{ |
||
Line 5,386: | Line 5,386: | ||
#endif |
#endif |
||
1, 2, -1, -10, 2002, -2002, 0 } ); |
1, 2, -1, -10, 2002, -2002, 0 } ); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}}<pre> |
{{out}}<pre> |
||
" 123: 123" |
" 123: 123" |
||
Line 5,410: | Line 5,410: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">/** |
||
* Optionally return the middle three digits of an integer. |
* Optionally return the middle three digits of an integer. |
||
* |
* |
||
Line 5,431: | Line 5,431: | ||
case Some(v) => "%03d".format(v) // Format the value, force leading zeroes |
case Some(v) => "%03d".format(v) // Format the value, force leading zeroes |
||
} mkString("\n") |
} mkString("\n") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>123 |
<pre>123 |
||
Line 5,453: | Line 5,453: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$include "seed7_05.s7i"; |
||
const func string: middle3 (in integer: number) is func |
const func string: middle3 (in integer: number) is func |
||
Line 5,476: | Line 5,476: | ||
writeln(number <& ": " <& middle3(number)); |
writeln(number <& ": " <& middle3(number)); |
||
end for; |
end for; |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,501: | Line 5,501: | ||
=={{header|SenseTalk}}== |
=={{header|SenseTalk}}== |
||
As a People Oriented Programming language, SenseTalk treats values in a fluid fashion. So although the calls to the middle3Digits handler pass an integer as the parameter, the handler itself treats the value as text, looking for occurrences of the text pattern <digit> in it. Similarly, the variable 'output' is initially set to a number, then becomes a text value when the middle digits are put into it at character position 15. |
As a People Oriented Programming language, SenseTalk treats values in a fluid fashion. So although the calls to the middle3Digits handler pass an integer as the parameter, the handler itself treats the value as text, looking for occurrences of the text pattern <digit> in it. Similarly, the variable 'output' is initially set to a number, then becomes a text value when the middle digits are put into it at character position 15. |
||
< |
<syntaxhighlight lang="sensetalk">set inputs to [123, 12345, 1234567, 987654321, |
||
10001, -10001, -123, -100, 100, -12345, |
10001, -10001, -123, -100, 100, -12345, |
||
1, 2, -1, -10, 2002, -2002, 0] |
1, 2, -1, -10, 2002, -2002, 0] |
||
Line 5,521: | Line 5,521: | ||
return digits joined by empty |
return digits joined by empty |
||
end middle3digits |
end middle3digits |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 5,545: | Line 5,545: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="ruby">func middle_three(n) { |
||
var l = n.len; |
var l = n.len; |
||
if (l < 3) { |
if (l < 3) { |
||
Line 5,560: | Line 5,560: | ||
1 2 -1 -10 2002 -2002 0 |
1 2 -1 -10 2002 -2002 0 |
||
); |
); |
||
nums.each { say middle_three(_) };</ |
nums.each { say middle_three(_) };</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 5,583: | Line 5,583: | ||
=={{header|SQL}}== |
=={{header|SQL}}== |
||
< |
<syntaxhighlight lang="sql">;WITH DATA |
||
AS (SELECT CAST(ABS(NUMBER) AS NVARCHAR(MAX)) charNum, |
AS (SELECT CAST(ABS(NUMBER) AS NVARCHAR(MAX)) charNum, |
||
NUMBER, |
NUMBER, |
||
Line 5,594: | Line 5,594: | ||
END Output, |
END Output, |
||
NUMBER Input |
NUMBER Input |
||
FROM DATA </ |
FROM DATA </syntaxhighlight> |
||
<pre> |
<pre> |
||
Line 5,625: | Line 5,625: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">var num:Int = \\enter your number here |
||
if num<0{num = -num} |
if num<0{num = -num} |
||
var numArray:[Int]=[] |
var numArray:[Int]=[] |
||
Line 5,640: | Line 5,640: | ||
i=i/2 |
i=i/2 |
||
print("\(numArray[i+1]),\(numArray[i]),\(numArray[i-1])") |
print("\(numArray[i+1]),\(numArray[i]),\(numArray[i-1])") |
||
}</ |
}</syntaxhighlight> |
||
<pre> |
<pre> |
||
123: 123 |
123: 123 |
||
Line 5,662: | Line 5,662: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc middleThree n { |
||
if {$n < 0} { |
if {$n < 0} { |
||
set n [expr {-$n}] |
set n [expr {-$n}] |
||
Line 5,675: | Line 5,675: | ||
set idx [expr {$idx / 2}] |
set idx [expr {$idx / 2}] |
||
string range $n $idx [expr {$idx+2}] |
string range $n $idx [expr {$idx+2}] |
||
}</ |
}</syntaxhighlight> |
||
Demonstrating: |
Demonstrating: |
||
< |
<syntaxhighlight lang="tcl">foreach n { |
||
123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345 |
123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345 |
||
1 2 -1 -10 2002 -2002 0 |
1 2 -1 -10 2002 -2002 0 |
||
Line 5,688: | Line 5,688: | ||
puts "found for ${n}: $mid" |
puts "found for ${n}: $mid" |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 5,715: | Line 5,715: | ||
{{works with|Z Shell}} |
{{works with|Z Shell}} |
||
< |
<syntaxhighlight lang="bash">function middle3digits |
||
{ |
{ |
||
typeset -i n="${1#-}" |
typeset -i n="${1#-}" |
||
Line 5,737: | Line 5,737: | ||
printf "%10d: " $n |
printf "%10d: " $n |
||
middle3digits "$n" |
middle3digits "$n" |
||
done</ |
done</syntaxhighlight> |
||
Output: <pre> 123: 123 |
Output: <pre> 123: 123 |
||
Line 5,759: | Line 5,759: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Option Explicit |
Option Explicit |
||
Line 5,781: | Line 5,781: | ||
End If |
End If |
||
End Function |
End Function |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>123 Return : 123 |
<pre>123 Return : 123 |
||
Line 5,802: | Line 5,802: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
< |
<syntaxhighlight lang="vb">'http://rosettacode.org/wiki/Middle_three_digits |
||
Function mid3n(n) |
Function mid3n(n) |
||
Line 5,822: | Line 5,822: | ||
WScript.StdOut.Write n & ": " & mid3n(n) |
WScript.StdOut.Write n & ": " & mid3n(n) |
||
WScript.StdOut.WriteLine |
WScript.StdOut.WriteLine |
||
Next</ |
Next</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 5,845: | Line 5,845: | ||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
< |
<syntaxhighlight lang="vedit">do { |
||
#1 = Get_Num("Enter a number, or 0 to stop: ", STATLINE) |
#1 = Get_Num("Enter a number, or 0 to stop: ", STATLINE) |
||
Ins_Text("Input: ") Num_Ins(#1, COUNT, 10) |
Ins_Text("Input: ") Num_Ins(#1, COUNT, 10) |
||
Line 5,873: | Line 5,873: | ||
} |
} |
||
Buf_Quit(OK) |
Buf_Quit(OK) |
||
Return </ |
Return </syntaxhighlight> |
||
Output: |
Output: |
||
Line 5,896: | Line 5,896: | ||
=={{header|Wart}}== |
=={{header|Wart}}== |
||
< |
<syntaxhighlight lang="wart">def (mid3 n) |
||
withs (digits (with outstring # itoa |
withs (digits (with outstring # itoa |
||
(pr abs.n)) |
(pr abs.n)) |
||
Line 5,902: | Line 5,902: | ||
mid (int max/2)) |
mid (int max/2)) |
||
if (and odd?.max (max >= 3)) |
if (and odd?.max (max >= 3)) |
||
(digits mid-1 mid+2)</ |
(digits mid-1 mid+2)</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 5,944: | Line 5,944: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
var middle3 = Fn.new { |n| |
var middle3 = Fn.new { |n| |
||
Line 5,962: | Line 5,962: | ||
for (e in a) { |
for (e in a) { |
||
System.print("%(Fmt.s(9, e)) -> %(middle3.call(e))") |
System.print("%(Fmt.s(9, e)) -> %(middle3.call(e))") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,986: | Line 5,986: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\stdlib; |
||
func Mid3Digits(I); \Return the middle three digits of I |
func Mid3Digits(I); \Return the middle three digits of I |
||
Line 6,009: | Line 6,009: | ||
Text(0, Mid3Digits(Passing(X))); CrLf(0); |
Text(0, Mid3Digits(Passing(X))); CrLf(0); |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 6,033: | Line 6,033: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn middle(ns){ |
||
ns.apply("toString").apply('-("-")) |
ns.apply("toString").apply('-("-")) |
||
.apply(fcn(n){nl:=n.len(); |
.apply(fcn(n){nl:=n.len(); |
||
Line 6,041: | Line 6,041: | ||
} |
} |
||
middle(T(123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345)).println() |
middle(T(123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345)).println() |
||
middle(T(1, 2, -1, -10, 2002, -2002, 0)).println();</ |
middle(T(1, 2, -1, -10, 2002, -2002, 0)).println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |