
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to sorting important mails from different mailboxes in Python
Suppose we have a list of mailboxes. Here in each mailbox a list of strings is given, here each string is either "J" for junk, "P" for personal, "W" for Work. We will go through each mailbox in round robin order starting from the first mailbox, filtering out J, to form a single list and return the list.
So, if the input is like mailboxes = [["W", "P"],["J", "P", "J"],["W"]], then the output will be ["W", "W", "P", "P"], as in order and without filtering, we have W -> J -> W -> P -> P -> J, now since we filter out the junk we get W -> W -> P -> P.
To solve this, we will follow these steps −
- n_mailboxes := size of mailboxes
- result := a new list
- counts := a list of size n_mailboxes, then fill with 0
- more := True
- while more is non-zero, do
- more := False
- for i in range 0 to n_mailboxes, do
- index := counts[i], mailbox := mailboxes[i]
- if index < size of mailbox, then
- more := True
- counts[i] := counts[i] + 1
- mail := mailbox[index]
- if mail is not same as "J", then
- insert mail at the end of result
- return result
Let us see the following implementation to get better understanding −
Example
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 ob = Solution() mailboxes = [["W", "P"],["J", "P", "J"],["W"]] print(ob.solve(mailboxes))
Input
[["W", "P"],["J", "P", "J"],["W"]]
Output
['W', 'W', 'P', 'P']
- Related Articles
- Why sorting of materials is important?
- Custom sorting using two different columns in MySQL?
- Different Methods to find Prime Number in Python Program
- C++ Program to Implement Sorting containers in STL
- Program to find number of different subsequences GCDs in Python
- Program to find number of different substrings of a string for different queries in Python
- Program to find shortest sublist so after sorting that entire list will be sorted in Python
- Program to count minimum semesters to cover all different courses in Python
- Analysis of Different Methods to find Prime Number in Python program
- Sorting the numbers from within in JavaScript
- Program to perform sorting using selection sort in 8085 Microprocessor
- C Program for Pancake sorting?
- Java Program for Pancake sorting
- 8085 Program to perform sorting using bubble sort
- 8085 Program to perform sorting using selection sort

Advertisements