75
\$\begingroup\$

Task

Given a string of English names of digits “collapsed” together, like this:

zeronineoneoneeighttwoseventhreesixfourtwofive

Split the string back into digits:

zero nine one one eight two seven three six four two five

Rules

  • The input is always a string. It always consists of one or more lowercase English digit names, collapsed together, and nothing else.

    • The English digit names are zero one two three four five six seven eight nine.
  • The output may be a list of strings, or a new string where the digits are delimited by non-alphabetic, non-empty strings. (Your output may also optionally have such strings at the beginning or end, and the delimiters need not be consistent. So even something like {{ zero0one$$two ); is a valid (if absurd) answer for zeroonetwo.)

  • The shortest answer in bytes wins.

Test cases

three -> three
eightsix -> eight six
fivefourseven -> five four seven
ninethreesixthree -> nine three six three
foursixeighttwofive -> four six eight two five
fivethreefivesixthreenineonesevenoneeight -> five three five six three nine one seven one eight
threesevensevensixninenineninefiveeighttwofiveeightsixthreeeight -> three seven seven six nine nine nine five eight two five eight six three eight
zeroonetwothreefourfivesixseveneightnine -> zero one two three four five six seven eight nine
\$\endgroup\$
11
  • 28
    \$\begingroup\$ This is an excellent challenge! The task is extremely easy to understand and verify, but the right approach to use isn't very obvious. And choosing the right approach could make a huge difference in score. +1 :) \$\endgroup\$
    – DJMcMayhem
    Nov 20 '17 at 17:27
  • 1
    \$\begingroup\$ After thinking this up, I remembered a similar, but more simplistic challenge on anarchy golf: yesno! It sparked some amazing C answers. I hope to see one of those soon :) \$\endgroup\$
    – Lynn
    Nov 20 '17 at 17:41
  • \$\begingroup\$ I do not think my C answer qualifies as such, but hopefully it's a starting point for others with a more twisted sense of humor than myself. \$\endgroup\$ Nov 20 '17 at 20:29
  • \$\begingroup\$ I'm pretty sure I've seen this same challenge, but where you're supposed to print the actual number. I'm almost certain it was also posted by, you, Lynn; but I've lost the link, hook me up with it? \$\endgroup\$ Nov 20 '17 at 22:21
  • 3
    \$\begingroup\$ @MichaelDorgan (or any other C coders), you may want to have a look at the algorithm I used in my Befunge answer. A straight conversion of that to C got me a 104 byte solution, which I think beats all of the existing C answers. I'm willing to bet that could be improved upon by someone with more C golfing skills. \$\endgroup\$ Nov 21 '17 at 3:06

36 Answers 36

1
2
0
\$\begingroup\$

GNU sed, 35 bytes

(including +1 for the -r flag)

s/([ots]|[zfn].|(se|th|ei).)../ &/g

Just a simple regexp replacement.

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

Ruby, 33 bytes

->s{s.scan(/..[eox]|[tse]?..../)}

Try it online!

(Everybody else is doing it, so why can't we?)

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

Perl5, 26 bytes

echo zeronineoneoneeighttwoseventhreesixfourtwofive \
| perl -ple 's/..[eox]|[tse]?..../$& /g'

Or just the program:

s/..[eox]|[tse]?..../$& /g
\$\endgroup\$
0
\$\begingroup\$

Deorst, 22 bytes

'..[eox]|[tse]?....'gf

Try it online!

Of course, this uses the regex found by Uriel. Although, it’s great when Deorst beats Pyth and Jelly :P

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

Matlab, 432 bytes

A long attempt.

s=char(input('','s'));o=[];while length(s)>0;switch s(1:2);case'ze';o=[o,' ',s(1:4)];s(1:4)=[];case'on';o=[o,' ',s(1:3)];s(1:3)=[];case'tw';o=[o,' ',s(1:3)];s(1:3)=[];case 'th';o=[o,' ',s(1:5)];s(1:5)=[];case'fo';o=[o,' ',s(1:4)];s(1:4)=[];case'fi';o=[o,' ',s(1:4)];s(1:4)=[];case 'si';o=[o,' ',s(1:3)];s(1:3)=[];case'se';o=[o,' ',s(1:5)];s(1:5)=[];case 'ei';o=[o,' ',s(1:5)];s(1:5)=[];otherwise;o=[o,' ',s(1:4)];s(1:4)=[];end;end;o

ungolfed:

s=char(input('','s'))
o=[]
while length(s)>0
switch s(1:2)
    case 'ze'
        o=[o,' ',s(1:4)]
        s(1:4)=[]
    case 'on'
        o=[o,' ',s(1:3)]
        s(1:3)=[]
    case 'tw'
        o=[o,' ',s(1:3)]
        s(1:3)=[]
    case 'th'
        o=[o,' ',s(1:5)]
        s(1:5)=[]
    case 'fo'
        o=[o,' ',s(1:4)]
        s(1:4)=[]
    case 'fi'
        o=[o,' ',s(1:4)]
        s(1:4)=[]
    case 'si'
        o=[o,' ',s(1:3)]
        s(1:3)=[]
    case 'se'
        o=[o,' ',s(1:5)]
        s(1:5)=[]
    case 'ei'
        o=[o,' ',s(1:5)]
        s(1:5)=[]
    otherwise
        o=[o,' ',s(1:4)]
        s(1:4)=[]
end
end
o
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$
    – H.PWiz
    Nov 26 '17 at 21:08
  • \$\begingroup\$ Thanks! First golf attempt, so not that great. But, I didn't see any other Matlab! \$\endgroup\$ Nov 26 '17 at 21:20
0
\$\begingroup\$

Ruby, 77 bytes

puts gets.gsub(/(one|two|three|four|five|six|seven|eight|nine)/,' \1 ').strip

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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