fork download
  1. s = input()
  2. li = s.split(' ')
  3.  
  4. for i in range(2, len(li)):
  5. if li[i] == 'and' or li[i] == 'or':
  6. if li[i - 1][-1] != ',' and li[i - 2][-1] == ',':
  7. li[i - 1] = li[i - 1] + ','
  8.  
  9. print(' '.join(li))
  10.  
Success #stdin #stdout 0.02s 9220KB
stdin
C, Python and Ruby.
stdout
C, Python, and Ruby.