
- 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 check all listed delivery operations are valid or not in Python
Suppose we have a list of strings called orders. Each element in the orders list starts with either "P" or "D". The "P" indicates that is picked up, and "D" means "delivery". And these letters are followed by the order id number. For example, "P6" indicates pick up order 6. We have to check whether orders list is valid or not based on these rules −
- We cannot delivery an order before pickup
- Every pickup must be delivered
- An order which is already been picked up and also delivered cannot be picked up or delivered again
So, if the input is like orders = ["P1", "D1", "P2", "P3", "D3", "D2"], then the output will be True, because first order is delivered after picking up, and for the second and third order they are picked up at once and also delivered finally.
To solve this, we will follow these steps −
- a := a new map
- if orders have any duplicate entries, then
- return False
- for each i in orders, do
- if i starts with "P", then
- a[pick up order number] = 1
- otherwise when i starts with "D", then
- if order number is not in a, then
- return False
- otherwise,
- a[delivery order number] decrease by 1
- if order number is not in a, then
- if i starts with "P", then
- return true when sum of all elements present in list of all values of a is same as 0, otherwise false
Example
Let us see the following implementation to get better understanding −
def solve(orders): a = {} if len(set(orders)) != len(orders): return False for i in orders: if i[0] == "P": a[i[1:]] = 1 elif i[0] == "D": if i[1:] not in a: return False else: a[i[1:]] -= 1 return sum(a.values()) == 0 orders = ["P1", "D1", "P2", "P3", "D3", "D2"] print(solve(orders))
Input
["P1", "D1", "P2", "P3", "D3", "D2"]
Output
True
- Related Articles
- Python program to check credit card number is valid or not
- Program to check whether given list is in valid state or not in Python
- Program to check all values in the tree are same or not in Python
- Program to check whether all leaves are at same level or not in Python
- Program to check all 1s are present one after another or not in Python
- Check whether triangle is valid or not if sides are given in Python
- Program to check whether a board is valid N queens solution or not in python
- C Program to check if a date is valid or not
- Program to check whether all palindromic substrings are of odd length or not in Python
- Check whether a string is valid JSON or not in Python
- Count All Valid Pickup and Delivery Options in C++
- Program to check whether parentheses are balanced or not in Python
- Program to check whether we can take all courses or not in Python
- Program to check whether we can unlock all rooms or not in python
- Program to check whether all can get a seat or not in Python

Advertisements