Skip to content

Commit 688cf23

Browse files
committed
feat(day 135): count email reply and forward markers using case-insensitive regex
1 parent 65503f1 commit 688cf23

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""
2+
Re: Fwd: Fw: Count
3+
Given a string representing the subject line of an email, determine how many times the email has been forwarded or replied to.
4+
5+
For simplicity, consider an email forwarded or replied to if the string contains any of the following markers (case-insensitive):
6+
7+
"fw:"
8+
"fwd:"
9+
"re:"
10+
Return the total number of occurrences of these markers.
11+
12+
13+
"""
14+
import unittest
15+
16+
class ReFwdFwCountTest(unittest.TestCase):
17+
18+
def test1(self):
19+
self.assertEqual(email_chain_count("Re: Meeting Notes"), 1)
20+
21+
def test2(self):
22+
self.assertEqual(email_chain_count("Meeting Notes"), 0)
23+
24+
def test3(self):
25+
self.assertEqual(email_chain_count("Re: re: RE: rE: Meeting Notes"), 4)
26+
27+
def test4(self):
28+
self.assertEqual(email_chain_count("Re: Fwd: Re: Fw: Re: Meeting Notes"), 5)
29+
30+
def test5(self):
31+
self.assertEqual(email_chain_count("re:Ref:fw:re:review:FW:Re:fw:report:Re:FW:followup:re:summary:Fwd:Re:fw:NextStep:RE:FW:re:Project:Fwd:Re:fw:Notes:RE:re:Update:FWD:Re:fw:Summary"), 23)
32+
33+
34+
35+
def email_chain_count(subject):
36+
subject = subject.lower()
37+
checkers = ["fw:","fwd:","re:"]
38+
count = 0
39+
for check in checkers:
40+
count += subject.count(check)
41+
42+
return count
43+
44+
"""
45+
The above solution works fine for some cases but there are issues with this code
46+
47+
1. Overlapping markers
48+
-> "fwd:" contains "fw:"
49+
-> if the subject has "fwd:", the above code will count both "fw:" and "fwd:"
50+
Example:
51+
print(email_chain_count("Fwd: Meetig"))
52+
# Output: 2 (counts "fw:" and "fwd:")
53+
54+
# Expected: 1
55+
56+
2. Exact matching:
57+
-> if you want to avoid double-counting, you need to ensure "fw:" and "fwd:" are treated as distinct
58+
tokens, not substrings.
59+
60+
=> This solution works fine unless "fwd:" appears, where it double-count because "fw:" is a substring of "fwd:".
61+
=> Regex avoids this overlap by treating each marker as a distinct option.
62+
=> if the requirement is okay with the slight overcount, this version is perfectly serviceable. If you want exact correctness,
63+
regext is the safer choice.
64+
65+
"""
66+
67+
import re
68+
# Corrected Version
69+
def email_chain_count(subject):
70+
71+
markers = ["fw:","fwd:","re:"]
72+
73+
pattern = re.compile(r"(fw:|fwd:|re:)",re.IGNORECASE)
74+
75+
matches = pattern.findall(subject)
76+
77+
return len(matches)
78+
79+
"""
80+
This solution
81+
82+
=> Use case-insensitive matching re.IGNORECASE in (python, /gi i JS).
83+
=> Regex is the cleanest way since it handles overlapping and mixed cases.
84+
=> The result is simply the length of all matches.
85+
86+
"""
87+
88+
89+
90+
if __name__ == "__main__":
91+
92+
unittest.main()

0 commit comments

Comments
 (0)