特克斯报价(UVa 272)


17

给定一个字符串,将所有左双引号替换为两个反引号,并将所有右双引号替换为两个单引号。

左引号表示以引号开头的引号。右引号表示以引号结尾的引号。报价不能嵌套。您可能会假设字符串中双引号的数量为偶数。

例子

输入:

"To be or not to be," quoth the Bard, "that
is the question".
The programming contestant replied: "I must disagree.
To `C' or not to `C', that is The Question!"

输出:

``To be or not to be,'' quoth the Bard, ``that
is the question''.
The programming contestant replied: ``I must disagree.
To `C' or not to `C', that is The Question!''

什么是左双引号和右双引号?
我的代名词是monicareinstate,

1
@someone左双引号是开始引号的引号。右双引号是结束引号的引号。
放射性

报价可以嵌套吗?
我的代名词是monicareinstate,

@someone不。他们不能。
放射性

2
@ LegionMammal978“可以嵌套引号吗?” ...“ @someone Nope。他们不能。”
乔纳森·艾伦,

Answers:


18

无法读取789777字节

通过使用变量X34而不是X6来获得-12个字节。

'“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“ '“”“”“'”“”“”“”“”“”“”“”“”“”“”“”“”“”“” “'”“'”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“” ”“'”“'”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“ '“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“” “”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“'”“'”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“ '“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“” '“”'“”“”“”“”“”“”“”“”“”“”“”“”“”'“”“”“”“”“”“”“”“'”'“”“”“”“”“”“”“”“”“”“”“”“”“'”“”“”“”“”“”“”“”“”“” “”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“” “”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“” '“”'“”'“”“”“”“”“”“”“”“”“”“”“”“”“”“”“” “'”“'”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“” ”“'”“'”“”“”“”“”“”“”“”“”“”“”“”“

在线尝试!

按照对语言名称的传统表示,以可变宽度字体显示。

我为这个挑战学会了不可读,因为它显然是完成这项工作的最佳工具。在不可读中允许的唯一字符是,'并且"当然可以完美地适应涉及"变为的挑战''。是不是

说明:

'""""""'""'""" assign to X2
'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""" 34 (double quote sign)
'""""""'""'""'""'""'""" assign to X5
'""'""'""'""'""'"""""""'""'""" X2+5 (apostrophe sign)
'"""""'""'""""""'"""'"""""""""" while (1+ (assign to X1 a value read from stdin, or -1 if stdin is empty) != 0)
 '"""" do 2 things
 '""""""'""'""'""'""" assign to X4
 '"""""""'""'""" the value of X2
 AND
 '"""" do 2 things
 '""""""'""'""'""" assign to X3
 '"""""""'""" the value of X1
 AND
 '"""" do 2 things
 '"""""'"""""""'""'""'""'""" while(X4 != 0)
  '"""" do 2 things
  '""""""'""'""'""'""" assign to X4
  '""""""""'"""""""'""'""'""'""" X4-1
  AND
  '""""""'""'""'""" assign to X3
  '""""""""'"""""""'""'""'""" X3-1
 end while
 AND
 '"""""""""'"""""""'""'""'""" if(X3 != 0)
  '"'"""""""'""" print X1
 else
  '" print the output of
  '"""""""""'"""""""'"""""""'""'""" if(X34 !=0)
   '"""" do 2 things
   '""""""'"""""""'""'"""'""""""""'""" assign X34=0
   AND
   '"'"""""""'""'""'""'""'""" print X5
  else
   '"""" do 2 things
   '""""""'"""""""'""'"""'""" assign X34=1
   AND
   '"'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'"""""""'""'""'""'""'""" print X5+57
 end if
end while

(由于X5 = 34,因此对X34的调用实际上是对X(X5)的调用。)


1
在quote挑战中使用一种quote语言大声笑
HighlyRadioactive 19'Aug

5

视网膜,13字节

快速学习Retina,因为某种原因,我不喜欢Japt获胜,并且觉得C#解决方案无论如何都会使用正则表达式。我知道有一个Retina答案,但是我在创建它时并没有使用它,反正我也找到了它。

#2$`"
``
"
''

在线尝试!


1
为什么讨厌Japt?:p
Shaggy




4

Japt,12 个字节

仅出于Japt的局限性(或可能是其中的错误),才会为11。

r'"ȲîT°g"`'

尝试一下

r'"ȲîT°g"`'     :Implicit input of string
r'"              :Replace double quotes
   È             :Pass each match through a function
    ²            :  Duplicate
     î           :  Replace each character with
      T°         :    Postfix increment T (initially 0)
        g"`'     :    Index into "`'" with wrapping

太糟糕了,您不能Q在这里使用:\
奥利弗(Oliver)

@Oliver,我可以(我原本这样做);我也需要一个,
毛茸茸的

我的意思是太糟糕了,您不能简单地Q代替'"
奥利弗(Oliver)

4

TeX,54 32字节

对于TeX报价替换挑战,我们当然也需要TeX版本!

\catcode`"13\def"#1"{``#1''}...\bye

... 是输入字符串,因此不会添加到字节数中。


1
为什么不简单\def"#1"{``#1''}呢?(或者,\long\def如果您希望引用会跨越段落边界)
Phelype Oleinik '19

1
出于显而易见的原因,我没有想到它:-)
siracusa

2

木炭,23字节

WS⊞υι⭆⪪⪫υ¶¦"⎇κ⁺ײ§'`κιι

在线尝试!链接是详细版本的代码。包括8个字节,以避免繁琐的输入格式。说明:

WS⊞υι

收集输入行,直到到达空行。

⪫υ¶¦

连接换行符上的行。

⪪..."

将输入拆分为引号。

⭆...

映射每个部分并连接结果以进行隐式打印。

⎇κ...ι

保持第一部分不变。

⁺ײ§'`κι

在适当的前缀前面加倍。


我在等木炭解决方案。真好!
放射性

2

R,40个字节

cat(scan(,"",,,'"',""),sep=c("``","''"))

在线尝试!

读取字符串输入,每个输入分开",给出一个字符串向量。然后粘贴这些字符串,在双反引号和双撇号之间交替作为分隔符,并根据需要回收它们。

有人可能会根据正则表达式发布一个较短的R答案。不过,我认为,这个答案是R的典型代表。

scan(,"",,,'"',"")零件说明:

scan(, # empty first parameter: read from STDIN
  "",  # type of input is a string
  ,    # default 3rd parameter nmax
  ,    # default 4th parameter n
  '"', # separate on character "
  "")  # do not treat any characters as quotations marks (necessary to handle ' in the input)





1

迷宫(43?)53字节

396"
 } "",)@
  ~"  (
 "~ 3_:
""" 4
" .;-
=   ;
..::;

在线尝试!

这个简单得多的92字节程序的高尔夫版本:

3 """
9 " ",)@
}96 " (
    " :_34-;;
    "     ; :
   """"""". :
   "        .
   """"""""=.

如果我们不需要处理包含零字节的输入,则为43个字节

39}9
@  6
`,""
`  "
: ."=..
_ ;   "
34-;;::


1

(GNU)sed38 33 30字节

-4通过移除-n国旗和隐式印刷上n-1通过重用以前/expression/,感谢@Cowsquack。-3通过使用隐式分支到末端。

:a
s/"/``/;T
:b
s//''/;ta
n;bb

在线尝试! 在线尝试! 在线尝试!

一些非常基本的标签跳跃。这大概可以打一两个字节。

:a          # label a
s/"/``/;T   # replace " -> ``. If unsuccessful, move to next line
:b          # label b
s//''/;ta   # replace " (implicit) -> ''. If successful, jump to a (w/o reading new line)
n;bb        # read in the next line, but jump to label b. 

1
顺便说一句,通常在头中指定使用的标志。您可以使用codegolf.stackexchange.com/a/167295/41805来打高尔夫球,此外,可以删除一些分支逻辑。然后,使解决方案在不带-n标志的情况下工作应节省更多。(当然,有一个简单的sed -z解决方案,我想您是故意避免的)
Kritixi Lithos,

好吧,我man sed稍微深一点地钻到了30点。随时让我知道我想念的任何事情,您已经在这门语言上有高尔夫经验。(哦,-z对我来说是新手,但我同意。我会没有它。)
GammaFunction

做得好,我总是很高兴看到sed的答案
Kritixi Lithos

1

05AB1E,15个字节

'"¡ā¨„'`sè2×.ιJ

在线尝试!

05AB1E中没有正则表达式,因此我们在上分割",列出交替的``'',然后将两者交错。


1
好的,看起来我不必修复和取消删除我的答案。;)
Kevin Cruijssen

1

Haskell67 60 58字节

(#0)
('"':x)#n=["``","''"]!!n++x#(1-n)
(a:b)#n=a:b#n
x#n=x

在线尝试!

相关功能是(#0)

由于我最初以为这个问题还要求我们在此处转换单引号,所以该版本可以处理两个引号:

Haskell,125个字节

(#(1<0,1<0))
('"':x)#(m,n)=last("``":["\""|m])++x#(not m,n)
('\'':x)#(m,n)=last('`':['\''|n]):x#(m,not n)
(a:x)#n=a:x#n
x#n=x

在线尝试!


"\""应该是"''"(两个撇号)
siracusa

@siracusa谢谢,我想因为您可以"在tex中使用,所以您可以在这里使用它。
发布Rock Garf Hunter

1

字节,14字节

"(.*?)"
``\1''

在线尝试!

使用@Adám的包装器对Dyalog APL进行简单的查找/替换 ⎕R eplace函数。

怎么样:

"(.*?)"  PCRE, finding anything between two double quotes and assigning it to group 1
``\1''   Transformation string, replacing the match with ``group_1''.


0

果冻,13 个字节

ṣ”"µJḊ⁾`'ṁḤż@

完整的程序。

在线尝试!

怎么样?

ṣ”"µJḊ⁾`'ṁḤż@ - Main Link: list of characters, T   e.g. ..."hi" - she "said"...
 ”"           - character '"'                           '"'
ṣ             - split (T) at ('"')                      ["...","hi"," - she ","said","..."]
   µ          - (call that X) start a new monadic chain
    J         - range of length (of X)                  [1,2,3,4,5]
     Ḋ        - dequeue                                 [2,3,4,5]
      ⁾`'     - list of characters                      ["`","'"]
         ṁ    - mould like                              ["`","'","`","'"]
          Ḥ   - double                                  ["``","''","``","''"]
           ż@ - (with reversed @rguments) zip (with X)  [["...","``"],["hi","''"],[" - she ","``"],["said","''"],["..."]]
              - implicit (smashing) print               ...``hi'' - she ``said''...



0

Stax,11 个字节

û╩↕H£ñ╟Uzay

运行并调试

程序:

  1. 接受所有输入,换行符和所有内容。
  2. 正则表达式替换'"'为一个块,该块产生反引号和引号对的交替输出(?)

0

Java 8,40字节

s->s.replaceAll("\"([^\"]+)\"","``$1''")

在线尝试。

说明:

s->                             // Method with String as both parameter and return-type
  s.replaceAll("\"([^\"]+)\"",  //  Replace all these matches,
               "``$1''")        //  with this replacement 

正则表达式说明:

 "([^"]+)"                      // MATCH:
 "                             "//  A literal "
   [^"]+                       "//  Followed by 1 or more non-" characters
  (     )                       //  (captured in capture group 1)
         "                     "//  Followed by a literal " again

``$1''                          // REPLACEMENT:
``                              //  Literal ``
  $1                            //  Followed by the match of capture group 1
    ''                          //  Followed by a literal ''
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.