-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday7-1.py
More file actions
74 lines (63 loc) · 1.88 KB
/
Copy pathday7-1.py
File metadata and controls
74 lines (63 loc) · 1.88 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
#input function for both python2.7 and 3
def getInput(prompt=''):
try:
line = raw_input(prompt)
except NameError:
line = input(prompt)
return line
print('Enter/paste content. Ctrl-D to save. ', end='')
user_input = []
while True:
try:
line = getInput()
except EOFError:
break
user_input.append(line)
class Program:
def __init__(self, name, weight):
self.name = name
self.weight = int(weight)
self.neighbors = []
def add_neighbor(self, neighbor):
self.neighbors.append(neighbor)
program_list = []
def readData(input):
data_list = input.split()
name = data_list[0]
weight = data_list[1][1:-1]
program_list.append(Program(name, weight))
if len(data_list) > 2:
for nei in range(3, len(data_list)):
neighbor_name = data_list[nei].strip(',')
program_list[-1].add_neighbor(neighbor_name)
def nameToIndex(query):
global program_list
for program in range(len(program_list)):
if program_list[program].name == query:
return program
def allNeighbors():
global program_list
all_neighbors = []
for program in program_list:
for neighbor in program.neighbors:
all_neighbors.append(neighbor)
return all_neighbors
def findBottom():
global program_list
candidates = []
candidates_neighbors = []
all_neighbors = allNeighbors()
for item in program_list:
if len(item.neighbors) > 0:
candidates.append(item.name)
candidates_neighbors.append(item.neighbors)
for candidate in candidates:
if candidate not in all_neighbors:
bottom = candidate
print("bottom program is",bottom)
for line in user_input:
readData(line)
findBottom()