Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. stringMatch("xxcaazz", "xxbaaz") → 3
  2. stringMatch("abc", "abc") → 2
  3. stringMatch("abc", "axc") → 0
  4.  
  5. import doctest
  6.  
  7.  
  8. def all_two_chars_occurencies(string):
  9. """
  10. >>> list(all_two_chars_occurencies('abcd'))
  11. ['ab', 'bc', 'cd']
  12. >>> list(all_two_chars_occurencies('xxcaazz'))
  13. ['xx', 'xc', 'ca', 'aa', 'az', 'zz']
  14. """
  15. for index, char in enumerate(string[:-1]):
  16. yield char + string[index + 1]
  17.  
  18.  
  19. def common_two_chars_occurences(a, b):
  20. """
  21. Given 2 strings, a and b, return the number of the positions where
  22. they contain the same length 2 substring.
  23.  
  24. >>> common_two_chars_occurences('xxcaazz', 'xxbaaz')
  25. 3
  26. >>> common_two_chars_occurences('abc', 'abc')
  27. 2
  28. >>> common_two_chars_occurences('abc', 'axc')
  29. 0
  30. """
  31. equal_duets = 0
  32. for a_duet, b_duet in zip(all_two_chars_occurencies(a),
  33. all_two_chars_occurencies(b)):
  34. if a_duet == b_duet:
  35. equal_duets += 1
  36. return equal_duets
  37.  
  38.  
  39. if __name__ == "__main__":
  40. doctest.testmod()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement