generated from hexmod/hash-code-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
146 lines (133 loc) · 5.39 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
###############################
##
# HASH CODE ENTRY
##
# Usage: python main.py <path_to_source_file> <output_location>
##
###############################
import os
import sys
import pickle
from Contributor import Contributor
from Project import Project
def do_with_cache(input_file_location, identifier, param):
#
# Sample method. This method will first check if the result
# is cached and use that if avaliable. Else it will do the
# work and then cache the results
#
input_file_name = os.path.basename(input_file_location).split(".")[0]
cache_file = os.path.join(
".\\cache", input_file_name + "_" + identifier + ".pkl")
if os.path.isfile(cache_file):
print(f"Using Cache for {identifier}")
with open(cache_file, "rb") as f:
res = pickle.load(f)
else:
# Do the work here
res = "Something..."
with open(cache_file, "wb") as f:
pickle.dump(res, f)
return res
def run(file_location, output_location):
print("Running Hash Code Entry")
# Hash Code here :)
allContributors = []
allProjects = []
input_file = open(file_location, "r")
numContributors = 0
seenContributors = 0
numProjects = 0
currentContributor = None
currentProject = None
currentContSkills = 0
currentProjectSkills = 0
count = 0
for line in input_file:
# Do Something
count+=1
if count == 1:
splitLine = line.split(" ")
numContributors = int(splitLine[0])
numProjects = int(splitLine[1])
elif seenContributors < numContributors:
if currentContSkills == 0:
splitLine = line.split(" ")
currentContributor = Contributor(splitLine[0], int(splitLine[1]))
currentContSkills = int(splitLine[1])
currentContSeenSkills = 0
else:
splitLine = line.split(" ")
currentContributor.add_skill(splitLine[0], int(splitLine[1]))
currentContSeenSkills += 1
if currentContSeenSkills == currentContSkills:
# Seen all skills
allContributors.append(currentContributor)
currentContSkills = 0
seenContributors += 1
else:
if currentProjectSkills == 0:
splitLine = line.split(" ")
currentProject = Project(splitLine[0], int(splitLine[1]), int(splitLine[2]), int(splitLine[3]), int(splitLine[4]))
currentProjectSkills = int(splitLine[4])
currentProjectSeenSkills = 0
else:
splitLine = line.split(" ")
currentProject.add_role(splitLine[0], int(splitLine[1]))
currentProjectSeenSkills += 1
if currentProjectSeenSkills == currentProjectSkills:
# Seen all project
allProjects.append(currentProject)
currentProjectSkills = 0
input_file.close()
allProjects.sort(key=lambda project: project.get_shortest_completion_difference())
completedProjects = []
### DO THE WORK
scheduledNewProject = True
while scheduledNewProject:
scheduledNewProject = False
for aProject in allProjects:
roleCount = 0
for aRole in aProject.get_roles():
required_skill = aProject.get_skill_for_role(roleCount)
for aContributor in allContributors:
if aContributor.get_skill_level(aRole) >= required_skill and not aProject.is_contributor_assigned(aContributor):
aProject.add_contributor(aContributor, roleCount)
break
roleCount += 1
if aProject.is_completed():
aProject.train_contributors()
scheduledNewProject = True
completedProjects.append(aProject)
else:
# Look for mentor
roleCount = 0
for aRole in aProject.get_roles():
required_skill = aProject.get_skill_for_role(roleCount)
if not aProject.is_role_filled(aRole) and aProject.has_mentor_for_role(aRole, required_skill):
for aContributor in allContributors:
if aContributor.get_skill_level(aRole) >= (required_skill - 1) and not aProject.is_contributor_assigned(aContributor):
aProject.add_contributor(aContributor, roleCount)
break
roleCount += 1
if not aProject.is_completed():
aProject.unassign_contributors()
for aProject in completedProjects:
if aProject in allProjects:
allProjects.remove(aProject)
completedProjectCount = 0
### PRINT
output_file = open(output_location, "w")
# Write to file here
for aProject in completedProjects:
if aProject.is_completed():
completedProjectCount += 1
output_file.write(f"{completedProjectCount}\n")
for aProject in completedProjects:
if aProject.is_completed():
output_file.write(f"{aProject.get_name()}\n")
output_file.write(f"{aProject.get_ordered_roles()}\n")
output_file.close()
# When run from the terminal
if __name__ == '__main__':
run(sys.argv[1], sys.argv[2])