17
\$\begingroup\$

I use "suffix" loosely here to mean "any sub-string that follows the prefix".

"Prefix" here means the START of a word, where a word's start is defined as either after a space or from the first character of the input text (for the first word). A "prefix" in the middle of a word is ignored.

E.g. if your input prefix is "arm" and the input text is "Dumbledore's army was fully armed for the impending armageddon" then the output list contains (y, ed, ageddon).

Test Cases

Assume case-sensitive, strings end after spaces. Input will not start with a space.

Removing duplicates is optional.


Input prefix: "1"

Input text:

"He1in aosl 1ll j21j 1lj2j 1lj2 1ll l1j2i"

Output: (ll, lj2j, lj2) - in any permutation

Input prefix: "frac"

Input text: 

"fracking fractals fracted fractional currency fractionally fractioned into fractious fractostratic fractures causing quite a fracas"

Output: (king, tals, ted, tional, tionally, tioned, tious, tostratic, tures, as)

Input prefix: "href="https://www.astrotheme.com/astrology/"

Input text: 

"(div style="padding: 0; background: url('https://www.astrotheme.com/images/site/arrondi_450_hd.png') no-repeat; text-align: left; font-weight: bold; width: 450px; height: 36px")
  (div class="titreFiche" style="padding: 5px 0 0 6px")(a href="https://www.astrotheme.com/astrology/Nolwenn_Leroy" title="Nolwenn Leroy: Astrology, birth chart, horoscope and astrological portrait")Nolwenn Leroy(br /)
(/div)
  (div style="text-align: right; border-left: 1px solid #b2c1e2; border-right: 1px solid #b2c1e2; width: 446px; padding: 1px 1px 0; background: #eff8ff")
    (table style="width: 100%")(tr)(td style="width: 220px")
(div style="padding: 0; background: url('https://www.astrotheme.com/images/site/arrondi_450_hd.png') no-repeat; text-align: left; font-weight: bold; width: 450px; height: 36px")
  (div class="titreFiche" style="padding: 5px 0 0 6px")(a href="https://www.astrotheme.com/astrology/Kim_Kardashian" title="Kim Kardashian: Astrology, birth chart, horoscope and astrological portrait")Kim Kardashian(br /)(span style="font-weight: normal; font-size: 11px")Display her detailed horoscope and birth chart(/span)(/a)(/div)
(/div)
(div style="padding: 0; background: url('https://www.astrotheme.com/images/site/arrondi_450_hd.png') no-repeat; text-align: left; font-weight: bold; width: 450px; height: 36px")
  (div class="titreFiche" style="padding: 5px 0 0 6px")(a href="https://www.astrotheme.com/astrology/Julia_Roberts" title="Julia Roberts: Astrology, birth chart, horoscope and astrological portrait")Julia Roberts(br /)(span style="font-weight: normal; font-size: 11px")Display her detailed horoscope and birth chart(/span)(/a)(/div)
    (td id="cfcXkw9aycuj35h" style="text-align: right")
  (/div)"

Output: (Nolwenn_Leroy", Kim_Kardashian", Julia_Roberts")

The Winner

This is , so the fewest bytes wins. :)

Can accept the inputs in any way that works, as long as your code can solve arbitrary problems like the test cases.

\$\endgroup\$
10
  • 2
    \$\begingroup\$ To be clear, the prefix has to be at the beginning of a word? If the second test case had the word 'diffraction' in it, would that change the output? \$\endgroup\$
    – Sundar R
    Commented Jul 16, 2018 at 12:11
  • 2
    \$\begingroup\$ How can the https://www.astrotheme.com/astrology/ can be a prefix when it's preceded by href="? \$\endgroup\$
    – Neil
    Commented Jul 16, 2018 at 12:19
  • 1
    \$\begingroup\$ May the suffix be empty? \$\endgroup\$
    – user202729
    Commented Jul 16, 2018 at 13:07
  • 1
    \$\begingroup\$ I'd suggest allowing people to split on other white-space as well as spaces as a few seem to be doing so anyway. I'd also suggest saying there wont be multiple spaces in a row in the input (or somewhat equivalently that empty words may result in undefined behaviour). I suggest both these things since the main part of the challenge is not the splitting into words part (I would suggest just allowing a list of words or even just a word as input, but it's way too late now with 22 answers - something to note for future challenges though). \$\endgroup\$ Commented Jul 16, 2018 at 19:02
  • 1
    \$\begingroup\$ -1 to allowing splitting on other whitespaces now. It would make sense for the challenge to have been that originally, but changing now would split the answers into ones that do two different things. And this is not like the cases where some languages can't handle for eg. 64-bit numbers or something, here it just means implementing a slightly (possibly) more complex match, so it makes more sense to correct answers with wrong assumptions and perhaps add a test case to check for this too. \$\endgroup\$
    – Sundar R
    Commented Jul 16, 2018 at 20:00

31 Answers 31

1
2
0
\$\begingroup\$

C (gcc), 99 bytes

This uses strsep instead of strtok, saving a few bytes. Oddly, I had to include the function prototype for strsep (and strtok) to avoid segment faults when not including string.h.

char*strsep();w;f(s,t,u)char*s,*t,*u;{for(w=strlen(t);u=strsep(&s," ");)strncmp(u,t,w)||puts(u+w);}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ This version uses only 91 bytes with strtok, but only works with clang: Try it online! \$\endgroup\$
    – ErikF
    Commented Jul 18, 2018 at 6:21
  • \$\begingroup\$ You can ditch the char at the beginning. \$\endgroup\$
    – ceilingcat
    Commented Jul 29, 2018 at 15:30
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.