41
\$\begingroup\$

Given a single letter from A to Z (except J and K) as input, output the smallest non-negative integer containing that letter in its written form. Assume numbers never contain the word "and", so 101 is "one hundred one", not "one hundred and one". Assume American (short-scale) counting, so one million is 10^6 and one billion is 10^9.

a 1000                           one thousand
b 1000000000                     one billion
c 1000000000000000000000000000   one octillion
d 100                            one hundred
e 0                              zero
f 4                              four
g 8                              eight
h 3                              three
i 5                              five
j 
k
l 11                             eleven
m 1000000                        one million
n 1                              one
o 0                              zero
p 1000000000000000000000000      one septillion
q 1000000000000000               one quadrillion
r 0                              zero
s 6                              six
t 2                              two
u 4                              four
v 5                              five
w 2                              two
x 6                              six
y 20                             twenty
z 0                              zero

J and K are not part of the input specification, so your behavior is undefined for them. Given one of the above letters, output the (decimal) number next to it. You can take input in lowercase or uppercase, but you cannot require that some inputs are lowercase and others uppercase.

This is , so the shortest answer in bytes wins.

\$\endgroup\$
  • 11
    \$\begingroup\$ I'm not quite sure why this challenge has been downvoted so much? As far as I can see, it's clear and on-topic. Sure it's most likely to simply be encoding each letter to its corresponding number, but I don't think that justifies 3 downvotes? \$\endgroup\$ – caird coinheringaahing Sep 3 at 15:39
  • 2
    \$\begingroup\$ @Jonah added, thanks for the feedback \$\endgroup\$ – Stephen Sep 3 at 20:56
  • 10
    \$\begingroup\$ You're saying "one bajillion" isn't a real number? \$\endgroup\$ – Jo King Sep 3 at 22:17
  • 2
    \$\begingroup\$ @JoKing what's it's decimal representation? :) \$\endgroup\$ – Stephen Sep 3 at 22:35
  • 8
    \$\begingroup\$ I reckon that a StackExchange user who thinks that k can't appear in a number's name is rather lakhing in imagination. \$\endgroup\$ – Andrew Grimm Sep 4 at 5:01

17 Answers 17

16
\$\begingroup\$

JavaScript (Node.js),  78 75 74  73 bytes

c=>(n=([x]=Buffer(c+'8>P7 $(#%  +;! MD &"$%"&4 '))[x-96]-53)<0?n+21:10**n

Try it online!

How?

Each value is encoded with a single printable character. We use the ASCII range \$[32..52]\$ to encode \$n-32\$ and the range \$[53..80]\$ to encode \$10^{n-53}\$.

Commented

c =>                                   // c = input character
  ( n =                                //
    ( [x] =                            // let x be the 1st byte of the
        Buffer(                        // buffer made of:
          c +                          //   c followed by
          '8>P7 $(#%  +;! MD &"$%"&4 ' //   the encoded values
        )                              //
    )[x - 96]                          // let n be the encoded value corresponding to c
    - 53                               // minus 53
  ) < 0 ?                              // if n is negative:
    n + 21                             //   return n + 21
  :                                    // else:
    10 ** n                            //   return 10 ** n
\$\endgroup\$
7
\$\begingroup\$

Ruby, 65 bytes

->n{i="CI[B?;7<:??4F>?XO?9=;:=9+?"[n.ord-65].ord-64;i>0?10**i:~i}

Try it online!

Improvements inspired by GB's comment.

Ruby, 70 bytes

->n{i="ci}b@DHCE@@KfA@xo@FBDEBFT@"[n.ord-65].ord;i>96?10**(i-96):i-64}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 67 bytes \$\endgroup\$ – G B Sep 3 at 19:45
  • \$\begingroup\$ @GB thanks, the idea of having negative values for i was useful. Now down to 65. \$\endgroup\$ – Level River St Sep 4 at 18:14
6
\$\begingroup\$

///, 125 bytes

/:/\/\///T/000:d/100:a/d0:m/aT:b/aTT:q/bTT:p/qTTT:c/pT:e/0:f/4:g/8:h/3:i/5:l/11:n/1:o/0:r/0:s/6:t/2:u/4:v/5:w/2:x/6:y/20:z/0/

Try it online!

Input is appended to the end of the code, as per I/O meta. The footer in the above TIO link tests all letters simultaneously, as a single newline-delimited string, but the code also works just fine when inputting a single character.

\$\endgroup\$
6
\$\begingroup\$

Wolfram Language (Mathematica), 50 bytes

Letters b, c, m, p and q are skipped for performance reasons when testing on TIO.

(i=0;While[1>StringCount[IntegerName@i,#],i++];i)&

Try it online!

\$\endgroup\$
6
\$\begingroup\$

Stax, 33 bytes

º░¡µ?Äz*B╥╪╩ΓoΣ4ù↓|♂5%⌡ÿΩ²┼h{☻4O└

Run and debug it

Procedure:

  1. Extract codepoint from input.
  2. Index into constant array [3, 5, 7, 9, 11, -6, 1, 0, -24, -15, 0, 6, 2, 4, 5, 2, 6, 20, 0, -3, -9, -27, -2, 0, 4, 8] using codepoint. (with wrap-around)
  3. If the result is negative, negate and raise 10 to that power, otherwise leave as-is.
\$\endgroup\$
6
\$\begingroup\$

Excel, 85 bytes

=CHOOSE(CODE(A1)-96,1E3,1E9,1E27,100,,4,8,3,5,,,11,1E6,1,,1E24,1E15,,6,2,4,5,2,6,20,)

2 Golfy bits:

  • Using exponents (e.g. 1E15) saves 26 bytes.
  • Default of CHOOSE when nothing is provided is 0, saves 4 bytes
\$\endgroup\$
4
\$\begingroup\$

05AB1E, 36 bytes

•—ßusδtθ}™-5„©‘öæH•57в₆-sÇ`èD0‹iÄ°

Port of @recursive's Stax answer.
Input in lowercase.

Try it online or verify all test cases.

Explanation:

•—ßusδtθ}™-5„©‘öæH• # Push compressed integer 3133432551338094772548436198140408157771728287
 57в                  # Converted to base-57 as list: [39,41,43,45,47,30,37,36,12,21,36,42,38,40,41,38,42,56,36,33,27,9,34,36,40,44]
    ₆-                # Subtract 36 from each: [3,5,7,9,11,-6,1,0,-24,-15,0,6,2,4,5,2,6,20,0,-3,-9,-27,-2,0,4,8]
      sÇ`             # Swap to take the input, and convert it to its unicode value
         è            # Index it into the list (with automatic wraparound)
          D0‹i        # Create a copy, and if this is negative:
              Ä       #  Take the absolute value
               °      #  And then take 10 the power this value
                      # (implicitly output the top of the stack as result)

See this 05AB1E tip of mine (section How to compress large integers? and How to compress integer lists?) to understand why •—ßusδtθ}™-5„©‘öæH• is 3133432551338094772548436198140408157771728287 and •—ßusδtθ}™-5„©‘öæH•57в is [39,41,43,45,47,30,37,36,12,21,36,42,38,40,41,38,42,56,36,33,27,9,34,36,40,44].

\$\endgroup\$
  • 2
    \$\begingroup\$ 32. 05AB1E should not lose to Stax! \$\endgroup\$ – Grimy Sep 4 at 15:41
3
\$\begingroup\$

Perl 5 -p, 84 bytes

$_=(1e3,1e9,1e27,100,0,4,8,3,5,1,1,11,1e6,1,0,1e24,1e15,0,6,2,4,5,2,6,20,0)[-65+ord]

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 72 bytes using lookup table from @JoKing's Perl 6 solution \$\endgroup\$ – Nahuel Fouilleul Sep 4 at 10:32
3
\$\begingroup\$

Python 3, 103 bytes

lambda x:(1000,10**9,10**27,100,0,4,8,3,5,0,0,11,10**6,1,0,10**24,10**15,0,6,2,4,5,2,6,20,0)[ord(x)-97]

Try it online!

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

C# (Visual C# Interactive Compiler), 77 74 68 bytes

x=>((x="1‘Ʊ!  aƁñ"[x-65])&15)*Math.Pow(10,x>>4)

Encodes each letter's output as \$a \times 10^b\$, where \$a\$ is represented by the lowest 4 bits of the character, while \$b\$ is represented by the next five bits after that. Uses capital letters.

Commented

x=>                                                 //Lambda taking in a char
  (x=                                         )     //Re-assign x to
     "1‘Ʊ!   aƁñ"[x-65]      //The character's value at index x-65
 (                                             &15) //Bitwise AND by 15                                
  * Math.Pow(10,x>>4)                               // Multiplied by 10**(floor(x/16))

Try it online!

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

Perl 6, 67 bytes

{/\-/??10**-$_!!$_}o{'`ZHacgkfhccn]dcKTciegheiwc'.ords[.ord-97]-99}

Try it online!

Uses a lookup table where a negative number means that is the negative of the exponent, otherwise it is the number itself.

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

05AB1E, 32 bytes

•н“вüQ;æ¡ζæÀÛß%aÜ×₃t•56вsCè2‰`i°

Try it online!

•н“вüQ;æ¡ζæÀÛß%aÜ×₃t•56в  # compressed list:
# [31, 0, 12, 4, 8, 10, 4, 12, 40, 0, 7, 19, 55, 5, 0, 8, 16, 6, 10, 1, 1, 22, 13, 2, 0, 49]

s                         # swap so the input is at the top
 C                        # parse input as "binary" (a -> 36, b -> 37, ...)
  è                       # index (wraps around)
   2‰                     # divmod 2: [n / 2, n % 2]
     `                    # dump both on the stack
      i                   # if the modulo is 1:
       °                  #  10 ** the quotient
                          # implicit output
\$\endgroup\$
  • \$\begingroup\$ I still don't get that 'binary' builtin, haha. xD But I guess it can be useful like this sometimes. ;) \$\endgroup\$ – Kevin Cruijssen Sep 4 at 17:00
2
\$\begingroup\$

Bash, 129 100 bytes

A=xDVw04835zzbA10SJ0624526k0
c=$[64#${A:$[64#$1-10]:1}]
[ $c -gt 30 ]&&printf 1%0$[c-30].0f||echo $c

Try it online!

Try it online!

How it works:

A=xDVw04835zzbA10SJ0624526k0

$A: Base64 encoded "a"-"z": numbers less than 100 are stored directly. Larger numbers are encoded as the number of zeros +30. (ex: 1,000 = 33, 100 = 32, etc.)

c=$[64#${A:$[64#$1-10]:1}]

Extract one letter from $A at position specified in argument $1 (base64 decoded, -10 to account for offset of 'a'). Base64 decode that character and store in c.

[ $c -gt 30 ]&&printf 1%0$[c-30].0f||echo $c

If $c is greater than 30, print "1" padded with $c-30 zeros. Else, print $c.

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

Sledgehammer, 17 bytes

Technically, this is 133 bits long, but that doesn't really make this 16.625 bytes, as the compressor claims.

⣜⢍⢞⢹⡱⡋⣽⡱⡆⢺⢦⡽⡐⡌⢗⠈⣵

This probably decodes to

x1 = Input[]; x2 = 0; While[StringFreeQ[IntegerName[x2], x1], x2++]; Print[x2]

(approximately the same as my Mathematica answer), although I barely got it encoded (it appears as though my PC has compatibility issues with everything), so good luck decoding it again to check. I might have made some mistakes while using the encoder, so be careful.

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

Jelly, 36 bytes

Oị“[@ịẆþĊ`o&÷ḲḞṘḂỊP¥t’b48¤_⁹⁵*ɗ¹>?20

Try it online!

A monadic link taking a lower case letter as its argument and returning an integer. Returns 0 for j and k.

Explanation

O                       | Convert to code point
 ị          ¤           | Index into following as a nilad (wraps around):
  “[...t’               | - Integer 5370441668223940717846370165240010583188867 (stored base 250)
         b48            | - Convert to base 48
                 ɗ >?20 | If >20, following as a dyad using 20 as right argument:
             _⁹         | - Subtract right argument (20)
               ⁵*       | - 10 to the power of this
                  ¹     | Else: leave unchanged (identity function)
\$\endgroup\$
1
\$\begingroup\$

Retina 0.8.2, 89 bytes

^
$'
T`l`111104835__111011062452620`^.
T`abcm\pq`139285
\d$
$*0$&$*0$&$*0
d
00
T`\lyl`10_

Try it online! Link includes test cases. Explanation:

^
$'

Duplicate the input.

T`l`111104835__111011062452620`^.

Change the first copy to the (first) digit of the relevant result.

T`abcm\pq`139285

If the number has a multiple of 3 trailing zeros, get that multiple now.

\d$
$*0$&$*0$&$*0

And actually convert it into the relevant number of trailing zeros. (Note that this would simplify to *3*0 in Retina 1.)

d
00

Fix up d.

T`\lyl`10_

Fix up l and y and remove any remaining letters.

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

PHP, 104 bytes

<?=A<($a='^FX]0483500GC10UL0624526P0'[ord($argn)-97])?20==($b=ord($a)%30)||11==$b?$b:str_pad(1,$b,0):$a;

Try it online!

I have a string ^FX]0483500GC10UL0624526P0 which holds a single character for every input letter from "a" to "z". I extract this character based on input and store it in $a. If the character is not a digit, its ASCII code mod 30 will be stored in $b.

If $a is a digit, same digit is printed, this is used for any input that needs an output between 0 and 9 (like "e", "f", etc).

Else if $b is 20 or 11, same number is printed, this is used for "l" and "y".

Else, digit "1" padded to $b with "0"s is printed. For example for input of "a", the character is "^" which has an ASCII code of 94. 94 % 30 = 4 and "1" padded to 4 with "0"s will be "1000".

\$\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.