Transit Frequencies

A company specialized in managing large shopping malls aims at avoiding crowded areas anywhere in the mall in order to comply with customers security standards. To that aim it is interested on an information system for automatic detection of areas in the mall where the customers transit is high. The system projected is composed of the following subsystems:

  • The Sensors Network: A number of sensors properly distributed throughout the sections in the mall where customers are passing.

  • the Customers App: An app installed in all customers mobile phones that will send a signal to the sensor when the customer enters that section.

  • The Information System: It gets information about the routes taken by each customer that visited the mall. This information is stored in the so-called trajectory dictionary where:
    • The key is the Customer Id (str)

    • The value is a list of the sensor points (each point is represented as an int) for which the customer has passed. Every pair of consecutive points determines a section.

See the example below. The first entry in the dictionary trajD says that the customer associated with the 'H11' has walked from point 1 to 5, and then from 5 o 7 and from 7 to 9 and so on.


>>> trajD = {
... 'H11':[1, 5, 7, 9, 6, 2, 6, 9],
... 'M22':[3, 1, 5, 7, 9, 6],
... 'H23':[5, 7, 9, 6, 2, 9, 6],
... }

Now, we define a transit dictionary as a dict where:

  • The key is a tuple with pair of points (i, j) where i < j determining a section.

  • The corresponding value is transit index of that section, namely the number of times that a customer passed through that section either way (hence walking from i to j and from j to i are both counted under the key (i,  j).

Given our purpose and this available information, please implement the following Python functions in the transit module (transit.py file) the following functions.

The first function is:

transit.gen_transitD(trajD)

such that

given trajD, dict, a trajectory dictionary as described above

returns a dict with the transit dictionary corresponding to trajD as defined above

For example, given the previous input, the outputs produced should be:


>>> transitD = gen_transitD(trajD)
>>> corrD = {
... (1, 5): 2, 
... (5, 7): 3, 
... (7, 9): 3, 
... (6, 9): 5, 
... (2, 6): 3,
... (1, 3): 1,
... (2, 9): 1
... }

>>> if transitD == corrD:
... 	True
... else:
... 	print(transitD)
True

Doctests are available at the gen_transitD.test file.


The second function is:

transit.gen_high_transitL(trajD, n)

such that

given

  • a transit dictionary as described above

  • n a int , n >= 0

returns a list with all sections where the transit has been higher than n. The section in this list must be decreasingly ordered by the transit index of the section. Those with the same index must be increasingly ordered by the points of the section (the first one first and the second for those who have the same first point).

For example, given the dictionary


>>> transitD = {
... (1, 5): 2, 
... (5, 7): 3, 
... (7, 9): 3, 
... (6, 9): 5, 
... (2, 6): 3,
... (1, 3): 1,
... (2, 9): 1,
... }

the function should return


>>> high_transitL = gen_high_transitL(transitD, 2)
>>> if high_transitL == [(6, 9), (2, 6), (5, 7), (7, 9)]:
... 	True
... else:
... 	print(high_transitL)
True

Doctests are available at the gen_high_transitL.test file.