सभी "उद्धृत" वर्णों की कुल लंबाई प्रिंट करें


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
अच्छा लग रहा है! मेरी प्रतिक्रिया सुनने के लिए धन्यवाद।
निक केनेडी

जवाबों:



4

एपीएल (Dyalog यूनिकोड) , 36 बाइट्स SBCS

पूरा कार्यक्रम। स्टड से इनपुट के लिए संकेत।

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

इसे ऑनलाइन आज़माएं!

t←⍬ एक संचायक स्थापित करें t( टी ओटाल के लिए)

⍞⊣ स्टड से स्ट्रिंग इनपुट के पक्ष में त्यागें (प्रतीक: सांत्वना में उद्धरण)

{}⍣≡ स्थिर होने तक निम्नलिखित अनाम लंबोदर लागू करें (फिक्स-पॉइंट; पिछला) अगला)

⊢⍵ तर्क पर

 ... प्रत्येक मैच के लिए निम्नलिखित 2, रिटर्निंग ग्रुप 2 के लिए ⎕S'\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

जावास्क्रिप्ट (ईएस 6), 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

जावास्क्रिप्ट (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

ब्रेन-फ्लैक , 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¿®

इसे ऑनलाइन आज़माएं!

एक पूर्ण प्रोग्राम जो एकल तर्क लेता है, इनपुट स्ट्रिंग को एक सूची में लपेटता है, और एक पूर्णांक के रूप में उद्धरण वर्णों की संख्या देता है।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.