+ 1

Can you guys help me check where my codes has gone wrong?Thanks in advance. The question is in the description..

You need to check whether the string is a smiley face. If yes – output 1, otherwise – 0. Smiley: Eyes is one of ":" ";" ">:", nose "-" either one or none at all, mouth is one of "{", "}", "[", "]", "(", ")", "p" at least once. One type may be repeated as many times as you want. All symbols go precisely in the listed order, smiley does not contain any other symbols. Input data String with length up to 100 symbols, consisting of uppercase and lowercase letters of the Latin alphabet and various characters. Output data Output 1, if the given string is a smiley. Otherwise output 0. https://code.sololearn.com/ct3cyhS8L959/?ref=app

30th Jan 2019, 11:41 AM
Keyi Huang
Keyi Huang - avatar
7 Respostas
+ 4
pattern = r"(:|;|(>:))\-?(\{+|\}+|\[+|\]+|\(+|\)+|p+)
quot; Last try. Must work!
30th Jan 2019, 4:10 PM
Maneren
Maneren - avatar
+ 9
mouths and nose are regex metacharacters so you have to escape them
30th Jan 2019, 12:53 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 3
Do sth like this: pattern=r"(:|;|(>:))\-?(\{|\}|\[|\]|\(|\)|p)+
quot; This should work PS. note the backslashes
30th Jan 2019, 1:05 PM
Maneren
Maneren - avatar
+ 2
It works! Thank youso much😄
30th Jan 2019, 10:57 PM
Keyi Huang
Keyi Huang - avatar
+ 1
I have escaped all the regex metacharacters but it still won't work for some cases like:()which the output should be 0 but my code would give 1...do u guys have any ideas about how to make sure that the mouth are all the same characters if there are more than 1 characters in the mouth portion(my code are not supposed to accept :()or:[) these kind of things)
30th Jan 2019, 2:20 PM
Keyi Huang
Keyi Huang - avatar
+ 1
So, do sth like this: pattern=r"(:|;|(>:))\-?(\{|\}|\[|\]|\(|\)|p)
quot; This should work PS. note removing the plus sing at the end of regex
30th Jan 2019, 2:30 PM
Maneren
Maneren - avatar
0
then cases like :))) will not pass..but they are still considereda smiley face
30th Jan 2019, 2:47 PM
Keyi Huang
Keyi Huang - avatar