Rules: no spoilers.
The other rules are made up as we go along.
Share code by link to a forge, home page, pastebin (Eric Wastl has one here) or code section in a comment.
Rules: no spoilers.
The other rules are made up as we go along.
Share code by link to a forge, home page, pastebin (Eric Wastl has one here) or code section in a comment.
I made one solution in Python, and another in Scheme. Here are the regex-related parts.
Python:
spoiler
Here is a regex:·
The leading
?=
is a lookahead assertion. If we can read any of the subexpressionsone
,two
,…, it produces the empty string as a match, with the actual matched string as a capturing group. Python’sre
module has afindall
function which finds all non-overlapping matches in a string … but since it’s the matches (not the capturing groups) that have to be non-overlapping, and lookahead produces empty strings as matches, we havere.findall(rx,"twone") == ["two","one"]
andre.findall(rx,"sevenineight") == ["seven","nine","eight"]
. Empty string doesn’t overlap!So here’s the core of the solution:
Where
numeralize
is a simple function that converts “eight” into “8”. Not to the number 8, since we want to do string concatenation on them rather than addition. :-)I haven’t written any Perl in ages, so I have no idea if Perl has an analogue to
findall
.Scheme:
spoiler
…but I do know that Scheme, at least CHICKEN, doesn’t. Here is the best my currently badly fever-addled brain could come up with:
I’m using the
irregex
library. I start by building an irregex out of a SRE (Scheme Regular Expression):This matches from the beginning of the string (hence
bos
), and doesn’t use any fancy lookahead. It only matches one string.Here is a little recursive function that finds all matches using the above:
It simply tries making a match from the beginning, and then continues making one from a substring with the first character hacked off, until it ends up with the empty string. Not exactly my proudest Scheme code, but it works.
Using that, the analogue of the solution I did for the Python version is:
Again, not my proudest Scheme code! I am currently nursing a fever and trying not to sneeze and cough my lungs to pieces.
fuck yes scheme. you might have just inspired me to write some Lisp Machine Lisp solutions, since I might need a Lisp Machine codebase to test one of my side projects
I have a toy Lisp implemenation I used to work on, but I’ve lost the motivation.
Not that I think one more little virtual machine project makes a material difference either way, but I really don’t feel like spending my free time writing nontrivial code for loathsome AI companies.
Re Perl findall, I used this regex in my “clean” solution which I didn’t post because I figure it’s more honest to submit what worked, not the smart stuff you found out later.
regex
# find all numerals and number in English from the string $line and put them into the array @spelled my @spelled = ( $line =~ (m/(?=(\d{1}|one|two|three|four|five|six|seven|eight|nine))/g );
Nice!
I like your condensed solve method, yay for concise code