fork(1) download
  1. phones = ['00-44 48 5555 8361', '34535 43 - 23', '023423 1 2-00',
  2. '345345345345', '(345)-345765 345 75', '8(913)345-341 2 3']
  3.  
  4. def canon_number(number):
  5. digits = [c for c in number if c.isdigit()]
  6. remainder = len(digits) % 3
  7. head = "{}{}{}-" * (len(digits) // 3 - 1)
  8.  
  9. if remainder == 1:
  10. tail = "{}{}-{}{}"
  11. elif remainder == 2:
  12. tail = "{}{}{}-{}{}"
  13. else:
  14. tail = "{}{}{}"
  15.  
  16. return (head + tail).format(*digits)
  17.  
  18.  
  19.  
  20. for phone in phones:
  21. print(canon_number(phone))
  22.  
Success #stdin #stdout 0.02s 27616KB
stdin
Standard input is empty
stdout
004-448-555-583-61
345-354-323
023-423-12-00
345-345-345-345
345-345-765-345-75
891-334-534-123