3
\$\begingroup\$
  1. Given a word, get the indexes of each letter in the alphabet.

    For example: uncopyrightable becomes: [21, 14, 3, 15, 16, 25, 18, 9, 7, 8, 20, 1, 2, 12, 5]

  2. Convert these indexes to the given numeral system.

    For example, if the radix is 36, then the array above would become:
    ['l', 'e', '3', 'f', 'g', 'p', 'i', '9', '7', '8', 'k', '1', '2', 'c', '5']

  3. Joining those gives le3fgpi978k12c5.

Of course you don't have to use arrays.

More examples:

  • dermatoglyphics, 3645id1kf7cpg893j
  • undiscoverability, 825164112331726522121114112431
  • superacknowledgement, 1613151051213bef17c5475d5e14

  • Your input will be a word (you decide if it'll be uppercase, lowercase or mixed) and a valid radix (2-36).
  • You can receive input through any of the standard IO methods.
  • This is , so shortest code in bytes wins!

(very similar question here)

\$\endgroup\$
4
  • 2
    \$\begingroup\$ You should provide an alphabet for the base conversions. For example, does base 36 use 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ? What's the maximum base value? 64? \$\endgroup\$ Commented Dec 21, 2021 at 19:04
  • \$\begingroup\$ @caird the index of a letter is the position of the letter in the alphabet - it shoudn't be affected by the base value. \$\endgroup\$ Commented Dec 21, 2021 at 19:08
  • 1
    \$\begingroup\$ May the output be in uppercase as well? \$\endgroup\$ Commented Dec 22, 2021 at 11:14
  • \$\begingroup\$ The title says to find the sum of the indexes, but it seems like you just want us to concatenate them \$\endgroup\$ Commented Dec 22, 2021 at 13:52

11 Answers 11

5
\$\begingroup\$

05AB1E, 13 8 bytes

Ç96-IBlJ

-5 bytes thanks to @ovs.

With string and integer inputs; and string output. Could be 1 byte less if we can output in uppercase by removing the l.

Try it online or verify all test cases.

An alternative for Ç96- is Ask>, if we take the input as a character-list: Try it online or verify all test cases.

Explanation:

     # Step 1: Convert the (implicit) input to their 1-based alphabetical index:
Ç    #   Convert the (implicit) input-string to a list of codepoint integers
 96- #   Subtract 96 from each
     #  OR
A    #   Push the lowercase alphabet
 s   #   Swap so the (implicit) input-list is at the top of the stack
  k  #   Get the (0-based) index of each character in the alphabet
   > #   And increase each by 1 to make it a 1-based index

     # Step 2: Convert it to the base of the second input, and output:
IB   #  Convert it to the base of the second input
  l  #  Lowercase all letters, because `B` results in uppercase A-Z
   J #  And join the list together to a string
     #  (after which the result is output implicitly)
\$\endgroup\$
2
4
\$\begingroup\$

Charcoal, 9 bytes

⭆θ⍘⊕⌕βιIη

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

 θ          First input
⭆           Map over characters and join
    ⌕       0-indexed index of
      ι     Current character
     β      In lowercase alphabet
   ⊕        Incremented i.e. 1-indexed
  ⍘         Convert to string base
        η   Second input
       I    Cast to integer

When given an integer base, the string base conversion uses the appropriate prefix of 0-9a-zA-z.

\$\endgroup\$
4
\$\begingroup\$

Jelly, 9 bytes

ØaiⱮb‘ịØB

Try It Online!

-1 byte thanks to caird coinheringaahing

\$\endgroup\$
2
  • \$\begingroup\$ Obviously, +1 \$\to\$ for -1 byte \$\endgroup\$ Commented Dec 21, 2021 at 19:13
  • \$\begingroup\$ @cairdcoinheringaahing ah, right. I just wanted to get it over with and stop trying to use my broken site on my phone :P \$\endgroup\$
    – hyper-neutrino
    Commented Dec 21, 2021 at 20:57
4
\$\begingroup\$

Wolfram Language (Mathematica), 36 bytes

""<>LetterNumber@#~IntegerString~#2&

Try it online!

87-36 bytes from @att

\$\endgroup\$
2
  • \$\begingroup\$ IntegerString exists. \$\endgroup\$
    – att
    Commented Dec 21, 2021 at 23:34
  • \$\begingroup\$ 36 bytes since IntegerString is Listable \$\endgroup\$
    – att
    Commented Dec 23, 2021 at 5:27
2
\$\begingroup\$

JavaScript (Node.js), 53 bytes

Expects (string)(radix).

s=>r=>Buffer(s).map(c=>s+=(c%32).toString(r),s='')&&s

Try it online!

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

Husk, 20 bytes

mȯc+48?+7I>9ṁȯB⁰%32c

Try it online!

No easy 0-9A-Z built into Husk, so 12 bytes used to roll our own.

Main part: get the digit values in the given base

ṁȯ              # map and flatten across input string
       c        # get the character code
    %32         # modulo 32 (so A=1, B=2, ...)
  B⁰            # convert to digits in base given by input number

Helper function: convert digit values to 0,1,2,...9,A,B,C,...,Y,Z

mȯ              # map over all digit values
      ?   >9    # if it's bigger than 9
       +7       # add 7
         I      # otherwise leave unchanged;
   +48          # then add 48
  c             # and convert to corresponding ASCII character
\$\endgroup\$
1
\$\begingroup\$

Java, 73 bytes

s->r->s.chars().mapToObj(c->r.toString(c%32,r)).reduce("",String::concat)

Try it online!

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

Japt -m, 9 8 bytes

#`nUc)sV

Try it

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

R, 89 bytes

\(x,y)Reduce(paste0,sapply(el(strsplit(x,"")),\(z)cwhmisc::int2B(grep(z,letters),y))[1,])

With R < 4.1, \ needs to be replaced with function resulting in a total of 104 bytes.

-5 bytes thanks to @pajonk.

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Nice to see one more R golfer! You may find it useful to check the tips page for golfing in R: codegolf.stackexchange.com/questions/4024/tips-for-golfing-in-r (for example use el(...) instead of ...[[1]] or Reduce(paste0,...) instead of paste(...,collapse="")). It would also be nice to have a link to an online interpreter, so anyone can check the answer, see for example tio.run/#r. \$\endgroup\$
    – pajonk
    Commented Dec 23, 2021 at 13:17
0
\$\begingroup\$

Vyxal, 12 bytes

C96-kr⁰Ẏ$vτ∑

Try it Online!

A bit messy but it works.

C            #  Charcodes
 96-         # -96
    kr⁰Ẏ     # 0-9a-zA-Z sliced to input length
        $vτ  # Convert each to that base
           ∑ # Sum the result
\$\endgroup\$
0
\$\begingroup\$

C (gcc), 57 56 bytes

f(s,r)char*s;{for(;*s;*s+=*s<10?49:56,++s)*s-=65,*s%=r;}

Try it online!

Saved a byte thanks to ceilingcat!!!

Inputs an uppercase string.
Maps the string such that A -> 1, B -> 2, \$\dots\$, J -> A, K -> B, \$\dots\$ modulus radix \$r\$.

\$\endgroup\$
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.