-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion
More file actions
35 lines (25 loc) · 1.6 KB
/
Copy pathRecursion
File metadata and controls
35 lines (25 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# to report infection chain
InfectedCaseDict = {} # Original case data, two columns consist of PersonID, infected by PersonID
InfectedPersonSet = set() # Staging variable, to host people infected, set of PersonID
InfectedCaseChainDict = {} # Final infected case chain, dictionary of PersonID, and a set of infected PersonID -- InfectedPersonSet
def traverseInfectionChain(PersonID):
InfectedCaseChainDict[PersonID].update(InfectedPersonSet)
# InfectedCaseChainDict[PersonID] = InfectedPersonSet
if int(InfectedCaseDict[PersonID]) != 0: # root of chain, patient 0
InfectedPersonSet.add(PersonID) # No. Add self to person infected
traverseInfectionChain(InfectedCaseDict[PersonID]) # go to person who infected this person
# Make the original case data. PersonID, Infected by PersonID
InfectedCaseDict = {1:0, 2:1, 3:1, 4:1, 5:2, 6:2, 7:3, 8:7, 9:7, 10:7, 11:9
, 12:9, 13:12, 14:1, 15:12, 100:0, 101:100, 102:100, 103:101, 16:4}
for PersonID in InfectedCaseDict:
InfectedPersonSet = set()
InfectedCaseChainDict[PersonID] = set()
traverseInfectionChain(PersonID)
for PersonID in InfectedCaseDict:
if InfectedCaseChainDict[PersonID].__len__() > 1:
print('Person ', PersonID, 'Infected ', InfectedCaseChainDict[PersonID].__len__()
, ' People, they are ', InfectedCaseChainDict[PersonID])
if InfectedCaseChainDict[PersonID].__len__() == 1:
print('Person ', PersonID, 'Infected 1 person, he / she is ', InfectedCaseChainDict[PersonID])
if InfectedCaseChainDict[PersonID].__len__() == 0:
print('Person ', PersonID, 'Infected no one.')