13
\$\begingroup\$

A quotation is "the repetition of one expression as part of another one". In American English, a quotation is surrounded by double-quotation marks ("), as in the previous sentence. However, this rule differs when a quote appears inside another quote. In this case, the inner quote is surrounded by single-quotation marks (').

Challenge

Given a string as input, output a quotable version of that string by exchanging double- and single-quotation marks that are used for inner quotation.

There's just one catch, though - not all single-quotation marks are used for quotation! For this challenge, single-quotation mark is used for quotation if and only if:

  • The mark is preceded by a non-alphanumeric character, or
  • The mark is followed by a non-alphanumeric character that is not a space

The marks used for quotations are guaranteed to be balanced; that is, you do not need to handle invalid input. Additionally, double-quotation marks will only be used in situations where single-quotation marks can be used to represent quotation.

Test Cases

A man, a plan, a canal, Panama.
"A man, a plan, a canal, Panama."

Socrates once said, "An unexamined life is not worth living."
"Socrates once said, 'An unexamined life is not worth living.'"

@musicman523 said, "Socrates once said, 'An unexamined life is not worth living.'"
"@musicman523 said, 'Socrates once said, "An unexamined life is not worth living."'"

"Nested 'quotes "can 'cause "recursion," if,' it," runs,' deep!"
"'Nested "quotes 'can "cause 'recursion,' if," it,' runs," deep!'"

"Contractions' behaviors can't be easy to account for," he warned.
"'Contractions' behaviors can't be easy to account for,' he warned."

@carusocomputing said, "Nested, 'quotes, it's unfortunate', to be sure."
"@carusocomputing said, 'Nested, "quotes, it's unfortunate", to be sure.'"

Scoring

This is , so the shortest answer in each language wins!

\$\endgroup\$
  • \$\begingroup\$ Sandbox \$\endgroup\$ – musicman523 Jun 13 '17 at 0:30
  • 2
    \$\begingroup\$ ... if' it" runs' deep" has single-quotes that are not preceded by a non-alphanumeric, nor are they followed by "non-alphanumeric character that is not a space", and yet they are being transformed! \$\endgroup\$ – Value Ink Jun 13 '17 at 0:45
  • 1
    \$\begingroup\$ Personally I don't feel that handling apostrophes adds much of value to the challenge. \$\endgroup\$ – ATaco Jun 13 '17 at 0:48
  • 1
    \$\begingroup\$ @JonathanAllan Yes. I think it's difficult to determine whether those single-quotation marks are used for quotation or possession without deeply complicating the rules. I think that would make a decent challenge on its own. \$\endgroup\$ – musicman523 Jun 13 '17 at 1:19
  • 1
    \$\begingroup\$ "Nested, 'quotes, it's unfortunate', to be sure." - I think you need a nested case with contractions. \$\endgroup\$ – Magic Octopus Urn Jun 13 '17 at 13:52
2
\$\begingroup\$

Ruby, 64 66 65 64+1 = 65 67 66 65 bytes

+1 byte for the -n flag.

gsub(/"|(?<!\w)'|'(?!\w|\s)/){"'\""["\"'".index$&]}
$><<?"+$_+?"

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Doesn't seem to do the right thing for single-quotation marks preceded by a space (which are indeed used for quotation) \$\endgroup\$ – musicman523 Jun 13 '17 at 1:08
  • \$\begingroup\$ @musicman523 thanks, fixed! \$\endgroup\$ – Value Ink Jun 13 '17 at 1:16
2
\$\begingroup\$

Retina, 34 27 bytes

-7 bytes thanks to @Leo

T`'"`"'`\W'|"|'[^\w ]
^|$
"

Try it online!

\$\endgroup\$
  • \$\begingroup\$ With @carusocomputing said, "Nested, 'quotes, it's unfortunate', to be sure." as the input, the single-quote following unfortunate is not changed to a double-quote, but should be. If it helps you debug, it works when the comma comes before the quote. (I don't know Retina so I can't really help any further.) \$\endgroup\$ – musicman523 Jun 13 '17 at 14:14
  • \$\begingroup\$ @musicman523 fixed it \$\endgroup\$ – ovs Jun 13 '17 at 14:34
  • \$\begingroup\$ You can use \w directly inside your character class \$\endgroup\$ – Leo Jun 13 '17 at 14:36
0
\$\begingroup\$

JavaScript (ES6), 90 bytes

Assumes the string doesn't contain #.

s=>'"'+s[r='replace'](/"/g,'#')[r](/(\W)'/g,'$1"')[r](/'([^\w ])/g,'"$1')[r](/#/g,"'")+'"'

f=
s=>'"'+s[r='replace'](/"/g,'#')[r](/(\W)'/g,'$1"')[r](/'([^\w ])/g,'"$1')[r](/#/g,"'")+'"'
;

`A man, a plan, a canal, Panama.
Socrates once said, "An unexamined life is not worth living."
@musicman523 said, "Socrates once said, 'An unexamined life is not worth living.'"
"Nested 'quotes "can 'cause "recursion," if,' it," runs,' deep!"
"Contractions' behaviors can't be easy to account for," he warned.
@carusocomputing said, "Nested, 'quotes, it's unfortunate', to be sure.`.split(`\n`).map(f).map(s=>console.log(s))

\$\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.