Q2) Represent and query origin-destination records
The origin is the warehouse where a shipment departs; the destination is the warehouse where it arrives. An origin-destination (OD) pair names these two warehouses in that order. A to B and B to A are different pairs. Table 1 contains four fictional shipment records.
| Origin | Destination | Shipments |
|---|---|---|
| A | B | 12 |
| B | A | 7 |
| A | C | 0 |
| C | A | 4 |
Each pair occurs once. Include the recorded zero when finding or printing pairs. Work with recorded pairs, rather than every possible combination of warehouses. Pairs from a warehouse to itself are outside this task. Any result order is acceptable.
- Propose one data structure and show how it stores these four records. You may use a list, a dictionary, or a matrix stored as a list of lists.
- Present algorithms in pseudocode, a flowchart, or Python for the four queries in Table 2. Each query uses your stored records and leaves them unchanged.
| Query | Additional input | Required result |
|---|---|---|
| a. Print all known OD pairs | None | Print each recorded origin, destination, and count; with no records, print nothing |
| b. Find destinations of an origin | An origin | All destinations in its recorded pairs; an empty collection if none |
| c. Find origins of a destination | A destination | All origins in its recorded pairs; an empty collection if none |
| d. Find a specific OD pair | An origin and destination | Its shipment count, including zero, or Unknown if unrecorded |