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.

Table 1: Recorded shipment counts for directed warehouse pairs. Zero is a known count; pairs absent from the table have unknown counts.
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.

  1. 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.
  2. 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.
Table 2: Inputs and results for queries on the stored records. For example, query b with C finds A; query c with B finds A; query d with B and A returns 7.
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