fork download
  1. def make_palindrome(s):
  2. char_count = Counter(s)
  3. flag = 0
  4. mid = (None, None)
  5.  
  6. for key, count in char_count.items():
  7. if count % 2 != 0:
  8. if flag == 0:
  9. flag = 1
  10. mid = (key, count % 2)
  11. char_count[key] = count - count % 2
  12. else:
  13. return 'Impossible'
  14.  
  15. sorted_list = sorted(char_count.keys())
  16.  
  17. palindrome = []
  18.  
  19. for el in sorted_list:
  20. palindrome.append(el*(char_count[el]//2))
  21.  
  22. if mid[0]:
  23. palindrome.append(mid[0]*mid[1])
  24.  
  25. for i in range(len(sorted_list) - 1, -1, -1):
  26. palindrome.append(sorted_list[i]*(char_count[sorted_list[i]]//2))
  27.  
  28. return ''.join(palindrome)
  29.  
  30.  
  31. s = input()
  32.  
  33. print(make_palindrome(s))
Runtime error #stdin #stdout #stderr 0.12s 23528KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 31, in <module>
EOFError: EOF when reading a line