-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
36 lines (29 loc) · 1.3 KB
/
solution.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
# The input is a string str of digits.
# Cut the string into chunks (a chunk here is a substring of the initial string) of size sz
# (ignore the last chunk if its size is less than sz).
# If a chunk represents an integer such as the sum of the cubes of its digits is divisible by 2,
# reverse that chunk; otherwise rotate it to the left by one position.
# Put together these modified chunks and return the result as a string.
# If
# sz is <= 0 or if str is empty return ""
# sz is greater (>) than the length of str it is impossible to take a chunk of size sz hence return "".
# Examples:
# revrot("123456987654", 6) --> "234561876549"
# revrot("123456987653", 6) --> "234561356789"
# revrot("66443875", 4) --> "44668753"
# revrot("66443875", 8) --> "64438756"
# revrot("664438769", 8) --> "67834466"
# revrot("123456779", 8) --> "23456771"
# revrot("", 8) --> ""
# revrot("123456779", 0) --> ""
# revrot("563000655734469485", 4) --> "0365065073456944"
def revrot(s, sz):
if sz <= 0 or not s:
return ''
chunks = [s[i : i + sz] for i in range(0, len(s), sz) if len(s[i : i + sz]) == sz]
for i, v in enumerate(chunks):
if not sum(int(x) ** 3 for x in v) % 2:
chunks[i] = chunks[i][::-1]
else:
chunks[i] = chunks[i][1:] + chunks[i][:1]
return ''.join(chunks)