Tüm "alıntılanan" karakterlerin toplam uzunluğunu yazdır


13

kurallar

Bu meydan okuma, ben "tırnak" tanımını biraz yeniden tanımlayacağım.

  • Tırnak işaretleri (AKA tırnak işaretleri ), doğrudan konuşma, tırnak veya bir cümle oluşturmak için çeşitli yazı sistemlerinde çift olarak kullanılan herhangi bir özdeş karakterdir . Parite , aynı karakter (büyük / küçük harfe duyarlı) olan bir açılış tırnak işareti ve bir kapanış tırnak işaretinden oluşur .

  • Birbiriyle çakışan alıntı çiftleri varsa,

    • Bir çift başka bir yuvayı iç içe geçiriyorsa, her iki çift de geçerlidir.
    • Bir çift başka bir yuvalama yapmıyorsa, ilk başlangıç ​​çifti geçerli kalır. Diğeri artık bir çift olarak kabul edilmiyor.
  • Alıntılanan karakterleri (bir çift tırnak uzunluğunu) sayarken,

    • Alıntıların kendisi sayılmaz.
    • Her çiftin uzunluğu bağımsız olarak sayılır. Çakışan bir başkasını etkilemez.

Hedef

Hedefiniz, geçerli tüm tekliflerin toplam uzunluğunu yazdırmaktır. Bu kod golf, bu nedenle en az bayt ile kod kazanır.

Örnekler

Legend:
    <foo>: Valid quotes
    ^    : Cannot be paired character

Input   : ABCDDCBA
`A`  (6): <BCDDCB>
`B`  (4):  <CDDC>
`C`  (2):   <DD>
`D`  (0):    <>
Output  : 12

Input   : ABCDABCD
`A`  (3): <BCD>
`B`  (0):  ^   ^
`C`  (0):   ^   ^
`D`  (0):    ^   ^
Output  : 3

Input   : AABBBBAAAABA
`A`  (0): <>    <><> ^
`B`  (0):   <><>    ^
Output  : 0

Input   : ABCDE
Output  : 0

Input   : Print the total length of all "quoted" characters
`r` (40):  <int the total length of all "quoted" cha>
`n` (14):    <t the total le>
`t` (15):     < >   <o>       <h of all "quo>
` `  (7):      ^   <total>      <of>   ^        ^
`h`  (0):        ^             ^                  ^
`e`  (8):         < total l>                 ^          ^
`o`  (0):            ^           ^         ^
`a`  (0):              ^            ^              ^ ^
`l`  (0):               ^ ^          <>
`"`  (0):                               ^      ^
`c`  (0):                                        ^    ^
Output  : 84

Input   : Peter Piper picked a peck of pickled peppers
`P`  (5): <eter >
`e`  (9):  <t>     ^      <d a p>           <d p>  ^
`r`  (0):     ^     ^
` `  (3):      ^     ^      <a>    <of>       ^
`i`  (5):        <per p>
`p`  (3):         <er >        ^       ^       ^ <>
`c`  (8):               <ked a pe>       ^
`k`  (7):                ^        < of pic>
`d`  (0):                  ^                 ^
Output  : 40

Input   : https://www.youtube.com/watch?v=dQw4w9WgXcQ
`h` (27): <ttps://www.youtube.com/watc>
`t`  (0):  <>            ^          ^
`/`  (0):       <>               ^
`w` (14):         <><.youtube.com/>         <4>
`.`  (7):            <youtube>
`o`  (0):              ^       ^
`u`  (1):               <t>
`c`  (0):                     ^      ^             ^
`Q`  (8):                                  <w4w9WgXc>
Output  : 57

@NickKennedy Kuralları gerçek tırnak işaretleri gibi düzelttim. Sanırım beklediğiniz bu. Bunu inceleyebilir misiniz?
user2652379

1
iyi görünüyor! Geri bildirimlerimi dinlediğiniz için teşekkürler.
Nick Kennedy

Yanıtlar:



4

APL (Dyalog Unicode) , 36 bayt SBCS

Tam program. Stdin'den girdi istemi.

≢∊t⊣{t,←'(.)(.*?)\1'S'\2'⊢⍵}⍣≡⍞⊣t←⍬

Çevrimiçi deneyin!

t←⍬ akümülatör kurmak t( t otal için)

⍞⊣ stdin'den dize girdisi lehine atın (sembol: tırnak işareti konsolda)

{}⍣≡ Kararlı olana kadar aşağıdaki anonim lambda'yı uygulayın (düzeltme noktası; önceki ≡ sonraki)

⊢⍵ tartışmada

 … ⎕S'\2' PCRE S aşağıdakileri arar , her maç için 2. grubu döndürür:

  (.)
  (.*?) Mümkün olduğunca az karakter (bu gruba 2 diyeceğiz  ) herhangi bir karakter (bu grup 1'i adlandıracağız)
  \1 grup 1 karakteri

t,←güncel değerine tekleyerek  güncellet

t⊣ lehine son maç listesi yok t

 içindeki karakter sayısını say


2

Ruby , 49 bayt

Özyinelemeli çözüm. Teklif gruplarını bulun, uzunluklarını sayın ve ardından alt grup uzunluklarını tekrar tekrar arayın ve her şeyi birlikte toplayın.

f=->s{s.scan(/(.)(.*?)\1/).sum{|a,b|b.size+f[b]}}

Çevrimiçi deneyin!


1

JavaScript (ES6), 64 bayt

f=([c,...a],i=a.indexOf(c))=>c?(~i&&i+f(a.splice(0,i+1)))+f(a):0

Çevrimiçi deneyin!

Yorumlananlar

f = (                       // f is a recursive function taking either the input string
                            // or an array of characters, split into
  [c, ...a],                // c = next character and a[] = all remaining characters
  i = a.indexOf(c)          // i = index of the 1st occurrence of c in a[] (-1 if not found)
) =>                        //
  c ?                       // if c is defined:
    ( ~i &&                 //   if i is not equal to -1:
      i +                   //     add i to the final result
      f(a.splice(0, i + 1)) //     remove the left part of a[] up to i (included) and
    )                       //     do a recursive call on it
    + f(a)                  //   add the result of a recursive call on a[]
  :                         // else:
    0                       //   stop recursion

1

JavaScript (Node.js) , 65 64 62 bayt

f=s=>(s=/(.)(.*?)\1(.*)/.exec(s))?f(s[3])+f(s=s[2])+s.length:0

Çevrimiçi deneyin!

Orijinal yaklaşım (64 bayt):

f=(s,r=/(.)(.*?)\1/g,t=r.exec(s))=>t?f(t=t[2])+t.length+f(s,r):0

Çevrimiçi deneyin!

f=s=>                              // Main function:
 (s=/(.)(.*?)\1(.*)/.exec(s))?     //  If a "quoted" segment can be found:
  f(s[3])                          //   Return the recursive result outside this segment,
  +f(s=s[2])                       //   plus the recursive result of this segment,
  +s.length                        //   plus the length of this segment
 :0                                //  If not: no quoted segment, return 0.

1

Brain-Flak , 100 bayt

({{<({}<>)<>(({<>(({}({})<>[({}<>)]))(){[{}()](<>)}{}}{}){(<>)})<>{}>{<>({}<<>({}<>)>)<>}<>[{}]}{}})

Çevrimiçi deneyin!

Yorumlananlar

# Loop over each character in input and sum iterations:
({{

  # Evaluate matching quote search as zero
  <

    # Move opening "quote" to right stack
    ({}<>)<>

    # Until match or end of search string found:
    # Note that the character to search for is stored as the sum of the top two entries in the right stack.
    (

      ({

        <>((

          # Character to search for
          {}({})

          # Subtract and move next character
          <>[({}<>)]

        # Push difference twice
        ))

        # Add 1 to evaluation of this loop
        ()

        # If no match, cancel out both 1 and pushed difference to evaluate iteration as zero (keep one copy of difference for next iteration)
        # (compare to the standard "not" snippet, ((){[()](<{}>)}{}) )
        # Then move to other stack
        {[{}()](<>)}{}

        # If a match was found, this will instead pop a single zero and leave a zero to terminate the loop, evaluating this iteration as 0+1=1.

      # Push 1 if match found, 0 otherwise
      }{})

      # If match found, move to left stack and push 0 denote end of "quoted" area.
      {(<>)}

    # Push the same 1 or 0 as before
    )

    # Remove representation of opening "quote" searched for
    # The closing quote is *not* removed if there is a match, but this is not a problem because it will never match anything.
    <>{}

  >

  # Move searched text back to left stack, evaluating each iteration as either the 1 or 0 from before.
  # This counts characters enclosed in "quotes" if a match is found, and evaluates as 0 otherwise.
  {<>({}<<>({}<>)>)<>}

  # Remove 0/1 from stack; if 1, cancel out the 1 added by the closing "quote"
  <>[{}]

# Repeat until two consecutive zeroes show up, denoting the end of the stack.
# (Because closing quotes are not removed, it can be shown that all other zeroes are isolated on the stack.)
}{}})

1

Jöle , 17 bayt

œṡḢẈṖ$Ḣ+ɼṛƲ)Ẏ$F¿®

Çevrimiçi deneyin!

Tek bir bağımsız değişkeni alan, girdi dizesi bir listede sarılmış ve tırnak karakteri sayısını bir tamsayı olarak döndüren tam bir program.

Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.