Quote my quote!

13

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!

musicman523

Posted 2017-06-13T00:30:03.053

Reputation: 4 472

Sandbox – musicman523 – 2017-06-13T00:30:36.050

2... 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! – Value Ink – 2017-06-13T00:45:17.170

@ValueInk Good catch! I updated the rules about single quotes after writing the test cases and didn't fix that one. It's been updated now. – musicman523 – 2017-06-13T00:47:10.277

1Personally I don't feel that handling apostrophes adds much of value to the challenge. – ATaco – 2017-06-13T00:48:27.430

@ATaco Personally, my rationale for coming up with challenge ideas is to make problems difficult enough that many different solution methods will appear, and it is more the solution that gets golfed rather than finding the right tool for the job. This is the value of adding this extra bit to the challenge IMO – musicman523 – 2017-06-13T00:54:19.187

So, I heard her say "We took place of the Jones'; they took the place of the Simths'." should become "I heard her say 'We took place of the Jones"; they took the place of the Simths".'" by these rules? – Jonathan Allan – 2017-06-13T01:16:04.943

1@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. – musicman523 – 2017-06-13T01:19:23.043

1"Nested, 'quotes, it's unfortunate', to be sure." - I think you need a nested case with contractions. – Magic Octopus Urn – 2017-06-13T13:52:09.133

@carusocomputing Thank you! I just added your test to the post. – musicman523 – 2017-06-13T14:09:46.307

@musicman523 I think there's 2-3 answers invalidated by that case, the ruby and retina ones are gold though. – Magic Octopus Urn – 2017-06-13T14:14:58.790

@carusocomputing Actually, I don't think the Retina answer quite works either. See my comment on that answer. – musicman523 – 2017-06-13T14:20:33.757

Answers

2

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!

Value Ink

Posted 2017-06-13T00:30:03.053

Reputation: 10 608

Doesn't seem to do the right thing for single-quotation marks preceded by a space (which are indeed used for quotation) – musicman523 – 2017-06-13T01:08:25.147

@musicman523 thanks, fixed! – Value Ink – 2017-06-13T01:16:29.517

2

Retina, 34 27 bytes

-7 bytes thanks to @Leo

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

Try it online!

ovs

Posted 2017-06-13T00:30:03.053

Reputation: 21 408

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.) – musicman523 – 2017-06-13T14:14:11.310

@musicman523 fixed it – ovs – 2017-06-13T14:34:50.280

You can use \w directly inside your character class – Leo – 2017-06-13T14:36:17.460

0

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))

darrylyeo

Posted 2017-06-13T00:30:03.053

Reputation: 6 214