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 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 an order is picked up, and "D" means delivery. These letters are followed by the order id number. For example, "P6" indicates pick up order 6. We have to check whether the orders list is valid or not based on these rules ?
- We cannot deliver an order before pickup
- Every pickup must be delivered
- An order which has already been picked up and 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 the first order is delivered after picking up, and for the second and third orders they are picked up first and also delivered finally.
Algorithm
To solve this, we will follow these steps ?
- Create a dictionary to track pickup status for each order
- Check for duplicate entries in the orders list
- For each operation in orders:
- If it starts with "P" (pickup), mark the order as picked up
- If it starts with "D" (delivery), check if order was picked up first, then mark as delivered
- Return True if all picked up orders are delivered (sum of values equals 0)
Example
Let us see the following implementation to get better understanding ?
def solve(orders):
pickup_status = {}
# Check for duplicate entries
if len(set(orders)) != len(orders):
return False
for operation in orders:
if operation[0] == "P":
# Pickup operation - mark order as picked up
order_id = operation[1:]
pickup_status[order_id] = 1
elif operation[0] == "D":
# Delivery operation - check if order was picked up
order_id = operation[1:]
if order_id not in pickup_status:
return False # Cannot deliver before pickup
else:
pickup_status[order_id] -= 1
# All picked up orders must be delivered (sum should be 0)
return sum(pickup_status.values()) == 0
# Test the function
orders = ["P1", "D1", "P2", "P3", "D3", "D2"]
print("Orders:", orders)
print("Valid:", solve(orders))
The output of the above code is ?
Orders: ['P1', 'D1', 'P2', 'P3', 'D3', 'D2'] Valid: True
Testing Different Cases
Let's test with various scenarios to understand how the validation works ?
def solve(orders):
pickup_status = {}
if len(set(orders)) != len(orders):
return False
for operation in orders:
if operation[0] == "P":
order_id = operation[1:]
pickup_status[order_id] = 1
elif operation[0] == "D":
order_id = operation[1:]
if order_id not in pickup_status:
return False
else:
pickup_status[order_id] -= 1
return sum(pickup_status.values()) == 0
# Test cases
test_cases = [
["P1", "D1", "P2", "D2"], # Valid - all picked up and delivered
["D1", "P1"], # Invalid - delivery before pickup
["P1", "P2", "D1"], # Invalid - P2 not delivered
["P1", "D1", "P1"] # Invalid - duplicate operations
]
for i, orders in enumerate(test_cases, 1):
result = solve(orders)
print(f"Case {i}: {orders} ? {result}")
The output of the above code is ?
Case 1: ['P1', 'D1', 'P2', 'D2'] ? True Case 2: ['D1', 'P1'] ? False Case 3: ['P1', 'P2', 'D1'] ? False Case 4: ['P1', 'D1', 'P1'] ? False
How It Works
The algorithm uses a dictionary to track the pickup status of each order. When an order is picked up, its value is set to 1. When delivered, the value decreases to 0. The validation ensures:
- No duplicates: Each operation can occur only once
- Pickup before delivery: Delivery operations check if the order exists in the dictionary
- All orders delivered: Sum of all values must be 0 (meaning all pickups have corresponding deliveries)
Conclusion
This solution efficiently validates delivery operations by tracking pickup status using a dictionary. The algorithm ensures proper order sequencing and completeness of delivery operations with O(n) time complexity.
