First Number to Contain Each Letter

41

2

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.

Stephen

Posted 2019-09-03T15:08:33.953

Reputation: 12 293

11I'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? – caird coinheringaahing – 2019-09-03T15:39:03.030

1didn't downvote, but fwiw even though it's strictly not required, it would have helped for clarity to list the names of the numbers. it's nothing more than a kolmogoriv-complexity / compression problem, but somehow that's not immediately clear from the description. input domain: a..z. output: defined by this table. actual ## sections like that would also help. – Jonah – 2019-09-03T20:48:59.493

2@Jonah added, thanks for the feedback – Stephen – 2019-09-03T20:56:37.307

11You're saying "one bajillion" isn't a real number? – Jo King – 2019-09-03T22:17:03.377

2@JoKing what's it's decimal representation? :) – Stephen – 2019-09-03T22:35:35.203

1It would also be interesting with any combination of letters. – Eric Duminil – 2019-09-04T03:02:57.290

@EricDuminil feel free to ask a separate question! – Stephen – 2019-09-04T03:03:38.310

1

"Googolpleiji" - J - 3^3^9. "Killillion" - K - 10^(3*10^3000+3) From here.

– Jonah – 2019-09-04T04:24:15.383

8I reckon that a StackExchange user who thinks that k can't appear in a number's name is rather lakhing in imagination. – Andrew Grimm – 2019-09-04T05:01:14.827

1@AndrewGrimm well, I don't want Mathematica builtins to be completely useless in a sea of compression answers – Stephen – 2019-09-04T05:03:33.317

1@Jonah, Googolpleiji is not the smallest for j : almost there's Googovij, and for k : Integral-dekapetaseptemdecile from the same page – Nahuel Fouilleul – 2019-09-04T10:41:00.613

I think Googolpleiji and Killillion might be a little bit challenging to output. – James – 2019-09-04T21:16:54.627

For j the output could be sqrt(-1). – rafa11111 – 2019-09-05T00:21:40.837

@Jonah "Googovij" (105,413,504) is shorter – Embodiment of Ignorance – 2019-09-05T02:57:04.600

2Integer Sequence A050933 – Paused until further notice. – 2019-09-05T21:25:30.117

Answers

16

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

Arnauld

Posted 2019-09-03T15:08:33.953

Reputation: 111 334

7

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!

Level River St

Posted 2019-09-03T15:08:33.953

Reputation: 22 049

67 bytes – G B – 2019-09-03T19:45:34.750

@GB thanks, the idea of having negative values for i was useful. Now down to 65. – Level River St – 2019-09-04T18:14:18.290

6

///, 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.

negative seven

Posted 2019-09-03T15:08:33.953

Reputation: 1 931

6

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!

my pronoun is monicareinstate

Posted 2019-09-03T15:08:33.953

Reputation: 3 111

41 bytes – attinat – 2019-09-04T01:04:06.243

6

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.

recursive

Posted 2019-09-03T15:08:33.953

Reputation: 8 616

6

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

Wernisch

Posted 2019-09-03T15:08:33.953

Reputation: 2 534

4

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].

Kevin Cruijssen

Posted 2019-09-03T15:08:33.953

Reputation: 67 575

232. 05AB1E should not lose to Stax! – Grimmy – 2019-09-04T15:41:36.113

3

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!

Embodiment of Ignorance

Posted 2019-09-03T15:08:33.953

Reputation: 7 014

3

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!

Xcali

Posted 2019-09-03T15:08:33.953

Reputation: 7 671

72 bytes using lookup table from @JoKing's Perl 6 solution – Nahuel Fouilleul – 2019-09-04T10:32:46.930

3

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!

Jitse

Posted 2019-09-03T15:08:33.953

Reputation: 3 566

2

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.

Jo King

Posted 2019-09-03T15:08:33.953

Reputation: 38 234

2

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

Grimmy

Posted 2019-09-03T15:08:33.953

Reputation: 12 521

I still don't get that 'binary' builtin, haha. xD But I guess it can be useful like this sometimes. ;) – Kevin Cruijssen – 2019-09-04T17:00:21.467

2

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.

spuck

Posted 2019-09-03T15:08:33.953

Reputation: 649

2

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.

my pronoun is monicareinstate

Posted 2019-09-03T15:08:33.953

Reputation: 3 111

1

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)

Nick Kennedy

Posted 2019-09-03T15:08:33.953

Reputation: 11 829

1

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.

Neil

Posted 2019-09-03T15:08:33.953

Reputation: 95 035

1

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".

Night2

Posted 2019-09-03T15:08:33.953

Reputation: 5 484