Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to sorting important mails from different mailboxes in Python
Suppose we have a list of mailboxes where each mailbox contains a list of strings. Each string represents either "J" for junk, "P" for personal, or "W" for work emails. We need to process each mailbox in round-robin order starting from the first mailbox, filter out junk emails, and return a single sorted list.
So, if the input is like mailboxes = [["W", "P"],["J", "P", "J"],["W"]], then the output will be ["W", "W", "P", "P"]. In processing order without filtering, we have W ? J ? W ? P ? P ? J. After filtering out junk emails, we get W ? W ? P ? P.
Algorithm
To solve this problem, we will follow these steps ?
- Initialize the number of mailboxes
- Create a result list to store important mails
- Create a counts array to track current position in each mailbox
- Use round-robin processing to iterate through all mailboxes
- Filter out junk mails and add important ones to result
Example
Let us see the following implementation to get better understanding ?
class Solution:
def solve(self, mailboxes):
n_mailboxes = len(mailboxes)
result = []
counts = [0] * n_mailboxes
more = True
while more:
more = False
for i in range(n_mailboxes):
index, mailbox = counts[i], mailboxes[i]
if index < len(mailbox):
more = True
counts[i] += 1
mail = mailbox[index]
if mail != "J":
result.append(mail)
return result
# Test the solution
ob = Solution()
mailboxes = [["W", "P"],["J", "P", "J"],["W"]]
print(ob.solve(mailboxes))
['W', 'W', 'P', 'P']
How It Works
The algorithm processes mailboxes in round-robin fashion:
- Round 1: Takes first email from each mailbox: "W" (mailbox 0), "J" (mailbox 1 - filtered), "W" (mailbox 2)
- Round 2: Takes second email: "P" (mailbox 0), "P" (mailbox 1), no more emails in mailbox 2
- Round 3: Only mailbox 1 has remaining email: "J" (filtered out)
Alternative Approach Using Queue
We can also solve this using a more Pythonic approach with iterators ?
from itertools import chain, zip_longest
def sort_important_mails(mailboxes):
# Transpose mailboxes to get round-robin order
rounds = zip_longest(*mailboxes, fillvalue=None)
# Flatten and filter out None and "J"
result = []
for round_mails in rounds:
for mail in round_mails:
if mail and mail != "J":
result.append(mail)
return result
# Test the solution
mailboxes = [["W", "P"],["J", "P", "J"],["W"]]
print(sort_important_mails(mailboxes))
['W', 'W', 'P', 'P']
Comparison
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| Manual Round-Robin | O(n) | O(m) | Medium |
| zip_longest Approach | O(n) | O(n) | High |
Where n is total number of emails and m is number of mailboxes.
Conclusion
The round-robin approach efficiently processes multiple mailboxes while filtering junk emails. The zip_longest method provides a more Pythonic solution with better readability for the same functionality.
