Middle three digits: Difference between revisions
Content deleted Content added
→{{header|REXX}}: added version 2 for REXX, added an INCORR flag for version 1. -- ~~~~ |
|||
Line 1,038: | Line 1,038: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
{{incorrect|REXX|--(version 1)-- the subroutine/function doesn't return the middle three digits as per the task's description. <br>}} |
|||
===version 1=== |
|||
<lang rexx>/* REXX *************************************************************** |
<lang rexx>/* REXX *************************************************************** |
||
* 03.02.2013 Walter Pachl |
* 03.02.2013 Walter Pachl |
||
Line 1,088: | Line 1,090: | ||
1e3 -> not just digits |
1e3 -> not just digits |
||
-17e-3 -> not just digits |
-17e-3 -> not just digits |
||
</pre> |
|||
===version 2=== |
|||
A premise: '''12.3e2''' is an integer (regardless of how it's displayed). |
|||
<br>So is a '''googol''' and a '''googleplex'''. |
|||
<br><br>This REXX version is limited to numbers whose absolute value ≤ 100,000 digits. |
|||
<lang rexx>/*REXX program returns the 3 middle digits of a number (or an error). */ |
|||
n ='123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345', |
|||
'2 -1 -10 2002 -2002 0 abc 1e3 -17e-3 1234567. 1237654.00', |
|||
'1234567890123456789012345678901234567890123456789012345678901234567' |
|||
do j=1 for words(n); z=word(n,j) /* [↓] format the output nicely.*/ |
|||
say 'middle 3 digits of' right(z,max(15,length(z))) '──►' middle3(z) |
|||
end /*j*/ |
|||
exit /*stick a fork in it, we're done.*/ |
|||
/*──────────────────────────────────MIDDLE3 subroutine──────────────────*/ |
|||
middle3: procedure; arg x; numeric digits 1e5; er=' ***error!*** ' |
|||
if datatype(x,'N') then x=abs(x)/1; L=length(x) |
|||
if \datatype(x,'W') then return er "arg isn't an integer" |
|||
if L<3 then return er "arg is less then three digits" |
|||
if L//2==0 then return er "arg isn't an odd number of digits" |
|||
return substr(x, (L-3)%2+1, 3)</lang> |
|||
'''output''' |
|||
<pre style="overflow:scroll"> |
|||
middle 3 digits of 123 ──► 123 |
|||
middle 3 digits of 12345 ──► 234 |
|||
middle 3 digits of 1234567 ──► 345 |
|||
middle 3 digits of 987654321 ──► 654 |
|||
middle 3 digits of 10001 ──► 000 |
|||
middle 3 digits of -10001 ──► 000 |
|||
middle 3 digits of -123 ──► 123 |
|||
middle 3 digits of -100 ──► 100 |
|||
middle 3 digits of 100 ──► 100 |
|||
middle 3 digits of -12345 ──► 234 |
|||
middle 3 digits of 2 ──► ***error!*** arg is less then three digits |
|||
middle 3 digits of -1 ──► ***error!*** arg is less then three digits |
|||
middle 3 digits of -10 ──► ***error!*** arg is less then three digits |
|||
middle 3 digits of 2002 ──► ***error!*** arg isn't an odd number of digits |
|||
middle 3 digits of -2002 ──► ***error!*** arg isn't an odd number of digits |
|||
middle 3 digits of 0 ──► ***error!*** arg is less then three digits |
|||
middle 3 digits of abc ──► ***error!*** arg isn't an integer |
|||
middle 3 digits of 1e3 ──► ***error!*** arg isn't an odd number of digits |
|||
middle 3 digits of -17e-3 ──► ***error!*** arg isn't an integer |
|||
middle 3 digits of 1234567. ──► 345 |
|||
middle 3 digits of 1237654.00 ──► 376 |
|||
middle 3 digits of 1234567890123456789012345678901234567890123456789012345678901234567 ──► 345 |
|||
</pre> |
</pre> |
||