कुछ तौलिए मोड़ो!


19

मैंने कहीं सुना है कि एक चीज जो तकनीक अभी तक नहीं कर सकती है वह है तौलिए 1 । तो अब उस बयान को झूठा साबित करना आपका काम है!

इनपुट के रूप में एक स्ट्रिंग को देखते हुए, आयतों (तौलिए) से बना, निम्न की तरह, प्रत्येक तौलिया को आधे से दो बार मोड़ें। उदाहरण के लिए:

+------+    +------+        +--+
|      |    |      |        |  |
|      |    |      |        |  |
|      | -> +------+ ->     +--+
|      |    
|      |    
|      |    
+------+    

ध्यान दें कि जब एक तौलिया मुड़ा हुआ होता है, तो इसे पहले ऊपर की ओर मोड़ा जाता है, फिर बाएं से दाएं। आपको इस कार्यक्रम की नकल करनी चाहिए। यह भी ध्यान दें कि परीक्षण के मामलों में, तौलिये एक ही स्थान पर रहते हैं, लेकिन मुड़े हुए होते हैं।

नियम:

  • इनपुट / आउटपुट के मानक तरीके।
  • मानक खामियां लागू होती हैं।
  • इनपुट और आउटपुट एक स्ट्रिंग के रूप में होना चाहिए।
  • ट्रेलिंग व्हाईवियर्स आउटपुट में ठीक हैं, जब तक कि तौलिये एक दूसरे के सापेक्ष सही जगह पर न हों।
  • आप मान सकते हैं कि तौलिया के प्रत्येक पक्ष की लंबाई हमेशा 2 से विभाज्य होगी।
  • इनपुट के रूप में पारित किए गए तौलिए हमेशा आयताकार होंगे।
  • तौलिए को हमेशा अलग किया जाएगा - हालांकि, उन्हें चर राशियों द्वारा अलग किया जा सकता है।

  • , इतना कम कोड जीतता है!

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

Input:
+------+
|      |
|      |
|      |
|      |
|      |
|      |
+------+
Output:
    +--+
    |  |
    |  |
    +--+




Input:
+--+ +--+ +--+
|  | |  | |  |
|  | |  | |  |
+--+ +--+ +--+

Output:
  ++   ++   ++
  ++   ++   ++


Input:
+----+
|    |
|    |
|    |
|    | ++
+----+ ++

Output:

   +-+
   | |
   +-+

        +

Input:
+--+
+--+     ++
         ||
         ||
         ++
Output:
  ++
          +
          +

1: यह Geobits और Laikoni द्वारा अस्वीकृत किया गया है। हालाँकि, मैंने इसे कहीं सुना था।


क्यों होता है पतन? अगर कुछ ऐसा है जिसे ठीक किया जा सकता है, तो कृपया मुझे बताएं।
कॉमरेड स्पार्कलेपनी


@ लिकोनी ऐसा लगता है जैसे टेक कैन कुछ भी कर सकता है :-)
श्री एक्सकोडर

@LuisMendo एडिटेड, तौलिये के बीच हमेशा जगह रहेगी।
कॉमरेड स्पार्कलपनी

क्या तौलिए हमेशा क्षैतिज रेखा देंगे? मेरा मतलब है कि अन्य किसी के तहत कोई तौलिया नहीं होगा?
मृत पोसुम

जवाबों:


5

रेटिना , 245 बाइट्स

m1+`^((.)*(?!\+ )[+|]([- ])*[+|].*¶(?<-2>.)*)([+|][- ]*[+|])
$1$.4$* 
m+`^((.)*) ( *) (.*¶(?<-2>.)*)(?(2)(?!))\|\3\|
$1|$3|$4 $3 
m+`^((.)*)\|( *)\|(?=.*¶(?<-2>.)*(?(2)(?!)) )
$1+$.3$*-+
([+|])\1
 $1
(?!\+ )([+|])([- ])\2(\2*)\3\1
$.3$*   $1$3$1

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

नोट: कुछ लाइनें रिक्त स्थान में समाप्त होती हैं। स्पष्टीकरण:

m1+`                    Repeatedly search from the beginning of input
    ^((.)*              Optional indentation
      (?!\+ )           Towel does not start with a plus and a space
      [+|]([- ])*[+|]   A row of towel
      .*¶               Rest of the line
      (?<-2>.)*         Some indentation on the next line
     )
     ([+|][- ]*[+|])    Another row of towel
$1$.4$*                 Replace the second row with blanks

हर टॉवल की हर दूसरी लाइन को डिलीट करें (यह काम करता है क्योंकि सभी तौलियों की ऊंचाई भी होती है),

m+`             Repeatedly search
   ^((.)*)      Optional indentation
    ( *)        Find some space
    (.*¶        Rest of the line
     (?<-2>.)*) Matching indentation on the next line
    (?(2)(?!))  Ensure that the indentation exactly matches
    \|\3\|      Piece of towel
$1|$3|$4 $3     Swap the piece into the space above

सभी अलग किए गए तौलिया के टुकड़ों को ऊपर स्थानांतरित करें,

m+`                 Repeatedly search
   ^((.)*)          Optional indentation
    \|( *)\|        Piece of towel
    (?=             Followed by
       .*¶          Rest of the line
       (?<-2>.)*    Matching indentation on the next line
       (?(2)(?!))   Ensure that the indentation exactly matches
        )           Nothing on the next line
$1+$.3$*-+          Change the piece into a towel bottom

और तौलिये के नीचे को ठीक करें, प्रभावी रूप से उन्हें ऊपर की तरफ मोड़ें।

([+|])\1    Match a row of towel of width 2, which is just ++ or ||
 $1         Change the first column to a space, which folds it

चौड़ाई 2 के तौलिए को दाईं ओर मोड़ें।

(?!\+ )     Towel does not start with a plus and a space
([+|])      Start with a + or a |
([- ])\2    Then at least two -s or spaces
(\2*)\3     Then an even number of -s or spaces
\1          Then another + or |
$.3$*       Replace the left half with spaces
$1$3$1      Replace the first character of the right half

बचे हुए तौलिये को दाईं ओर मोड़ें।


मैं इस बारे में अधिक विस्तृत व्याख्या करना चाहूंगा कि रेगेक्स कैसे काम करता है
क्रिति लिथोस

@KritiiLithos ऐसा कुछ है, या कुछ विशिष्ट था?
नील

हाँ धन्यवाद। और क्या मैं यह मानने में सही हूं कि <-2>एक .NET बैलेंसिंग ग्रुप है?
क्रिक्सी लिथोस

@KritiiLithos यह उनका उपयोग करता है: (?<-2>.)*प्रत्येक बार कैप्चर को पॉप करता है , इसलिए अधिक बार रिपीट नहीं किया जा सकता है (.)*, जबकि (?(2)(?!))जाँच करता है कि कोई कैप्चर नहीं बचा है, ताकि यह उसी समय की संख्या को दोहराए।
नील

3

ऑक्टेव विद इमेज पैकेज , 277 272 बाइट्स

function x=f(x)
[i,j,v]=find(bwlabel(x-32));a=@(w)accumarray(v,w,[],@max);r=-a(-i);R=a(i);s=-a(-j);S=a(j);x(:)=32;for k =1:nnz(r)
u=(r(k)+R(k)-1)/2;v=(s(k)+S(k)+1)/2;p=v:S(k);x(r(k),p)=45;x(u,p)=45;q=r(k):u;x(q,v)=124;x(q,S(k))=124;x(u,[v S(k)])=43;x(r(k),[v S(k)])=43;end

इनपुट और आउटपुट 2 डी चार ऐरे हैं।

इसे ऑनलाइन आज़माएं! या सभी परीक्षण मामलों को सत्यापित करें: 1 , 2 , 3 , 4 । (ध्यान दें किendfunction परीक्षण के मामलों में फ़ंक्शन को बाद के कोड से अलग करने की आवश्यकता है। यह आवश्यक नहीं है कि फ़ंक्शन अपनी फ़ाइल में सहेजा गया है।)

पठनीय संस्करण और स्पष्टीकरण

function x = f(x)
[i,j,v] = find(bwlabel(x-32)); % make background equal to 0 by subtracting 32.
% Then label each connected component (i.e. towel) with a unique integer
% Then get row indices (i) and column indices (j) of nonzero values (v)
a = @(w)accumarray(v,w,[],@max); % helper function. Gives the maximum of w for
% each group given by an integer label in v
r = -a(-i); % upper coordinate of each towel (minimum row index)
R = a(i); % lower coordinate of each towel (maximum row index)
s = -a(-j); % left coordinate of each towel (minimum column index)
S = a(j); % right coordinate of each towel (maximum column index)
x(:) = 32; % remove all towels: fill x with spaces
for k = 1:nnz(r) % for each original towel: create the new, folded towel 
    u = (r(k)+R(k)-1)/2; % new lower coordinate
    v = s(k)+S(k)+1)/2; % new left coordinate
    p = v:S(k); % column indices of horizontal edges
    x(r(k),p) = 45; % fill upper horizontal edge with '-'
    x(u,p) = 45; % fill lower horizontal edge with '-'
    q = r(k):u; % row indices of vertical edges
    x(q,v) = 124; % fill left vertical edge with '|'
    x(q,S(k)) = 124; % fill right vertical edge with '|'
    x(u,[v S(k)]) = 43; % fill lower corners with '+'
    x(r(k),[v S(k)]) = 43; % fill upper corners with '+'
end
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.