-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek3-medium5.py
More file actions
65 lines (52 loc) · 1.72 KB
/
week3-medium5.py
File metadata and controls
65 lines (52 loc) · 1.72 KB
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
# algorithms -> strings -> richie rich
# https://www.hackerrank.com/challenges/richie-rich
import sys
l, kmax = map(int, raw_input().strip().split())
n= raw_input().strip()
def FoldItUp(l, n):
if l % 2 == 0:
folded, middle = zip(n[:(l/2)+1], list(reversed(n[(l/2):]))), ''
else:
folded, middle = zip(n[:(l/2)], list(reversed(n[(l/2)+1:]))), str(n[l/2])
ct = 0
new_list = []
for x, y in folded:
if x == y:
new_list.append([x])
else:
ct += 1
new_list.append([x,y])
return new_list, middle, ct
folded, middle_char, kmin = FoldItUp(l, n)
new_string, changes_made = '', 0
if kmax >= kmin:
for idx in folded:
if kmax - kmin > changes_made:
if len(idx) < 2:
if idx != ['9'] and (kmax-(kmin + changes_made) >=2):
new_string += '9'
changes_made += 2
else:
new_string += idx[0]
else:
kmin -= 1
if max(idx) == '9':
new_string += '9'
changes_made +=1
elif kmax-(kmin + changes_made) >=2:
new_string += '9'
changes_made +=2
else:
new_string += max(idx)
else:
new_string += max(idx)
if len(idx) == 2:
changes_made += 1
kmin -= 1
if middle_char and changes_made < kmax:
middle_char = '9'
changes_made += 1
else:
new_string = None
result = str(new_string + middle_char + ''.join(list(reversed(new_string)))) if new_string is not None else '-1'
print result