fork download
  1. f=lambda s,r='',c='':f(s[1:],*{'B':(r[:-1],c),'C':(r,r),'D':('',c),'P':(r+c,c),'':(r+s[0],c)}[s[0][1:2]])if s else r
  2.  
  3. tests = [
  4. ['H', 'e', 'l', 'l', 'o'],
  5. ['H', 'e', 'l', 'l', 'o', ' ', '[C]', '[P]'],
  6. ['[D]', 'D', '[B]'],
  7. [''],
  8. ['e', '[C]', '[B]', 'I', ' ', 'l', 'i', 'k', '[P]', ' ', 'b', '[P]', '[P]', 's', '!'],
  9. ['N', '[P]'],
  10. ['#', '5', '0', 'K', '0', '0', '1', '[D]', '#', 'n', 'o', 't']]
  11. note = '* Note: output is repr(f(input)), to quote the string for clarity *'
  12. border1 = '*'*len(note)+'\n'
  13. border2 = '*'+' '*(len(note)-2)+'*'+'\n'
  14. print border1+border2+note+'\n'+border2+border1
  15. for test in tests:
  16. print 'Input:', test
  17. print 'Output:', repr(f(test))
  18. print ''
Success #stdin #stdout 0.01s 9024KB
stdin
Standard input is empty
stdout
*******************************************************************
*                                                                 *
* Note: output is repr(f(input)), to quote the string for clarity *
*                                                                 *
*******************************************************************

Input: ['H', 'e', 'l', 'l', 'o']
Output: 'Hello'

Input: ['H', 'e', 'l', 'l', 'o', ' ', '[C]', '[P]']
Output: 'Hello Hello '

Input: ['[D]', 'D', '[B]']
Output: ''

Input: ['']
Output: ''

Input: ['e', '[C]', '[B]', 'I', ' ', 'l', 'i', 'k', '[P]', ' ', 'b', '[P]', '[P]', 's', '!']
Output: 'I like bees!'

Input: ['N', '[P]']
Output: 'N'

Input: ['#', '5', '0', 'K', '0', '0', '1', '[D]', '#', 'n', 'o', 't']
Output: '#not'