-
Notifications
You must be signed in to change notification settings - Fork 8
/
processes.py
65 lines (48 loc) · 1.89 KB
/
processes.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
"""
In this task you have to code process planner.
You will be given initial thing, target thing and a set of processes to turn one thing into another
(in the form of [process_name, start_thing, end_thing]).
You must return names of shortest sequence of processes to turn initial thing into target thing,
or empty sequence if it's impossible.
If start already equals end, return [], since no path is required.
Example:
test_processes = [
['gather', 'field', 'wheat'],
['bake', 'flour', 'bread'],
['mill', 'wheat', 'flour']
];
processes('field', 'bread', test_processes) # should return ['gather', 'mill', 'bake']
processes('field', 'ferrari', test_processes) # should return []
processes('field', 'field', test_processes) # should return [], since no processes are needed
"""
def processes(start, end, processes_table):
start_end_dict = {}
process_name_dict = {}
for process in processes_table:
start_end_dict[process[1]] = process[2]
process_name_dict[process[1]] = process[0]
results = []
current_start = start
while True:
if current_start not in start_end_dict:
return []
results.append(process_name_dict[current_start])
end_thing = start_end_dict[current_start]
if end_thing == end:
break
current_start = start_end_dict[current_start]
return results
from unittest import TestCase
class TestProcesses(TestCase):
def test_processes(self):
test_processes = [
['gather', 'field', 'wheat'],
['bake', 'flour', 'bread'],
['mill', 'wheat', 'flour']
]
self.assertEquals(
processes('field', 'bread', test_processes), ['gather', 'mill', 'bake'])
self.assertEquals(
processes('field', 'ferrari', test_processes), [])
self.assertEquals(
processes('field', 'field', test_processes), [])