1class Solution(object):2def reorderSpaces(self, text):3word_list = text.split()4words, spaces = len(word_list), text.count(" ")56if words > 1:7q, r = spaces // (words - 1), spaces % (words - 1)8return (" " * q).join(word_list) + " " * r9else:10return "".join(word_list) + " " * spaces