-
Notifications
You must be signed in to change notification settings - Fork 0
/
q3.py
88 lines (75 loc) · 2.89 KB
/
q3.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import numpy as np
from collections import Counter, defaultdict
import settings
import random
def readfile(filename, randomseed=42):
# open the file to read
infile = open(filename, "r")
random.seed(randomseed)
# get the number of tests
T = int(infile.readline().strip())
# print("Number of tests: ", T)
# results are returned as a list of people ids
results = []
for i in range(T):
# settings.dispaths makes it accessible in a console
settings.dispaths = defaultdict()
# read in Vt people and D days
line = infile.readline().split()
Vt = int(line[0])
D = int(line[1])
# print("Number of people Vt =", Vt)
# print("Period of", D, "days")
# for each day
for j in range(D):
# read number of contacts
nC = int(infile.readline().strip())
# print("Number of contacts on day", j+1, "=", nC)
# for each contact
for k in range(nC):
# read a line and split to A->B with prob alpha
contact = infile.readline().split()
contactA = int(contact[0])
contactB = int(contact[1])
#
# structure here is {'contactB': contactA}
# Only set if PRNG is less than infection probability
#
if random.random() <= float(contact[2]):
#
# only set {'contactB': contactA}
# if contactB is not set
# does not handle all cases - what if?
# should set contactB = dispaths[contactA]
#
if contactB not in settings.dispaths.keys():
settings.dispaths[contactB] = contactA
# print(settings.dispaths)
# used to find the most used person in settings.dispaths
track = dict()
for key, value in settings.dispaths.items():
if value not in track:
track[value] = 1
else:
track[value] += 1
maxval = max(track, key=track.get)
# add max value (person) to
results.append(maxval)
infile.close()
# used to return list of answers to run_many()
return results
def run_many(filename, runs=5, people=10, randomseed=42):
"""
runs readfile_1(filename) * 'runs'
result added to numpy array
median save to txt file outn.txt (set as parameter?)
and returned by function
"""
res = np.empty(shape=(runs, people), dtype='int')
for i in range(runs):
results = readfile(filename)
# print(results)
res[i] = results
resmed = np.median(res, axis=0)
np.savetxt("out-{}.txt".format(filename), resmed, delimiter=" ", fmt="%d")
return resmed