Digits from words
Convert a number in words to digits.
Input
- A number in words from
onetoninety-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.
Vyxal 3, 93 bytes ``` "◌Ṫ£ …
4mo ago
Japt, 449 bytes Naïve solut …
4mo ago
Perl, 127 bytes Reads strin …
4mo ago
Compressed Lua, 214 200 bytes …
4mo ago
Vyxal, 6 bytes ``` ∆ċ?=)ṅ …
4mo ago
5 answers
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-dhtydvty-vty-vty- dvty-sdvty-fidvty-fdvty-dvty-tdvty-dvtydxty-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ÝÆdd³dlddnidgÝs¹dfdhdt`qd Ôa!øU
Try it (includes all test cases)
0 comment threads
Vyxal 3, 93 bytes
"◌Ṫ£ZN≊◌∪0⑤①_¿0∑wæ↻∥”⌈:"⑵3oẄ↻∑Ṭ▦]ƶÞ⇩Πf%Ww%⎋”⌈:£X'-¨j7ʁ9×↜⎀"W③j?boδyjrKϾ⑷Dᑂ0⎄↯⇄℈t⎘ ★µ”⌈$J¥$JF›
"◌Ṫ£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.
0 comment threads
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
'
0 comment threads
Vyxal, 6 bytes
∆ċ?=)ṅ
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.
1 comment thread
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.)

0 comment threads