Counting smiley faces with Python

The challenge

Given an array (arr) as an argument complete the function countSmileys that should return the total number of smiling faces.

Rules for a smiling face:

  • Each smiley face must contain a valid pair of eyes. Eyes can be marked as : or ;
  • A smiley face can have a nose but it does not have to. Valid characters for a nose are - or ~
  • Every smiling face must have a smiling mouth that should be marked with either ) or D

No additional characters are allowed except for those mentioned.

Valid smiley face examples: :) :D ;-D :~)
Invalid smiley faces: ;( :> :} :]

Example

countSmileys([':)', ';(', ';}', ':-D']); // should return 2; countSmileys([';D', ':-(', ':-)', ';~)']); // should return 3; countSmileys([';]', ':[', ';*', ':$', ';-D']); // should return 1;
Code language: JavaScript (javascript)

Note

In case of an empty array return 0. You will not be tested with invalid input (input will always be an array). Order of the face (eyes, nose, mouth) elements will always be the same.

Test cases

Test.describe("Basic tests") Test.assert_equals(count_smileys([]), 0) Test.assert_equals(count_smileys([':D',':~)',';~D',':)']), 4) Test.assert_equals(count_smileys([':)',':(',':D',':O',':;']), 2) Test.assert_equals(count_smileys([';]', ':[', ';*', ':$', ';-D']), 1)
Code language: Python (python)

The solution using Python

def count_smileys(arr): valid_eyes = [':',';'] valid_nose = ['-','~'] valid_mouth = [')', 'D'] count = 0 for i, item in enumerate(arr): if len(item)==2: # no nose if item[0] in valid_eyes and item[1] in valid_mouth: count += 1 elif len(item)==3: # has nose if item[0] in valid_eyes and item[1] in valid_nose and item[2] in valid_mouth: count += 1 return count
Code language: Python (python)
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments