सी ++, 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();});}",})
;
}