t470.py 1011 B

1234567891011121314151617181920212223242526272829303132
  1. def helper(got,expect):
  2. if got == expect: print True
  3. else: print False, expect, got
  4. print "\nstr.find"
  5. helper('hello world'.find('l',-2),9)
  6. helper('hello world'.find('l',4,6),-1)
  7. helper('hello world'.find('o',2,5),4)
  8. helper('hello world'.find('o',2,-5),4)
  9. helper('hello world'.find('o',-8,-5),4)
  10. helper('hello world'.find('o',-3,-1),-1)
  11. print "\nstr.index"
  12. helper('hello world'.index('l',-2),9)
  13. helper('hello world'.index('o',2,5),4)
  14. helper('hello world'.index('o',2,-5),4)
  15. helper('hello world'.index('o',-8,-5),4)
  16. print "\nstr.rfind"
  17. helper('hello world'.rfind('h',-2),-1)
  18. helper('hello world'.rfind('l',2,4),3)
  19. helper('hello world'.rfind('l',2,8),3)
  20. helper('hello world'.rfind('l',-1,10),-1)
  21. helper('hello world'.rfind('l',1,-3),3)
  22. helper('hello world'.rfind('l',-9,-2),3)
  23. print "\nstr.rindex"
  24. helper('hello world'.rindex('l',-2),9)
  25. helper('hello world'.rindex('l',0,-3),3)
  26. helper('hello world'.rindex('o',2,7),4)
  27. helper('hello world'.rindex('o',2,-2),7)
  28. helper('hello world'.rindex('o',-5,-2),7)