-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
59 lines (43 loc) · 1.62 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
import random as r
### Implementation ###
"""
Solution of the problem implemented as two different version, but used the first version which is iterative one.
"""
def shared_tasks(P, Q):
R = []
for task_P in P: # Iterate over all tasks in P bag
for task_Q in Q: # Iterate over all tasks in Q bag for each task in P bag
if task_Q == task_P: # Compare each task in Q bag with current task in P bag
R.append(Q.pop(Q.index(task_Q))) # If two tasks are equal then remove it from Q bag
break # in order not to compare again and add it shared tasks bag of R
return R
def shared_tasks_v2(P, Q):
return [Q.pop(Q.index(task_Q)) for task_P in P for task_Q in Q if task_Q == task_P]
### Test Cases ###
print("Test Case 1")
P = ['V','Y','X','Y','U','V','W','W']
Q = ['Y','V','X','U','U','V']
print("Tasks Bag P\t\t: ",P)
print("Tasks Bag Q\t\t: ",Q)
R = shared_tasks(P,Q)
print("Shared Tasks Bag R\t: ",R)
print("Test Case 2")
L_MIN = 5
L_MAX = 10
T = 'UVWXY'
P = [r.choice(T) for i in range(r.randint(L_MIN, L_MAX))]
Q = [r.choice(T) for i in range(r.randint(L_MIN, L_MAX))]
print("Tasks Bag P\t\t: ",P)
print("Tasks Bag Q\t\t: ",Q)
R = shared_tasks(P,Q)
print("Shared Tasks Bag R\t: ",R)
print("Test Case 3")
L_MIN = 5
L_MAX = 10
T = 'MNTUVXYZ'
P = [r.choice(T) for i in range(r.randint(L_MIN, L_MAX))]
Q = [r.choice(T) for i in range(r.randint(L_MIN, L_MAX))]
print("Tasks Bag P\t\t: ",P)
print("Tasks Bag Q\t\t: ",Q)
R = shared_tasks(P,Q)
print("Shared Tasks Bag R\t: ",R)