एक नॉनोग्राम पहेली बनाएं


24

एक नॉनोग्राम एक दो-आयामी तर्क पहेली है जो कुछ इस तरह दिखता है (गेम पिक्सो , मेरे पसंदीदा नॉनोग्राम गेम से स्क्रीनशॉट ):

एक खाली नॉनोग्राम बोर्ड

खेल का लक्ष्य यह पता लगाना है कि कौन सी छवि उन संख्याओं को एन्कोडिंग कर रही है। नियम सरल हैं: एक स्तंभ या पंक्ति पर एक संख्या का मतलब है कि उस स्तंभ या पंक्ति में, कि एक पंक्ति में कई बक्से भरे हुए हैं। उदाहरण के लिए, ऊपर की छवि में नीचे की पंक्ति में कोई बक्सा नहीं होना चाहिए, जबकि इसके ऊपर वाले हिस्से में इसके सभी बॉक्स भरे होने चाहिए। नीचे से तीसरी पंक्ति में 8 भरे हुए बॉक्स हैं, और वे सभी एक पंक्ति में होंगे।

एक ही कॉलम या पंक्ति के लिए दो या अधिक संख्याओं का मतलब है कि भरे हुए बक्से के कई "रन" हैं, जिनकी लंबाई के बीच कम से कम एक जगह है। आदेश संरक्षित है। उदाहरण के लिए, उपरोक्त छवि के बहुत दाहिने स्तंभ पर बक्से में तीन भरे हुए हैं, उनके नीचे कम से कम एक स्थान है, और फिर एक और बॉक्स में भरा हुआ है।

यहाँ वही पहेली है, लगभग पूरी:

लगभग समाप्त नॉनोग्राम बोर्ड

(एक्स महत्वपूर्ण नहीं हैं, वे सिर्फ एक संकेत है कि खिलाड़ी खुद को यह कहने के लिए छोड़ देता है कि "यह वर्ग निश्चित रूप से भरा नहीं है"। माइन्सवेपर में झंडे के बारे में सोचें। उनका कोई नियम नहीं है।)

उम्मीद है कि आप देख सकते हैं, उदाहरण के लिए, "2 2" कहने वाले संकेत वाले मध्य स्तंभों में बक्से में दो 2-लंबाई के रन हैं।

आपका मिशन, क्या आपको इसे स्वीकार करना चाहिए, एक प्रोग्राम या फ़ंक्शन लिखना है जो इस तरह से एक पहेली बनाएगा। आपको स्टड पर या एक तर्क के रूप में एक पूर्णांक (5 <= n <= 50) के रूप में बोर्ड का आकार दिया जाता है (कोई कारण नहीं है कि एक नॉनोग्राम पहेली को वर्ग होना चाहिए, लेकिन इस चुनौती के लिए यह होगा)। उसके बाद, आपको छवि में भरे हुए और अधूरे वर्गों का प्रतिनिधित्व करते हुए क्रमशः 1s और 0s की एक श्रृंखला दी जाएगी। उनमें से पहला एन शीर्ष पंक्ति है, फिर अगली पंक्ति, आदि। आप 2 * 1 कोशिकाओं के एक बोर्ड को वापस करने के लिए लौटेंगे या प्रिंट करेंगे (क्योंकि वे बेहतर दिखते हैं, और यह आपको स्तंभ के लिए 2-अंकों के संकेत के लिए जगह देता है। ), वे सभी खाली हैं, जो इनपुट डेटा के अनुरूप संकेत के साथ हैं।

आउटपुट स्वरूप

आउटपुट स्वरूप

नमूना

इनपुट:

./nonogram <<< '5 0 1 1 1 0 1 1 0 1 1 1 0 1 0 1 1 1 0 1 1 0 1 1 1 0'
                                 OR
      n(5,[0,1,1,1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1,1,1,0])

छवि:

पहला उदाहरण छवि

आउटपुट:

           1
         2 1 2
       3 2 1 2 3
     +----------
    3|
  2 2|
1 1 1|
  2 2|
    3|

इनपुट:

./nonogram <<< '15 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 0 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 1 0 1 1 1 1 0 0 0 1 1 1 1 1 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1'

छवि:

दूसरा उदाहरण छवि

आउटपुट:

                   1                       1
                 1 1 3       3 5   5 3   3 1
                 7 2 3 2 4 2 3 210 2 3 0 4 215
               +------------------------------
              2|
              1|
              1|
              1|
              1|
            1 1|
        3 3 1 1|
        1 5 1 1|
          3 5 3|
          1 5 1|
          1 3 1|
      1 1 1 1 1|
1 1 1 1 1 1 1 1|
           11 3|
           11 3|

स्पष्टीकरण

  • आपके आउटपुट को सॉल्व करने योग्य पहेली नहीं होना चाहिए। सभी नॉनोग्राम्स सॉल्व नहीं होते हैं, लेकिन यह आपकी चिंता का विषय नहीं है। बस इनपुट के अनुरूप संकेत उत्पन्न करते हैं, चाहे वे एक अच्छी पहेली के लिए बनाते हों या नहीं।
  • एक प्रोग्राम जो कमांड लाइन पर तर्क लेता है उसे अनुमति दी जाती है। यह ऊपर कहा गया है, लेकिन गलत विचार आना संभव है। यही स्पष्टीकरण हैं।
  • 0एक पंक्ति या स्तंभ के लिए मुद्रण , जिसमें कोई बक्सा नहीं है, अनिवार्य है। मैं इसे शब्दों के साथ कहीं भी नहीं कहता, लेकिन यह नमूना डेटा में है।

मैं बस अपने समाधान के साथ कर रहा हूँ। क्या हम मान सकते हैं कि एक पंक्ति या स्तंभ में 3-अंकों की संख्या नहीं होगी?
उर्फ

2
@voidpigeon: 5<=n<=50युक्ति है, इसलिए कोई भी 3-अंकीय संख्या नहीं हो सकती है
काइल कानोस

इस सवाल को पोस्ट करने के बाद, मैंने खुद एक समाधान पर काम करना शुरू कर दिया। मैं इसे अभी तक पोस्ट नहीं करूंगा ( इस मेटा उत्तर के अनुसार ), लेकिन मैं अपनी बाइट की गिनती पोस्ट करूंगा ताकि आप लोगों को इसके लिए प्रयास करने के लिए कुछ मिलें: पायथन 2.7 में 404 बाइट्स
भूमिगत

क्या आपका पहला उदाहरण आउटपुट में एक से -अधिक नहीं होना चाहिए?
वेंचरो

@Ventro आप सही हैं! मुझे पता था कि मैं ऐसा करने के लिए एक कार्यक्रम कैसे लिखूंगा, लेकिन मैंने वास्तव में इसे अभी तक नहीं किया था, इसलिए मेरे नमूना आउटपुट हाथ से थे। ऊप्स! (मैंने दूसरा नमूना आउटपुट भी तैयार किया, लेकिन मैंने कोई जवाब देने से पहले इसे ठीक कर लिया।)
भूमिगत

जवाबों:


9

GolfScript, 128 वर्ण

~](:k/.{{1,%{,}%.!,+}%}:^~{' ':s*}%.{,}%$-1=:9{s*\+9~)>'|'n}+%\zip^.{,~}%$0=){.~[s]*@+>{s\+-2>}%}+%zip{9)s*\n}%\[9s*'+''--'k*n]\

अंतरिक्ष में अलग-अलग संख्याओं के रूप में इनपुट STDIN पर प्रदान किया जाना है।

आप यहां उदाहरण का परीक्षण कर सकते हैं

टिप्पणी कोड:

# Parse the input into an 2D array of digits. The width is saved to variable k
~](:k/

# Apply the code block ^ to a copy of this array
.
{                # begin on code block
  {              # for each line
   1,%           #   split at 0s (1, => [0]) (leading, trailing, multiple 0s are 
                 #   removed because of operator % instead of /)
   {,}%          #   calculate the length of each run of 1s                 
   .!,+          #   special case: only zeros, i.e. []
                 #   in this case the ! operator yiels 1, thus [0], else []
  }%             # end for
}:^              # end of code block
~                # apply

# Format row headers
{' ':s*}%        # join numbers with spaces
.{,}%$-1=:9      # calulate the length of the longest row header
                 # and save it to variable <9>
{                # for each row
  s*\+           #   prepend padding spaces
  9~)>           #   and cut at length <9> from the right
  '|'n           #   append '|' and newline
}+%              # end for

# Format column headers
\zip^            # transpose input array and apply the code block ^
                 # i.e. calculate length of runs
.{,~}%$0=)       # determine (negative) length of the longest column header
{                # for each column
  .~[s]*@+       #   prepend enough spaces
  >              #   and cut at common length (from right)
  {s\+-2>}%      #   format each number/empty to 2 chars width
}+%              # end for
zip              # transpose column header into output lines
{9)s*\n}%        # prepend spaces to each line and append newline

# Inject separator line
\[
9s*              # spaces
'+'              # a plus sign
'--'k*           # k (width) number of '--'
n                # newline
]\

1
+1 अच्छा, मैंने इस पोस्ट से काफी अच्छी तरकीबें सीखी हैं
क्रिस्टियन लुपस्कू

मैं इसे 123 वर्णों में शामिल करने में कामयाब रहा: ~](:k/.zip\]{{1,%{,}%.!,+}%}/{' ':^*}%{.{,}%$-1=}:f~:r{^*\+r~)>'|'n}+%\f{.~)\[^]*@+>{^\+-2>}%}+%zip{r)^*\n}%r^*'+''--'k*n]((किसी कारण के लिए lettercount.com कहता है कि यदि आप इसे कॉपी करते हैं तो 125 वर्ण कहते हैं, लेकिन मैं आपको आश्वस्त करता हूं, यह 123 वर्ण है)। एल्गोरिथ्म के कुछ हिस्सों को बदल दिया गया है, लेकिन बहुमत अभी भी समान है। मैंने कुछ परिवर्तनशील नामों को भी बदल दिया (9 जैसा कि एक चर स्मार्ट है, लेकिन साथ ही भ्रमित करने वाला भी है), लेकिन यदि आप चाहें तो आप उन्हें वापस बदल सकते हैं।
अस्थिरता

7

रूबी, २१६ 255

n=$*.shift.to_i;k=*$*.each_slice(n)
u=->k{k.map{|i|r=i.join.scan(/1+/).map{|i|"%2d"%i.size}
[*["  "]*n,*r[0]?r:" 0"][-n,n]}}
puts u[k.transpose].transpose.map{|i|" "*(n-~n)+i*""},"  "*n+?++"--"*n,u[k].map{|i|i*""+?|}

हालांकि यह प्रश्न में दिए गए सटीक नमूना आउटपुट का उत्पादन नहीं करता है, यह चश्मा का पालन करता है। उदाहरणों के लिए केवल इतना अंतर है कि मैं कुछ प्रमुख स्थानों / newlines को प्रिंट करता हूं।

उदाहरण:

$ ruby nonogram.rb 15 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 0 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 1 0 1 1 1 1 0 0 0 1 1 1 1 1 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1
# empty lines removed for brevity
                                  1                       1  
                                1 1 3       3 5   5 3   3 1  
                                7 2 3 2 4 2 3 210 2 3 0 4 215
                              +------------------------------
                             2|
                             1|
                             1|
                             1|
                             1|
                           1 1|
                       3 3 1 1|
                       1 5 1 1|
                         3 5 3|
                         1 5 1|
                         1 3 1|
                     1 1 1 1 1|
               1 1 1 1 1 1 1 1|
                          11 3|
                          11 3|

बदलाव का:

  • 240 -> 231: स्टड के बजाय कमांड लाइन तर्कों का उपयोग करने के लिए परिवर्तित इनपुट प्रारूप।
  • 231 -> 230: मान चेक को से स्थानांतरित करके एक स्थान को हटा chunkदिया map
  • 230 -> 226: [nil]कॉल करने के बजाय घटाना Array#compact
  • 226 -> 216: संकेत निर्माण को सरल बनाएं।

आप कुछ अतिरिक्त न्यूलाइन्स और स्पेस प्रिंट करते हैं, लेकिन अभी तक मेरे सभी परीक्षणों में वे "0 या अधिक" विनिर्देश के अनुरूप हैं, इसलिए आप अच्छे हैं। मैं आपको अभी चेतावनी दे रहा हूं, हालांकि, अगर मैं अपने मॉनिटर के बाईं ओर हवा में तैरती संख्याओं को देखना शुरू करता हूं, तो मुझे इस उत्तर को अयोग्य ठहराना होगा :)
भूमिगत

1
@undergroundmonorail: आउटपुट को इस तरह से प्रिंट किया जाता है कि length(leading spaces + numbers to the left) == 2*nऔर height(leading newlines + numbers at the top) == n... इसलिए जब तक आपका मॉनिटर 3*n+1 × 2*n+2पात्रों के लिए काफी बड़ा है , आपको मुझे अयोग्य नहीं ठहराना चाहिए। :)
वेंचरो

4

रूबी, 434

n=$*[i=0].to_i
a,b=[],[]
a.push $*.slice!(1..n)*""while $*.size>1
(b.push a.map{|c|c[i]}*"";i+=1)while i<n
a,b=*[a,b].map{|c|c.map{|d|e=d.split(/[0]+/).map(&:size).select{|f|f>i=0}.map &:to_s;(e.size>0)?e:[?0]}}
m,k=*[a,b].map{|c|c.map(&:size).max}
s="  "*m
k.times{$><<s+"  "+b.map{|c|(" "+((c.size==k-i)?(c.shift):(" ")))[-2..-1]}*"";i+=1;puts}
puts s+" "+?++?-*n*2
a.each{|c|puts"  "*(m-c.size)+" "+c.map{|d|(" "+d)[-2..-1]}*""+?|}

आप इसे कैसे चलाते हैं? मैंने कोशिश की ruby $yourprogram <<< $inputलेकिन मिल गया ruby_nanograms:7:in '<main>': undefined method '+' for nil:NilClass (NoMethodError)
अंडरग्राउंडोरेल

@undergroundmonorail ruby nonogram.rb 2 1 0 0 1एक 2x2 के लिए
afuous

यह एक अच्छा जवाब है, लेकिन आप 0दूसरे उदाहरण में चौथे-अंतिम कॉलम के लिए प्रिंट नहीं करते हैं ।
भूमिगत

मैंने अभी देखा कि +------... लाइन एक बहुत अधिक रिक्त स्थान से प्रेरित है, भी।
भूमिगत

1
@undergroundmonorail ने दोनों को तय किया।
afuous

4

गोल्फस्क्रिप्ट 149 147

कोड

~](:s/.zip{{[0]%{,`}%['0']or}%.{,}%$)\;:¶;{.,¶\-[' ']*\+}%}:f~¶:v;\[f~]\zip{{{.,2\-' '*\+}%''*}:d2*)' '*:z\+{puts}:o~}%z(;'+'s'-'2**++o~{d'|'+o}/

संपादन:

  • बेकार जगह को हटा दिया
  • putsएक और चर को बचाने के लिए एक पुन: प्रयोज्य वन-चार फ़ंक्शन को परिभाषित किया

ऑनलाइन डेमो

कोड का कुछ एनोटेट संस्करण

# split lines
~](:s/

# make transposed copy
.zip

#prepare numbers to show in the header
{{[0]%{,`}%['0']or}%.{,}%$)\;:¶;{.,¶\-[' ']*\+}%}:f~¶:v;

# prepare numbers to show in the left column
\[f~]\zip

#print header (vertical hints)
{  {{.,2\-' '*\+}%''*}:d~  2*)' '*:z\+puts}%

#print first line
z(;'+'s'-'2**++puts

#print horizontal hints
~{d'|'+ puts}/

4

जावास्क्रिप्ट (E6) 314 334 357 410

N=(d,l)=>{J=a=>a.join(''),M=s=>(s.match(/1+/g)||['']).map(x=>x.length),f=' '.repeat(d+1),c=[n='\n'],o=n+f+'+'+'--'.repeat(d);for(i=-1;++i<d;)c[i]=M(J(l.map((e,p)=>p%d-i?'':e))),o+=n+(f+J(M(J(l).substr(i*d,d)).map(P=n=>n>9?n:n<10?' '+n:'  '))+'|').slice(-d-2);for(;--i;)o=n+f+' '+J(c.map(e=>P(e.pop())))+o;return o}

Ungolfed

N=(d,l)=> {
  J = a => a.join(''),
  M = s => (s.match(/1+/g)||['']).map(x=>x.length),
  f=' '.repeat(d+1), c=[n='\n'], o=n+f+'+'+'--'.repeat(d);
  for(i = -1; ++i < d;)
    c[i] = M(J(l.map((e,p)=>p%d-i?'':e))),
    o += n+(f+J(M(J(l).substr(i*d,d)).map(P=n=>n>9?n:n<10?' '+n:'  '))+'|').slice(-d-2);
  for(;--i;)
    o=n+f+' '+J(c.map(e=>P(e.pop())))+o;
  return o
}

प्रयोग

N(5,[0,1,1,1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1,1,1,0])

N(15,[0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,0,0,1,0,1,0,0,1,0,0,0,1,1,1,1,1,0,1,0,1,1,1,1,0,0,0,1,1,1,1,1,0,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,0,0,0,1,0,0,0,1,0,0,0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1])

इतिहास संपादित करें

1 हटाए गए regexp को स्तंभों को खोजने के लिए उपयोग किया
जाता है। ऑवरकिल 2 सिंपल बेहतर है। एक स्ट्रिंग के लिए उत्पादन, एक सरणी के लिए नहीं। हटाए गए सहायक फ़ंक्शन FILL (F)
3 और भी सरल। मैं इससे बेहतर नहीं कर सकता। फिर भी गोल्फस्क्रिप्ट की तुलना नहीं कर सकते :(


अच्छा लगा। मैंने एक जावास्क्रिप्ट संस्करण की भी कोशिश की, लेकिन लगभग 500 बाइट्स के साथ कुछ समाप्त हो गया और मैंने फैसला किया कि मैं इसे यहाँ रखने के लिए बहुत बड़ा था। मूल चर नाम (यदि आपके पास अभी भी है) के साथ अनगुल्ड संस्करण पोस्ट करना अच्छा होगा। इसके अलावा, आप इसे कैसे चलाते हैं? अगर मैं इसे क्रोम कंसोल विंडो में पेस्ट करता हूं तो मुझे "ReferenceError: असाइनमेंट में लेफ्ट-हैंड साइड अवैध" मिलता है। क्या बदलने से पहले कुछ जोड़ना या जोड़ना है?
टाइगरो

@tigrou क्षमा करें "=>" सिंटैक्स केवल फ़ायरफ़ॉक्स में काम करता है। विविधताएं: सी कॉलन संकेत, डी आयाम, इनपुट की सूची, ओ आउटपुट, आई लूप वेरिएबल, क्यू और जेड
टेम्प


@nderscore कोड के साथ घूमता हुआ, मुझे 326 मिला। आपके कोड में आर इनिशियलाइज्ड नहीं है (आसान गलती जब आप बार-बार कोशिश करते हैं ...)
edc65

1

आर, 384 चार्ट

a=scan();p=function(x)paste(x,collapse="");P=paste0;s=sapply;l=length;f=function(i)lapply(apply(matrix(a[-1],nr=a,b=T),i,rle),function(x)if(any(x$v)){x$l[!!x$v]}else{0});g=function(j,i)apply(s(j,function(x)sprintf("%2s",c(rep("",max(s(j,l))-l(x)),x))),i,p);c=P(g(f(1),2),"|");d=g(f(2),1);h=p(rep(" ",nchar(c[1])-1));e=P(h,"+",p(rep("-",nchar(d[1]))));d=P(h," ",d);cat(d,e,c,sep="\n")

इंडेंटेशन और कुछ स्पष्टीकरण के साथ:

a=scan() #Takes input

p=function(x)paste(x,collapse="") #Creates shortcuts
P=paste0
s=sapply
l=length

#This function finds the number of subsequent ones in a line (using rle = run length encoding).
#It takes 1 or 2 as argument (1 being row-wise, 2 column-wise
f=function(i)lapply(apply(matrix(a[-1],nr=a,b=T),i,rle),function(x)if(any(x$v)){x$l[!!x$v]}else{0})

#This function takes the result of the previous and format the strings correctly (depending if they are rows or columns)
g=function(j,i)apply(s(j,function(x)sprintf("%2s",c(rep("",max(s(j,l))-l(x)),x))),i,p)

c=paste0(g(f(1),2),"|") #Computes the rows
d=g(f(2),1) #Computes the columns
h=p(rep(" ",nchar(c[1])-1)) 
e=paste0(h,"+",p(rep("-",nchar(d[1])))) #Prepare vertical border
d=paste0(h," ",d) #Pad column indices with spaces
cat(d,e,c,sep="\n") #Prints

उपयोग:

> a=scan();p=function(x)paste(x,collapse="");P=paste0;s=sapply;l=length;f=function(i)lapply(apply(matrix(a[-1],nr=a,b=T),i,rle),function(x)if(any(x$v)){x$l[!!x$v]}else{0});g=function(j,i)apply(s(j,function(x)sprintf("%2s",c(rep("",max(s(j,l))-l(x)),x))),i,p);c=P(g(f(1),2),"|");d=g(f(2),1);h=p(rep(" ",nchar(c[1])-1));e=P(h,"+",p(rep("-",nchar(d[1]))));d=P(h," ",d);cat(d,e,c,sep="\n")
1: 15 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 0 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 1 0 1 1 1 1 0 0 0 1 1 1 1 1 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1
227: 
Read 226 items
                    1                       1  
                  1 1 3       3 5   5 3   3 1  
                  7 2 3 2 4 2 3 210 2 3 0 4 215
                +------------------------------
               2|
               1|
               1|
               1|
               1|
             1 1|
         3 3 1 1|
         1 5 1 1|
           3 5 3|
           1 5 1|
           1 3 1|
       1 1 1 1 1|
 1 1 1 1 1 1 1 1|
            11 3|
            11 3|

1

सी - 511

सी निश्चित रूप से अच्छी तरह से आउटपुट स्वरूपण के लिए नहीं बनाया गया था। वर्ण गणना में केवल आवश्यक स्थान / नई लाइनें शामिल हैं।

इनपुट STDIN से है, स्पेस से अलग किए गए नंबर।

#define P printf
#define L(x) for(x=0;x<s/2+1;x++)
#define l(x) for(x=0;x<s;x++)
#define B(x,y) x[i][j]||y==s/2?P("%2d",x[i][j]):P("  ");
a[50][50],i,j,s,h[25][50],v[50][25],H[50],V[50],x[25],y[25];
main(){
    scanf("%d",&s);
    L(j)x[j]=y[j]=s/2+1;
    l(i)l(j)scanf("%d",&a[i][j]);
    for(i=s-1;i>=0;i--)
        for(j=s-1;j>=0;j--)
            a[i][j]?
                !H[j]&&(x[j]--,H[j]=1),
                h[x[j]][j]++,
                !V[i]&&(y[i]--,V[i]=1),
                v[i][y[i]]++:
            (H[j]=V[i]=0);
    L(i){
        L(j)P("  ");
        P(" ");
        l(j)B(h,i);
        P("\n");
    }
    L(i)P("  ");
    P("+");
    l(i)P("--");
    P("\n");
    l(i){
        L(j)B(v,j);
        P("|\n");
    }
}

1

यह कुछ दिनों का है और किसी ने अजगर में जवाब नहीं दिया है, इसलिए यहां मेरा (शायद बहुत गरीब) प्रयास है:

अजगर 2.7 - 404 397 380 बाइट्स

def p(n,m):
 k=str.join;l=[];g=lambda y:[['  ']*(max(map(len,y))-len(t))+t for t in[[' '*(a<10)+`a`for a in map(len,k("",c).split('0'))if a]or[' 0']for c in y]]
 while m:l+=[map(str,m[:n])];m=m[n:]
 x=g(l);j=k('\n',['  '*max(map(len,x))+'+'+k("",a)for a in zip(*[list(a)+['--']for a in g(zip(*l))])]);return j.replace('+',' ',j.count('+')-1)+'\n'+k('\n',[k("",a+['|'])for a in x])

मैं जल्द ही एक ungolfed संस्करण पोस्ट करूंगा, लेकिन फिलहाल मुझे लगता है कि यह बहुत पठनीय है। :)

संपादित करें: असंगठित संस्करण लिखते समय, मैंने कुछ सुधारों पर ध्यान दिया जिन्हें मैं जोड़ सकता था जो काफी महत्वपूर्ण थे! किसी कारण के लिए, जो मैं समझा नहीं सकता, उसके पास अब बाईं ओर शीर्ष और रिक्त स्थान पर अतिरिक्त newlines है (भले ही मुझे नहीं लगता कि मैंने कुछ भी कार्यात्मक बदल दिया है), लेकिन यह अभी भी कल्पना को पूरा करता है।Ungolfed संस्करण आ रहा है!

Ungolfed:

def nonogram(board_size, pixels):
    def hints(board):
        output = []
        for row in board:
            # Convert the row to a string of 1s and 0s, then get a list of strings
            # that came between two 0s.
            s = "".join(row).split('0')

            # A list of the length of each string in that list.
            l = map(len, s)

            # We now have our horizontal hints for the board, except that anywhere
            # there were two 0s consecutively we have a useless 0.
            # We can get rid of the 0s easily, but if there were no 1s in the row at
            # all we want exactly one 0.
            # Solution:
            output.append([h for h in l if h != 0] or [0])
            # In this context, `foo or bar` means `foo if foo is a truthy value, bar
            # otherwise`.
            # An empty list is falsey, so if we strip out all the strings we hardcode
            # the 0.
        return output

    def num_format(hints):
        # For both horizontal and vertical hints, we want a space before single-
        # digit numbers and no space otherwise. Convert hints to strings and add
        # spaces as necessary.
        output = []

        for row in hints:
            output.append([' '*(a < 10) + str(a) for a in row])
            # Multiplying a string by n repeats it n times, e.g. 'abc'*3=='abcabcabc'
            # The only numbers that need a space are the ones less than 10.
            # ' '*(a < 10) first evaluates a < 10 to get a True or False value.
            # Python automatically converts True to 1 and False to 0.
            # So, if a is a one digit number, we do `' '*(1) + str(a)`.
            # If it's a two digit number, we do `' '*(0) + str(a)`.
        return output

    def padding(hints):
        output = []
        longest = max(map(len, hints)) # how long is the longest row?
        for row in hints:
            output.append(['  ']*(longest - len(row)) + row)
            # Add '  ' to the beginning of every row until it's the same length
            # as the longest one. Because all hints are two characters wide, this
            # ensures all rows of hints are the same length.
        return output

    board = []

    while pixels: # non-empty list == True
        # Make a list of the first (board_size) pixels converted to strings, then
        # add that list to board. Remove those pixels from the list of pixels.
        # When pixels is empty, board has a seperate list for each row.
        board.append([str(n) for n in pixels[:board_size]])
        pixels = pixels[board_size:]

    horizontal_hints = padding(num_format(hints(board)))

    vertical_hints = padding(num_format(hints(zip(*board))))
    # zip(*l) is a common way to transpose l.
    # zip([1,2,3], [4,5,6], [7,8,9]) == [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
    # the star operator unpacks an iterable so the contents can be used as
    # multiple arguments, so
    # zip(*[[1,2,3],[4,5,6],[7,8,9]]) is the same as what we just did.
    # Transposing the board and getting the horizontal hints gives us the
    # vertical hints of the original, but transposed. We'll transpose it back,
    # but we'll also add '--' to the end of all of them to make up the line
    vertical_hints = zip(*[a + ['--'] for a in vertical_hints])

    # add n spaces, where n is the length of the longest horizontal hint, plus
    # one space to the beginning of each line in the vertical hints, then join
    # with newlines to make it all one string.
    vertical_hints = '\n'.join(['  '*max(map(len, horizontal_hints)) + '+' +
                               ''.join(a) for a in vertical_hints])

    # find the number of plus signs in the string
    # replace the first (that many - 1) plus signs with spaces
    vertical_hints = vertical_hints.replace('+', ' ', vertical_hints.count('+')-1)

    # add a pipe to each row of horizontal hints, then join it with newlines
    horizontal_hints = '\n'.join([''.join(a + ['|']) for a in horizontal_hints])

    # add and return
    return vertical_hints + '\n' + horizontal_hints

पठनीयता की खातिर कुछ बदलाव किए गए ( gतीन नामित कार्यों में विभाजित होने के कारण , forलूप्स में बनाई गई जटिल सूची समझ ) लेकिन तार्किक रूप से यह ठीक उसी तरह से काम करता है।

यही कारण है कि यह भ्रामक है कि यह एक अतिरिक्त स्थान और newlines नहीं छापता है, जबकि गोल्फ एक करता है। ¯ \ _ (ツ) _ / ¯


1
उम्म, मुझे आपका हल नहीं मिल रहा है। (क्षमा करें, वर्णों की संख्या के बारे में सिर्फ एक भयानक मजाक, मुझे कोई आपत्ति नहीं है :))
दरवाज़े

@ डोर अहा! अपना HTTP त्रुटि कोड चुटकुले बनाने की कोशिश करें! : P
भूमिगत
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.