C++, 199 183 181 175 bytes
This template function accepts lines as a collection of strings (which may be wide strings), passed as a pair of iterators.
#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();});}
Thanks are due to user Erroneous for reminding me of the back()
member of std::string
and for pointing out that npos+1
is zero.
Ungolfed equivalent
The only real golfing is to concatenate the first and last lines so we can perform a single find
for spaces in those.
#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(); });
}
Test program
#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();});}",})
;
}
Sandbox – Mego – 2018-05-02T13:25:47.230
2Related. – AdmBorkBork – 2018-05-02T13:41:16.840
9So, a one-liner without any space is a valid submission, correct? – Arnauld – 2018-05-02T13:45:36.903
1related – Rod – 2018-05-02T14:34:00.190
1Can we take input as an array of strings, one for each line? Or must we input a single long string that includes the line breaks? – BradC – 2018-05-02T14:46:25.283
@Arnauld Yes, that is correct. – Mego – 2018-05-02T22:42:18.653
@BradC Either is fine, so long as it is unambiguous. – Mego – 2018-05-02T22:42:30.983
@Laikoni, the question says we'll be given a string that contains at least one character that is neither a newline nor a space. That implies we don't need to handle an empty string. – Toby Speight – 2018-05-03T10:56:54.783
Is the tab character (ASCII value 9) considered printable? – Jonathan Frech – 2018-05-03T15:00:58.963
@JonathanFrech No – Mego – 2018-05-03T15:02:15.763