7
\$\begingroup\$

Background

There's a terrible problem in my console - the quotes never get included inside the arguments! So, when given this argument:

["abc","def","ghi","jkl"]

it says that the argument is like this:

[abc,def,ghi,jkl]

It would be very nice if you can fix this problem!

Challenge

Add double-quotes (") in order to surround a word (i.e. something that matches [a-z]+).

Test cases

[[one, two, three], -> [["one", "two", "three"],
[one, two, three],     ["one", "two", "three"],
[one, two, three],     ["one", "two", "three"],
[one, two, three]]     ["one", "two", "three"]]

[abc,def,ghi,jkl] -> ["abc","def","ghi","jkl"]

this is a test    -> "this" "is" "a" "test"

test              -> "test"

this "one" "contains' "quotations -> "this" ""one"" ""contains"' ""quotations"

But This One Is SpeciaL! -> B"ut" T"his" O"ne" I"s" S"pecia"L!

Rules

  • The inputted words are never capitalized. Capital letters are not considered part of a word.
  • The input is always a string, not a list. The test-cases aren't quoted because that's the only possible type.
  • A word is something that matches [a-z]+.
\$\endgroup\$
  • \$\begingroup\$ Sandbox \$\endgroup\$ – Member for 3 months Feb 23 at 14:59
  • 5
    \$\begingroup\$ Don't understand how the rule: The inputted words are never capitalized. works with the test case: But This One Is SpeciaL! -> B"ut" T"his" O"ne" I"s" S"pecia"L! \$\endgroup\$ – Noodle9 Feb 23 at 16:20
  • 10
    \$\begingroup\$ I don't get how the lists of lists in the first code block relates to the challenge. \$\endgroup\$ – xnor Feb 23 at 16:26
  • 4
    \$\begingroup\$ Some of the test case inputs are strings, some are arrays. Is it our choice which to support? It's also unclear what the first block quoted example of the array [one,two,three] repeated 4 times is supposed to demonstrate. \$\endgroup\$ – Jonah Feb 23 at 17:15
  • 1
    \$\begingroup\$ @Jonah I interpreted the first test case [abc,def,ghi,jkl] to be a string that happens to look like an array (the first character of the string is '[', etc.). \$\endgroup\$ – Mitchell Spector Feb 23 at 19:12

18 Answers 18

6
\$\begingroup\$

Gema, 8 characters

<J>="$0"

Copy of Adám's QuadR solution, just adjusted the syntax and the explanation:

Replace
<J> one or more lowercase letters
with
"$0" the quoted match.

Sample run:

bash-5.0$ gema '<J>="$0"' <<< '[abc,def,ghi,jkl]'
["abc","def","ghi","jkl"]

Try it online!

| improve this answer | |
\$\endgroup\$
5
\$\begingroup\$

perl -pE, 15 bytes

s/[a-z]+/"$&"/g
| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

QuadR, 10 bytes

[a-z]+
"&"

Try it online!

Replace
[a-z]+ one or more lowercase letters
with
"&" the quoted match.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Japt, 11 bytes

r"%a+"`"$&"

Try it

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I never knew "$&" existed, I was trying "$0", because C# does that and I assumed other languages would too. When that didn't work, I thought Japt doesn't have that built in. But you learn something new each day! \$\endgroup\$ – Embodiment of Ignorance Feb 24 at 6:11
2
\$\begingroup\$

sed -E, 20 15 14 bytes

Saved 5 bytes thanks to @Neil! And then 1 more byte thanks to @manatwork pointing out that we no longer have to add bytes to our score for command-line options.

s/[a-z]+/"&"/g

Original solution:

s/\([a-z]\+\)/"\1"/g

Test run:

$ cat quotelc.sed 
s/\([a-z]\+\)/"\1"/g

$ sed -f quotelc.sed << EOF
> [abc,def,ghi,jkl]
> this is a test
> test
> this "one" "contains' "quotations 
> But This One Is SpeciaL!
> EOF
["abc","def","ghi","jkl"]
"this" "is" "a" "test"
"test"
"this" ""one"" ""contains"' ""quotations" 
B"ut" T"his" O"ne" I"s" S"pecia"L!

Note: In TIO's sed, there's some issue with the collating sequence; the pattern [a-z] matches upper-case letters too. If you want to run this in TIO, you can do something like this.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ s/[a-z]\+/"&"/g saves 5 bytes, I think? \$\endgroup\$ – Neil Feb 24 at 0:32
  • \$\begingroup\$ Thanks, @Neil! I haven't golfed for a while, and I forgot about '&' in sed. \$\endgroup\$ – Mitchell Spector Feb 24 at 4:41
  • \$\begingroup\$ Since command line arguments are for free, with GNU sed you can specify --regexp-extended (or -r) to not need to escape the + quantifier. \$\endgroup\$ – manatwork Feb 24 at 8:59
  • \$\begingroup\$ @manatwork : I've understood that you need to add the number of bytes in any additional options. That would be 3 bytes here (including the space before the option), which wouldn't be worth it. (By the way, grep has a standard POSIX option for extended regexes now: -E instead of the old -r. So if you want to do that, -E is more portable.) \$\endgroup\$ – Mitchell Spector Feb 24 at 9:11
  • 1
    \$\begingroup\$ They're free, but it's considered a different language. \$\endgroup\$ – S.S. Anne Mar 16 at 23:29
1
\$\begingroup\$

Python 3, 47 bytes

lambda s:re.sub('([a-z]+)',r'"\1"',s)
import re

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Jelly, 15 bytes

Ż;0e€ØaIkƊj”"ḊṖ

Try it online!

A monadic link taking a Jelly string and returning a Jelly string. No regex in Jelly so identifies boundaries between lower letter segments and others, splits and joins with quotes.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 20 bytes

F⁺S «ω✂"⁼№⪪β¹ω№βι≔ιω

Try it online! Link is to verbose version of code. I was stuck at 24 bytes for some time until I came up with this approach. Explanation:

F⁺S «

Loop over the input with a space appended. (Any non-alphalower character would do.)

ω

Print the previous character. On the first pass through the loop, this variable holds the empty string, so nothing is output. Additionally, we never print the appended space, as it's never the previous character.

✂"⁼№⪪β¹ω№βι

Check whether both or neither the previous or current characters are alphalower characters. If exactly one is, then output a quote. (The predefined alphalower string has to be split for the first check otherwise the empty string would match 27 times on the first pass.)

≔ιω

Save the current character so that it's the previous character on the next pass of the loop.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Retina, 11 bytes

[a-z]+
"$0"

Pretty straight-forward approach.

Try it online.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

J, 39 35 bytes

load'regex'
f=.'[a-z]+'dquote rxapply]

Try it online!

-4 bytes thanks to Bubbler teaching me about dquote builtin

NOTE: The TIO link is showing an error, but this works correctly on my machine (j807), and passes the test cases. I'm unsure if it's a version issue or something else causing TIO to fail.

Task is accomplished almost entirely by the rxapply adverb. We simply have to supply the regex to apply to [a-z]+ and the verb to apply: surround in quotes dquote

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Use dquote stdlib verb instead of ('"',,&'"') for 35 bytes (regex lib still not working). \$\endgroup\$ – Bubbler Feb 27 at 9:55
  • \$\begingroup\$ Thanks @Bubbler. TIL... \$\endgroup\$ – Jonah Feb 27 at 13:16
1
\$\begingroup\$

05AB1E, 14 13 bytes

0.ø.γ.l}'"ý¦¨

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Deorst, 20 bytes

'"\1"'@
'([a-z]+)'gs

Try it online!

Again, just uses a regex based approach

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

PHP, 69 bytes

for(;$c=$argn[$i++];){if(($n=$c>'`'&$c<'{')^$p)echo'"';echo$c;$p=$n;}

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Japt, 12 bytes

r"%a+"@Q+X+Q

Try it

| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ Sorry, edited your solution, thinking it was mine and that I'd somehow managed to post the wrong one! It's been a long weekend! \$\endgroup\$ – Shaggy Feb 23 at 20:17
0
\$\begingroup\$

Jelly, 13 bytes

Ø1je€ØanƝ”"ẋż

A full program which prints the result.

Try it online! Or see the test-suite.

How?

Ø1je€ØanƝ”"ẋż - Main Link: list of characters e.g.  Foo 'Bar', baz
                                                ->  ['F','o','o',' ',"'",'B','a','r',"'",',',' ','b','a','z']
Ø1            - ones                                [1,1]
  j           - join                                [1,'F','o','o',' ',"'",'B','a','r',"'",',',' ','b','a','z',  1]
     Øa       - lower-case alphabet                 ['a','b',...,'z']
    €         - for each:
   e          -   exists in?                        [0,  0,  1,  1,  0,  0,  0,  1,  1,  0,  0,  0,  1,  1,  1,  0]
        Ɲ     - for neighbouring pairs:
       n      -   not equal?                        [  0,  1,  0,  1,  0,  0,  1,  0,  1,  0,  0,  1,  0,  0,  1]
         ”"   - quote character                     '"'
           ẋ  - repeat (vectorises)                 [ [],['"'],[],['"'],[],[],['"'],[],['"'],[],[],['"'],[],[],['"']]
            ż - zip together                        [[[],'F'],[['"'],'o'],[[],'o'],[['"'],' '],[[],"'"],[[],'B'],[['"'],'a'],[[],'r'],[['"'],"'"],[[],','],[[],' '],[['"'],'b'],[[],'a'],[[],'z'],[['"']]]
              - implicit, smashing print            F"oo" 'B"ar"', "baz"
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Burlesque, 32 bytes

J"[^a-z]+"~?[~jqup"[a-z]+"~aj.+Q

Try it online!

Depressingly, more than half of this is attempting to overcome what I can only assume is a bug where the ~a cuts off the last non-match.

J            # Duplicate
"[^a-z]+"~?  # All matches for not a-z
[~           # Take the last one
j            # Swap
# Main block (13 bytes)
qup          # Unparse to string
"[a-z]+"     # Matches a-z
~a           # Apply to those that match regex
####
j.+          # Add on hacky fix
Q            # Pretty print
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

PHP, 43 41 bytes

<?=preg_replace('/[a-z]+/','"$0"',$argn);

Try it online!

A simple straightforward regex implementation

EDIT: Thanks to @manatwork for saving 2 bytes!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ There you capture the entire matched substring into $1, so it contains the same as $0. So better use that one and that way you can remove the capturing (..). \$\endgroup\$ – manatwork Feb 26 at 10:13
0
\$\begingroup\$

JavaScript (V8), 33 bytes

f=i=>i.replace(/[a-z]+/g,'"$&"');

-9 Bytes thanks to manatwork

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Why the callback? '"$&"' is shorter than e=>"\""+e+"\"". (But if you really want to use callback, e=>'"'+e+'"' is shorter. Not mentioning e=>`"${e}"`.) \$\endgroup\$ – manatwork Feb 27 at 9:32
  • \$\begingroup\$ Same moment you wrote the shorter callback version i wanted to update to this solution too. And yes, first one is even shorter.. wasn't able to produce this due to errors I got.. update to post incoming :D \$\endgroup\$ – pixma140 Feb 27 at 9:36

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.