Tex Quotes(UVa 272)


17

文字列が与えられたら、すべての左二重引用符を2つのバッククォートに、すべての右二重引用符を2つのシングルクォートに置き換えます。

左引用符は、引用符を開始する引用符を意味します。右引用とは、引用を終了する引用を意味します。引用符はネストできません。文字列内に偶数の二重引用符があると仮定することができます。

入力:

"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!''

左二重引用符と右二重引用符とは何ですか?
私の代名詞は

1
@someone左二重引用符は、引用符で始まる引用符です。右二重引用符は、引用符を終了する引用符です。
高放射能

引用符をネストできますか?
私の代名詞は

@someoneいや。できません。
高放射能

2
@ LegionMammal978「引用符をネストできますか?」...「@someoneいいえ、彼らはできません。」
ジョナサンアラン

回答:


18

読み取り不可789 777バイト

X6ではなく変数X34を使用して-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

(X34への呼び出しは、X5 = 34であるため、実際にはX(X5)への呼び出しです。)


1
引用チャレンジLOLの引用言語
HighlyRadioactive

5

網膜、13バイト

何らかの理由でJaptが勝つことを嫌い、C#ソリューションが正規表現を使用するように感じるため、Retinaをすばやく学習します。Retinaの回答があることは知っていますが、これを作成するときに使用していません。とにかく(正確に)見つけました。

#2$`"
``
"
''

オンラインでお試しください!


1
なぜJaptが嫌いなのか!:p
シャギー

5

JavaScript(ES9)、34バイト

引用されたブロックでの作業:

s=>s.replace(/"(.*?)"/gs,"``$1''")

オンラインでお試しください!


JavaScript(ES6)、38バイト

各二重引用符を個別に処理する:

s=>s.replace(/"/g,_=>(c="'`"[s^=1])+c)

オンラインでお試しください!


RegExpのdotAllフラグはECMAScript 2018の機能であり、ES6には含まれていません。
tsh

@tsh良いキャッチ。更新しました。
アーナルド


4

Python 3、65バイト

f=lambda s:s and(s[0],"`'"[s.count('"')%2]*2)[s[0]=='"']+f(s[1:])

オンラインでお試しください!

Erik the Outgolferのおかげで-8バイト


2番目の関数については、8バイトを削除できます(見た目ほど違いはありませんが、他の変更とともにs[0].replace、明示的なs[0]=='"'チェックに置き換えました)。
エリック・ザ・アウトゴルファー

@EriktheOutgolfer素敵な発見、ありがとう!
Jitse

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代わりに使用することができなかったのはあまりにも悪いことを意味します'"
オリバー

4

TeX、54 32バイト

TeX引用符交換の課題には、もちろんTeXバージョンも必要です!

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

... 入力文字列であるため、バイトカウントに追加されません。


1
なぜもっと単純ではないの\def"#1"{``#1''}ですか?(または\long\def、引用が段落の境界を越えると
思われる場合

1
私がそれを考えなかった明らかな理由のために:-)
シラクーサ

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

(GNU)sed38 33 30バイト

-4-nフラグを削除し、暗黙的にに印刷することによりn-1前回のを再利用すること/expression/により、@ Cowsquackに感謝します。-3暗黙的なブランチツーエンドを使用して。

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

オンラインでお試しください! オンラインでお試しください! オンラインでお試しください!

かなり基本的なラベルのジャンプ。これはおそらく1バイトまたは2バイトでゴルフすることができます。

: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を使用して1バイトをゴルフできます。さらに、分岐ロジックの一部を削除できます。その後、-nフラグを使用せずにソリューションを機能させると、さらに節約できます。(もちろん、sed -z意図的に回避したと思われる些細な解決策もあります)
Kritixi Lithos

さて、少し掘り下げman sedて30になりました。見逃したことは何でも教えてください。このラングでゴルフを体験できます。(ああ、-z私にとっては新しいですが、私は同意します。私はそれなしに滞在します。)
GammaFunction

うまく行って、私はいつもsedの答えを見て嬉しい
KritixiのLithos


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

オンラインでお試しください!


"\""する必要があります"''"(2つのアポストロフィ)
シラクサ

@siracusaありがとう、"texで使えるのでここで使えると思った。
ウィートウィザード

1

QuadR、14バイト

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

オンラインでお試しください!

Dyalog APLの@Adámのラッパーを使用した簡単な検索/置換 ⎕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

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 ''
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.