Search text for a prefix and list all its suffixes in the text

17

1

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.

DrQuarius

Posted 2018-07-16T12:06:46.970

Reputation: 562

2To 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? – sundar - Reinstate Monica – 2018-07-16T12:11:27.850

"Prefix" here means ONLY if preceded by a space (or the start of the input).

So the "1" in "He1in" is ignored, and in "l1j2i", just as the "frac" in "diffraction" is ignored. – DrQuarius – 2018-07-16T12:16:23.383

2How can the https://www.astrotheme.com/astrology/ can be a prefix when it's preceded by href="? – Neil – 2018-07-16T12:19:26.660

Thank you Neil, that's very well spotted. I will have to add spaces. Thanks for noting. – DrQuarius – 2018-07-16T12:20:11.717

1May the suffix be empty? – user202729 – 2018-07-16T13:07:23.487

Empty suffix is fine, whether that leaves an empty slot in the output etc. It's also fine to ignore it. – DrQuarius – 2018-07-16T13:38:51.900

Also can there be two consecutive whitespaces (empty word) in the text? – user202729 – 2018-07-16T13:48:13.583

1I'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). – Jonathan Allan – 2018-07-16T19:02:40.980

1-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. – sundar - Reinstate Monica – 2018-07-16T20:00:42.067

Yes, the whitespace thing is a clear misstep by me for specifying 'space', I am happy for answers to do either. For the sake of the contest, any code that can solve my test cases is valid so things like "inputs with consecutive spaces" etc are fine since my test cases don't do that anyway. – DrQuarius – 2018-07-17T04:37:06.623

Answers

5

R, 63 bytes

function(s,p,z=el(strsplit(s,' ')))sub(p,'',z[startsWith(z,p)])

Try it online!

The positive-lookbehind implementation is unfortunately 5 bytes longer due to the huge regmatches/gregexpr combination :

function(s,p)regmatches(s,gregexpr(paste0('(?<=',p,')[^ ]*'),s,,T))

digEmAll

Posted 2018-07-16T12:06:46.970

Reputation: 4 599

2

A naive sub(grep()) is slightly better than the lookbehind at 66, but still doesn't encroach on the startsWith(). I don't see much room for improvement here without a change of approach. Try it online!

– CriminallyVulgar – 2018-07-17T14:06:05.357

4

Jelly, 12 bytes

Ḳfṛ"€¥Ḋ€ṫ€L}

Try it online!

Erik the Outgolfer

Posted 2018-07-16T12:06:46.970

Reputation: 38 134

A work of art. :') Will be using this to parse html into lists, thank you so much. :) – DrQuarius – 2018-07-16T13:44:22.287

2

@DrQuarius Regex cannot parse HTML, and so is Jelly.

– user202729 – 2018-07-16T14:30:32.093

Seems to work ok to me. Not sure why?

– DrQuarius – 2018-07-17T04:45:35.310

1@DrQuarius It's a famous joke, and user202729 extended it. – Erik the Outgolfer – 2018-07-17T11:25:11.007

4

C (gcc), 113 109 106 105 bytes

-4 bytes thanks to @LambdaBeta!
-3 bytes thanks to @WindmillCookies!

i;f(char*s,char*t){for(i=strlen(s);*t;t++)if(!strncmp(t,s,i))for(t+=i,puts("");*t^32&&*t;)putchar(*t++);}

Try it online!

betseg

Posted 2018-07-16T12:06:46.970

Reputation: 8 493

1You can save 4 bytes by removing both ^0's. Just ;*t; and &&*t; – LambdaBeta – 2018-07-16T14:21:26.983

@LambdaBeta thanks! I missed that. – betseg – 2018-07-16T14:24:00.957

1

I was able to get it down to 107 using a different strategy, sorry :)

– LambdaBeta – 2018-07-16T14:48:21.687

@LambdaBeta I actually thought of that method but I didn't think it would've been shorter than the solution that I posted. Nice answer, upvoted. – betseg – 2018-07-16T14:50:55.807

@WindmillCookies I don't know what you did since you linked the same program as my answer but I got 106 using puts(), thanks! – betseg – 2018-07-17T09:14:59.160

ok, seems like I forgot to get the permalink, lol – Windmill Cookies – 2018-07-17T09:21:01.477

103 bytes – ceilingcat – 2019-10-09T01:19:55.273

4

Python 2, 57 56 bytes

lambda i,j:[w[len(i):]for w in j.split()if w.find(i)==0]

Try it online!

-1 with thanks to @mypetlion

ElPedro

Posted 2018-07-16T12:06:46.970

Reputation: 5 301

2lambda i,j:[w[len(i):]for w in j.split()if w.find(i)==0] for -1 byte – mypetlion – 2018-07-16T15:40:06.927

4

Japt, 9 bytes

8 bytes if we can take input as an array of words.

¸kbV msVl
¸         // Shorthand for `qS`, split into words.
 kbV      // Filter the words, selecting only those that start with the prefix.
     msVl // For each remaining word, remove prefix length chars from the start.

Try it online!

Nit

Posted 2018-07-16T12:06:46.970

Reputation: 2 667

Very nice, but doesn't seem to work for the last test case. Might be due to quotation marks inside the string? or new lines?

– DrQuarius – 2018-07-17T04:52:07.200

@DrQuarius Your last test case is faulty, is it not? All the strings you're looking for are in the middle of the words (surrounded by url('')), none of them are at the start. – Nit – 2018-07-17T08:00:29.603

3

Retina, 31 bytes

L`(?<=^\2¶(.|¶)*([^ ¶]+))[^ ¶]+

Try it online! First line should be the desired prefix, the rest is the input text. Does not remove duplicates. Would be 25 bytes if any white space was a valid seprator. Explanation: We want to list the suffixes of valid prefixes. The [^ ¶]+ matches the suffix itself. The prefix of the regexp is a lookbehind that ensures that the prefix of the suffix is the input prefix. As a lookbehind is evaluated right-to-left, this starts by matching the prefix (using the same pattern but inside ()s to capture it), then any characters, before finally matching the prefix on its own line at the beginning of the input.

Neil

Posted 2018-07-16T12:06:46.970

Reputation: 95 035

White space meaning spaces and/or line breaks? I think that's a valid solution if so, but to be fair to all I will leave the problem as stated. – DrQuarius – 2018-07-16T13:40:50.213

@DrQuarius No, any white space includes tabs, formfeeds and even ellipses.

– Neil – 2018-07-16T14:19:26.550

Retina was the first language that came to mind when I saw the post (though I don't know the language yet). I thought it would be shorter though. Could I bother you for an explanation? For eg. the docs say is a newline character, but I can't figure out why so many are needed here. – sundar - Reinstate Monica – 2018-07-16T16:12:30.470

@sundar Sorry I was in a bit of a rush at the time. The first ensures that the whole first line is matched to the prefix. The second is needed because it isn't known how many intermediate lines there are. The last two s work the same way - negated character classes normally include newlines but we don't want that here. – Neil – 2018-07-16T18:48:16.077

No problem, thanks for adding it in. "normally include newlines but we don't want that here" <- If I understand correctly, we do want that here. OP specifies strictly that only spaces count as separators, that prefixes begin at and suffixes end at spaces. So for eg. "dif\nfractional" shouldn't match for "frac" because the prefix comes after a newline, not a space. Similarly "fracture-\nrelated" should return suffix "ture-\nrelated". Which is good news here I think, because you can remove at least one , possibly more. – sundar - Reinstate Monica – 2018-07-16T19:05:50.813

3

Japt, 16 12 bytes

Port of Arnauld Answer

-4 bytes from @Shaggy

iS qS+V Å®¸g

iS                  Insert S value (S = " ") at beginning of first input (Implicit)
   q                split using
    S+V             S + Second input
        Å           slice 1
         ®          map
          ¸         split using S
           g        get first position

Try it online!

Luis felipe De jesus Munoz

Posted 2018-07-16T12:06:46.970

Reputation: 9 639

112 bytes – Shaggy – 2018-07-16T14:00:03.903

Should probably make mention that this is a port of Arnauld's solution. (Assuming it wasn't independently derived, of course) – Shaggy – 2018-07-16T14:17:55.553

@Shaggy Honestly I did not notice this was the same answer, anyway I'll give him credit. sorry – Luis felipe De jesus Munoz – 2018-07-16T14:22:48.210

There's a 9 byte solution if you want to give it a try. – Shaggy – 2018-07-16T15:45:18.267

@Shaggy Did you mean this or did you have something different in mind?

– Nit – 2018-07-16T21:51:11.033

Exactly it (only I'd used a instead of b). Nice one :) – Shaggy – 2018-07-16T21:53:16.217

3

05AB1E, 11 bytes

#ʒηså}εsgF¦

Try it online! (here is a demo for multiline strings)

How does it work?

#ʒηså}εsgF¦    Full program.
#              Split the first input by spaces.
 ʒ   }         Filter the words by ...
  ηså          ... "Does the second input occur in the prefixed of the word?"
      ε        And for each valid word
       sg      Retrieve the length of the of the second input.
         F¦    And drop the first character of the word that number of times.

Mr. Xcoder

Posted 2018-07-16T12:06:46.970

Reputation: 39 774

:) Very nice, thanks for the multiline demo! I think that was causing issues for other programs. – DrQuarius – 2018-07-17T04:55:41.143

3

Stax, 8 bytes

·B¬╤²*6&

Run and debug it

Explanation:

j{x:[fmx|- Full program, implicit input: On stack in order, 1st input in X register
j          Split string on spaces
 {   f     Filter:
  x:[        Is X a prefix?
      m    Map passing elements:
       x|-   Remove all characters in X the first time they occur in the element
             Implicit output

I could also use x%t (length of X, trim from left), which is equally long but packs to 9 bytes.

wastl

Posted 2018-07-16T12:06:46.970

Reputation: 3 089

Beautiful. :) I think this might be the winner. Most of the lowest byte score contenders haven't been able to parse the third test case. :) – DrQuarius – 2018-07-17T04:54:41.573

Ahhh... but I see how you've done it now, you had to let the program know that the quotation marks in the string were not part of the program. I think that's fine. Also, yours is still the shortest regardless. :) – DrQuarius – 2018-07-17T04:58:01.547

3

Brachylog, 24 21 bytes

tṇ₁W&h;Wz{tR&h;.cR∧}ˢ

Try it online!

Could have been a few bytes shorter if there was variable sharing with inline predicates.

Input is an array with the prefix as the first element and the text as the second element.

tṇ₁W                    % Split the text at spaces, call that W
    &h;Wz               % Zip the prefix with each word, to give a list of pairs
         {         }ˢ   % Select the outputs where this predicate succeeds:
          tR            % Call the current word R
            &h;.c       % The prefix and the output concatenated
                 R      % should be R
                  ∧     % (No more constraints on output)

sundar - Reinstate Monica

Posted 2018-07-16T12:06:46.970

Reputation: 5 296

2

IBM/Lotus Notes Formula, 54 bytes

c:=@Explode(b);@Trim(@If(@Begins(c;a);@Right(c;a);""))

Takes it's input from two fields named a and b. Works because Formula will recursively apply a function to a list without the need for a @For loop.

No TIO available so here's a screenshot:

enter image description here

ElPedro

Posted 2018-07-16T12:06:46.970

Reputation: 5 301

2

APL (Dyalog Unicode), 23 bytesSBCS

Full program. Prompts for text and prefix from stdin. Prints list to stdout.

(5⌽'(\w+)\b',⎕)⎕S'\1'⊢⎕

Try it online!

 prompt (for text)

 yield that (separates '\1' from )

()⎕S'\1' PCRE Search and return list of capture group 1 from the following regex:

 prompt (for prefix)

'(\w+)\b', prepend this string (group of word characters followed by a word boundary)

5⌽ rotate the first 5 characters to the end; '\bPREFIX(\w+)'

Adám

Posted 2018-07-16T12:06:46.970

Reputation: 37 779

2

MATL, 17 bytes

Yb94ih'(.*)'h6&XX

Try it on MATL Online

How?

Yb - Split the input at spaces, place the results in a cell array

94 - ASCII code for ^ character

ih - Get the input (say "frac"), concatenate '^' and the input

'(.*)'h - Push the string '(.*)' into the stack, concatenate '^frac' and '(.*)'. So now we have '^frac(.*), a regex that matches "frac" at the beginning of the string and captures whatever comes after.

6&XX - Run regexp matching, with 6& specifying 'Tokens' mode i.e., the matched capture groups are returned instead of the entire match.

Implicitly output the results.

sundar - Reinstate Monica

Posted 2018-07-16T12:06:46.970

Reputation: 5 296

So that's what 'Tokens' does; good to know! – Luis Mendo – 2018-07-16T18:02:26.540

1Haha. I had no idea either, figured it out by trial and error for this answer. – sundar - Reinstate Monica – 2018-07-20T11:53:44.917

2

C (clang), 107 bytes

i;f(s,t,_)char*s,*t,*_;{i=strlen(s);_=strtok(t," ");while((strncmp(_,s,i)||puts(_+i))&&(_=strtok(0," ")));}

Try it online!

Description:

i;f(s,t,_)char*s,*t,*_;{   // F takes s and t and uses i (int) and s,t,u (char*)
    i=strlen(s);           // save strlen(s) in i
    _=strtok(t," ");       // set _ to the first word of t
    while(                 // while loop
        (strncmp(_,s,i)||  // short-circuited if (if _ doesn't match s to i places)
         puts(_+i))        // print _ starting at the i'th character
        &&                 // the previous expression always returns true
        (_=strtok(0," "))) // set _ to the next word of t
    ;                      // do nothing in the actual loop
}

Has to be clang because gcc segfaults without #include <string.h> due to strtok problems.

LambdaBeta

Posted 2018-07-16T12:06:46.970

Reputation: 2 499

100 bytes – ceilingcat – 2018-07-17T22:29:44.413

2

PowerShell 3.0, 60 62 59 bytes

param($p,$s)-split$s|%{if($_-cmatch"^$p(.*)"){$Matches[1]}}

Lost some bytes suppressing the cmatch output. Had a janky solution that gained some by purposely causing duplicates. But it also threw redlines if it didn't match on the first but that is not fine now that I think about it. +2 bytes to fix it though.

Veskah

Posted 2018-07-16T12:06:46.970

Reputation: 3 580

Solution with 60 bytes returns double answer in some cases king, tals, ted, tional, tional, tionally, tioned, tioned, tious, tostratic, tures,tures,tures, tures, as and show index error on He1in example. Powershell 5.1, 6.0.2. Solution with 62 bytes is Ok. – mazzy – 2018-07-18T07:28:02.257

1@mazzy I knew that, I was just abusing the "Duplicates is allowed" bit to have it return even more duplicates when it comes across a no-match and throw red on a no-match 1st iteration. – Veskah – 2018-07-18T19:56:10.637

2

Bash + grep, 20 bytes

grep -Po "\b$1\K\S*"

Prefix is given as a command-line parameter, and input text is piped in via stdin.

Try it online!

Digital Trauma

Posted 2018-07-16T12:06:46.970

Reputation: 64 644

1

JavaScript (Node.js), 64 59 bytes

-5 bytes from @Shaggy

a=>b=>b.match(eval(`/\\b${a}\\w*/g`)).map(x=>x.split(a)[1])

Try it online!

Luis felipe De jesus Munoz

Posted 2018-07-16T12:06:46.970

Reputation: 9 639

159 bytes – Shaggy – 2018-07-16T13:57:57.150

1

JavaScript (ES6), 57 bytes

Takes input in currying syntax (text)(prefix). Does not remove duplicates.

s=>p=>(' '+s).split(' '+p).slice(1).map(s=>s.split` `[0])

Try it online!

Arnauld

Posted 2018-07-16T12:06:46.970

Reputation: 111 334

1

Perl 6, 30 bytes

{$^t.comb: /[^|' ']$^p <(\S+/}

Test it

Expanded:

{  # bare block lambda with placeholder params $p, $t

  $^t.comb:    # find all the substrings that match the following
  /
    [ ^ | ' ' ] # beginning of string or space
    $^p        # match the prefix
    <(         # don't include anything before this
    \S+        # one or more non-space characters (suffix)
  /
}

Brad Gilbert b2gills

Posted 2018-07-16T12:06:46.970

Reputation: 12 713

@sundar fixed​ ​ – Brad Gilbert b2gills – 2018-07-16T18:25:15.610

You seem to have an extra space between 'p' and '<' btw. – sundar - Reinstate Monica – 2018-07-16T18:39:15.000

@sundar The space between p and <( is necessary as otherwise it may be seen as $v<…> which is short for $v{qw '…'}. – Brad Gilbert b2gills – 2018-07-17T06:25:14.047

1Seems to work without it though, at least in this case. – sundar - Reinstate Monica – 2018-07-17T07:33:10.870

1@sundar Technically it just warns, but I don't like writing code that warns when it is only one byte different than code that doesn't warn. – Brad Gilbert b2gills – 2018-07-18T15:23:46.883

Ah, I can understand that. – sundar - Reinstate Monica – 2018-07-18T15:57:58.963

1

Perl 5 with -asE, 23 22 21 bytes (?)

say/^$b(.*)/ for@F

Try it online!

Can be run as a commandline one-liner as perl -asE 'say/^$b(.*)/ for@F' -- -b=frac -, or with a filename in place of the last -.
Or from a script file, say perl -as -M5.010 script.pl -b=frac - (thanks to @Brad Gilbert b2gills for the TIO link demonstrating this).

The code itself is 18 bytes, I added 3 bytes for the -b= option which assigns its value (the prefix input) to a variable named $b in the code. That felt like an exception to the usual "flags aren't counted" consensus.

-a splits each input line at spaces and places the result in the array @F. -s is a shortcut way of assigning a command-line argument as a variable, by giving a name on the command-line. Here the argument is -b=frac, which places the prefix "frac" in a variable $b.

/^$b(.*)/ - Matches the value of $b at the beginning of the string. .* is whatever comes after that, until the end of the word, and the surrounding parantheses capture this value. The captured values are automatically returned, to be printed by say. Iterating through space-separated words with for @F means we don't have to check for initial or final spaces.

sundar - Reinstate Monica

Posted 2018-07-16T12:06:46.970

Reputation: 5 296

1

Jelly,  11  9 bytes

Ḳœṣ€ḢÐḟj€

A dyadic link accepting the text (a list of characters) on the left and the prefix (a list of characters) on the right which yields a list of lists of characters (the resulting suffixes).

Try it online! (footer joins with spaces to avoid full-program's implicit smashing)
Note: I added three edge cases to the string in the OP - unfrackled and nofracfracheremate to the beginning, which should not output and fracfracit to the end which should output fracit.

How?

Ḳœṣ€ḢÐḟj€ - Link: text, prefix                        e.g. "fracfracit unfracked", "frac"
Ḳ         - split (text) at spaces -> list of words        ["fracfracit", "unfracked"]
   €      - for each (word):
 œṣ       -   split around sublists equal to (prefix)       ["","","it"]  ["un","ked"]
     Ðḟ   - filter discard items for which this is truthy:
    Ḣ     -   head
          -   -- Crucially this modifies the list:             ["","it"]       ["ked"]
          -   -- and yields the popped item:                 ""            "un"
          -   -- and only non-empty lists are truthy:       kept          discarded
          -            ...so we end up with the list:      [["","it"]]
        € - for each (remaining list of lists of characters):
       j  -   join with the prefix                          "fracit"                                             
          -                                                ["fracit"]

previous 11 byter:

Ḳs€L}Ḣ⁼¥ƇẎ€

Also a dyadic link as above.

Try it online!

Jonathan Allan

Posted 2018-07-16T12:06:46.970

Reputation: 67 804

1

Haskell, 51 bytes

p#x=[length p`drop`y|y<-words x,and$zipWith(==)p y]

Try it online!

ბიმო

Posted 2018-07-16T12:06:46.970

Reputation: 15 345

1

Husk, 11 bytes

Pretty much just a port of the Haskell answer:

m↓L⁰foΠz=⁰w

Try it online!

Explanation

m↓L⁰f(Πz=⁰)w  -- prefix is explicit argument ⁰, the other one implicit. eg: ⁰ = "ab" and implicit "abc def"
           w  -- words: ["abc","def"]
    f(    )   -- filter by (example w/ "abc"
       z=⁰    -- | zip ⁰ and element with equality: [1,1]
      Π       -- | product: 1
              -- : ["abc"]
m             -- map the following
 ↓            -- | drop n elements
  L⁰          -- | n being the length of ⁰ (2)
              -- : ["c"]

ბიმო

Posted 2018-07-16T12:06:46.970

Reputation: 15 345

1

Java 10, 94 bytes

p->s->{for(var w:s.split(" "))if(w.startsWith(p))System.out.println(w.substring(p.length()));}

Try it online here.

Ungolfed:

p -> s -> { // lambda taking prefix and text as Strings in currying syntax
    for(var w:s.split(" ")) // split the String into words (delimited by a space); for each word ...
        if(w.startsWith(p)) //  ... test whether p is a prefix ...
            System.out.println(w.substring(p.length())); // ... if it is, output the suffix
}

O.O.Balance

Posted 2018-07-16T12:06:46.970

Reputation: 1 499

1

Small Basic, 242 bytes

A Script that takes no input and outputs to the TextWindow Object

c=TextWindow.Read()
s=TextWindow.Read()
i=1
While i>0
i=Text.GetIndexOf(s," ")
w=Text.GetSubText(s,1,i)
If Text.StartsWith(w,c)Then
TextWindow.WriteLine(Text.GetSubTextToEnd(w,Text.GetLength(c)+1))
EndIf
s=Text.GetSubTextToEnd(s,i+1)
EndWhile

Try it at SmallBasic.com! Requires IE/Silverlight

Taylor Scott

Posted 2018-07-16T12:06:46.970

Reputation: 6 709

1

Python 2, 53 bytes

lambda i,j:[w.split()[0]for w in j.split(i)if len(w)]

Try it online!

Raphaël Côté

Posted 2018-07-16T12:06:46.970

Reputation: 81

Interesting idea but fails for the first test case, input: "1", "He1in aosl 1ll j21j 1lj2j 1lj2 1ll l1j2i" – Chas Brown – 2018-07-18T02:20:17.770

Right, I'll try to fiddle with it. I'm sure I'm onto something... – Raphaël Côté – 2018-07-18T20:23:09.700

1

Brachylog, 12 bytes

hṇ₁∋R&t;.cR∧

Try it online!

Takes input as [text, prefix] through the input variable, and generates each word through the output variable. This was originally sundar's answer, which I started trying to golf after reading that it "could have been a few bytes shorter if there was variable sharing with inline predicates", which is possible now. Turns out that generator output saves even more bytes.

    R           R
   ∋            is an element of
h               the first element of
                the input
 ṇ₁             split on spaces,
     &          and the input
      t         's last element
         c      concatenated
       ;        with
        .       the output variable
          R     is R
           ∧    (which is not necessarily equal to the output).

My first two attempts at golfing it down, using fairly new features of the language:

With the global variables that had been hoped for: hA⁰&tṇ₁{∧A⁰;.c?∧}ˢ (18 bytes)

With the apply-to-head metapredicate: ṇ₁ᵗz{tR&h;.cR∧}ˢ (16 bytes)

And my original solution:

Brachylog, 15 bytes

ṇ₁ʰlᵗ↙X⟨∋a₀⟩b↙X

Try it online!

Same I/O. This is essentially a generator for words with the prefix, ṇ₁ʰ⟨∋a₀⟩, modified to remove the prefix.

                   The input variable
  ʰ                with its first element replaced with itself
ṇ₁                 split on spaces
    ᵗ              has a last element
   l               the length of which
     ↙X            is X,
       ⟨   ⟩       and the output from the sandwich
       ⟨∋  ⟩       is an element of the first element of the modified input
       ⟨ a₀⟩       and has the last element of the input as a prefix.
                   The output variable
       ⟨   ⟩       is the output from the sandwich
            b      with a number of characters removed from the beginning
             ↙X    equal to X.

A very different predicate with the same byte count:

Brachylog, 15 bytes

hṇ₁∋~c₂Xh~t?∧Xt

Try it online!

Same I/O.

   ∋               An element of
h                  the first element of
                   the input variable
 ṇ₁                split on spaces
    ~c             can be un-concatenated
      ₂            into a list of two strings
       X           which we'll call X.
        h          Its first element
         ~t        is the last element of
           ?       the input variable,
            ∧      and
             Xt    its last element is
                   the output variable.

Unrelated String

Posted 2018-07-16T12:06:46.970

Reputation: 5 300

0

Red, 62 bytes

func[p s][parse s[collect[any[p keep to[" "| end]| thru" "]]]]

Try it online!

Galen Ivanov

Posted 2018-07-16T12:06:46.970

Reputation: 13 815

0

Pyth, 21 20 18 17 16 bytes

AQVcH)IqxNG0:NG"

Try it online!

-1 by using V instead of FN because V implicitly sets N

-2 after some further reading about string slicing options

-1 using x to check for the presence of the substring at index 0

-1 using replace with "" for getting the end of the string

I'm sure this could use some serious golfing but as a Pyth beginner, just getting it to work was a bonus.

How does it work?

assign('Q',eval_input())
assign('[G,H]',Q)
for N in num_to_range(chop(H)):
    if equal(index(N,G),0):
        imp_print(at_slice(N,G,""))

ElPedro

Posted 2018-07-16T12:06:46.970

Reputation: 5 301

0

Excel VBA, 86 bytes

Takes input as prefix in [A1] and values in [B1] and outputs to the console.

For each w in Split([B1]):?IIf(Left(w,[Len(A1)])=[A1],Mid(w,[Len(A1)+1])+" ","");:Next

Taylor Scott

Posted 2018-07-16T12:06:46.970

Reputation: 6 709

0

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!

ErikF

Posted 2018-07-16T12:06:46.970

Reputation: 2 149

This version uses only 91 bytes with strtok, but only works with clang: Try it online!

– ErikF – 2018-07-18T06:21:56.253

You can ditch the char at the beginning. – ceilingcat – 2018-07-29T15:30:13.467