Write a function that will take a string and determine if it is an acceptable sequence. The string will be composed of + and = symbols with several characters between them. For the string to be acceptable, each character must be surrounded by + symbols.
Examples:
(++d+===+c++==a) => false, because a is not surrounded by +.
(+d+e+===+q+=) => true, because all characters are surrounded by +.
Test cases:
"f++d+"
"++q++qq=z"
"==++z+=+q+"
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (10)
simple Haskell solution:
I took your solution and simplified it:
Also note that nothing in the question indicates the characters other than
=
and+
can only be letters. Your version would accept1
even though1
is a character and it is not surrounded by+
s.This solution assumes that the only other character is =. No idea if you can assume this
If I'm reading this correctly, it means that the characters are categorized into three categories:
+
=
So any character other than
+
and=
must be surrounded by+
s - which is exactly what my version checks.Whoa, I was comming with something that involved using an indexedMap but your solution is very clever. Thanks for sharing your awesome answer!
Also, isn't
isLetter
part ofData.Char
? Or no need to import that to use theisLetter
function?yes, that function is from
Data.Char
, I omitted the import together with the module header.I've never been the best with Regex, but here's a possible attempt:
JavaScript
EDIT: Misread the requirements, don't need to account for end and beginning of line characters as exceptions.
That's a good point. I figured I'd use
\w
since nothing in the challenge indicated that the characters need be only alphabetical, but obviously you could just as easily sub in[a-zA-Z]
or whatever fits the use case.Javascript:
That definitely makes the most sense, given the examples. I really appreciate you reaching out to clarify - definitely want to make sure I'm accurately hitting objectives.