25
\$\begingroup\$

Story

Have you seen this post from 9gag? Maybe you got the feeling to make your own sentences. But then you realize that you could just golf a script in half an hour, and you will never have to deal time with that.

The submission

Your program will get an input string which it will return with added quotation marks as explained below. Standard loopholes are forbidden. Output as a list of lines is allowed. Trailing spaces and empty lines that don't break the output are allowed.

The rules of input

  • The input only contains printable ASCII characters.
  • The input may contain spaces. The words are determined with them.
  • It's guaranteed that a space will never be followed by another space.
  • The case of no input or empty string doesn't matter.

The rules of output

If one word is given then the program has to return the string between quotation marks.

If the input string has 2 or more words, it first returns the initial input, but the first word is in quotation marks. Then on the next line, it returns the initial input, but with the second word in quotes. And so on for the remaining words.

In general, the program has to return as many lines as there are words in the input.

Examples:

test -> "test"

This is codegolf -> "This" is codegolf
                    This "is" codegolf
                    This is "codegolf"

This is a significantly longer, but not the longest testcase -> "This" is a significantly longer, but not the longest testcase
                                                                This "is" a significantly longer, but not the longest testcase
                                                                This is "a" significantly longer, but not the longest testcase
                                                                This is a "significantly" longer, but not the longest testcase
                                                                This is a significantly "longer," but not the longest testcase
                                                                This is a significantly longer, "but" not the longest testcase
                                                                This is a significantly longer, but "not" the longest testcase
                                                                This is a significantly longer, but not "the" longest testcase
                                                                This is a significantly longer, but not the "longest" testcase
                                                                This is a significantly longer, but not the longest "testcase"

Here is an another one -> "Here" is an another one
                          Here "is" an another one
                          Here is "an" another one
                          Here is an "another" one
                          Here is an another "one"

This is , so the least byte answer wins!

\$\endgroup\$

40 Answers 40

1
\$\begingroup\$

Perl 6, 46 bytes

{map {.prematch~"\"$_\""~.postmatch},m:g/\S+/}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ The match part can just be m:g/\S+/, and I think the pre/post match part can be shorter too \$\endgroup\$ – Jo King May 11 at 6:33
  • \$\begingroup\$ /\S/ doesn't match the tab character. I'm actually not sure how whitespace other than space should be treated. \$\endgroup\$ – bb94 May 11 at 6:47
  • \$\begingroup\$ The question says printable ASCII, which only includes spaces, not tabs or newlines \$\endgroup\$ – Jo King May 11 at 6:48
1
\$\begingroup\$

Red, 113 bytes

func[s][repeat i length? u: split s" "[j: 1
foreach w u[prin rejoin[t: pick[{"}""]i = j w t sp]j: j + 1]prin lf]]

Try it online!

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

APL+Win, 59 bytes

(⌽⍳⍴s)⊖((¯1 0+⍴s)⍴s)⍪(('"',¨s←(+\' '=s)⊂s←' ',⎕),¨'"')~¨' '

Prompts for string

Try it online! Courtesy of Dyalog Classic

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

Jelly, 15 bytes

⁾""j$€⁹¦K
ḲçⱮL$

Try it online!

Thanks to @JonathanAllan for saving 4 bytes

\$\endgroup\$
  • \$\begingroup\$ I'm pretty sure you need to count the Y unless you get the OP to allow a list of lines as function output. You can then save 4 like this (although mine is then just a one Link version of that). \$\endgroup\$ – Jonathan Allan May 11 at 11:07
  • \$\begingroup\$ @JonathanAllan it says a list of lines is ok as output. I’ve added in your improvements - hope that’s ok. Thanks. \$\endgroup\$ – Nick Kennedy May 11 at 11:15
  • \$\begingroup\$ Ah so it does! :) \$\endgroup\$ – Jonathan Allan May 11 at 11:54
1
\$\begingroup\$

Python 3, 107 bytes

import re
def f(s):
 for w in re.finditer('[^ ]+',s):p=w.span();print(f'{s[:p[0]]}"{w.group()}"{s[p[1]:]}')

Try it online!

I... decided to use regex against my better judgement. This is the result.

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

J, 45 bytes

   f =: ' 'joinstring"1(]`(dquote&.>])@.=($~$@=))@cut
   f 'this is a test string'
"this" is a test string
this "is" a test string
this is "a" test string
this is a "test" string
this is a test "string"

If the input can be a list of boxes, then my solution can be 30 bytes:

   f =: (]`(dquote&.>])@.=($~$@=))@cut
   f 'this is a test'
┌──────┬────┬───┬──────┐
│"this"│is  │a  │test  │
├──────┼────┼───┼──────┤
│this  │"is"│a  │test  │
├──────┼────┼───┼──────┤
│this  │is  │"a"│test  │
├──────┼────┼───┼──────┤
│this  │is  │a  │"test"│
└──────┴────┴───┴──────┘
\$\endgroup\$
  • \$\begingroup\$ +1 from me, but I think you need to make it a verb to comply with the PPCG rules: Try it online: \$\endgroup\$ – Galen Ivanov May 13 at 10:53
  • 1
    \$\begingroup\$ Ah I didn't realize that was a rule. Fixed. \$\endgroup\$ – hoosierEE May 13 at 12:58
1
\$\begingroup\$

Oracle SQL, 92 bytes

select regexp_replace(s,'(\S+)','"\1"',1,level)from t connect by level<regexp_count(s,' ')+2

Assuming there is a table in a form

create table t as select 'This is a significantly longer, but not the longest testcase' s from dual;

Test in SQL*Plus

SQL> select regexp_replace(s,'(\S+)','"\1"',1,level)from t connect by level<regexp_count(s,' ')+2
  2  /

REGEXP_REPLACE(S,'(\S+)','"\1"',1,LEVEL)
--------------------------------------------------------------------------------
"This" is a significantly longer, but not the longest testcase
This "is" a significantly longer, but not the longest testcase
This is "a" significantly longer, but not the longest testcase
This is a "significantly" longer, but not the longest testcase
This is a significantly "longer," but not the longest testcase
This is a significantly longer, "but" not the longest testcase
This is a significantly longer, but "not" the longest testcase
This is a significantly longer, but not "the" longest testcase
This is a significantly longer, but not the "longest" testcase
This is a significantly longer, but not the longest "testcase"

10 rows selected.
\$\endgroup\$
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 93 bytes

	S =' '
	I =INPUT S
S	I ARB . R S REM . I	:F(END)
	OUTPUT =L '"' R '"' S I
	L =L R S	:(S)
END

Try it online!

Each line ends with an additional space.

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

Python 2, 108 bytes

def f(s):
 W=s.split();n=len(W)
 for x in range(n*n):print[W[x%n],'"'+W[x%n]+'"'][x%-~n==0]+'\n'*(x%n==n-1),

Try it online!

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

PHP, 134 bytes

<?php $i=explode(' ',file_get_contents('php://stdin'));for($x=0;$x<count($i);$x++){$o=$i;$o[$x]='"'.$i[$x].'"';echo join(' ',$o).'
';}

Try it online!

\$\endgroup\$

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.