t442.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. def helper(got,expect):
  2. if got == expect: print True
  3. else: print False,expect,got
  4. print "\nstr.split()"
  5. helper(''.split(),[])
  6. helper(''.split(None),[])
  7. helper(''.split(None,1),[])
  8. helper(''.split('a'),[''])
  9. helper(''.split('a',1),[''])
  10. helper('hello'.split(),['hello'])
  11. helper('hello'.split(None),['hello'])
  12. helper(' hello world '.split(),['hello', 'world'])
  13. helper(' hello world '.split(None),['hello', 'world'])
  14. helper(' hello world '.split(None,1),['hello', 'world '])
  15. helper('hello world ! '.split(),['hello','world','!'])
  16. helper('hello'.split('l'),['he','','o'])
  17. helper('hello'.split('l',1),['he','lo'])
  18. print "\nSEP AS A REGULAR EXPRESSION"
  19. print "without regex syntax"
  20. helper('aaaba'.split('a'),['','','','b',''])
  21. helper('aaaba'.split('b'),['aaa','a'])
  22. print "\nsyntax: ."
  23. helper('aaaba'.split('a.'),['aaaba'])
  24. helper('aaaba'.split('.a'),['aaaba'])
  25. helper('aaaba'.split('a.',1),['aaaba'])
  26. helper('aaaba'.split('.a',1),['aaaba'])
  27. helper('aaaba'.split('b.'),['aaaba'])
  28. helper('aaaba'.split('.b'),['aaaba'])
  29. print "\nsyntax: ^"
  30. helper('aaaba'.split('^a'),['aaaba'])
  31. helper('aaaba'.split('^b'),['aaaba'])
  32. print "\nsyntax: $"
  33. helper('aaaba'.split('a$'),['aaaba'])
  34. helper('aaaba'.split('b$'),['aaaba'])
  35. print "\nsyntax: *"
  36. helper('aaaba'.split('a*'),['aaaba'])
  37. helper('aaaba'.split('b*'),['aaaba'])
  38. helper('aaaba'.split('ab*'),['aaaba'])
  39. helper('aaaba'.split('ab*',1),['aaaba'])
  40. print "\nsyntax: +"
  41. helper('aaaba'.split('a+'),['aaaba'])
  42. helper('aaaba'.split('b+'),['aaaba'])
  43. helper('aaaba'.split('ab+'),['aaaba'])
  44. print "\nsyntax: ?"
  45. helper('aaaba'.split('a?'),['aaaba'])
  46. helper('aaaba'.split('a?',1),['aaaba'])
  47. helper('aaaba'.split('b?'),['aaaba'])
  48. helper('aaaba'.split('ab?'),['aaaba'])
  49. helper('aaaba'.split('ab?',1),['aaaba'])
  50. print "\nsyntax: *?"
  51. helper('aaaba'.split('a*?'),['aaaba'])
  52. helper('aaaba'.split('b*?'),['aaaba'])
  53. helper('aaaba'.split('ab*?'),['aaaba'])
  54. helper('aaaba'.split('ab*?',1),['aaaba'])
  55. print "\nsyntax: +?"
  56. helper('aaaba'.split('a+?'),['aaaba'])
  57. helper('aaaba'.split('a+?',1),['aaaba'])
  58. helper('aaaba'.split('b+?'),['aaaba'])
  59. helper('aaaba'.split('ab+?'),['aaaba'])
  60. print "\nsyntax: ??"
  61. helper('aaaba'.split('a??'),['aaaba'])
  62. helper('aaaba'.split('b??'),['aaaba'])
  63. helper('aaaba'.split('ab??'),['aaaba'])
  64. helper('aaaba'.split('ab??',1),['aaaba'])
  65. print "\nsyntax: {}"
  66. helper('aaaba'.split('a{2}'),['aaaba'])
  67. helper('aaaba'.split('a{1,2}'),['aaaba'])
  68. helper('aaaba'.split('a{1,2}',1),['aaaba'])
  69. helper('aaaba'.split('a{,2}'),['aaaba'])
  70. helper('aaaba'.split('a{1,}'),['aaaba'])
  71. helper('aaaba'.split('a{1,}',1),['aaaba'])
  72. helper('aaaba'.split('b{1}'),['aaaba'])
  73. helper('aaaba'.split('b{1,2}'),['aaaba'])
  74. helper('aaaba'.split('b{,2}'),['aaaba'])
  75. helper('aaaba'.split('b{1,}'),['aaaba'])
  76. print "\nsyntax: {}?"
  77. helper('aaaba'.split('a{2}?'),['aaaba'])
  78. helper('aaaba'.split('a{1,2}?'),['aaaba'])
  79. helper('aaaba'.split('a{1,2}?',1),['aaaba'])
  80. helper('aaaba'.split('a{,2}?'),['aaaba'])
  81. helper('aaaba'.split('a{1,}?'),['aaaba'])
  82. helper('aaaba'.split('a{1,}?',1),['aaaba'])
  83. helper('aaaba'.split('b{1}?'),['aaaba'])
  84. helper('aaaba'.split('b{1,2}?'),['aaaba'])
  85. helper('aaaba'.split('b{,2}?'),['aaaba'])
  86. helper('aaaba'.split('b{1,}?'),['aaaba'])
  87. print "\nsyntax: []"
  88. helper('aaaba'.split('[a-z]'),['aaaba'])
  89. helper('aaaba'.split('[a-z]',1),['aaaba'])
  90. helper('aaaba'.split('[ab]'),['aaaba'])
  91. helper('aaaba'.split('[ab]',1),['aaaba'])
  92. print "\nsyntax: |"
  93. helper('aaaba'.split('a|b'),['aaaba'])
  94. helper('aaaba'.split('a|b',1),['aaaba'])
  95. print "\nsyntax: (...)"
  96. helper('aaaba'.split('(a)(a)(b)(a)'),['aaaba'])
  97. helper('aaaba'.split('(a)(a)(b)(a)',1),['aaaba'])
  98. helper('aaaba'.split('(a{2})(.b.)'),['aaaba'])
  99. helper('aaaba'.split('(a{2})(.b.)',1),['aaaba'])