स्टैकिन बोर्ड्स


11

मेरे पास बोर्डों का एक गुच्छा है मुझे जितना संभव हो उतना छोटा स्थान में ढेर करने की आवश्यकता है। दुर्भाग्य से, बोर्ड गिर जाते हैं यदि मैं उन्हें 10 से अधिक उच्च करता हूं। मुझे यह बताने के लिए एक कार्यक्रम की आवश्यकता है कि बोर्डों को जितना संभव हो उतना कम क्षैतिज स्थान लेने के लिए स्टैक किया जाए, बिना बोर्डों को दस से अधिक ऊंचा किए बिना, या खाली जगह पर बोर्ड लटकाए बिना।

आपका कार्य:

एक प्रोग्राम या फ़ंक्शन लिखें, जब बोर्ड की लंबाई वाली एक सरणी दी जाती है, तो ASCII के रूप में आउटपुट, बोर्ड को स्टैक करने के तरीके को जितना संभव हो उतना क्षैतिज स्थान को संरक्षित करने के लिए, 10 से अधिक उच्च या किसी भी हिस्से के बिना बोर्डों को स्टैक करने के बिना। बोर्ड खाली जगह पर लटका हुआ है। आपकी ASCII कला को एक अलग चरित्र का उपयोग करके दिखाए गए प्रत्येक के साथ बोर्डों के कॉन्फ़िगरेशन को दिखाना चाहिए। इसमें अधिकतम 20 बोर्ड होंगे। उदाहरण के लिए, यदि इनपुट [2,2,4,2,2,4,4,4] था, एक संभावित आउटपुट है:

dhh
dgg
dff
dee
abc
abc
abc
abc

जो एक स्थिर विन्यास है (हालांकि यह वास्तविक जीवन में ~ 0.1 सेकंड में गिर जाएगा)।

इनपुट:

एक सरणी जिसमें 20 पूर्णांक होते हैं, जो बोर्डों की लंबाई दिखाते हैं।

आउटपुट:

ASCII कला बोर्डों के विन्यास दिखा रहा है, जैसा कि ऊपर उल्लिखित है।

परीक्षण के मामलों:

ध्यान दें कि परीक्षण मामलों के लिए अन्य समाधान हो सकते हैं, और प्रत्येक बोर्ड के लिए दिखाए गए वर्ण भिन्न हो सकते हैं।

[12,2,2,2,3,4,4,8,8]        -> ffgghhiii
                               ddddeeeeeeee
                               bbbbbbbbcccc
                               aaaaaaaaaaaa

[4,4,4,4,4,4,4,4,4,4,4,4]   -> llll
                               aaaa
                               cfghk
                               cfghk
                               cfghk
                               cfghk
                               debij
                               debij
                               debij
                               debij

[4,4,4,4,4,4,3,3,3,2,2,2,1] -> jjml
                               iiil
                               hhhk
                               gggk
                               ffff
                               eeee
                               dddd
                               cccc
                               bbbb
                               aaaa

स्कोरिंग:

यह , बाइट्स जीत में सबसे कम स्कोर

जवाबों:


3

पायथन 3 , 513 512 511 509 499 497 485 465 459 458 444 बाइट्स

अविश्वसनीय रूप से खराब रनटाइम, कुछ बिंदु पर समाप्त होगा

e,j,c=enumerate,len,range
def f(n,p=[],o=97):
    r,l,m=lambda x:min(b,f(n[:i]+n[i+1:],x,o+1),key=j),chr(o),j(p)
    b=[p,l*(sum(n)*2+m)][n>[]]
    for i,a in e(n):
        for h,d in e(p):
            if a<11-j(d):b=r([p[f]+l*a*(f==h)for f in c(m)])
            if(j(d)<10)*all(j(x)==j(d)for x in p[h:h+a])*(a<=m-h):b=r([p[f]+l*(h<=f<h+a)for f in c(m)])
        if a<11:b=r(p+[l*a])
        b=r(p+[l]*a)
    return["\n".join("".join(9-u<j(x)and x[9-u]or" "for x in b)for u in c(10)),b][o>97]

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

संपादित करें: - 2 -8 बाइट्स @Mr के लिए धन्यवाद Xcoder संपादित करें: -8 बाइट्स @ नॉटजैगन के लिए धन्यवाद

व्याख्या

e,j,c=enumerate,len,range      
         # These built-ins are used a lot
def f(n,p=[],o=97):
         # n is the remaining blocks
         # p is the current stack
         # o is the ASCI code for the next letter to use
    r,l,m=lambda x:min(b,f(n[:i]+n[i+1:],x,o+1),key=j),chr(o),j(p)
         # r is the recursive call, that also selects the smallest stack found
         # l is the letter to use next
         # m is the length of the current stack
    b=[p,l*(sum(n)*2+m)][n>[]]
         # Sets the current best, if there are no remaining blocks, select the found stack, else we set it to be worse than the possible worst case
    for i,a in e(n):
         # Loop through all the remaining blocks
        for h,d in e(p):
         # Loop through all the columns in the current stack
            if a<11-j(d):b=r([p[f]+l*a*(f==h)for f in c(m)])
         # If we can place the current block vertically in the current column, try it
            if(j(d)<10)*all(j(x)==j(d)for x in p[h:h+a])*(a<=m-h):b=r([p[f]+l*(h<=f<h+a)for f in c(m)])
         # If we can place the current block horizontally starting in the current column, try it
        if a<11:b=r(p+[l*a])
         # If the current block is lower than 10, try place it vertically to the right of the current stack
        b=r(p+[l]*a)
         # Try to place the current horizontally to the right of the current stack
    return["\n".join("".join(9-u<j(x)and x[9-u]or" "for x in b)for u in c(10)),b][o>97]
         # Return the best choice if we aren't in the first call to the function, that is the next letter is a. Else return the found best option formatted as a string

पायथन 3 , 587 बाइट्स

वास्तव में परीक्षण के कुछ मामलों के लिए टीआईओ पर चलने योग्य

e,j,c=enumerate,len,range
def f(n,p=[],o=97,b=[]):
    if(not n):return p
    if not b:b="a"*sum(n)*2
    r,q,s,l,m=lambda x:q(f(n[:i]+n[i+1:],x,o+1,b)),lambda x:[b,x][j(b)>j(x)],b,chr(o),j(p)
    if j(b)<=m:return b
    for i,a in e(n):
        for h,d in e(p):
            if a<11-j(d):b=r([p[f]+l*a*(f==h)for f in c(m)])
            if j(d)<10 and a<=m-h and all(map(lambda x:j(x)==j(d),p[h:h+a])):b=r([p[f]+l*(h<=f<h+a)for f in c(m)])
        if s==b:
            if a<11and m+1<j(b):b=r(p[:]+[l*a])
            if m+a<j(b):b=r(p[:]+[l for r in c(a)])
    return["\n".join("".join(map(lambda x:" "if u>=j(x)else x[u],b))for u in c(9,-1,-1)),b][o>97]

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

दोनों समाधानों को संभवत: थोड़ा-थोड़ा किया जा सकता है।




श्री Xcoder, दूसरा एक 50 बाइट्स के पास से कम किया जा सकता है, मैं सिर्फ पहले एक दूसरे के लिए परिवर्तन लागू नहीं किया है
Halvard Wil

मुझे पता है कि दूसरे को बहुत अधिक गोलबंद किया जा सकता है, लेकिन पहले वाले बदलाव सहायक होने चाहिए।
श्री Xcoder

1
आपने मेरे उत्थान को अर्जित किया, एक अद्भुत व्याख्या के साथ कुछ महान कोड के लिए, जो बहुत प्रयास और विचार दिखाता है। पीपीसीजी को बधाई और स्वागत है!
श्री एक्सकोडर
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.