# Max of the reversed of two numbers

Given two real numbers (postive or negative) in the limits of float pointing number data types, i.e., ($$\-3.4e38\$$ to $$\3.4e38\$$) not inclusive, you should output the max of the two reversed numbers.

## Input:

135
210


## Output:

531


$$\531\$$ is $$\135\$$ reversed, which is greater than $$\012\$$, i.e., $$\210\$$ reversed.

Negative numbers are allowed:

## Input:

-135
-210


## Output:

-012


$$\-012\$$ is $$\-210\$$ reversed, which is greater than $$\-531\$$, i.e., $$\-135\$$ reversed.

Real numbers are allowed:

## Input:

13.5
10


## Output:

5.31


$$\5.31\$$ is $$\13.5\$$ reversed, which is greater than $$\01\$$, i.e., $$\10\$$ reversed.

## Input:

-1112313.5
-312.5671


## Output:

-5.3132111


$$\-5.3132111\$$ is $$\-1112313.5\$$ reversed (it reverses the number but is still negative, which is greater than $$\-1765.213\$$, i.e., $$\-312.5671\$$ reversed.

## Input:

0.0000001
0


## Output:

1000000.0


$$\1000000.0\$$ is $$\0.0000001\$$ reversed, which is greater than $$\0\$$, i.e., still $$\0\$$ reversed.

## Input:

121
121


## Output:

121


Both numbers are equal and are palindrome. Either one is correct.

# Rules

## Good Luck!

• So, 10.0 would reverse to 0.01, while 10 would reverse to 01 or 1? Some languages might consider 10 and 10.0 equal or even not distinguish them at all. It seems like it's really a number string that's the input. I guess it don't matter since probably everyone will take the number as a string anyway (which is allowed by default). – xnor Nov 2 '19 at 3:32
• There are two example 4s and the first one is wrong (answer should be -5.3132111 – Nick Kennedy Nov 2 '19 at 21:57
• Now there are two example 5s :P – Night2 Nov 3 '19 at 6:52
• The rule for real numbers is strange. Just consider $1 = 0.{\dot {9}}$... – tsh Nov 3 '19 at 8:46
• Your test cases show some extraneous 0s in the results, like -012 or 1000000.0. Are these 0s required in the output, or could you output -12 and 1000000? – Business Cat Nov 4 '19 at 19:47

# 05AB1E, 67 6 bytes

+1 byte to fix cases when only one of the inputs is negative.
-1 byte by Grimmy

í'-δ†Z


Try it online!

Test all inputs

The input is taken as a list of strings [x, y].

Explanation:

í        Reverse each input
'-      Push a minus sign
δ†    Apply "filter out the minus sign to the front" to each input
Z   Take the maximum

• Unfortunately it fails on test 5 : [0.0000001,0] just take input as string may fix it.. Try it online! – AZTECCO Nov 3 '19 at 16:46
• Yes, you are right. I must have overlooked that. Thanks! – Wisław Nov 3 '19 at 20:08
• I think it also fails if only the second string is negative, as in this example: tio.run/##yy9OTMpM/f//UdOaoCh13UcNC/7/… – Bart-Jan van Rossum Nov 3 '19 at 21:28
• Thanks. To fix this, it seems like I have to filter out the minus sign to the front before I take the maximum, so the byte count went up by 1, since I had to replace € with ε } – Wisław Nov 4 '19 at 9:15
• -1 with í'-δ†Z. – Grimmy Nov 4 '19 at 13:47

# Wren, 126 124 bytes

As always Wren is the longest. I hate Wren's verbosity.

Fn.new{|a|a.map{|i|i>0?"":"-"+(i.abs.toString[-1..-(i.abs.toString.count)])}.map{|i|Num.fromString(i)}.reduce{|x,y|x<y?x:y}}


Try it online!

## Explanation

Fn.new{                                                                                                              // new anonymous function
|a|                                                                                                           // with the parameter a
a.map{|i|                                                                                                  // for each item in this parameter
(i>0)?"":"-"                                                                       // The sign of each item. If the item is larger than 0, return the null string, otherwise return a negative sign.
+(                                           )                         // Append this with the following string:
i.abs                                                                // The absolute value of i
.toString                                                       // Convert this value to a string
[-1..-(i.abs.toString.count)]                          // Reverse this string
Num.fromString(                                                          )                        // convert this to a number from a string
}.reduce{|x,y|x<y?x:y}} // Find the minimum value of the list


# Perl 5, 51 bytes:

sub f{($x,$y)=map{/-?/;$&.reverse$'}@_;$x>$y?$x:$y}


Try it online!

...or if using a function from a core module is allowed (max in List::Util) it's 35 bytes:

# 35 bytes:

sub f{max map{/-?/;$&.reverse$'}@_}


Try it online!

# Zsh, 63 bytes

for x;a+=(${(M)x#-}${(j::)${(Oas::)x#-}}) <<<$a[1+$[a<a]]  Try it online! ${x#-} removes the leading - if it exists. Adding the (M) flag causes the - to be substituted instead of what remains. Then this construct reverses the remaining string: ${(j::)${(Oas::)var}}.

For each element, we append to an array, then use a comparison to index into the array.

# JavaScript (ES6), 100 bytes

Takes input as (a)(b).

a=>b=>Math.max((g=n=>(s=n>0||-1)*[...(s*n).toFixed(9).replace(/\.0+$/,0)].reverse().join)(a),g(b))  Try it online! # Python 3.8, 69 bytes lambda*v:max(map(lambda x:(-1)**(z:=eval(x)<0)*float(x[z:][::-1]),v))  An unnamed function accepting 2 (actually, any number of) strings, which yields the maximal reversed version as a floating point number. Try it online! • Any reason for the (pre-release) notice? Python 3.8 was released Oct. 14, 2019. – Noodle9 Nov 4 '19 at 14:36 • @Noodle9 It's because the name on TIO has not been updated, if Py 3.8 is out I guess that should be updated :) – Jonathan Allan Nov 4 '19 at 19:08 • Here is the name definition, but I'm unsure what else should be updated. – Jonathan Allan Nov 4 '19 at 19:14 # Python 3, 89 86 bytes Disclaimer: This answer does not work for the second example #4 due to floating point accuracy constraints in Python. Feel free to disqualify. Update: This answer now works for all examples thanks to @JonathanAllan f=float r=lambda a:-f(a[:1:-1])if eval(a)<0else f(a[::-1]) m=lambda a,b:max(r(a),r(b))  Try it online! • It isn't floating point inaccuracy that breaks example 5, it's that str(0.0000001) is '1e-07' which, when reversed, is an invalid input to float. Take strings and use eval - you'll need to switch to<0 but it's shorter anyway :) – Jonathan Allan Nov 3 '19 at 17:02 • Also m= is unnecessary, it may be unnammed. For even more savings see my Python 3.8 entry. – Jonathan Allan Nov 3 '19 at 17:04 • '%.16f'%a fixes the 0.0000001 – Alexey Burdin Nov 5 '19 at 15:20 • @JonathanAllan That solution with eval is clever. Answer updated with credit – senox13 Nov 6 '19 at 15:46 • @AlexeyBurdin The reason I didn't go with this as a solution is that all you need to do to find a new input number that will break things is add some more zeros – senox13 Nov 6 '19 at 15:47 # Red, 74 bytes func[a][forall a[a/1:(sign? a/1)* do reverse to""absolute a/1]max a/1 a/2]  Try it online! Takes the input as a list of two numbers. The 5th test case (0.0000001 0) doesn't work in TIO, but works fine in the Red console: # JavaScript (Node.js), 64 bytes a=>b=>(R=([h,...t])=>h?h+1<0?h+R(t):R(t)+h:t)(a)>+R(b)?R(a):R(b)  Try it online! Input / output as strings. # PHP, 56 bytes fn($a)=>max(array_map(fn($n)=>strrev($n)*($n>0?:-1),$a))


Try it online!

Input is an array of two string numbers, example: ['123', '25.5'].

Simply reverses both numbers as strings, then returns the max of them as numbers. To handle reversed negative numbers, the *($n>0?:-1) part is used which multiplies the reversed number by 1 if the original number was more than zero, else multiplies it by -1. In PHP if you try to convert a string which starts with numeric values to a number, it will take the numeric part and ignore the rest. For example '321-' converted to a number will be 321 and the multiply operation automatically forces this numeric conversion. • Example 5 (the first of them) says output should be 1000000.0 not 1000000 as in your tests. – Kjetil S. Nov 3 '19 at 2:58 • @KjetilS. 1000000.0 is equal to 1000000 . I don't see a problem here, unless OP explicitly asks for reversed string outputs. – Night2 Nov 3 '19 at 6:04 • see your point (I just read the examples) – Kjetil S. Nov 3 '19 at 12:30 # APL+WIN, 32 bytes Prompts for a nested string of the two numbers. ⌈/⍎¨(n↑¨'¯'),¨⌽¨(n←'¯'=↑¨m)↓¨m←⎕  Try it online! Courtesy of Dyalog Classic # R, 82 bytes options(scipen=99);b=sign(a<-scan());max(b*as.numeric(stringi::stri_reverse(b*a)))  Try it online! Input is taken as two numbers. Setting scipen=99 is needed to deal with more than a few decimals. # Ruby, 51 bytes ->*a{a.map{|w|(w<=>0)*"#{w.abs}".reverse.to_f}.max}  Try it online! # Japt-g -N, 12 bytes ®®w}'-Ãn@Y-X  Try it Thanks to @Arnauld for spotting an error Takes input as array of strings U.m // for each element (function(Z) { return Z.m // split on '-' (function(Z) { return Z.w() // reverse and convert to number }, "-") }) // rejoins .n(function(X, Y, Z) { return Y - X }) // sort in descending order Flag -gn to output first element obtained Flag -N to output it as a number (remove leading 0s)  • – Embodiment of Ignorance Nov 4 '19 at 2:54 • Also, you can't combine flags like that, and -gn is not a real flag. Either choose -g1 or -h – Embodiment of Ignorance Nov 4 '19 at 2:55 • @Embodiment of Ignorance the -N flag is not really necessary as leading 0 are acceptable, I'll update just use -g and -N for nice output purpose. About your improvement.. It seems not to satisfy the specifics – AZTECCO Nov 4 '19 at 8:33 • @EmbodimentofIgnorance, that won't work, it needs to be 8 bytes. Note also that you can use -gN as a flag and that, although unusual, multiple flags can be used in some cases, particularly if one is lowercase and one is uppercase - e.g., -g -N. – Shaggy Nov 7 '19 at 12:22 # Haskell, 70 bytes f a b=max(g a)(g b) g('-':x)=(0-).h$x
g xs=h xs


Try it here

or if Haskell language pragma aren't allowed (like TypeApplications)

# 77 bytes

f a b=max(g a)(g b)
g('-':x)=(0-).h$x g xs=h xs h=read.reverse::String->Float  # Perl 6, 28 27 bytes -1 byte thanks to Jo King &max o**.&{.abs.flip*.sign}  Try it online! • 27 bytes using a HyperWhatever. In an optimal world it would just be 25, but HyperWhatevers are rather disappointing – Jo King Nov 4 '19 at 2:12 # APL(NARS), 17 chars, 34 bytes {⌈/(⍎¨⌽¨⍕¨∣⍵)××⍵}  test:  f←{⌈/(⍎¨⌽¨⍕¨∣⍵)××⍵} f 135 210 531 f ¯135 ¯210 ¯12 f 13.5 10 5.31 f ¯1112313.5 ¯312.5671 ¯5.3132111 f 0.0000001 0 SYNTAX ERROR f 0.000001 0 100000 f 121 121 121 f 121 ¯3 6 0.0001 9 1000  # Keg-hd5341353427 11 bytes (SBCS) (2|¿⑶&)@MMƒ  -16 bytes with new reverse t.o.s function ## Answer History ### 27 bytes (2|᠀÷^:\-=[_^\-^]⅀&)ℝ$ℝ@MMƒ


-7 bytes due to using the new built-in function library which will be soon populated with helpful functions.

MM is the maximum function, and returns the maximum of the two items passed to it.

(2|᠀÷^:\-=[_^\-^]⅍⅀&)ℝ$ℝ:&^:^<[&|^  Try it online! -1 byte due to using ⅍ ## 35 bytes (2|᠀÷^:\-=[_^\-]^⅀&)ℝ$ℝ:&^:^<[&|^


Hey, so, uh, Keg has flags now. -hd prints only the top item if nothing has been printed. Also, summate command works as expected now. The github interpreter works, but not TIO yet.

(2|᠀÷^:-=[_^-^]∑+)&)ℝ$ℝ:&^:^<[&|^].  Try it online! -12 bytes due to not using functions ## 53 bytes @m2|:&^:^<[&|^]ƒ(2|᠀÷^:-=[_^-^] (!;|+)&)ℝ$ℝ@mƒ.


Try it online!

I dont even know where to begin when explaining this, but it works. I know I could remove the need for a function, but I'm on my phone, so I'll do it later.

I would have used the summate command, but there's a bug with it.

# Charcoal, 13 bytes

Ｉ⌈Ｅ²Ｉ⪫Ｅ⪪Ｓ-⮌λ-


Try it online! Link is to verbose version of code. Explanation:

   ²            Literal 2
Ｅ             Map over implicit range
Ｓ       Input string
⪪ -      Split on -s
Ｅ         Map over substrings
λ    Current substring
⮌     Reversed
⪫      -   Join with -s
Ｉ           Cast to number
⌈              Maximum
Ｉ               Cast to string
Implicitly print

• ... why the downvote? – Neil Nov 3 '19 at 17:21

# Jelly, 11 bytes

ḟ€”-UV×VṠ$Ṁ  Try it online! A monadic link taking a list of two strings and returning the maximum reversed number as a float or integer depending on the input. ## Explanation ḟ€”- | Filter out “-“ from each U | Reverse each V | Evaluate as Jelly code ×$  | Multiply by following as a monad:
V    | - Evaluate as Jelly code
Ṡ   | - Sign
Ṁ | Max


# Bash + coreutils, 40 bytes

rev|sed 's/$$.*$$-/-\1/'|sort -n|tail -1


Try it online!

r n=read((\s->if head s=='-'then"-"++reverse(drop 1 s)else reverse s)$show n)::Float f a b=max(r a)(r b)  Try it online! The 0.0000001 0 test case doesn't work though, I think there's an error on read. Typing could also be improved when the input is type Int for example # Gaia, 11 bytes ⟨v'-%v$⟩¦d⌠

⟨v'-%v$d⟩¦⌉  Try it online! ⟨ ⟩¦ Map this block over the list: v Reverse the string '-% Split it around -'s, leaving the separators in the result v Reverse the splitting$       Rejoin it