-
Notifications
You must be signed in to change notification settings - Fork 2
/
createString_N_K_ABC.py
executable file
·113 lines (81 loc) · 2.48 KB
/
createString_N_K_ABC.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
#!/bin/python
class ABC:
mapL = {
'A': 1,
'B': 2,
'C': 3
}
def countSum(self, retStr):
count = 0
i = 0
for s in retStr:
for x in range(i+1, len(retStr)):
if(self.mapL[retStr[x]] > self.mapL[s]):
count += 1
i += 1
return count
def createString_BC(self, N, b, c):
retStr = ""
for i in xrange(N):
if(i<N-b-c):
retStr += "A"
elif(i<N-c):
retStr += "B"
else:
retStr += "C"
return retStr
def bruteForce(self, N, K, myLst, idx_start, idx_end):
for i in range(idx_start, idx_end):
myLst[i] = "B"
if self.countSum(myLst) == K:
return "".join(myLst)
else:
myLst[i] = "A"
myLst[idx_start] = "B"
return self.bruteForce(N, K, myLst, idx_start+1, idx_end)
def findMatch(self, N, K, cnt_B, cnt_C):
myStr = self.createString_BC(N, cnt_B, cnt_C)
mySum = self.countSum(myStr)
print "DEBUG:",myStr, mySum
raw_input('Press any key...')
# if((cnt_C*(N-cnt_C) + cnt_B*(N-cnt_C-cnt_B) + cnt_B*cnt_C) == K):
if mySum <= 0:
# no match, return ""
return ""
elif mySum == K:
# it matches, return
return myStr
elif mySum < K:
# add C or B
if(cnt_B > cnt_C*2):
return self.findMatch(N, K, cnt_B-1, cnt_C+1)
else:
return self.findMatch(N, K, cnt_B+1, cnt_C)
else:
# we overcame the number
# craft a string with cnt_B B's followed by cnt_C
return self.bruteForce(N, K, list(myStr), 0, N-cnt_B-cnt_C)
def createString(self, N, K):
ret = ""
# check input: 3<=N<=30
# ...
# check input: 0<=K<=N*(N-1)/2
# ...
if K<N:
# just add C to the Kth place
for i in xrange(N):
if(i<K):
ret += "A"
elif(i == K):
ret += "C"
else:
ret += "A"
return ret
# start with the end C
else:
cnt_C = 1
cnt_B = 0
return self.findMatch(N, K, cnt_B, cnt_C)
a = ABC()
# print "Result: " + a.createString(15, 36)
print "Result: " + a.createString(3, 0)