Analysis: Smiley Hunt 🕵️‍♂️

Let's look at the submitted solution:

import re

def num_emojis(arg):
return sum([re.match("^[:;][~-]?[)D]$",i) is not None for i in arg])


Does it work? Yes. Would you leave this in production? No.

1️⃣ Allocation sin
The square brackets inside sum([ ... ]) mean that Python will first create a full list of True/False values in memory, the size of the original array, and only then sum it. If the input is a log of 10 million lines, your memory will leave the chat.
Remove the brackets — you get a generator expression. Memory O(1) instead of O(n).

2️⃣ Redundant checks
The construct match(...) is not None is acceptable, but in Python it's common to use the built-in truthiness of objects. Plus, we just need to count the number of matches. Idiomatic approach: sum(1 for ... if ...)

3️⃣ Recompilation
Calling re.match inside a loop each time forces Python to access the regex cache. If there are many strings, the pattern should be compiled once before the loop.

Let's throw out the garbage and write like this:
import re

# Move compilation to the top
SMILEY_PATTERN = re.compile(r"^[:;][~-]?[)D]$")

def count_smileys(faces: list[str]) -> int:
return sum(1 for face in faces if SMILEY_PATTERN.match(face))


☝️But the main problem with this solution is not the syntax.
Why do we even need regex here?

Let's count: we have 2 eye options, 3 nose options (including absence), and 2 mouth options.
There are exactly 12 valid smileys. As @archimage_wiz wrote, instead of running a heavy state machine of regular expressions on each line, we just need to check if the string is in a pre-prepared set.

Set lookup works in constant time O(1) (it's a hash table).

The healthy person's solution:
from typing import List

VALID_SMILES = {
':)', ';)', ':-)', ';-)', ':~)', ';~)',
':D', ';D', ':-D', ';-D', ':~D', ';~D'
}

def num_emojis_pro(arr: List[str]) -> int:
return sum(s in VALID_SMILES for s in arr)


This code does not require importing the re module, it is obvious to any junior at first glance, and on large data volumes it will destroy regex in execution speed.

Complex tools are cool. But the ability to get by with simple ones is already a skill.

#algosobes