DEV Community

Cover image for Python 🐍 challenge_26βš”οΈ
Mahmoud EL-kariouny
Mahmoud EL-kariouny

Posted on

Python 🐍 challenge_26βš”οΈ

Count the smiley faces

  • 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: ;( :> :} :]

Examples:

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

Enter fullscreen mode Exit fullscreen mode

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.
Task URL: Link

My Solution:

def count_smileys(arr):
    valid_smiley = [':)', ':D', ';-D', ':~)', ';~)', ';D', ':-D', ':-)', ';~D']
    smile = [x for x in arr if x in valid_smiley]
    return len(smile)
Enter fullscreen mode Exit fullscreen mode

Code Snapshot:

Image description

Learn Python

Python top free courses from CourseraπŸπŸ’―πŸš€

πŸŽ₯

Connect with Me 😊

πŸ”— Links

linkedin

twitter

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay