3
\$\begingroup\$

To save codepoints, a compute-er can share 0 and -, and share . with $(indicating a variable name).

A number matches RegEx /^-?(0|[1-9]\d*)(\.\d+)?$/ and a variable matches RegEx /^\$\d+$/.

Given input where 0 and - are represented as same char(your choice) and so do . and $, return as number or variable. You can assume that it's possible.

Test cases

0 => 0
10 => 10
01 => -1
00 => -0
001 => (undefined behavior)
1.3 => 1.3
.33 => $33
0.33 => 0.33
00.33 => -0.33
42.0 => 42.0
3.4.5 => (undefined behavior)

Shortest code wins.

\$\endgroup\$
1
  • \$\begingroup\$ Is 42 a valid input? If so, add it add another testcase. \$\endgroup\$
    – tsh
    Commented Dec 10 at 8:17

7 Answers 7

3
\$\begingroup\$

Retina 0.8.2, 14 bytes

1T1`0.`-$`^.\d

Try it online! Link includes test cases. Explanation: Port of @tsh's JavaScript answer.

1T1`

Transliterate only the first character of the first match. (There will only be one match, but I still need to specify it so that I can only transliterate the first character.)

0.`-$`

Transliterate 0 to - and . to $.

^.\d

Match only the first two characters and only if the second is a digit. (Transliterate will ignore the first character if it's not a 0 or ..)

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

JavaScript (Node.js), 42 bytes

s=>s.replace(/^[0.](?=\d)/,x=>x<1?'-':'$')

Try it online!

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

Perl 5 -p, 25 21 bytes

@Neil saved 4 bytes

s/^[.\$]/\$/;s;^0\B;-

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You can use \B instead of (?=\d) to save 4 bytes. \$\endgroup\$
    – Neil
    Commented Dec 10 at 16:56
1
\$\begingroup\$

05AB1E, 21 16 bytes

¦нdić„-$ì¤0tskèì

Try it online or verify all test cases.

Explanation:

¦                # Remove the first character of the (implicit) input
 н               # Pop and take the new first character (aka the second),
                 # or an empty string if the input was 1 character long
  di             # Pop, and if this second character is a digit:
    ć            #  Extract head; push first character and remainder-string
     „-$ì        #  Prepend "-$" before this first character
         ¤       #  Push the last character of this triplet (without popping),
                 #  aka that same first character
          0t     #  Push 0.0 (square root of 0)
            s    #  Swap so the first character is at the top again
             k   #  Get the 0-based index of this character in "0.0",
                 #  or -1 if it's neither "0" nor "."
              è  #  Use that index to index into "-$ÿ",
                 #  where -1 indexes into this last character (`ÿ`)
               ì #  Prepend that to the remainder-string
                 #  (after which it is output implicitly as result)
                 # (implicit else: output the implicit input as is)
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 45 bytes

f(char*x){*x=*x-48|x[1]%46<1?*x-46?*x:36:45;}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Charcoal, 21 bytes

Pθ¿‹.⊟KD²→§⁺-$KK⌕0.KK

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

Pθ

Output the input string without moving the cursor.

¿‹.⊟KD²→

If the second character exists and is a digit, then...

§⁺-$KK⌕0.KK

... transliterate the character under the cursor from 0 to - or . to $ or anything else to itself.

\$\endgroup\$
0
\$\begingroup\$

Java, 57 53 bytes

s->s.replaceAll("^0\\B","-").replaceAll("^\\.","\\$")

-4 bytes thanks to @Neil.

Try it online.

Explanation:

s->                     // Method with String as both parameter and return-type
  s.replaceAll("^0      //  Replace a leading "0",
                  \\B", //  which has a (zero-width) digit after it
               "-")     //  with "-"
   .replaceAll("^\\.",  //  Then(/or) replace a leading "."
               "\\$")   //  with "$"
\$\endgroup\$
1
  • 1
    \$\begingroup\$ replaceAll("^0\\B","-") saves 4 bytes. \$\endgroup\$
    – Neil
    Commented Dec 10 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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