Image

Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Digits from words

+4
−0

Convert a number in words to digits.

Input

  • A number in words from one to ninety-nine.

Output

  • The corresponding number in digits from 1 to 99.
  • This may be a string of digits or a number of that value (such as a float or an integer).

Test cases

Test cases are in the format input : output.

one : 1
two : 2
three : 3
four : 4
five : 5
six : 6
seven : 7
eight : 8
nine : 9
ten : 10
eleven : 11
twelve : 12
thirteen : 13
fourteen : 14
fifteen : 15
sixteen : 16
seventeen : 17
eighteen : 18
nineteen : 19
twenty : 20
twenty-one : 21
twenty-two : 22
twenty-three : 23
twenty-four : 24
twenty-five : 25
twenty-six : 26
twenty-seven : 27
twenty-eight : 28
twenty-nine : 29
thirty : 30
thirty-one : 31
thirty-two : 32
thirty-three : 33
thirty-four : 34
thirty-five : 35
thirty-six : 36
thirty-seven : 37
thirty-eight : 38
thirty-nine : 39
forty : 40
forty-one : 41
forty-two : 42
forty-three : 43
forty-four : 44
forty-five : 45
forty-six : 46
forty-seven : 47
forty-eight : 48
forty-nine : 49
fifty : 50
fifty-one : 51
fifty-two : 52
fifty-three : 53
fifty-four : 54
fifty-five : 55
fifty-six : 56
fifty-seven : 57
fifty-eight : 58
fifty-nine : 59
sixty : 60
sixty-one : 61
sixty-two : 62
sixty-three : 63
sixty-four : 64
sixty-five : 65
sixty-six : 66
sixty-seven : 67
sixty-eight : 68
sixty-nine : 69
seventy : 70
seventy-one : 71
seventy-two : 72
seventy-three : 73
seventy-four : 74
seventy-five : 75
seventy-six : 76
seventy-seven : 77
seventy-eight : 78
seventy-nine : 79
eighty : 80
eighty-one : 81
eighty-two : 82
eighty-three : 83
eighty-four : 84
eighty-five : 85
eighty-six : 86
eighty-seven : 87
eighty-eight : 88
eighty-nine : 89
ninety : 90
ninety-one : 91
ninety-two : 92
ninety-three : 93
ninety-four : 94
ninety-five : 95
ninety-six : 96
ninety-seven : 97
ninety-eight : 98
ninety-nine : 99

Scoring

This is a code golf challenge. Your score is the number of bytes in your code. Lowest score for each language wins.

Explanations are optional, but I'm more likely to upvote answers that have one.

History

0 comment threads

5 answers

+3
−0

Japt, 449 bytes

Naïve solution that finds the last 0-based index of an element that's contained in the input in a compressed array that's constructed by finding the shortest substring of each number that's not yet contained in the array. one is removed to save a byte, so the resulting index is then incremented by 2.

ÒÒ`ety-Ìy-À†y-Ðty-s¸ty-fÅÆy-f¸ty-¸ty-t¸ty-¸tydhty-˜hty-‚hty- dhty-sdhty-fidhty-fdhty-dhty-tdhty-dhtydvty-˜vty-‚vty- dvty-sdvty-fidvty-fdvty-dvty-tdvty-dvtydxty-˜xty-‚xty- dxty-sdxty-fidxty-fdxty-dxty-tdxty-dxtydfty-˜fty-‚fty- dfty-sdfty-fidfty-fdfty-dfty-tdfty-dftyܖy-Ìty-À’âD Jty-sܖy-fÅÒty-fܖy-Ü–y-tܖy-ܗrty-Ì6y-À¶y-Ðty-sdrty-fÅöy-fdrty-drty-tdrty-drtyd-˜-‚- d-sd-fid-fd-d-td-Ýhtd˛dxt¹fÝÆd–d³dld’dnidgÝs¹dfdhdt`qd Ôa!øU

Try it (includes all test cases)

History

0 comment threads

+3
−0

Vyxal 3, 93 bytes

"◌Ṫ£ZN≊◌∪0⑤①_¿0∑wæ↻∥”⌈:"⑵3oẄ↻∑Ṭ▦]ƶÞ⇩Πf%Ww%⎋”⌈:£X'-¨j7ʁ9×↜⎀"W③j?boδyjrKϾ⑷Dᑂ0⎄↯⇄℈t⎘ ★µ”⌈$J¥$JF›

Vyxal It Online!

"◌Ṫ£ZN≊◌∪0⑤①_¿0∑wæ↻∥”⌈:"⑵3oẄ↻∑Ṭ▦]ƶÞ⇩Πf%Ww%⎋”⌈:£X'-¨j7ʁ9×↜⎀"W③j?boδyjrKϾ⑷Dᑂ0⎄↯⇄℈t⎘ ★µ”⌈$J¥$JF›­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏⁠‎⁡⁠⁤⁢‏⁠‎⁡⁠⁤⁣‏⁠‎⁡⁠⁤⁤‏⁠‎⁡⁠⁢⁡⁡‏⁠‎⁡⁠⁢⁡⁢‏⁠‎⁡⁠⁢⁡⁣‏⁠‎⁡⁠⁢⁡⁤‏⁠‎⁡⁠⁢⁢⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢⁢⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁢⁣‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁢⁤‏⁠‎⁡⁠⁢⁣⁡‏⁠‎⁡⁠⁢⁣⁢‏⁠‎⁡⁠⁢⁣⁣‏⁠‎⁡⁠⁢⁣⁤‏⁠‎⁡⁠⁢⁤⁡‏⁠‎⁡⁠⁢⁤⁢‏⁠‎⁡⁠⁢⁤⁣‏⁠‎⁡⁠⁢⁤⁤‏⁠‎⁡⁠⁣⁡⁡‏⁠‎⁡⁠⁣⁡⁢‏⁠‎⁡⁠⁣⁡⁣‏⁠‎⁡⁠⁣⁡⁤‏⁠‎⁡⁠⁣⁢⁡‏⁠‎⁡⁠⁣⁢⁢‏⁠‎⁡⁠⁣⁢⁣‏⁠‎⁡⁠⁣⁢⁤‏⁠‎⁡⁠⁣⁣⁡‏⁠‎⁡⁠⁣⁣⁢‏⁠‎⁡⁠⁣⁣⁣‏⁠‎⁡⁠⁣⁣⁤‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁣⁤⁡‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁣⁤⁢‏⁠‎⁡⁠⁣⁤⁣‏‏​⁡⁠⁡‌⁢⁣​‎‎⁡⁠⁣⁤⁤‏‏​⁡⁠⁡‌⁢⁤​‎‎⁡⁠⁤⁡⁡‏⁠‎⁡⁠⁤⁡⁢‏⁠‎⁡⁠⁤⁡⁣‏⁠‎⁡⁠⁤⁡⁤‏‏​⁡⁠⁡‌⁣⁡​‎‏​⁢⁠⁡‌⁣⁢​‎‎⁡⁠⁤⁣⁡‏⁠‎⁡⁠⁤⁣⁢‏‏​⁡⁠⁡‌⁣⁣​‎‎⁡⁠⁤⁢⁡‏⁠‎⁡⁠⁤⁢⁢‏‏​⁡⁠⁡‌⁣⁤​‎‎⁡⁠⁤⁢⁣‏⁠‎⁡⁠⁤⁢⁤‏‏​⁡⁠⁡‌⁤⁡​‎‏​⁢⁠⁡‌⁤⁢​‎‎⁡⁠⁤⁣⁣‏⁠‎⁡⁠⁤⁣⁤‏⁠‎⁡⁠⁤⁤⁡‏⁠‎⁡⁠⁤⁤⁢‏⁠‎⁡⁠⁤⁤⁣‏⁠‎⁡⁠⁤⁤⁤‏⁠‎⁡⁠⁢⁡⁡⁡‏⁠‎⁡⁠⁢⁡⁡⁢‏⁠‎⁡⁠⁢⁡⁡⁣‏⁠‎⁡⁠⁢⁡⁡⁤‏⁠‎⁡⁠⁢⁡⁢⁡‏⁠‎⁡⁠⁢⁡⁢⁢‏⁠‎⁡⁠⁢⁡⁢⁣‏⁠‎⁡⁠⁢⁡⁢⁤‏⁠‎⁡⁠⁢⁡⁣⁡‏⁠‎⁡⁠⁢⁡⁣⁢‏⁠‎⁡⁠⁢⁡⁣⁣‏⁠‎⁡⁠⁢⁡⁣⁤‏⁠‎⁡⁠⁢⁡⁤⁡‏⁠‎⁡⁠⁢⁡⁤⁢‏⁠‎⁡⁠⁢⁡⁤⁣‏⁠‎⁡⁠⁢⁡⁤⁤‏⁠‎⁡⁠⁢⁢⁡⁡‏⁠‎⁡⁠⁢⁢⁡⁢‏⁠‎⁡⁠⁢⁢⁡⁣‏⁠‎⁡⁠⁢⁢⁡⁤‏⁠‎⁡⁠⁢⁢⁢⁡‏‏​⁡⁠⁡‌⁤⁣​‎‎⁡⁠⁢⁢⁢⁢‏‏​⁡⁠⁡‌⁤⁤​‎‎⁡⁠⁢⁢⁢⁣‏⁠‎⁡⁠⁢⁢⁢⁤‏‏​⁡⁠⁡‌⁢⁡⁡​‎‎⁡⁠⁢⁢⁣⁡‏⁠‎⁡⁠⁢⁢⁣⁢‏⁠‎⁡⁠⁢⁢⁣⁣‏‏​⁡⁠⁡‌⁢⁡⁢​‎‎⁡⁠⁢⁢⁣⁤‏‏​⁡⁠⁡‌⁢⁡⁣​‎‎⁡⁠⁢⁢⁤⁡‏‏​⁡⁠⁡‌­
"◌Ṫ£ZN≊◌∪0⑤①_¿0∑wæ↻∥”                                                                          # ‎⁡Push the string "twenty thirty forty fifty sixty seventy eighty ninety"
                     ⌈                                                                         # ‎⁢Split the string on spaces
                      :                                                                        # ‎⁣Duplicate
                       "⑵3oẄ↻∑Ṭ▦]ƶÞ⇩Πf%Ww%⎋”                                                   # ‎⁤Push the string "one two three four five six seven eight nine"
                                            ⌈                                                  # ‎⁢⁡Split the string on spaces
                                             :£                                                # ‎⁢⁢Put a copy in the register
                                               X                                               # ‎⁢⁣Cartesian product
                                                '-¨j                                           # ‎⁢⁤Join each element in the resulting list by "-"
# ‎⁣⁡We now have the list ["twenty-one", "twenty-two", ..., "thirty-one", "thirty-two", ..., "ninety-nine"]
                                                        ↜⎀                                     # ‎⁣⁢Insert the elements of the first list into the result using the following indices:
                                                    7ʁ                                         # ‎⁣⁣Range [0, 7]
                                                      9×                                       # ‎⁣⁤Multiply by 9
# ‎⁤⁡We now have the list ["twenty", "twenty-one", ..., "thirty", "thirty-one", ..., "ninety-nine"]
                                                          "W③j?boδyjrKϾ⑷Dᑂ0⎄↯⇄℈t⎘ ★µ”          # ‎⁤⁢Push the string "ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen"
                                                                                     ⌈         # ‎⁤⁣Split the string on spaces
                                                                                      $J       # ‎⁤⁤Prepend that to the list we're building
                                                                                        ¥$J    # ‎⁢⁡⁡Prepend the list in the register to the list we're building
                                                                                           F   # ‎⁢⁡⁢Get index of input in the list
                                                                                            ›  # ‎⁢⁡⁣Increment
💎

Created with the help of Luminespire.

History

0 comment threads

+2
−0

Perl, 127 bytes

Reads string on stdin; display appropriate integer on stdout

perl -nE'%h=@a=o1te10el11twel12th3t2fo4f5si6s7e8n9=~/\D+|\d+/g;$"="|";/^(@a)/;$a=$h{$1}+10*/een/;say/y$|-(@a)/?10*$a+$h{$1}:$a'
perl -nE'
    # split bareword into list of non-digits and digits
    # initialise regex alternation list @a from list
    # initialise mapping table %h from @a
    %h = @a = o1te10el11twel12th3t2fo4f5si6s7e8n9 =~ /\D+|\d+/g;

    # set array->string interpolation character for use in regex
    $"="|";

    /^(@a)/;        # get prefix of string
    $a = $h{$1}     # map it to number
       + 10*/een/;  # add ten if Xteen

    say                    # display result
        /y$|-(@a)/         #   if Xty or Xty-X
        ? 10*$a + $h{$1}   #      then scale up
        : $a               #   else as-is
'

Try it online!

History

0 comment threads

+2
−0

Vyxal, 6 bytes

∆ċ?=)ṅ

Try it Online!

Why go to all the trouble of building lists from dictionary compressed strings when I can just do it the easy™ way?

Explained

∆ċ?=)ṅ­⁡​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏‏​⁡⁠⁡‌­
    )ṅ  # ‎⁡First integer n where
∆ċ      # ‎⁢Converting that integer to words
  ?=    # ‎⁣Equals the input
💎

Created with the help of Luminespire.

History

1 comment thread

Can't blame me for not adding that builtin to the new version of the lang :P (2 comments)
+2
−0

Compressed Lua, 214 200 bytes

After redoing the code, it turns out that I can do it slightly shorter by optimizing for single letters. There're probably a few more bytes I can shave, but eh…

M=0∀k∈t:gmatch("[^-]+")⟨m=k:f("y$")∧10∨1 a=k:f("g")∧8∨k:f("h")∧3∨k:f("x")∧6∨k:f("u")∧4∨k:f("lv")∧12∨k:f("^o")∧1∨k:f("^f")∧5∨k:f("w")∧2∨k:f("e$")∧9∨k:f("^t")∧10∨k:f("s")∧7∨11 M=M+a*m⟩→t:f("een")∧10+M∨M

Behold its visage! (Sorry for the awful site design. And how long that link is. And the fact that I couldn't even put down all the test cases because I'm a lazy hack.)

History

0 comment threads

Sign up to answer this question »