打印所有“带引号”字符的总长度


13

规则

在这个挑战中,我将重新定义“引号”的定义。

  • 引号(AKA 引号)是在各种书写系统中成对使用的任何相同字符,用于引出直接语音,引号或短语。该对包括一个引号和一个引号,它们是相同的字符(区分大小写)。

  • 如果有报价对彼此重叠,

    • 如果一对嵌套,则两个对仍然有效。
    • 如果一对未嵌套,则第一个起始对保持有效。另一个不再被视为一对。
  • 计算带引号的字符(一对引号的长度)时,

    • 引号本身不计算在内。
    • 每对的长度独立计算。重叠不会影响另一个。

目标

您的目标是打印所有有效报价的总长度。这是代码高尔夫球,因此字节最少的代码获胜。

例子

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我将规则固定为更类似于实际报价。我认为这就是您的期望。你能复习一下吗?
user2652379

1
看起来不错!感谢您收听我的反馈。
尼克·肯尼迪

Answers:



4

APL(Dyalog Unicode),36 字节SBCS

完整程序。提示从stdin输入。

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

在线尝试!

t←⍬ 设置的蓄能器t(用于 otal)

⍞⊣ 抛弃那些支持从stdin输入字符串的符号(符号:控制台中的引号)

{}⍣≡ 应用以下匿名lambda直到稳定(定点;上一个≡下一个)

⊢⍵ 论据

 ... ⎕S'\2' PCRE 小号 EARCH为以下,为每个匹配返回组2:

  (.) 任何字符(我们会打电话给该组1)
  (.*?) 尽可能少的字符越好(我们称之为第2组)
  \1 的第1组字符

t,← 更新t通过追加,为t目前的价值

t⊣ 抛弃(没有匹配的最终列表),转而支持 t

 计算其中的字符数


2

红宝石,49字节

递归解决方案。查找报价组,计算其长度,然后递归查找子组的长度,并将所有内容加在一起。

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

在线尝试!


1

JavaScript(ES6),64字节

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

在线尝试!

已评论

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字节

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

在线尝试!

原始方法(64字节):

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

在线尝试!

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字节

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

在线尝试!

已评论

# 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

果冻,17个字节

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

在线尝试!

一个完整的程序,它使用一个参数,将输入字符串包装在列表中,并以整数形式返回引号字符。

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.