Middle three digits: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 835: | Line 835: | ||
Press any key to continue . . .</pre> |
Press any key to continue . . .</pre> |
||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
Line 1,073: | Line 1,072: | ||
Error, 0 is too short. |
Error, 0 is too short. |
||
The middle three digits of 1234567890 are 456. |
The middle three digits of 1234567890 are 456. |
||
</pre> |
|||
=={{header|C_sharp|C#}}== |
|||
<lang csharp>using System; |
|||
namespace RosettaCode |
|||
{ |
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
string text = Math.Abs(int.Parse(Console.ReadLine())).ToString(); |
|||
Console.WriteLine(text.Length < 2 || text.Length % 2 == 0 ? "Error" : text.Substring((text.Length - 3) / 2, 3)); |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre>123:123 |
|||
12345:234 |
|||
1234567:345 |
|||
987654321:654 |
|||
10001:000 |
|||
-10001:000 |
|||
-123:123 |
|||
-100:100 |
|||
100:100 |
|||
-12345:234 |
|||
1:Error |
|||
2:Error |
|||
-1:Error |
|||
-10:Error |
|||
2002:Error |
|||
-2002:Error |
|||
0:Error |
|||
</pre> |
</pre> |
||
Line 1,157: | Line 1,121: | ||
middleThreeDigits(-2002): even number of digits |
middleThreeDigits(-2002): even number of digits |
||
middleThreeDigits(0): less than three digits</pre> |
middleThreeDigits(0): less than three digits</pre> |
||
=={{header|C_sharp|C#}}== |
|||
<lang csharp>using System; |
|||
namespace RosettaCode |
|||
{ |
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
string text = Math.Abs(int.Parse(Console.ReadLine())).ToString(); |
|||
Console.WriteLine(text.Length < 2 || text.Length % 2 == 0 ? "Error" : text.Substring((text.Length - 3) / 2, 3)); |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre>123:123 |
|||
12345:234 |
|||
1234567:345 |
|||
987654321:654 |
|||
10001:000 |
|||
-10001:000 |
|||
-123:123 |
|||
-100:100 |
|||
100:100 |
|||
-12345:234 |
|||
1:Error |
|||
2:Error |
|||
-1:Error |
|||
-10:Error |
|||
2002:Error |
|||
-2002:Error |
|||
0:Error |
|||
</pre> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 1,665: | Line 1,664: | ||
} |
} |
||
</lang> |
</lang> |
||
=={{header|DCL}}== |
=={{header|DCL}}== |
||
<lang>$ list = "123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,1,2,-1,-10,2002,-2002,0" |
<lang>$ list = "123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,1,2,-1,-10,2002,-2002,0" |
||
Line 1,786: | Line 1,786: | ||
</pre> |
</pre> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
Line 2,078: | Line 2,079: | ||
if 2/ 1- chars + 3 else drop 0 then |
if 2/ 1- chars + 3 else drop 0 then |
||
;</lang> |
;</lang> |
||
=={{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. |
||
Line 2,836: | Line 2,838: | ||
-2002 => invalid length: 4 |
-2002 => invalid length: 4 |
||
0 => invalid length: 1 </lang> |
0 => invalid length: 1 </lang> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{works with|Julia|1.0.3}} |
{{works with|Julia|1.0.3}} |
||
Line 3,571: | Line 3,574: | ||
or more efficiently as |
or more efficiently as |
||
<lang parigp>digits(n)=Vec(apply(n->n-48,Vectorsmall(Str(n))))</lang> |
<lang parigp>digits(n)=Vec(apply(n->n-48,Vectorsmall(Str(n))))</lang> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{works with|Free Pascal}} |
{{works with|Free Pascal}} |
||
Line 3,629: | Line 3,633: | ||
0: got too few digits |
0: got too few digits |
||
</pre> |
</pre> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<lang Perl>#!/usr/bin/perl |
<lang Perl>#!/usr/bin/perl |
||
Line 3,675: | Line 3,680: | ||
Middle 3 digits can't be shown : 0 too short! |
Middle 3 digits can't be shown : 0 too short! |
||
</pre> |
</pre> |
||
=={{header|Perl 6}}== |
|||
<lang Perl6>sub middle-three($n) { |
|||
given $n.abs { |
|||
when .chars < 3 { "$n is too short" } |
|||
when .chars %% 2 { "$n has an even number of digits" } |
|||
default { "The three middle digits of $n are: ", .substr: (.chars - 3)/2, 3 } |
|||
} |
|||
} |
|||
say middle-three($_) for < |
|||
123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345 |
|||
1 2 -1 -10 2002 -2002 0 |
|||
>;</lang> |
|||
{{out}} |
|||
<pre>The three middle digits of 123 are: 123 |
|||
The three middle digits of 12345 are: 234 |
|||
The three middle digits of 1234567 are: 345 |
|||
The three middle digits of 987654321 are: 654 |
|||
The three middle digits of 10001 are: 000 |
|||
The three middle digits of -10001 are: 000 |
|||
The three middle digits of -123 are: 123 |
|||
The three middle digits of -100 are: 100 |
|||
The three middle digits of 100 are: 100 |
|||
The three middle digits of -12345 are: 234 |
|||
1 is too short |
|||
2 is too short |
|||
-1 is too short |
|||
-10 is too short |
|||
2002 has an even number of digits |
|||
-2002 has an even number of digits |
|||
0 is too short</pre> |
|||
It is also possible to write a regular expression with a code assertion: |
|||
<lang perl6>for [\~] ^10 { say "$_ => $()" if m/^^(\d+) <(\d**3)> (\d+) $$ <?{ $0.chars == $1.chars}> / }</lang> |
|||
{{out}} |
|||
<pre>01234 => 123 |
|||
0123456 => 234 |
|||
012345678 => 345</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 3,931: | Line 3,897: | ||
-2002: [ERROR] even number of digits. |
-2002: [ERROR] even number of digits. |
||
0: [ERROR] too short.</pre> |
0: [ERROR] too short.</pre> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Line 4,167: | Line 4,132: | ||
'("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")</lang> |
"error: number has even length" "error: number has even length" "error: number too small")</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
<lang perl6>sub middle-three($n) { |
|||
given $n.abs { |
|||
when .chars < 3 { "$n is too short" } |
|||
when .chars %% 2 { "$n has an even number of digits" } |
|||
default { "The three middle digits of $n are: ", .substr: (.chars - 3)/2, 3 } |
|||
} |
|||
} |
|||
say middle-three($_) for < |
|||
123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345 |
|||
1 2 -1 -10 2002 -2002 0 |
|||
>;</lang> |
|||
{{out}} |
|||
<pre>The three middle digits of 123 are: 123 |
|||
The three middle digits of 12345 are: 234 |
|||
The three middle digits of 1234567 are: 345 |
|||
The three middle digits of 987654321 are: 654 |
|||
The three middle digits of 10001 are: 000 |
|||
The three middle digits of -10001 are: 000 |
|||
The three middle digits of -123 are: 123 |
|||
The three middle digits of -100 are: 100 |
|||
The three middle digits of 100 are: 100 |
|||
The three middle digits of -12345 are: 234 |
|||
1 is too short |
|||
2 is too short |
|||
-1 is too short |
|||
-10 is too short |
|||
2002 has an even number of digits |
|||
-2002 has an even number of digits |
|||
0 is too short</pre> |
|||
It is also possible to write a regular expression with a code assertion: |
|||
<lang perl6>for [\~] ^10 { say "$_ => $()" if m/^^(\d+) <(\d**3)> (\d+) $$ <?{ $0.chars == $1.chars}> / }</lang> |
|||
{{out}} |
|||
<pre>01234 => 123 |
|||
0123456 => 234 |
|||
012345678 => 345</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 4,671: | Line 4,676: | ||
</pre> |
</pre> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
<lang swift>var num:Int = \\enter your number here |
<lang swift>var num:Int = \\enter your number here |
||
Line 4,707: | Line 4,713: | ||
0: Invalid Input |
0: Invalid Input |
||
</pre> |
</pre> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |