गोल्फ मेरा एडा सरणियाँ


10

पृष्ठभूमि

एडा एक प्रोग्रामिंग लैंग्वेज है जो वास्तव में अपनी थिरता के लिए नहीं जानी जाती है।

हालांकि, इसके सरणी शाब्दिक वाक्यविन्यास सिद्धांत में काफी सरणी सरणी विनिर्देशों के लिए अनुमति दे सकते हैं। यहाँ सरणी शाब्दिक सिंटैक्स का एक सरल ईबीएनएफ विवरण है ( बॉटलकैप्स के लिए निष्क्रिय) :

array ::= positional_array | named_array
positional_array ::= expression ',' expression (',' expression)*
                   | expression (',' expression)* ',' 'others' '=>' expression
named_array ::= component_association (',' component_association)*
component_association ::= discrete_choice_list '=>' expression
discrete_choice_list ::= discrete_choice ('|' discrete_choice)*
discrete_choice ::= expression ('..' expression)? | 'others'

हम अपने आप को सरलता के लिए पूर्णांक के 1-आयामी सरणियों तक सीमित कर देंगे। इसका मतलब है कि हम अभिव्यक्ति मूल्यों के लिए केवल पूर्णांक का उपयोग करेंगे। शायद भविष्य की चुनौती में हम कुछ और उन्नत करने की कोशिश कर सकते हैं (जैसे कि चर और बहुआयामी सरणियों की घोषणा करना)। आप करते नहीं गोल्फ के लिए पूर्णांक शाब्दिक है

यहाँ एडा सरणी शाब्दिक और स्पष्टता के लिए एक पायथन-एस्क समकक्ष प्रतिनिधित्व के कुछ उदाहरण दिए गए हैं:

(1, 2, 3) = [1, 2, 3]
(1, others => 2) = [1, 2, 2, ..., 2]
(others => 1) = [1, 1, ..., 1]
(1 => 1, 2 => 3) = [1, 3]
(1|2 => 1, 3 => 2) = [1, 1, 2]
(1 => 1, 3 => 2, others => 3) = [1, 3, 2, 3, 3, ..., 3]

चुनौती

इस चुनौती का लक्ष्य किसी दिए गए इनपुट सरणी के लिए सबसे छोटा बाइट-काउंट Ada array शाब्दिक आउटपुट करना है। ध्यान दें कि जो भी सूचकांक वांछित है, उससे Ada सरणियाँ शुरू हो सकती हैं, इसलिए आप वह चुन सकते हैं जो आप चाहते हैं कि आरंभिक सूचकांक तब तक हो जब तक प्रत्येक मान अनुक्रमिक हो। इस उदाहरण में, मैं 1 पर शुरू करना चुनता हूं, जो कि Ada के लिए मुहावरेदार है, हालांकि आप किसी अन्य पूर्णांक में शुरू करना चुन सकते हैं।

इनपुट

आपके इनपुट में पूर्णांकों की एक सूची शामिल होगी, जो भी रूप में सुविधाजनक है।

उत्पादन

आपका आउटपुट कम से कम मान्य Ada सरणी शाब्दिक पाठ का एक स्ट्रिंग होगा जो इनपुट पूर्णांक की सूची का प्रतिनिधित्व करता है। आप इस सरणी पर अपनी इच्छानुसार किसी भी आरंभिक सूचकांक का उपयोग कर सकते हैं, लेकिन आपकी पसंद (जो भी हो) आपके उत्तर में निर्दिष्ट होनी चाहिए (शुरुआती सूचकांक भी गतिशील हो सकता है)।

पूर्णांक उदाहरण के रूप में, हस्ताक्षरित दशमलव संख्याओं के रूप में दर्शाए जाते हैं। यह चुनौती पूर्णांक मानों की गोल्फिंग को कवर नहीं करती है।

उदाहरण

यहाँ कुछ उदाहरण हैं:

Simple: [1, 2, 3] -> (1,2,3)
Range: [1, 1, 1, 1, 1, 1, 1,] -> (1..7=>1)
Others: [1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1] -> (6=>2,others=>1)
Multiple Ranges: [1,1,1,1,1,2,2,2,2,2,1,1,1,1,1,2,2,2,2,2,1,1,1,1,1] -> (6..10|16..20=>2,others=>1)
Tiny Ranges: [1,1,2,2,1,1,1,1,1] -> (3|4=>2,others=>1)
Far Range: [[1]*5, [2]*100, [3]*5] -> (1..5=>1,6..105=>2,others=>3)
Alternation: [1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2] -> (1|3|5|7|9|11|13|15|17=>1,others=>2)
Big Number: [1234567890,1,1234567890] -> (2=>1,1|3=>1234567890)
Big-ish Number: [1234567,1,1234567] -> (1234567,1,1234567)
Solo: [-1] -> (1=>-1)
Huge Input: [[0],[1]*1000000000] -> (0,others=>1)
Positional Others: [1, 2, 3, 3, 3, 3, 3, 3] -> (1,2,others=>3)
Range and Choice, no Others: [1,1,1,12,12,3,3,3,3,3,3,3,3,3,3,4] -> (1..3=>1,4|5=>12,6..15=>3,16=>4)

न्यूनतम आवश्यकताएं

  • कम से कम 100 नंबरों का समर्थन और लंबाई में कम से कम 256 नंबरों का इनपुट।

  • ऐसे सभी इनपुट के लिए सही परिणाम तैयार करें

    • अंत में 'दूसरों' को शामिल करना शामिल है
    • एकल आइटम सरणियों के लिए एक इंडेक्स शामिल करना शामिल है
  • उपरोक्त इनपुट्स में से प्रत्येक के लिए एक मिनट में (TIO पर अधिमानतः) समाप्त करें।

बाइट्स जीत में सबसे छोटा समाधान!

संदर्भ कार्यान्वयन

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

यह कार्यान्वयन इनपुट को अपने सरणी के रूप में उपयोग करता है, जिसमें प्रत्येक वर्ण एक संख्या है। बड़े अक्षरों के लिए कैपिटल अक्षर विशेष स्थिरांक हैं। कार्यक्रम का तर्क उपयोग करने के लिए 'स्टार्ट इंडेक्स' है।

TIO लिंक में "कोड" अनुभाग समस्या का एक सही समाधान है, जबकि "हेडर" और "पाद" परीक्षण संरचना को लागू करते हैं।


3
"सुदूर रेंज" मामले अस्तित्व बस इंगित करने के लिए है कि हम उस प्रारूप में इनपुट ले अगर हम कर सकते हैं चुनें या हाइलाइट हम चाहते हैं कि करने के लिए है के रूप में अच्छी तरह से है कि इनपुट प्रारूप को संभालने के लिए सक्षम होने के लिए सामान्य सरणियों के रूप में? इसके अलावा, पिछले परीक्षण मामले सिर्फ उत्पादन नहीं होना चाहिए (-1)?
झबरा

3
"फ़ॉरेस्ट रेंज" केस को केवल अंतरिक्ष को बचाने के लिए लिखा गया है, वास्तविक इनपुट एक फ्लैट सरणी होगी जिसमें 110 पूर्णांक होंगे, लेकिन आउटपुट सही है। इसका उद्देश्य उन मामलों को प्रदर्शित करना है जहां 'दूसरों' कीवर्ड को एक छोटी सीमा पर जाना चाहिए, जिसमें लंबा प्रतिनिधित्व है। ( 106..110=>3,others=>2अधिक लंबा होगा) अंतिम मामले के लिए एक इंडेक्स की आवश्यकता होती है, क्योंकि व्याकरण एकल तत्व स्थितीय सरणियों की अनुमति नहीं देता है ( positional_array ::= expression ',' expression (',' expression)*)
लैम्बडाटा

1
1(1=>1,others=>1)(1..100000000=>1)

2
क्या आप पुष्टि कर सकते हैं कि इसके (1|3=>1234567,2=>1)लिए एक और वैध आउटपुट है [1234567,1,1234567]?
अरनौलद

1
क्या हमें अपनी पसंद की भाषा के रूप में एडा का उपयोग करने की अनुमति है?
बेंजामिन उरक्हार्ट

जवाबों:


5

जावास्क्रिप्ट (ईएस 6),  307  304 बाइट्स

सहेजे गए 2 बाइट @KevinCruijssen को धन्यवाद

यह शर्मनाक रूप से लंबा है ...

a=>[b=([...a,m=''].map(o=(v,i)=>(i?p==v?!++n:m=o[(o[p]=[o[p]&&o[p]+'|']+(n?i-n+(n>1?'..':'|')+i:i))[m.length]?(x=i-n,j=p):j]:1)&&(p=v,M=n=0)),Object.keys(o).map(k=>j-k|!m[6]?o[k]+'=>'+k:O,O='others=>'+j).sort()),1/a[1]?[...a]:b,j-a.pop()?b:a.slice(0,x-1)+[,O]].map(a=>M=M[(s=`(${a})`).length]||!M?s:M)&&M

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


डुप्लिकेट के लिए एक चर बनाकर 305 बाइट्स (-2)'others=>'
केविन क्रूज़सेन

@KevinCruijssen धन्यवाद! (NB: आपके संस्करण में, tइसे परिभाषित करने से पहले उपयोग किया जाता है; इसका कारण यह नहीं है कि दुर्घटना है कि पहले 2 परीक्षण मामले इसे बिल्कुल नहीं करते हैं; यह आसानी से बिना किसी लागत के तय किया जा सकता है।)
अर्नुलड

आह अच्छा। मैं वास्तव में आपके जवाब को देखने के लिए नहीं आया था कि क्या इस्तेमाल किया गया था। मैंने बस 'others'दो बार ध्यान दिया और आउटपुट को बदले बिना इसके लिए एक वैरिएबल बनाने की कोशिश की। ;) यह समझाने के लिए धन्यवाद, और अल्पविराम के अच्छे गोल्फ का उपयोग करके [,O]। :)
केविन क्रूज़सेन

2

05AB1E , 136 134 132 बाइट्स

"',ý'(ì')«ˆ"©.V"θ…ˆ†=>쪮.V"Uγ¨D€gPi˜IX.V}\ÙεQƶ0KDāαγ€g£}D2Fε¾iεнyg≠iyθyg<i'|ë„..}ý}}ë˜}'|ý„=>«Iyнн<è«}Ю.VgFDN._ć'>¡X.V}\¼}¯éIgi¦}н

संपादित करें: अब सभी परीक्षण मामलों के लिए निश्चित।

इसे ऑनलाइन आज़माएँ या सभी परीक्षण मामलों को सत्यापित करें ('बड़ा इनपुट' को छोड़कर, क्योंकि यह बहुत बड़ा है)।

स्पष्टीकरण:

"',ý'(ì')«ˆ"       # Push this string (function 1), which does:
 ',ý              '#  Join a list by ","
    '(ì           '#  Prepend a "("
       ')«        '#  Append a ")"
          ˆ        #  Pop and add it to the global array
            ©      # Store this string in the register (without popping)
             .V    # And execute it as 05AB1E code on the (implicit) input-list
"θ…ˆ†=>쪮.V"      # Push this string (function 2), which does:
 θ                 #  Pop and push the last element of the list
  …ˆ†=>ì           #  Prepend dictionary string "others=>"
        ª          #  Append that to the list which is at the top of the stack
         ®.V       #  And execute function 1 from the register     
             U     # Pop and store this string in variable `X`
γ                  # Get the chunks of equal elements in the (implicit) input-list
 ¨                 # Remove the last chunk
  D                # Duplicate the list of remaining chunks
   g              # Get the length of each
     Pi     }      # If all chunk-lengths are 1:
       ˜           #  Flatten the list of remaining chunks
        I          #  Push the input-list
         X.V       #  Execute function 2 from variable `X`
             \     # Discard the top of the stack (in case we didn't enter the if-statement)
Ù                  # Uniquify the (implicit) input-list
 ε                 # Map each unique value `y` to:
  Q                #  Check for each value in the (implicit) input-list if it's equal to `y`
                   #  (1 if truthy; 0 if falsey)
   ƶ               #  Multiply each by its 1-based index
    0K             #  Remove all 0s
      D            #  Duplicate it
       ā           #  Push a list [1, length] without popping the list itself
        α          #  Get the absolute difference at the same indices
         γ         #  Split it into chunks of the same values
          g       #  Get the length of each
            £      #  And split the duplicated indices-list into those parts
                   # (this map basically groups 1-based indices per value.
                   #  i.e. input [1,1,2,1,1,2,2,1,1] becomes [[[1,2],[4,5],[8,9]],[[3],[6,7]]])
 }D                # After the map: duplicate the mapped 3D list
   2F              # Loop 2 times:
     ε             #  Map the 3D list of indices to:
      ¾i           #   If the counter_variable is 1:
        ε          #    Map each list `y` in the 2D inner list to:
         н         #     Leave the first value
         ygi      #     And if there is more than one index:
             yθ    #      Push the last value as well
             yg<i  #      If there are exactly two indices:
              '|  '#       Push string "|"
             ë     #      Else (there are more than two indices)
              „..  #       Push string ".."
                 #      And join the first and last value by this string
        }}         #    Close the if-statement and map
      ë            #   Else:
       ˜           #    Flatten the 2D list
      }'|ý        '#   After the if-else: join by "|"
          „=>«     #   Append "=>"
       yнн         #   Get the very first index of this 2D list
          <        #   Decrease it by 1 to make it 0-based
      I    è       #   And index it into the input-list to get its value again
            «      #   Which is also appended after the "=>"
                 #  After the map: triplicate the result
       ®.V         #  Execute function 1 from the register
       g           #  Get the amount of items in the triplicated list
        F          #  Loop that many times:
         D         #   Duplicate the list
          N._      #   Rotate it the index amount of times
          ć        #   Extract the head; pop and push remainder and head
           '>¡    '#   Split this head by ">"
              X.V  #   And then function 2 is executed again from variable `X`
        }\         #  After the loop: discard the list that is still on the stack
          ¼        #  And increase the counter_variable by 1
                 # After looping twice: push the global array
     é             # Sort it by length
      Igi }        # If the input only contained a single item:
         ¦         #  Remove the very first item
           н       # And then only leave the first item
                   # (which is output implicitly as result)

मेरा यह 05AB1E टिप देखें (अनुभाग संपीड़ित तार शब्दकोश का हिस्सा नहीं कैसे? ) को समझने के लिए क्यों …ˆ†=>है "others=>"

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