https://leetcode-cn.com/problems/license-key-formatting/

    1. class Solution:
    2. def licenseKeyFormatting(self, S: str, K: int) -> str:
    3. NewS = ''
    4. for i in range(len(S)):
    5. if S[i] !='-':
    6. NewS+=S[i]
    7. Left = len(NewS)%K
    8. R = ''
    9. if Left!=0:
    10. for i in range(Left):
    11. R+=self.upper(NewS[i])
    12. R += '-'
    13. NewS = NewS[Left:]
    14. for i in range(len(NewS)):
    15. R+=self.upper(NewS[i])
    16. if (i+1)%K == 0:
    17. R += '-'
    18. return R[:-1]
    19. def upper(self,s):
    20. if ord(s) <= ord('z') and ord(s) >= ord('a'):
    21. return chr(ord(s) - ord('a') + ord('A'))
    22. else:
    23. return s

    1. class Solution:
    2. def licenseKeyFormatting(self, S: str, K: int) -> str:
    3. N = 0
    4. L = len(S)
    5. R = ''
    6. for i in range(L):
    7. if S[L-i-1] != '-':
    8. N += 1
    9. R = self.upper(S[L-i-1]) + R
    10. if N%K == 0:
    11. R = '-' + R
    12. if len(R) ==0:
    13. return ''
    14. elif R[0] == '-':
    15. return R[1:]
    16. else:
    17. return R
    18. def upper(self,s):
    19. if ord(s) <= ord('z') and ord(s) >= ord('a'):
    20. return chr(ord(s) - ord('a') + ord('A'))
    21. else:
    22. return s