आयताकार कोड के साथ आयताकार पाठ का पता लगाएं


19

मुद्रण योग्य ASCII पाठ की एक स्ट्रिंग (newlines और रिक्त स्थान सहित) को देखते हुए जिसमें कम से कम एक वर्ण होता है जो न तो एक नई रेखा है और न ही कोई स्थान है, यदि स्ट्रिंग आयताकार है, और एक गलत मान है तो सत्य मूल्य का उत्पादन करें। इसके अतिरिक्त, आपके समाधान के लिए स्रोत कोड आयताकार होना चाहिए

एक स्ट्रिंग आयताकार है यदि यह निम्नलिखित सभी स्थितियों को पूरा करती है:

  1. पहली पंक्ति और अंतिम पंक्ति में कोई स्थान नहीं है।
  2. प्रत्येक पंक्ति का पहला और अंतिम वर्ण स्थान नहीं है।
  3. सभी पंक्तियों में वर्णों की संख्या समान है।

उदाहरण के लिए, निम्नलिखित पाठ आयताकार है:

abcd
e fg
hijk

यह पाठ, हालांकि, आयताकार नहीं है (आवश्यकता # 3):

1234
567
8900

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

Truthy:

sdghajksfg
asdf
jkl;
qwerty
u i op
zxcvbn
1234
5  6
7890
abcd
e fg
hijk

Falsey:

a b c
123
456
7 9
12
345
qwerty
 uiop
zxcvnm
1234
567
8900

यह , इसलिए बाइट्स जीत में सबसे छोटा समाधान है।




9
तो, बिना किसी स्थान के एक-लाइनर एक वैध प्रस्तुत करना सही है?
अरनौलद


1
क्या हम इनपुट को स्ट्रिंग्स की एक सरणी के रूप में ले सकते हैं, प्रत्येक पंक्ति के लिए एक? या क्या हमें एक एकल लंबी स्ट्रिंग इनपुट करनी चाहिए जिसमें लाइन ब्रेक शामिल हैं?
ब्रैडेक

जवाबों:


12

सी (जीसीसी) , 127 125 124 118 बाइट्स

  • गोल्फ से दो बाइट्स सहेजा r*=!e&(!t|t==c);करने के लिए r>>=e||t&&t-c;(यह गोल्फ मेरे हाल ही के सी टिप्स के उत्तर की प्रेरणा थी, फ्लैग अपडेट अपडेट ।)
  • गोल्फ से एक बाइट सहेजा *(_-2)करने के लिए _[~1]
  • गोल्फ ब्रेड *_++-10||(...)को छह बाइट्स में सहेजा गया *_++<11?...:0और प्लेसहोल्डर शून्य का उपयोग किया गया ...:0(जिसका उपयोग रचनात्मक रूप से नहीं किया गया है) c++वेतन वृद्धि के लिए। उन गोल्फरों ने कुछ और लूप फेरबदल की अनुमति दी।
  • जब कोई कई फाल्सी वैल्यूज का उपयोग कर सकता है , तो 114 बाइट्स संभव हो सकते हैं।
r,e,c,t;_(char*_){for(r=1,t=c=0;*_;*_++<11?r*=(t||(t=c,!e))&*_>32&_[~1]>32&t==c,c=e=0:c++)*_-32||(e=1);r>>=e||t&&t-c;}

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

एक लंबा आयत प्राप्त करने वाला स्रोत लेआउट।

व्याख्या

निम्नलिखित 124 बाइट्स लंबे संस्करण की व्याख्या करता है।

r,e,c,t;_(char*_){     // `r` is the boolean result flag, `e` a boolean flag if the current line contains
                       //  a space, `t` the first line's width, `c` the current line's current width
 for(r=1,t=c=0;*_;c++) // initialize, loop through entire string
  *_-32||              // if the current char is a space,
   (e=1),              //  the current line contains a space
  *_++-10||            // if the current char is a newline (char pointer `_` now incremented)
   (r*=(t||(t=c,!e))   // if t is not yet set, the current line is the first line; set it
                       //  to this line's length, check that no spaces where found
    &*_>32             // the next line's first char should not be a space
    &_[~1]>32          // this line's last char should not have been a space
    &t==c,c=~0,e=0);   // the line lengths should match, reset `c` and `e` to zero
                       //  (`~0 == -1`, countering the loop's increment of `c`)
 r>>=e||t&&t-c;}       // return boolean flag, check that the last line does not contain spaces,
                       //  there was either no newline or line lengths match
                       //  (here) equivalent to `r*=!e&(!t|t==c)`

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


10
+1 के लिएr,e,c,t
मैजिक ऑक्टोपस Urn

4

जावा 10, 214 176 169 152 144 139 बाइट्स

s->{String[]a=s.split("\n")
;int r=1,i=0,R=a.length;for
(;i<R;i++)if(i<1|i>R-2?a[i]
.contains(" "):a[i].trim( )
!=a[i])r=0;return-r<0;}////

-5 बाइट्स @Neil की बदौलत

के String[]aबजाय उपयोग करता है var a; return-r<0;के बजाय return r>0;; और //बहुत अंत में एक टिप्पणी जोड़ी गई , इसलिए पहली और अंतिम पंक्तियों में स्थान नहीं हैं।

ध्यान दें कि यह आयत सिंगल-लाइन इनपुट से छोटा है, क्योंकि int r=1,...;इसे प्रतिस्थापित किया जाना चाहिए int[]v{1,...};, और पूर्णांक के सभी उपयोग तब हो जाएंगे v[n](जहां n सरणी में चर का सूचकांक है v)।

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

स्पष्टीकरण:

s->{                        // Method with String parameter and boolean return-type
  String[]a=s.split("\n");  //  Input split by new-lines
  int r=1,                  //  Result-integer, starting at 1
      i=0,                  //  Index `i`, starting at 0
      R=a.length;           //  Amount of rows `R`
  for(;i<R;i++)             //  Loop `i` over the rows
    if(i<1                  //   If it's the first row,
       |i>R-2?              //   or the last row:
        a[i].contains(" ")  //   And the current row contains a space
       :a[i].trim()!=a[i])  //   Or either column of the current row contains a space
      r=0;                  //    Set the result `r` to 0
   return-r<0;}             //  Return whether `r` is still 1
////                        // Comment to comply to the rules of the challenge

यहाँ रिक्त स्थान के साथ एक ही आधार कार्यक्रम है ( 128 126 बाइट्स ):

s->{var a=s.split("\n");int r=1,i=0,R=a.length;for(;i<R;i++)if(i<1|i>R-2?a[i].contains(" "):a[i].trim()!=a[i])r=0;return r>0;}

-2 बाइट्स @Neil की बदौलत

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



3

टी-एसक्यूएल, 237 207 बाइट्स

SELECT(SELECT(IIF(max(len(v))=min(len(v)),1,0)*IIF(SUM(len(v+'x')-len
(trim(v))-1)=0,1,0))FROM t)*(SELECT(IIF(SUM(charindex(' ',v))=0,1,0))
FROM[t]WHERE[i]IN(SELECT(min(i))FROM[t]UNION(SELECT(max(i))FROM[t])))

आयताकार के लिए आउटपुट 1, अन्यथा 0। मुझे रिक्त स्थान को खत्म करने के लिए अतिरिक्त पार्न्स और ब्रैकेट का उपयोग करना पड़ा, मुझे यकीन है कि सुधार के लिए विशाल जगह है।

स्पष्टीकरण :

हमारे अनुमत I / O विकल्प और प्रश्न टिप्पणियों में स्पष्टीकरण के अनुसार, इनपुट को पहले से मौजूद टेबल टी में अलग-अलग पंक्तियों के रूप में लिया जाता है । क्योंकि SQL में डेटा स्वाभाविक रूप से अनियंत्रित है, उस तालिका में "पंक्ति संख्या" पहचान क्षेत्र i शामिल है :

CREATE TABLE t (i INT IDENTITY(1,1), v VARCHAR(999))

मूल रूप से मेरी एसक्यूएल 3 उपश्रेणी करती है, जिनमें से प्रत्येक रिटर्न 0या 1"आयताकार" कोड के 3 मानदंडों के आधार पर है। उन 3 मूल्यों को एक साथ गुणा किया जाता है, केवल 1उस कोड के लिए लौट रहा है जो सभी 3 को संतुष्ट करता है।

संपादित करें : अंतरिक्ष को बचाने के लिए एक ही चयन में संयुक्त मापदंड 2 और 3

SELECT(
SELECT(IIF(max(len(v))=min(len(v)),1,0)                  --All rows same length
      *IIF(SUM(len(v+'x')-len(trim(v))-1)=0,1,0))FROM t) --no leading or trailing spaces
*(SELECT(IIF(SUM(charindex(' ',v))=0,1,0))               --No spaces at all in
FROM[t]WHERE[i]IN(SELECT(min(i))FROM[t]                  --   first row or
            UNION(SELECT(max(i))FROM[t])))               --   last row

TRIM(v)समारोह केवल एसक्यूएल 2017 और इसके बाद के संस्करण के द्वारा समर्थित है। पहले के संस्करणों की आवश्यकता होगी LTRIM(RTRIM(v)), जिसके लिए पंक्तियों के पुनर्संतुलन की आवश्यकता होगी।

एक यादृच्छिक ध्यान दें: LEN()SQL में फ़ंक्शन ट्रेलिंग स्पेस को अनदेखा करता है, इसलिए LEN('foo ') = 3। एक "सही" लंबाई पाने के लिए आपको एक चरित्र को अंत तक निपटाना होगा फिर एक: P को घटाना होगा


3

सी ++, 199 183 181 175 बाइट्स

यह टेम्प्लेट फ़ंक्शन लाइनों को स्ट्रिंग्स के संग्रह के रूप में स्वीकार करता है (जो कि विस्तृत स्ट्रिंग्स हो सकता है), पुनरावृत्तियों की एक जोड़ी के रूप में पारित किया गया।

#include<algorithm>//
template<class I>bool
f(I a,I b){return!~+(
*a+b[-1]).find(' ')&&
std::all_of(a,b,[&a](
auto&s){return' '+-s.
back()&&s[0]-' '&&a->
size()==s.size();});}

धन्यवाद उपयोगकर्ता इर्रोनस के कारण होता है जो मुझे उस back()सदस्य की याद दिलाने std::stringऔर npos+1शून्य होने की ओर इशारा करता है।

अघोषित समकक्ष

एकमात्र वास्तविक गोल्फिंग पहली और आखिरी लाइनों को समतल करना है ताकि हम findउन स्थानों के लिए एकल प्रदर्शन कर सकें ।

#include <algorithm>
template<class It>
bool f(It a, It b)
{
    return (*a+b[-1]).find(' ') == a->npos
        && std::all_of(a, b,
                       [=](auto s) {
                           return s.back() != ' '
                               && s.front() != ' '
                               && s.size() == a->size(); });
}

परीक्षण कार्यक्रम

#include <iostream>
#include <string>
#include <vector>
int expect(const std::vector<std::string>& v, bool expected)
{
    bool actual = f(v.begin(), v.end());
    if (actual == expected) return 0;
    std::cerr << "FAILED " << (expected ? "truthy" : "falsey") << " test\n";
    for (auto const& e: v)
        std::cerr << "  |" << e << "|\n";
    return 1;
}
int expect_true(const std::vector<std::string>& v) { return expect(v, true); }
int expect_false(const std::vector<std::string>& v) { return expect(v, false); }
int main()
{
    return
        // tests from the question
        + expect_true({"sdghajksfg"})
        + expect_true({"asdf", "jkl;",})
        + expect_true({"qwerty", "u i op", "zxcvbn",})
        + expect_true({"1234", "5  6", "7890",})
        + expect_true({"abcd", "e fg", "hijk",})
        + expect_false({"a b c",})
        + expect_false({"123", "456", "7 9",})
        + expect_false({"12", "345",})
        + expect_false({"qwerty", " uiop", "zxcvnm",})
        + expect_false({"1234", "567", "8900",})
        // extra tests for leading and trailing space
        + expect_false({"123", " 56", "789"})
        + expect_false({"123", "45 ", "789"})
        // the function source
        + expect_true({"#include<algorithm>//",
                       "template<class I>bool",
                       "f(I a,I b){return!~+(",
                       "*a+b[-1]).find(' ')&&",
                       "std::all_of(a,b,[&a](",
                       "auto&s){return' '+-s.",
                       "back()&&s[0]-' '&&a->",
                       "size()==s.size();});}",})
        ;
}

यह आगे 223 लाइन चौड़ाई के साथ 183 बाइट्स के लिए गोल्फ हो सकता है, का उपयोग कर .find(' ')+1==0, और s.back()इसके बजाय *s.rbegin()
इरोनियस




2

हास्केल , 79 बाइट्स

g(x:r)=all((==(0<$x)).(0<$))r&&all(>='!')(x++last(x:r)++(head<$>r)++(last<$>r))

इसे ऑनलाइन आज़माएं! इनपुट को लाइनों की सूची के रूप में लेता है।

पैटर्न g(x:r)= ...पहली पंक्ति को xऔर शेष लाइनों की सूची (संभवतः खाली) को बांधता है r। फिर all((==(0<$x)).(0<$))rजाँच करता है कि सभी लाइनों की rलंबाई समान है x( इस टिप का उपयोग करके )।

यदि नहीं, तो संयोजन &&शॉर्ट-सर्किट और रिटर्न False, अन्यथा दाहिने हाथ की ओर का मूल्यांकन किया जाता है। वहां एक स्ट्रिंग का निर्माण होता है जिसमें xपहली पंक्ति के last(x:r)लिए होता है, अंतिम पंक्ति के लिए r(या मामले में पहली पंक्ति फिर rसे खाली होती है) और प्रत्येक पंक्ति (head<$>r)के पहले और (last<$>r)अंतिम वर्ण के लिए। इस स्ट्रिंग के लिए, all(>='!')जांचता है कि इसमें कोई स्थान नहीं है (हम (>' ')स्रोत कोड प्रतिबंध के कारण उपयोग नहीं कर सकते हैं )।


"\ N \ n" पर त्रुटियां
Angs

@ अच्छी पकड़। सौभाग्य से, ओपी ने स्पष्ट किया कि इनपुट contains at least one character that is neither a newline nor a space, जो खाली सूची मामले को छोड़ने की भी अनुमति देता है।
लकोनी

ओह अच्छा है, ध्यान नहीं दिया जा रहा है
13

2

MATL , 13 बाइट्स

ctgF6Lt&()32>

इनपुट स्ट्रिंग्स की एक सरणी है, प्रारूप में {'abc' 'de'}

आउटपुट एक ऐसा सरणी है जिसमें केवल वही होता है, जो सत्य है , या कम से कम शून्य से युक्त एक सरणी है, जो कि गलत है

इसे ऑनलाइन आज़माएं! या सत्यता / मिथ्या परीक्षा सहित सभी परीक्षण मामलों को सत्यापित करें

व्याख्या

c       % Implicit input. Convert to char. This concatenates the
        % strings of the input cell array as rows of a rectangular
        % char array, right-padding with spaces as needed
tg      % Duplicate, convert to logical. Gives a logical array with
        % the same size containing true in all its entries
F       % Push false
6L      % Push the array [2, j-1], where j is the imaginary unit.
        % When used as an index, this is interpreted as 2:end-1
t       % Duplicate
&(      % Assignment indexing with 4 inputs: original array, new
        % value, two indexing arrays. This writes false at the inner
        % rectangle (2:end-1)×(2:end-1) of the logical array that
        % initially only contained true. This will be used as a
        % logical index (mask) into the rectangular char array
)       % Reference indexing. This selects the border of the char
        % array. The result is a column vector of chars
32>     % Is each entry greater than 32? (ASCII code for space)
        % Implicit display

11 बाइट्स: cO6Lt&(32=~ इसे ऑनलाइन आज़माएं! बस गैर-सीमा भागों को बाहर निकालता है, फिर जांचता है कि क्या कोई रिक्त स्थान है।
सूंदर -

@ सूंदर अच्छा विचार है! यह काफी अलग है, इसे स्वयं पोस्ट करें
लुइस मेंडो

1
नाह, आपके उत्तर के समान है, खासकर अगर मैं इसे लिखता हूं cF6Lt&(32=~। इसे संपादित करने के लिए स्वतंत्र महसूस करें, या नहीं तो हम इसे केवल टिप्पणियों में छोड़ सकते हैं।
सूंदर -


1

कैनवस , 17 15 बाइट्स

4[↷K;}┐){SL]∑4≡

यहाँ कोशिश करो!

स्पष्टीकरण (मोनोस्पेस के लिए ASCII-fied):

4[↷K;}┐){SL]∑4=  full program; pushes the input to the stack.
4[   }           repeat 4 times
  ↷                rotate ToS clockwise. This also pads the input with spaces
   K;              take off the last line and put it below the item
      ┐          pop the remaining of the input (the center)
       )         and wrap the rest (the sides) in an array
        {  ]     map over those
         S         split on spaces - should result to one item in the array
          L        and get the length
            ∑    sum those lengths together
             4=  check if equal 4

4
मुझे यह विडंबना लगती है कि एक मोनोस्पेस जैसे फॉन्ट में UTF8 अक्षर यह महसूस करते हैं कि स्रोत में कई रिक्त स्थान हैं। (कम से कम, वे मेरे ब्राउज़र में करते हैं।)
अरनौल्ड

1
@Arnauld फ़ुल विस्फ़ोटक वर्ण ऐसा करते हैं। और यही कारण है कि मैंने अपने दुभाषिए के लिए एक फ़ॉन्ट बनाकर उन्हें
प्रीतिकर बनाया


1

लाल , 216 191 बाइट्स

func[s][d:(length?(first(s:(split(s)"^/"))))sp:
func[a][none = find a" "]b: on foreach c s[b: b
and(d = length? c )and(c/1 <>" ")and(" "<> last
c)]res:(sp(first(s)))and(sp(last(s)))and(b)res]

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

मैंने पहली और आखिरी पंक्तियों में बहुत सारे अन्यथा आवश्यक कोष्ठकों को नहीं रखा है।


0

जेली , 17 बाइट्स

Ỵµ.ịЀ;ịɗẎ⁶e<L€E$

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


@JonathanFrech आह, तय। > _>
आउटगॉल्फ

@MagicOctopusUrn हुह? क्या आप किसी इनपुट से लिंक कर सकते हैं जहाँ यह सही व्यवहार नहीं करता है?
मृग

ओह, नहीं, आपने Does not seem to enforce equal line lengthमुझे भी बुलाया था, जो मैं कह रहा था।
मैजिक ऑक्टोपस Urn


1
@Angs इसे उद्धृत करने का प्रयास करें। यह स्पष्ट रूप से कुछ भी नहीं के रूप में अगर आप इसे उस तरह से रखा है।
एरिक आउटगॉल्फ

0

जेली , 15 बाइट्स

मेंमोनिक द्वारा विकसित एक विधि का उपयोग करता है (वर्तमान में - एक किनारे-मामले की विफलता के कारण) पायथ जमा को हटा दिया गया। (यदि यह अब तय हो गया है, तो कुछ क्रेडिट दें !)

ỴµL€Eȧt€⁶ZUƊ4¡⁼

एक मोनडिक लिंक जो पात्रों की एक सूची को स्वीकार करता है जो 1 या 0 देता है।

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

कैसे?

ỴµL€Eȧt€⁶ZUƊ4¡⁼ - Link: list of characters
Ỵ               - split at newlines (making a list of lists - the rows)
 µ              - start a new monadic chain, call that ROWS
  L€            - length of €ach row in ROWS
    E           - all equal? (an integer: 1 if so, otherwise 0)
            4¡  - repeat four times:
           Ɗ    -   last three links as a monad:
      t€⁶       -     trim spaces (⁶) from €ach row in current ROWS
         Z      -     transpose that result
          U     -     upend (reverse each new row)
     ȧ          - logical AND (0 if L€E was 0 else the result of the repeated transform)
              ⁼ - equal to X? (the integer 0 is not equal to any listy of characters)

@ मेनोनिक - जेली-फिट :)
जोनाथन एलन

0

जाप , 22 बाइट्स

गैर-जवाब देने वाला उत्तर: जाप में एक ज्ञात बग है , जहां दो-आयामी सरणी घुमाव परिणामों को काटते हैं । उस बग के कारण नीचे का कोड केवल उन इनपुट पर काम करता है जो चौकोर हैं। यदि बग मौजूद नहीं था, हालांकि, नीचे दिए गए कोड को पूरी तरह से सही ढंग से काम करना चाहिए।

e_ʶUÌÊéUeº4o)r_z)mx}U
e_                      // Check if every line in the input array
  ʶUÌÊ                 // has the same length as the last item.
       é               // Also,
               r_z)mx}U // check if rotating and trimming the input array
           º4o)         // four times
         Ue             // is equal to the input array.

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


0

रूबी 2.5+, 63 बाइट्स

->a{!a.uniq(&:size)[1]&&a.none?(/^\s|\s$/)&&!(a[0]+a[-1])[?\s]}

स्ट्रिंग के एक सरणी के रूप में इनपुट लेता है। कोई परीक्षण लिंक नहीं है, क्योंकि TIO (2.4) पर संस्करण इस एक के लिए बहुत पुराना है। इसके बजाय, यहाँ परीक्षण के लिए थोड़ा लंबा (69 बाइट्स) संस्करण है:

->a{!a.uniq(&:size)[1]&&a.none?{|l|l=~/^\s|\s$/}&&!(a[0]+a[-1])[?\s]}

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

अंतर यह है कि चूंकि 2.5 रूबी सीधे रेगेक्स पैटर्न को पारित करने के all?, any?, none?तरीकों का समर्थन करता है , जो हमें कुछ बाइट्स बचाता है। यह विधि स्वयं काफी व्याख्यात्मक है - हम परीक्षण करते हैं:

  1. यदि केवल 1 अद्वितीय रेखा आकार है
  2. यदि लाइन सीमाओं पर कोई रिक्त स्थान हैं
  3. यदि पहली और आखिरी लाइनों पर रिक्त स्थान हैं।


0

सी # (.NET कोर) , 145 167 बाइट्स

S[0].Length>1&&S[0].IndexOf
(" ") + S[ S.Count() - 1 ].
IndexOf(" ")<-1&Array.Find(
S,x=>x[0]==' '| x [x.Length
-1]  ==  ' '  | S[0].Length
!=x.Length)==null?11>0:0>1;

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

S[0].Length>1&                                    // And if the lenght of the first argument is more than 1 char
Array.Find(                                       // Find a string in an array
    S,                                            // The array which will be searched in
    x=>                                           // For x as the current string from the array
    x.Length!=S[0].Length|                        // If the string lenght match not the first argument lenght
    x[0]==' '|                                    // Or if the string begins with a spacer
    x[x.Length-1]==' '                            // Or if the string ends with a spacer
)==null&                                          // And if there was no string found which matched the conditions
S[0].IndexOf(" ")+S[S.Count()-1].IndexOf(" ")<-1  // And if the first and last string doesn't have a spacer
?                                                 // If all above is true do
1>0                                               // Return True
:                                                 // Else
0>1                                               // Return False

पहली पंक्ति में कोई स्थान नहीं।
फ्रॉनीफ्रॉग

@FrownyFrog S[0].IndexOf(" ")पहली लाइन में स्पेस S[S.Count()-1].IndexOf(" ")खोज रहा है और आखिरी लाइन में सर्च कर रहा है। यदि पहली और अंतिम पंक्ति में कोई स्थान नहीं है, तो यह -2 है जो तब पर सत्य है -2 < -1
हिले

2
मेरा मतलब चुनौती है, आपके कोड में समान प्रतिबंध है, इसलिए आपके पास पहली पंक्ति में स्थान नहीं हो सकते।
FrownyFrog

1
Trueजब आपका प्रोग्राम पास हो जाए तो आपका कोड वापस आ जाना चाहिए । यह इस चुनौती में एक अतिरिक्त प्रतिबंध है।
FrownyFrog

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