甲报价是“一种表达的重复作为另一个的一部分”。在美式英语中,引号与双引号(“)一样,如上一句话。但是,当引号出现在另一个引号内时,此规则有所不同。在这种情况下,内部引号由单引号引起来(')。
挑战
给定一个字符串作为输入,请通过交换用于内部引号的双引号和单引号来输出该字符串的引用版本。
不过,只有一个问题-并非所有的单引号都用于引号!对于此挑战,当且仅当以下情况下,才使用单引号引起来:
- 标记之前是非字母数字字符,或者
- 标记后跟非字母数字字符,没有空格
报价中使用的标记必须保证平衡;也就是说,您不需要处理无效的输入。此外,双引号将仅在单引号可用于表示引号的情况下使用。
测试用例
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.'"
计分
这是代码高尔夫球,因此每种语言中最短的答案将获胜!
沙盒
—
musicman523
... if' it" runs' deep"
的单引号前面没有非字母数字字符,也没有跟着“不是空格的非字母数字字符”,但是它们正在转换!
就我个人而言,我认为处理撇号不会给挑战带来很多价值。
—
ATaco
@JonathanAllan是的。我认为,在不使规则复杂化的情况下,很难确定是将单引号引起来还是将其引起使用。我认为这本身将构成一个体面的挑战。
—
musicman523
"Nested, 'quotes, it's unfortunate', to be sure."
-我认为您需要一个带有收缩的套盒。