29
\$\begingroup\$

Convert a string containing digits as words into an integer, ignoring leading zeros.

Examples

  • "four two" -> 42.
  • "zero zero zero one" -> 1.

Assumptions

Submissions can assume that:

  1. The input string is comprised of space-separated digit words.
  2. All words are valid (in the range "zero".."nine") and lowercase. Behaviour for empty input is undefined.
  3. The input string always represents an unsigned number within the range of int and is never an empty string.

Scoring

Answers will be scored in bytes with fewer bytes being better.

\$\endgroup\$
13
  • 3
    \$\begingroup\$ Welcome to the site. There are a couple of things that we usually expect from questions that are missing here. The most important would be an objective scoring criterion which all challenges must have. \$\endgroup\$
    – Wheat Wizard
    Commented Sep 22, 2019 at 21:06
  • 3
    \$\begingroup\$ Aside from that this question is very sparse on specification. You should specify exactly what is required of submissions without ambiguity. One sentence and an example just isn't up to our clarity standards for challenges. \$\endgroup\$
    – Wheat Wizard
    Commented Sep 22, 2019 at 21:08
  • 3
    \$\begingroup\$ On top of what has been said already, we have a sandbox where users can post their challenges before posting them to main. That way you will miss less information when making posts. If you look at other recent posts on the site with a reasonably positive reception I think you will see that both your question and solution aren't quite in line with what we do here. \$\endgroup\$ Commented Sep 22, 2019 at 21:13
  • 3
    \$\begingroup\$ At the risk of being pedantic, I'd like to point out that the range "zero".."nine" is not fully specified. \$\endgroup\$ Commented Sep 22, 2019 at 21:22
  • 4
    \$\begingroup\$ Annoyingly, the builtin Interpreter@"SemanticNumber" does exactly this in Mathematica—except that it fails on strings starting with zero zero . \$\endgroup\$ Commented Sep 23, 2019 at 5:45

39 Answers 39

1
2
1
\$\begingroup\$

Vyxal, 63 bitsv2, 7.875 bytes

⌈ƛ₀ʁ∆ċḟ;ṅ⌊

Try it Online!

Bitstring:

110010010001010010110111001001001001010010011010110111110111010
⌈ƛ₀ʁ∆ċḟ;ṅ⌊­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏⁠‎⁡⁠⁢⁤‏⁠‏​⁡⁠⁡‌⁣​‎⁠⁠⁠⁠‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌­
⌈           # ‎⁡split on spaces
 ƛ     ;    # ‎⁢map over each 
      ḟ     # ‎⁣their index in the list
  ₀ʁ∆ċ      # ‎⁤0-9 as words
        ṅ⌊  # ‎⁢⁡convert back to number
💎

Created with the help of Luminespire.

\$\endgroup\$
0
\$\begingroup\$

BaCon, 83 72 bytes

Assuming the string is provided in w$, this code looks up the index in "zeontwthfofisiseeini" using a regular expression based on the unique first 2 characters of each word. The index is then divided by 2 providing the correct result.

FOR x$ IN w$:r=r*10+REGEX("zeontwthfofisiseeini",LEFT$(x$,2))/2:NEXT:?r
\$\endgroup\$
0
\$\begingroup\$

AWK, 76 64 bytes

More of the same, but with a pinch of AWK sparkling magic.

Edit: at first, I didn't looked for the answers, and my result was actually similar to some of them. I'm mesmerized by all these ingenious solutions.

{i=OFS=e;for(;NF>i++;)$i=index("othuvsein",substr($i$i$i,7,1))}1

Try it online!

{
i=OFS=e;     # We start setting the _i_ variable and the
             # Output Field Separator as the _e_ variable, which is
             # just empty, for never being assigned anything,
             # so _e_ is both evaluated as zero for _i_ and null for _OFS_
             # (thanks Brian Kernighan for that!)
for(;
    NF>i++;  # the looping goes on until _i_ reaches the Number of Fields (NF).
   )         # also, _i_ is incremented by 1 each turn.

     $i=                          # $i is each field text
        index("othuvsein",        # we get this magic Klingon and locate the
              substr($i$i$i,7,1)  # 7th letter of the input concatenated 3 times
             )                    # this has the same effect as looking
                                  # for the second to last letter on the word.
        
}
1            # finally, we print all the $i together
```
\$\endgroup\$
0
\$\begingroup\$

Perl 5 -p, 55 bytes

for$b(z,on,w,th,u,iv,x,v,g,ni){s/$b/@a/eg;$#a++}s/\D//g

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Bash (pure), 85 83 bytes

r=rothuvsein
for j in $@
do
s=$j$j$j
t=${r%${s:6:1}*}
((i=i*10+${#t}))
done
echo $i

Try it online!

Literally just the same as every other rothuvsein entry. Concatenates three times and gets it at index 6.

First version which uses no string concatenation: (85 bytes)

r=rothuvsein
for j in $@
do
t=${r%${j:$((6%${#j})):1}*}
((i=i*10+${#t}))
done
echo $i

Try it online!

Pass the string as command line arguments. Expects $i to be unset.

Ungolfed version:

# Default value is zero
val=0
# declare our lookup table
lut=rothuvsein
# loop through command line arguments
for word in $@
do
    # The index is at 6 % len(word) (see my Thumb submission)
    idx=$(( 6 % ${#word} ))
    # chr = word[6 % len(word)]
    chr=${word:$idx:1}
    # Find a substring using a modified technique from the linked SO post
    substr=${lut%$chr*}
    # Add the length to val * 10
    (( val = val * 10 + ${#substr} ))
done
# Echo the result
echo $val

How to check if a string contains a substring in Bash - Stack Overflow

\$\endgroup\$
0
\$\begingroup\$

Lua (110 bytes)


A translation to Lua of the rothuvsein trick, sorry. Note that we use 7 instead of 6 because Lua counts from 1, as sane humans do. 😉

function x(s)z=""for w in s:gmatch("%a+")do z=z..(("rothuvsein"):find(w:rep(3):sub(7,7))-1) end return z+0 end
\$\endgroup\$
0
\$\begingroup\$

Pyth, 21 20 bytes

ssm`x."evt8†À‘"@d6c

Try it online!

Translates literally to print(int("".join(map(lambda d: str("rothuvsein".index(d[6%len(d)])), eval(input()).split())))) in Python 3.

\$\endgroup\$
0
\$\begingroup\$

Kotlin, 64 bytes

{split(" ").fold(0){a,b->10*a+"rothuvsein".indexOf((b+b+b)[6])}}

Improving upon this answer.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Swift, 135 128 91 bytes

let f={Int(.init(($0+"").split{$0<"0"}.map{[_]("2839016547")[Int($0,radix:36)!%204%13]}))!}

This is pretty much a port of @Arnauld's JavaScript answer.

\$\endgroup\$
1
2

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.