loading...

Daily Challenge #103 - Simple Symbols

thepracticaldev profile image dev.to staff ・1 min read

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!

Discussion

pic
Editor guide
Collapse
jvanbruegge profile image
Jan van Brügge

simple Haskell solution:

verify :: String -> Bool
verify [] = True
verify ('+':x:'+':xs)
    | isLetter x = verify $ '+':xs
verify (x:xs)
    | isLetter x = False
    | otherwise = verify xs
Collapse
aminnairi profile image
Amin

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 of Data.Char? Or no need to import that to use the isLetter function?

Collapse
jvanbruegge profile image
Jan van Brügge

yes, that function is from Data.Char, I omitted the import together with the module header.

Collapse
idanarye profile image
Idan Arye

I took your solution and simplified it:

verify :: String -> Bool
verify [] = True
verify ('+':x:'+':xs) = verify $ '+':xs
verify ('+':xs) = verify xs
verify ('=':xs) = verify xs
verify (_) = False

Also note that nothing in the question indicates the characters other than = and + can only be letters. Your version would accept 1 even though 1 is a character and it is not surrounded by +s.

Collapse
jvanbruegge profile image
Jan van Brügge

This solution assumes that the only other character is =. No idea if you can assume this

Thread Thread
idanarye profile image
Idan Arye

The string will be composed of + and = symbols with several characters between them.

If I'm reading this correctly, it means that the characters are categorized into three categories:

  1. +
  2. =
  3. Other

So any character other than + and = must be surrounded by +s - which is exactly what my version checks.

Collapse
edreeseg profile image
Ed Reeseg

I've never been the best with Regex, but here's a possible attempt:

JavaScript

const acceptableSequence = str => !/(?<!\+)\w|\w(?!\+)/.test(str);

EDIT: Misread the requirements, don't need to account for end and beginning of line characters as exceptions.

Collapse
citizen428 profile image
Michael Kohl

\w generally includes the underscore, which is why I used the explicit character class.

Collapse
edreeseg profile image
Ed Reeseg

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.

Thread Thread
citizen428 profile image
Michael Kohl

I interpreted “The string will be composed of + and = symbols with several characters between them.” as only +, = and letters. 😀

Thread Thread
edreeseg profile image
Ed Reeseg

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.

Thread Thread
citizen428 profile image
Michael Kohl

I'm just assuming either, but at least this is turning into a good lesson on how requirements get confused between whoever plans them and developers 😉

Collapse
erezwanderman profile image
erezwanderman

Javascript:

acceptable = str => [...str].every((x, i, a) => x == '+' || x == '=' || (a[i - 1] == '+' && a[i + 1] == '+'))
Collapse
citizen428 profile image
Michael Kohl

Ruby:

def verify(s)
  s !~ /(?<!\+)[a-z]|[a-z](?!\+)/
end

verify "f++d+"
#=> false

verify "++q++qq=z"
#=> false

verify "==++z+=+q+"
#=> true
Collapse
citizen428 profile image
Michael Kohl

Here's an alternative version in Raku (formerly Perl6), which IMHO is more readable due to the more expressive lookaround assertions:

sub verify { @_ !~~ /<!after \+><[a..z]>|<[a..z]><!before \+>/ }

verify "f++d+"
# False
verify "++q++qq=z"
# False
verify "==++z+=+q+"
# True