Introduction
My gnome friends are writing a programming language and have asked for my help. Due to their size, the gnomes can only handle small superscript numbers instead of big numbers. However, the language they're writing the interpreter in only accepts big numbers!
Your Challenge
Given an input of a superscript number (a series of characters that can be any of ⁰¹²³⁴⁵⁶⁷⁸⁹), convert it to normal ASCII numbers and print the result. This is code golf, so shortest answer wins!
Test Cases
¹ -> 1⁵ -> 5¹²³ -> 123⁶⁵⁵³⁵ -> 65535⁰¹²³ -> 123- 5\$\begingroup\$The reason that I ask is that I'm not absolutely confident that by copy-pasting from your question in my internet browser will give the correct characters. Please could you specify in the question exactly what the characters should (or could) be?\$\endgroup\$Dominic van Essen– Dominic van Essen2021-12-13 14:07:35 +00:00CommentedDec 13, 2021 at 14:07
- 3\$\begingroup\$@DominicvanEssen Sure. The characters have unicode codepoints 2070, 00B9, 00B2, 2074, 2075, 2076, 2077, 2078, and 2079.\$\endgroup\$Ginger– Ginger2021-12-13 14:09:51 +00:00CommentedDec 13, 2021 at 14:09
- 5\$\begingroup\$Can we return a list of digits?\$\endgroup\$Aaroneous Miller– Aaroneous Miller2021-12-13 18:48:56 +00:00CommentedDec 13, 2021 at 18:48
- 13\$\begingroup\$The new test case is invalidating most answers. :-/\$\endgroup\$Arnauld– Arnauld2021-12-14 00:08:37 +00:00CommentedDec 14, 2021 at 0:08
- 7\$\begingroup\$
ord(⁰)= 8304,ord(¹)= 185,ord(²)= 178... Unicode, you're drunk. -.-\$\endgroup\$Trang Oul– Trang Oul2021-12-14 07:02:01 +00:00CommentedDec 14, 2021 at 7:02
40 Answers40
- 7\$\begingroup\$+1 for finding the builtin.\$\endgroup\$Neil– Neil2021-12-13 19:44:40 +00:00CommentedDec 13, 2021 at 19:44
- \$\begingroup\$
+s.normalize…to remove leading zeros\$\endgroup\$customcommander– customcommander2022-09-07 22:14:27 +00:00CommentedSep 7, 2022 at 22:14
JavaScript (ES6), 43 bytes
Edit: +1 to support0123 ->123 ...
s=>+s.replace(/./g,c=>c.charCodeAt()%92%12)Conversion table
char. | code | mod 92 | mod 12-------+------+--------+-------- '⁰' | 8304 | 24 | 0 '¹' | 185 | 1 | 1 '²' | 178 | 86 | 2 '³' | 179 | 87 | 3 '⁴' | 8308 | 28 | 4 '⁵' | 8309 | 29 | 5 '⁶' | 8310 | 30 | 6 '⁷' | 8311 | 31 | 7 '⁸' | 8312 | 32 | 8 '⁹' | 8313 | 33 | 9- \$\begingroup\$For the second one, you can get away with
c/58&c.\$\endgroup\$dingledooper– dingledooper2021-12-13 23:49:15 +00:00CommentedDec 13, 2021 at 23:49 - \$\begingroup\$@dingledooper Nice one! There's also
c%12&c(found by Lynn). Unfortunately, the challenge update and OP comments suggest that this kind of answer is not valid anymore, so I'm removing it for now.\$\endgroup\$Arnauld– Arnauld2021-12-14 00:16:39 +00:00CommentedDec 14, 2021 at 0:16
Hexagony, side length 4, 37 bytes
92},/%=\_@=..}{<.{\<.>".2//{%.1!}>'/_Unwrapped code:
9 2 } , / % = \ _ @ = . . } { < . { \ < . > " . 2 / / { % . 1 ! } > ' / _This is my first post on Code Golf, and first time using Hexagony. It's as painful as the name implies, but so satisfying to get a result! I ended up with more mirrors than I expected, but I'm pretty proud of what I achieved.
Credit to the previous users who worked out the modulo trick. My hexagon would have been a lot bigger without%92%12!
- 2\$\begingroup\$Welcome to Code Golf! I haven't seen a Hexagony answer in ages, glad to see more usage of it!\$\endgroup\$2021-12-15 04:29:37 +00:00CommentedDec 15, 2021 at 4:29
- 1\$\begingroup\$It's rare to see a first post in Hexagony so well done! It doesn't look like
⁰is handled correctly though: for inputs of¹⁰and¹⁰¹I'm seeing outputs of1and11, respectively.\$\endgroup\$Dingus– Dingus2021-12-16 04:25:07 +00:00CommentedDec 16, 2021 at 4:25 - \$\begingroup\$Oh yeah! my test case started with ⁰, so I just didn't print it in order to stop leading zeroes from printing. I'll have to go back and fix it :)\$\endgroup\$Joundill– Joundill2021-12-16 04:37:06 +00:00CommentedDec 16, 2021 at 4:37
- 1\$\begingroup\$I think the hexagony.net interpreter has changed to use byte by byte input now, but this should still work for codepoint inputs\$\endgroup\$Jo King– Jo King2023-06-20 05:55:27 +00:00CommentedJun 20, 2023 at 5:55
Jelly, 7 bytes
O%12&ƊḌO Get ordinal values %12&Ɗ Apply x ↦ x%12&x Ḍ Digits to numberI found thex%12&x formula by computer search. If I/O were super duper lenient, I guess%12& would be a valid 4-byte answer.
R,4553 51 bytes
Edit +8 bytes to comply with the updated rules (0123 ->123), but then -2 bytes thanks to pajonk
function(s)el(chartr("⁰¹²³⁴-⁹","0-9",s):1)R,5351 57 bytes /25 bytes with IO as character codes
Edit: -4 bytes thanks to pajonk, but then +6 bytes (again thanks to pajonk) to comply with the updated rules
function(s,x=utf8ToInt(s))el(intToUtf8(x%%64-8*!x-185):1)Function without multi-byte character codes, but sadly the verboseutf8ToInt andintToUtf8 function names make it quite long if we want to accept input directly as a string...
The new rule that⁰¹²³ should output123 invalidates the second version with IO as character codes. It could be fixed for+17 bytes, but it's shorter to completely change approach (below), portingArnauld's charcode-to-digit method.
R, 40 bytes with IO as character codes
function(c)c%%92%%12%*%10^rev(seq(!c)-1)- 1\$\begingroup\$You could save some bytes by I/O as char codes (as is by default allowed for string challenges) by removing
utf8ToIntandintToUtf8.\$\endgroup\$pajonk– pajonk2021-12-13 15:22:36 +00:00CommentedDec 13, 2021 at 15:22 - \$\begingroup\$@pajonk - That's an excellent suggestion. Thankyou. Now suddenly the nicer function seems more competitive...!\$\endgroup\$Dominic van Essen– Dominic van Essen2021-12-13 15:34:15 +00:00CommentedDec 13, 2021 at 15:34
- 2\$\begingroup\$
(c==185)->!c-185for -2.\$\endgroup\$pajonk– pajonk2021-12-13 20:14:33 +00:00CommentedDec 13, 2021 at 20:14 - \$\begingroup\$@pajonk - Neat! Thanks again!\$\endgroup\$Dominic van Essen– Dominic van Essen2021-12-13 22:26:00 +00:00CommentedDec 13, 2021 at 22:26
- 1
Charcoal, 2 bytes
IVTry it online! Explanation:
V Eval as a Charcoal integer literalI Cast to string Implicitly print- \$\begingroup\$verbose code in charcoal?\$\endgroup\$Fmbalbuena– Fmbalbuena2021-12-13 15:42:47 +00:00CommentedDec 13, 2021 at 15:42
- \$\begingroup\$@Fmbalbuena Correct me if I'm wrongNeil, but I don't think this program is possible in Verbose Charcoal, since it only has explicit inputs. So it would be
Cast(Evaluate(StringInput()))orCast(Evaluate(q)), which would actually beIVSorIVθinstead ofIV. Unless there is a way for implicit inputs in Verbose Charcoal I'm unaware of, perhaps with a certain flag-argument?\$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2021-12-13 15:53:09 +00:00CommentedDec 13, 2021 at 15:53 - 1\$\begingroup\$Ha! Right language for the job. :)\$\endgroup\$DLosc– DLosc2021-12-13 17:04:15 +00:00CommentedDec 13, 2021 at 17:04
- 1\$\begingroup\$@KevinCruijssen Technically
Cast Evaluatewill parse in Verbose mode, but it doesn't really read like normal Verbose Charcoal, so I don't bother in such a case.\$\endgroup\$Neil– Neil2021-12-13 19:40:25 +00:00CommentedDec 13, 2021 at 19:40
Python 3.8 (pre-release),46 50 bytes
-1 thanks to ovs!
lambda x:int(x.translate('%7d 45678923'%10*999))Helper program to generate this:helper
- \$\begingroup\$you need an
int()around the whole thing\$\endgroup\$Razetime– Razetime2021-12-14 08:16:37 +00:00CommentedDec 14, 2021 at 8:16 - \$\begingroup\$@Razetime other people are outputting as a string so I thought that was alright maybe\$\endgroup\$rak1507– rak15072021-12-14 10:04:54 +00:00CommentedDec 14, 2021 at 10:04
- \$\begingroup\$You can save a byte with
'%7d 45678923'%10*999. But with according to the new test case you will have to addint(...).\$\endgroup\$ovs– ovs2021-12-14 17:22:56 +00:00CommentedDec 14, 2021 at 17:22 - \$\begingroup\$@ovs nice, clever\$\endgroup\$rak1507– rak15072021-12-15 08:36:03 +00:00CommentedDec 15, 2021 at 8:36
Raku,141011 8 bytes
+1 and +4 to remove leading 0's.
-3 bytes thanks toMoonchild!
+~*.NFKDRaku-p,1519 bytes
-1 byte and -1 flag thanks toJo King!
$_=+S:g{.}=$/.EVALRaku uses superscript digits as exponents, but single digits on their own evaluate to their numeric value.
- \$\begingroup\$Can you not then take the 2-log of 2 with that exponent?\$\endgroup\$Adám– Adám2021-12-14 09:46:57 +00:00CommentedDec 14, 2021 at 9:46
- \$\begingroup\$@Adám Yes that would work, but I think I still need
EVAL, and the shortest I can do is{log2 EVAL 2~$_}at 16 bytes (Doesn't work on TIO because of old version)\$\endgroup\$ovs– ovs2021-12-14 10:08:32 +00:00CommentedDec 14, 2021 at 10:08 - \$\begingroup\$@Adám that would be shorter with removing leading zeros, but as it is not working on TIO and returns
Inffor the largest case, I'm not going to include it.{-1+chars EVAL 10~$_}would fix those issues, but is longer again.\$\endgroup\$ovs– ovs2021-12-14 10:52:18 +00:00CommentedDec 14, 2021 at 10:52 - 1\$\begingroup\$I think you can shave off a couple of characters with
+~*.NFKD\$\endgroup\$Moonchild– Moonchild2021-12-16 05:16:03 +00:00CommentedDec 16, 2021 at 5:16 - 1\$\begingroup\$You can use
$/instead of$<>andEVALas a method to remove that extra flag. I really wish you could doS:g{.}.=&EVALthough\$\endgroup\$Jo King– Jo King2022-01-30 12:54:16 +00:00CommentedJan 30, 2022 at 12:54
C (gcc),83\$\cdots\$60 46 bytes
n;f(int*s){for(n=0;*s;)n+=9*n+*s++%92%12;n=n;}Inputs a pointer to awchar_t string of superscript numbers.
Returns the normal number.
Discovered then % 92 % 12 formula independently.
JavaScript,656261 60 bytes
s=>s.replace(/./g,x=>-~'¹²³⁴⁵⁶⁷⁸⁹'.search(x))Saved 3 bytes thanks toNeil.
Saved 1 byte thanks toShaggy.
Saved 1 byte thanks toArnauld.
- 1\$\begingroup\$
s=>s.replace(/./g,x=>'⁰¹²³⁴⁵⁶⁷⁸⁹'.indexOf(x))saves 3 bytes.\$\endgroup\$Neil– Neil2021-12-13 19:50:12 +00:00CommentedDec 13, 2021 at 19:50 - 1\$\begingroup\$Save another byte\$\endgroup\$Shaggy– Shaggy2021-12-13 21:49:37 +00:00CommentedDec 13, 2021 at 21:49
- 1\$\begingroup\$It should be safe to use
searchin place ofindexOf.\$\endgroup\$Arnauld– Arnauld2021-12-14 20:15:46 +00:00CommentedDec 14, 2021 at 20:15
- \$\begingroup\$I just commented for clarification that this was allowed before posting it lol, guess I got ninja'd. Sadge, since I already had the answer written up and everything.\$\endgroup\$Aaroneous Miller– Aaroneous Miller2021-12-13 18:50:07 +00:00CommentedDec 13, 2021 at 18:50
- \$\begingroup\$That being said, you can use the
Kflag in 2.4.1 to get 6 bytes:Try it Online!\$\endgroup\$Aaroneous Miller– Aaroneous Miller2021-12-13 18:51:09 +00:00CommentedDec 13, 2021 at 18:51 - \$\begingroup\$According to OP, I guess a list of digits is invalid, so here's a valid 7 bytes:Try it Online!\$\endgroup\$Aaroneous Miller– Aaroneous Miller2021-12-13 18:55:42 +00:00CommentedDec 13, 2021 at 18:55
- \$\begingroup\$^ This is invalid because list of digits is invalid. You can use lynn's formula though to keep the 7 bytes:
C:12%⋏ṅ\$\endgroup\$naffetS– naffetS2022-09-06 20:29:12 +00:00CommentedSep 6, 2022 at 20:29
Pip,9 12 bytes
+3 bytes to handle more stringent rules
+:A_%E#A_MJaExplanation
After some trial and error, I found a really nice property of these numbers' codepoints:
- ¹, ², and ³ have codepoints 185, 178, and 179. Mod 8 (or 4), these become 1, 2, and 3.
- The other superscript numbers have codepoints equal to the corresponding digit plus 8304. Mod 16 (or 12, or many other options), these become their respective digits.
So all we need to do is take the codepoint of each digit mod 8 if it's 3 digits and mod 16 if it's 4 digits...
a First command-line argument MJ Map this function to each character, joining the results together: A_ Codepoint of the character % Mod E 2 to the power of # Length of A_ Codepoint of the character+: Treat the resulting string as a number (removing leading 0s)- \$\begingroup\$if
unicodedatamodule is not bulitin then changePython 3toPython 3 + Unicodedata\$\endgroup\$Fmbalbuena– Fmbalbuena2021-12-13 14:11:16 +00:00CommentedDec 13, 2021 at 14:11 - 1\$\begingroup\$@Fmbalbuena I think it is builtin.\$\endgroup\$Alan Bagel– Alan Bagel2021-12-13 14:24:55 +00:00CommentedDec 13, 2021 at 14:24
05AB1E,11 8bytes
Ç92%12%J-3 bytes porting@Arnauld's JavaScript method (thanks@Neil for the heads up).
Add a trailingï (+1 byte) if you want to remove leading 0s..
Try it online orverify all test cases.
Explanation:
Ç # Convert the (implicit) input to a list of its codepoint-integers # e.g. "⁰¹²³⁴⁵⁶⁷⁸⁹" → [8304,185,178,179,8308,8309,8310,8311,8312,8313] 92% # Modulo-92 # → [24,1,86,87,28,29,30,31,32,33] 12% # Modulo-12 # → [0,1,2,3,4,5,6,7,8,9] J # Join the list together # → 0123456789 ï # (Optionally) Remove leading 0s by casting to an integer # → 123456789 # (after which the result is output implicitly)- \$\begingroup\$Apparently a list of digits isn't allowed?\$\endgroup\$Neil– Neil2021-12-13 19:53:14 +00:00CommentedDec 13, 2021 at 19:53
- \$\begingroup\$@Neil Ah ok.. Added a join after it, thanks for the heads up.\$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2021-12-13 21:32:28 +00:00CommentedDec 13, 2021 at 21:32
- 1\$\begingroup\$Also, everyone seems to be porting @Arnauld's answer which I assume would be
Ç92%12%Jin this case.\$\endgroup\$Neil– Neil2021-12-14 00:33:38 +00:00CommentedDec 14, 2021 at 0:33
Python 3,63 62 bytes
lambda n: int("".join([str("⁰¹²³⁴⁵⁶⁷⁸⁹".index(c))for c in n]))
Thanks @Fmbalbuena!
- 1\$\begingroup\$
lambda n:int("".join([str("⁰¹²³⁴⁵⁶⁷⁸⁹".index(c))for c in n]))-2 bytes\$\endgroup\$Fmbalbuena– Fmbalbuena2021-12-13 14:08:28 +00:00CommentedDec 13, 2021 at 14:08 - 1\$\begingroup\$
lambda n:int("".join(str("⁰¹²³⁴⁵⁶⁷⁸⁹".index(c))for c in n))will work, i think\$\endgroup\$Alan Bagel– Alan Bagel2021-12-13 14:08:56 +00:00CommentedDec 13, 2021 at 14:08
Retina 0.8.2, 19 bytes
T`⁰-⁹_¹²³`ddTry it online! Link includes test cases. Explanation:d is a shorthand for0123456789. As⁰-⁹ expands to⁰ⁱ⁴⁵⁶⁷⁸⁹, and assuming none ofⁱ will appear in the input, those supserscript digits will get mapped to ASCII digits, while the_¹²³ gets mapped to0123 (the remaining digits ind get ignored)._ is actually a placeholder on the LHS and does nothing except to skip0 on the RHS (on the RHS it causes the character on the LHS to be deleted).
C (gcc) with-funsigned-char, 63 bytes
This version takes a string of UTF-8 characters. If the leading byte isE2 then I mask the third byte with 15; otherwise I mask the second byte with 3. It's useful thatB9 (the trailing byte of ¹) is odd!
i;f(char*s){for(i=0;*s;s++)i=(*s++-226?*s&3:*++s&15)+i*10;i=i;}APL (dzaima/APL),16 15 bytes
−1 byte thanks to rak1509
Anonymous tacit prefix function. Requires 0-based indexing.
10⊥'⁰¹²³⁴⁵⁶⁷⁸'⍳10⊥ evaluate in base 10
'⁰¹²³⁴⁵⁶⁷⁸'⍳ the respectiveindices of the argument characters in this string (any character not in the string gets the first index beyond the end, i.e. 9)
APOL,46 27 bytes
I(j(ƒ(i %(%(↶(∋) 92) 12))))
Uses Arnauld's 92/12 solution.
Note: 3 bytes can be saved by removing theI instruction, however this version is not case 5-compliant.
Explanation
I( Cast to integer j( Join string (default is no seperator) ƒ( List-builder for loop (returns a list of every returned value of the passed instruction) i Get input %( Modulo 2 (12) %( Modulo 1 (92) ↶( Get codepoint ∋ The current item in the for loop ) 92 ) 12 ) ) ))Implicit printOld code (uses lookup table instead):
v(0 []);f(i a(0 t(⌕("⁰¹²³⁴⁵⁶⁷⁸⁹" ∋))));I(j(⁰))
Nibbles,6.5 6 bytes (12 nibbles)
`@~.$`&%$12A port ofLynn's Jelly answer comes outhalf-a-byte a byte shorter inNibbles.
. # Map over $ # the input string: `& # bitwise-and of $ # each element % # modulo 12 # twelve # and itself`@~ # Finally, convert from base-10 digits- 1\$\begingroup\$Java wins longest builtin again?\$\endgroup\$Neil– Neil2021-12-13 19:46:40 +00:00CommentedDec 13, 2021 at 19:46
Husk,10 9 bytes
dm(Sn%12cPort ofArnauld's Javascript answerLynn's Jelly answer.
The 'charcode-to-charcode'modulo-except-when-it's-185 approach is discouragingly long inHusk, but Lynn's 'charcode-to-digit' modulo-AND approach is very well-suited toHusk'sd (digits to decimal number) function.
Turing Machine Code, 99 bytes
* ⁰ 0 r ** ¹ 1 r ** ² 2 r ** ³ 3 r ** ⁴ 4 r ** ⁵ 5 r ** ⁶ 6 r ** ⁷ 7 r ** ⁸ 8 r ** ⁹ 9 r *- \$\begingroup\$Doesn't work for the last testcase.\$\endgroup\$pajonk– pajonk2021-12-14 13:10:58 +00:00CommentedDec 14, 2021 at 13:10
- 1
Lua, 94 bytes
r=0 t={[185]=1,[178]=2,[179]=3}for _,c in utf8.codes(...)do r=r*10+(t[c]or c-8304)end print(r)Excel,72 63 bytes
Saved 9 bytes thanks toJvdV
=--CONCAT(MOD(MOD(UNICODE(MID(A1,SEQUENCE(LEN(A1)),1)),92),12))Not very exciting, really. It's the Excel version ofArnauld's answer.
MID(A1,SEQUENCE(LEN(A1)),1)pulls each character from the input one at a time.MOD(MOD(UNICODE(MID(~)),92),12))converts that character to unicode and then takes the mod 92 and mod 12.--CONCAT(MOD(~))combines all those code values into a string and then coerces that string into a number by making it negative twice (so back to positive again) which also will drop any leading zeros.
- \$\begingroup\$Using
CONCAT()and the double unary can save a few bytes here:=--CONCAT(MOD(MOD(UNICODE(MID(A1,SEQUENCE(LEN(A1)),1)),92),12))== 63 bytes.\$\endgroup\$JvdV– JvdV2022-09-05 14:44:07 +00:00CommentedSep 5, 2022 at 14:44
Explore related questions
See similar questions with these tags.
















