9
\$\begingroup\$

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.


TEST CASES

Example

Input:

135
210

Output:

531

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


Negative numbers are allowed:

Example 2

Input:

-135
-210

Output:

-012

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


Real numbers are allowed:

Example 3

Input:

13.5
10

Output:

5.31

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


Example 4

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.


Example 5

Input:

0.0000001
0

Output:

1000000.0

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


Example 6

Input:

121
121

Output:

121

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


Rules


Good Luck!

\$\endgroup\$
  • 7
    \$\begingroup\$ 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). \$\endgroup\$ – xnor Nov 2 at 3:32
  • 3
    \$\begingroup\$ There are two example 4s and the first one is wrong (answer should be -5.3132111 \$\endgroup\$ – Nick Kennedy Nov 2 at 21:57
  • 3
    \$\begingroup\$ Now there are two example 5s :P \$\endgroup\$ – Night2 Nov 3 at 6:52
  • 2
    \$\begingroup\$ The rule for real numbers is strange. Just consider \$ 1 = 0.{\dot {9}} \$... \$\endgroup\$ – tsh Nov 3 at 8:46
  • 2
    \$\begingroup\$ 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? \$\endgroup\$ – Business Cat Nov 4 at 19:47

23 Answers 23

4
\$\begingroup\$

05AB1E, 6 7 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
\$\endgroup\$
  • 1
    \$\begingroup\$ Unfortunately it fails on test 5 : [0.0000001,0] just take input as string may fix it.. Try it online! \$\endgroup\$ – AZTECCO Nov 3 at 16:46
  • \$\begingroup\$ Yes, you are right. I must have overlooked that. Thanks! \$\endgroup\$ – Wisław Nov 3 at 20:08
  • 1
    \$\begingroup\$ I think it also fails if only the second string is negative, as in this example: tio.run/##yy9OTMpM/f//UdOaoCh13UcNC/7/… \$\endgroup\$ – Bart-Jan van Rossum Nov 3 at 21:28
  • \$\begingroup\$ 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 ε } \$\endgroup\$ – Wisław Nov 4 at 9:15
  • 2
    \$\begingroup\$ -1 with í'-δ†Z. \$\endgroup\$ – Grimmy Nov 4 at 13:47
6
\$\begingroup\$

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
\$\endgroup\$
5
\$\begingroup\$

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!

\$\endgroup\$
4
\$\begingroup\$

Zsh, 63 bytes

for x;a+=(${(M)x#-}${(j::)${(Oas::)x#-}})
<<<$a[1+$[a[1]<a[2]]]

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.

\$\endgroup\$
4
\$\begingroup\$

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!

\$\endgroup\$
4
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ Any reason for the (pre-release) notice? Python 3.8 was released Oct. 14, 2019. \$\endgroup\$ – Noodle9 Nov 4 at 14:36
  • \$\begingroup\$ @Noodle9 It's because the name on TIO has not been updated, if Py 3.8 is out I guess that should be updated :) \$\endgroup\$ – Jonathan Allan Nov 4 at 19:08
  • \$\begingroup\$ Here is the name definition, but I'm unsure what else should be updated. \$\endgroup\$ – Jonathan Allan Nov 4 at 19:14
4
\$\begingroup\$

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!

\$\endgroup\$
  • 1
    \$\begingroup\$ 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 :) \$\endgroup\$ – Jonathan Allan Nov 3 at 17:02
  • 1
    \$\begingroup\$ Also m= is unnecessary, it may be unnammed. For even more savings see my Python 3.8 entry. \$\endgroup\$ – Jonathan Allan Nov 3 at 17:04
  • \$\begingroup\$ '%.16f'%a fixes the 0.0000001 \$\endgroup\$ – Alexey Burdin Nov 5 at 15:20
  • \$\begingroup\$ @JonathanAllan That solution with eval is clever. Answer updated with credit \$\endgroup\$ – senox13 Nov 6 at 15:46
  • \$\begingroup\$ @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 \$\endgroup\$ – senox13 Nov 6 at 15:47
3
\$\begingroup\$

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:

enter image description here

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Example 5 (the first of them) says output should be 1000000.0 not 1000000 as in your tests. \$\endgroup\$ – Kjetil S. Nov 3 at 2:58
  • 2
    \$\begingroup\$ @KjetilS. 1000000.0 is equal to 1000000 . I don't see a problem here, unless OP explicitly asks for reversed string outputs. \$\endgroup\$ – Night2 Nov 3 at 6:04
  • \$\begingroup\$ see your point (I just read the examples) \$\endgroup\$ – Kjetil S. Nov 3 at 12:30
2
\$\begingroup\$

APL+WIN, 32 bytes

Prompts for a nested string of the two numbers.

⌈/⍎¨(n↑¨'¯'),¨⌽¨(n←'¯'=↑¨m)↓¨m←⎕

Try it online! Courtesy of Dyalog Classic

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

Ruby, 51 bytes

->*a{a.map{|w|(w<=>0)*"#{w.abs}".reverse.to_f}.max}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ 7 bytes: petershaggynoble.github.io/Japt-Interpreter/… \$\endgroup\$ – Embodiment of Ignorance Nov 4 at 2:54
  • \$\begingroup\$ Also, you can't combine flags like that, and -gn is not a real flag. Either choose -g1 or -h \$\endgroup\$ – Embodiment of Ignorance Nov 4 at 2:55
  • \$\begingroup\$ @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 \$\endgroup\$ – AZTECCO Nov 4 at 8:33
  • \$\begingroup\$ @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. \$\endgroup\$ – Shaggy Nov 7 at 12:22
2
\$\begingroup\$

Haskell, 70 bytes

f a b=max(g a)(g b)
g('-':x)=(0-).h$x
g xs=h xs
h=read @Float .reverse

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
\$\endgroup\$
2
\$\begingroup\$

Perl 6, 28 27 bytes

-1 byte thanks to Jo King

&max o**.&{.abs.flip*.sign}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 27 bytes using a HyperWhatever. In an optimal world it would just be 25, but HyperWhatevers are rather disappointing \$\endgroup\$ – Jo King Nov 4 at 2:12
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

Keg -hd 53 41 35 34 27 bytes (SBCS)

(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.

34 bytes

(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.

41 bytes

(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.

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 13 bytes

I⌈E²I⪫E⪪S-⮌λ-

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

   ²            Literal `2`
  E             Map over implicit range
        S       Input string
       ⪪ -      Split on `-`s
      E         Map over substrings
           λ    Current substring
          ⮌     Reversed
     ⪫      -   Join with `-`s
    I           Cast to number
 ⌈              Maximum
I               Cast to string
                Implicitly print
\$\endgroup\$
  • \$\begingroup\$ ... why the downvote? \$\endgroup\$ – Neil Nov 3 at 17:21
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Bash + coreutils, 40 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Haskell, 104 bytes

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

\$\endgroup\$
1
\$\begingroup\$

Gaia, 11 bytes

⟨v'-%v$⟩¦d⌠

This is a function which accepts a list of two numbers as strings and leaves the result on the stack. OP wasn't clear about whether leading 0's or 0 decimals need to be included, but the program should be equally long either way. This function does not include these 0's:

⟨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
         d⌠  Return the element of the list which gives the highest value when parsed as a number
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.