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.

The Fifth Marshal
6,2631 gold badge26 silver badges46 bronze badges
asked Dec 10, 2024 at 5:53
\$\endgroup\$
1
  • \$\begingroup\$ Is 42 a valid input? If so, add it add another testcase. \$\endgroup\$ Commented Dec 10, 2024 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 ..)

answered Dec 10, 2024 at 9:54
\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 42 bytes

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

Try it online!

answered Dec 10, 2024 at 8:05
\$\endgroup\$
2
\$\begingroup\$

Perl 5 -p, (削除) 25 (削除ここまで) 21 bytes

@Neil saved 4 bytes

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

Try it online!

answered Dec 10, 2024 at 15:02
\$\endgroup\$
1
  • \$\begingroup\$ You can use \B instead of (?=\d) to save 4 bytes. \$\endgroup\$ Commented Dec 10, 2024 at 16:56
2
\$\begingroup\$

C (gcc), 45 bytes

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

Try it online!

answered Dec 12, 2024 at 23:57
\$\endgroup\$
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)
answered Dec 10, 2024 at 9:56
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 21 bytes

Pθ¿‹.⊟KD2→§+-$KK⌕0.KK

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

Output the input string without moving the cursor.

¿‹.⊟KD2→

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.

answered Dec 10, 2024 at 11:57
\$\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 "$"
answered Dec 10, 2024 at 15:42
\$\endgroup\$
1
  • 1
    \$\begingroup\$ replaceAll("^0\\B","-") saves 4 bytes. \$\endgroup\$ Commented Dec 10, 2024 at 16:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.