n = int(input())
string = input()
string_list = list(string)
division_list = []
for i in range(n):
if n%(i+1) == 0:
division_list.append(i+1)
division_list.sort(reverse=True)
print(division_list)
for division in division_list:
temp1 = string_list[:division]
temp2 = string_list[division:]
print('before reverse')
print(temp1)
temp1.sort(reverse=True)
print('after reverse')
print(temp1)
string_list = temp1 + temp2
ans = ''.join(string_list)
print(ans)
I was solving problem in code forces, and realized sort function doesn't work more than one in for loop. Why? And is there any further stuffs I can do on my code?
This is the link of the question.
https://codeforces.com/problemset/problem/999/B
edited) I put 4 and abcd as a input. This is the output. I expected to see ['c', 'd'] after second temp1.sort(reverse=True). But it doesn't reversed.
4
abcd
[4, 2, 1]
before reverse
['a', 'b', 'c', 'd']
after reverse
['d', 'c', 'b', 'a']
before reverse
['d', 'c']
after reverse
['d', 'c']
before reverse
['d']
after reverse
['d']
dcba
.sort()to do. If you wanted to reverse whatever order the list happened to have, that's not a job for.sort(), that'stemp1 = temp1[::-1].