-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrearrange_characters.py
169 lines (127 loc) · 3.83 KB
/
rearrange_characters.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import collections
# s = "aaaaaaabbbbbbbbbccd"
def get_dict(s):
result = collections.defaultdict(int)
for i in range(len(s)):
result[s[i]] += 1
return result
# print get_dict(s)
#
# print sorted(get_dict(s).items(), key=lambda x: x[1], reverse=True)
def get_rearranged(s):
list_tuples = sorted(get_dict(s).items(), key=lambda x: x[1], reverse=True)
list_arr = map(lambda x: list(x), list_tuples)
result = ""
first = True
while len(list_arr) != 0:
if first:
result += list_arr[0][0]
list_arr[0][1] -= 1
if list_arr[0][1] == 0:
list_arr.remove(list_arr[0])
else:
first = False
else:
result += list_arr[1][0]
list_arr[1][1] -= 1
if list_arr[1][1] == 0:
list_arr.remove(list_arr[1])
first = True
return result
# print get_rearranged(s)
def get_indexes(empty_i, count):
print range(empty_i, empty_i + count * 2, 2)
def get_rearrange2(s):
list_tuples = sorted(get_dict(s).items(), key=lambda x: x[1], reverse=True)
list_arr = map(lambda x: list(x), list_tuples)
even_empty = 0
odd_empty = 1
even = True
for arr in list_arr:
if even:
print get_indexes(even_empty, arr[1])
even_empty = even_empty + arr[1] * 2 + 1
even = False
else:
print get_indexes(odd_empty, arr[1])
odd_empty = odd_empty + arr[1] * 2 + 1
even = True
# get_rearrange2(s)
def get_max_occurence(length):
if length % 2 == 0:
return length / 2
return length / 2 + 1
# print get_max_occurence(6)
# print get_max_occurence(5)
# s = "aaaaacccdc"
def permutate(s):
l = list(s)
mutated = True
if len(s) > 1:
while mutated:
mutated = False
for j in range(len(l) - 2):
if l[j] == l[j + 1] and l[j + 1] != l[j + 2]:
l[j + 1], l[j + 2] = l[j + 2], l[j + 1]
mutated = True
elif l[j] != l[j + 1] and l[j + 1] == l[j + 2]:
l[j + 1], l[j] = l[j], l[j + 1]
mutated = True
if l[-1] == l[-2]:
return None
return "".join(l)
def permutate2(s):
l = list(s)
mutated = True
if len(s) > 1:
while mutated:
mutated = False
for j in range(len(l) - 2):
if l[j] == l[j + 1]:
l[j + 2], l[j + 1] = l[j + 1], l[j + 2]
mutated = True
if l[-1] == l[-2]:
return None
return "".join(l)
def perm(s):
l = list(s)
for i in range(len(l)):
for j in range(len(l) - 2 - i):
if l[j] == l[j + 1]:
l[j + 2], l[j + 1] = l[j + 1], l[j + 2]
return "".join(l)
def shortBubbleSort(s):
alist = list(s)
exchanges = True
passnum = len(alist) - 1
while passnum > 0 and exchanges:
exchanges = False
for i in range(1, passnum):
if alist[i - 1] == alist[i] and alist[i] != alist[i + 1]:
exchanges = True
temp = alist[i]
alist[i] = alist[i + 1]
alist[i + 1] = temp
passnum = passnum - 1
return "".join(alist)
s = "aaaaacccdccccddddddddddddd"
# print shortBubbleSort(list(s))
def p(s):
l = list(s)
stack = []
result = []
while len(l) != 0:
buf = l.pop(0)
if len(stack) != 0 and stack[-1] == buf:
stack.append(buf)
elif len(stack) != 0 and stack[-1] != buf:
result.append(stack.pop())
result.append(buf)
return "".join(result)
# print p(s)
def reverse(s):
l = list(s)
for i in range(len(s) // 2):
l[i], l[-i - 1] = l[-i - 1], l[i]
return "".join(l)
print reverse("123456")