-
Notifications
You must be signed in to change notification settings - Fork 22
/
companyTag.py
70 lines (52 loc) · 2.03 KB
/
companyTag.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
# Change a Company Tag using algorithms #
print (
"""
There is a Company with a Tag comment (string) that when we divide the
comment into equal sub strings - All are supposed to be equal
However we have incorrect Tags assigned to the company which we need
to change.
The Task is:
for a given Tag comment we need to check that all the sub-strings are equal
if not, we need to change the sub-strings so that all will be same
in such a way that the cost of doing this operation is minimum
The cost of changing a to b is 1 which is difference between the
locations of a and b in alphabets
Example: Given string -> aabaaabab, K = 3,
aab | aaa | bab
The cost is equal to a->b + b->a i.e 1+1 = 2
result : aab | aab | aab (All are equal)
"""
)
import string
import numpy as np
STRG = input()
k = int(input())
alphabs = list(string.ascii_lowercase)
costs = [alphabs.index(i)+1 for i in alphabs]
CHARS = len(alphabs)
def correction_strg(strg,K):
if (len(strg)%K == 0):
sub_lists = []
for i in range(0,len(strg),K):
sub_lists.append(strg[i:i+K])
if (len(list(set(sub_lists)))!=1):
uniq_lists = list(set(sub_lists))
costArr=[]
for s in uniq_lists:
costArr.append([alphabs.index(i) for i in s])
costArr = np.array(costArr)
diff=[]
for I in range(len(costArr)):
sum_diff=0
for J in range(len(costArr)):
if (I!=J):
w = (I,J)
q = abs(np.subtract(costArr[I],costArr[J])).sum()
sum_diff+=q
diff.append(sum_diff)
return (diff,costArr,sub_lists)
else:
print ('Unequal Partitions')
return
x = correction_strg(STRG,k)
print (min(x[0]))