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.
7 Answers 7
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 .
.)
-
\$\begingroup\$ You can use
\B
instead of(?=\d)
to save 4 bytes. \$\endgroup\$Neil– Neil2024年12月10日 16:56:15 +00:00Commented Dec 10, 2024 at 16:56
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)
Charcoal, 21 bytes
Pθ¿‹.⊟KD2→§+-$KK⌕0.KK
Try it online! Link is to verbose version of code. Explanation:
Pθ
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.
Java, (削除) 57 (削除ここまで) 53 bytes
s->s.replaceAll("^0\\B","-").replaceAll("^\\.","\\$")
-4 bytes thanks to @Neil.
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 "$"
-
1\$\begingroup\$
replaceAll("^0\\B","-")
saves 4 bytes. \$\endgroup\$Neil– Neil2024年12月10日 16:55:02 +00:00Commented Dec 10, 2024 at 16:55
42
a valid input? If so, add it add another testcase. \$\endgroup\$