Guest User

Untitled

a guest
Jul 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. a = "hellllllllllooooooooooooo"
  2. match = re.search('(.)\1+', a)
  3.  
  4. if match:
  5. print 'found'
  6. print re.sub('(.)\1+', '\1', a)
  7. print re.sub('(.)\1+', '\1\1', a)
  8. else:
  9. print 'not found'
  10.  
  11. helo
  12. helloo
  13.  
  14. def doppelstring(s):
  15. letter_groups = ((val, list(group)) for val, group in itertools.groupby(s))
  16. max_vector = ((val, min(len(group), 2)) for val, group in letter_groups)
  17. vector_components = ([dim * (l + 1) for l in range(maxlen)] for dim, maxlen in max_vector)
  18. return [''.join(letters) for letters in itertools.product(*vector_components)]
  19.  
  20. def doppelstring_(s):
  21. max_vector = ((val, min(len(list(group)), 2)) for val, group in itertools.groupby(s))
  22. vector_components = ([dim * (l + 1) for l in range(maxlen)] for dim, maxlen in max_vector)
  23. return [''.join(letters) for letters in itertools.product(*vector_components)]
  24.  
  25. import re
  26.  
  27. def permute(seq):
  28. if len(seq) < 2:
  29. yield seq
  30. else:
  31. for tail in permute(seq[2:]):
  32. yield seq[:2] + tail
  33. yield seq[:2] + seq[1:2] + tail
  34.  
  35. text = "hellllllllllooooooooooooo"
  36. seq = re.split('(.)\1+', text)
  37.  
  38. for result in permute(seq):
  39. print ''.join(result)
  40.  
  41. def squeeze(str, chars='abcdefghijklmnopqrstuvwxyz', min=1):
  42. new_str = str
  43. for c in chars:
  44. new_str = new_str.replace(c*(1+min),c*min)
  45. if new_str != str:
  46. new_str = squeeze(new_str, min=min)
  47. return new_str
  48.  
  49. >>> squeeze('aaaabbbbcccc')
  50. 'abc'
  51. >>> squeeze('aaaabbbbcccc', min=2)
  52. 'aabbcc'
  53.  
  54. def squeezutations(str):
  55. str = squeeze(str, chars=set(str), min=2)
  56. for j,k in ((j,k) for j in range(2,0,-1) for k in range(1,3)):
  57. for c in set(str):
  58. yield squeeze(squeeze(str, chars=c, min=k), chars=set(str)-set(c), min=j )
  59.  
  60. >>> set(squeezutations('appppppppple'))
  61. set(['apple', 'aple'])
Add Comment
Please, Sign In to add comment