Middle three digits: Difference between revisions

Added Uiua solution
(Added Uiua solution)
 
(6 intermediate revisions by 3 users not shown)
Line 452:
</pre>
<p>VERSION 2:</p>
<syntaxhighlight lang="c">
#include <basico.h>
 
algoritmo
n=0, c="",
temp = 0, candidatos={}
'123, 12345, 1234567, 987654321, 10001, -10001,-123'
'-100, 100, -12345,1, 2, -1, -10, 2002, -2002, 0'
enlistar en 'candidatos'
decimales '0'
tomar 'candidatos' ---retener---, obtenerquitar el signo, multiplicar
convertir a cadena ---retener---, guardarobtener enlargo, mover a 'tempn' ,
guardar en 'temp'
guardar ' replicar ( temp, #(n>=3 && not(is even(n))))' en 'c'
token.separador(NL)
justificar derecha(11,#(string(candidatos))), " : ", concatenar esto
#( copy( 3, (n/2-1), c ) ), unir esto
luego imprime
terminar
</syntaxhighlight>
{{out}}
<pre>
123 : 123
12345 : 234
1234567 : 345
987654321 : 654
10001 : 000
-10001 : 000
-123 : 123
-100 : 100
100 : 100
-12345 : 234
1 :
2 :
-1 :
-10 :
2002 :
-2002 :
0 :
</pre>
 
<p>VERSION 3:</p>
<syntaxhighlight lang="c">
#include <basico.h>
Line 458 ⟶ 507:
n=0, c="", m=0,
temp = 0, candidatos={}
'123, 12345, 1234567, 987654321, 10001, -10001,-123'
'-100, 100, -12345,1, 2, -1, -10, 2002, -2002, 0'
enlistar en 'candidatos'
tomar 'candidatos' ---retener---, obtener signo, multiplicar
convertir a cadena, guardar en 'temp'
decimales '0'
para cada elemento (v,candidatos,17)
tomar'v' ---retener; copiar en 'm'---, obtenerquitar el signo, multiplicar,
guardar en 'v'
Line 2,656 ⟶ 2,702:
if(len<3)
{
InvalidArgumentException.new:("n must have 3 digits or more").raise()
}
else if(len.isEven())
{
InvalidArgumentException.new:("n must have an odd number of digits").raise()
};
Line 2,671 ⟶ 2,717:
{
new int[]{123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0}
.forEach::(n)
{
console.printLine("middleThreeDigits(",n,"):",middleThreeDigits(n) \\ on::(e => e.Message))
}
}</syntaxhighlight>
Line 6,169 ⟶ 6,215:
A flashing 9.999999999 99 appears on the screen indicating an error.
 
=={{header|Uiua}}==
{{Works with |Uiua|0.12.0-dev.1}}
Uses `assert` and `try` to handle the exceptions.
<syntaxhighlight lang="uiua">
[123 12345 1234567 987654321 10001 ¯10001 ¯123 ¯100 100 ¯12345
1 2 ¯1 ¯10 2002 ¯2002 0]
Mid ← (
⍤("too short")≥3.⧻.°□°⋕⌵
⍤("even length")≠0◿2.
↘¯⟜↘⌊÷2-3
)
⍉⊟⟜≡⍚⍣(Mid|⊂"Error: "◌) # Convert each
≡(&p$"_\t->\t_"°⊟) # Display
</syntaxhighlight>
{{out}}
<pre>
123 -> 123
12345 -> 234
1234567 -> 345
987654321 -> 654
10001 -> 000
-10001 -> 000
-123 -> 123
-100 -> 100
100 -> 100
-12345 -> 234
1 -> Error: too short
2 -> Error: too short
-1 -> Error: too short
-10 -> Error: too short
2002 -> Error: even length
-2002 -> Error: even length
0 -> Error: too short
</pre>
=={{header|UNIX Shell}}==
{{works with|Bourne Again Shell}}
Line 6,448 ⟶ 6,528:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var middle3 = Fn.new { |n|
Line 6,465 ⟶ 6,545:
 
for (e in a) {
SystemFmt.print("%(Fmt.s(9$9d -> $n", e)), -> %(middle3.call(e))")
}</syntaxhighlight>
 
162

edits