Advertisement
gubichas

Бонус 2 семинар 4

Nov 2nd, 2022 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1.  # Анаграммы ( решение 1)
  2. import string
  3. string.punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
  4. s = input().lower()
  5. p = input().lower()
  6. for i in string.punctuation:
  7.     if i in s:
  8.         s = s.replace(i, '')
  9. is_anagram = lambda x1, x2: sorted(x1) == sorted(x2)
  10.  
  11. if (len(p)> len(s)):
  12.     print('No anagram')
  13. else:
  14.     list =[]
  15.     start_index = 0
  16.     end_index = len(p)-1
  17.  
  18.     while (end_index < len(s) ):
  19.         string = s[start_index:end_index+1]
  20.         print(string)
  21.         if is_anagram(string,p):
  22.             list.append(start_index)
  23.  
  24.         start_index+=1
  25.         end_index+=1
  26. print(list)
  27.  
  28. # решение 2
  29. from collections import Counter
  30. import string
  31. string.punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
  32.  
  33. s = input().lower()
  34. p = input().lower()
  35. for i in string.punctuation:
  36.     if i in s:
  37.         s = s.replace(i, '')
  38. def anagram(input1, input2):
  39.     return Counter(input1) == Counter(input2)
  40. print(anagram(s,p))
  41. start_index = 0
  42. end_index = len(p)-1
  43. while (end_index < len(s) ):
  44.     string = s[start_index:end_index+1]
  45.     if(anagram(string,p)):
  46.         print(str(start_index),end=' ')
  47.     end_index+=1
  48.     start_index+=1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement