Advertisement
Epillion

Assignment Task 1

Mar 31st, 2023
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.43 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Mar 20 15:56:04 2023
  4.  
  5. @author: ahsan
  6. """
  7.  
  8. def getZarr(string, z):
  9.     n = len(string)
  10.  
  11.     # [L,R] make a window which matches
  12.     # with prefix of s
  13.     l, r, k = 0, 0, 0
  14.     for i in range(1, n):
  15.  
  16.         # if i>R nothing matches so we will calculate.
  17.         # Z[i] using naive way.
  18.         if i > r:
  19.             l, r = i, i
  20.  
  21.             # R-L = 0 in starting, so it will start
  22.             # checking from 0'th index. For example,
  23.             # for "ababab" and i = 1, the value of R
  24.             # remains 0 and Z[i] becomes 0. For string
  25.             # "aaaaaa" and i = 1, Z[i] and R become 5
  26.             while r < n and string[r - l] == string[r]:
  27.                 r += 1
  28.             z[i] = r - l
  29.             r -= 1
  30.         else:
  31.  
  32.             # k = i-L so k corresponds to number which
  33.             # matches in [L,R] interval.
  34.             k = i - l
  35.  
  36.             # if Z[k] is less than remaining interval
  37.             # then Z[i] will be equal to Z[k].
  38.             # For example, str = "ababab", i = 3, R = 5
  39.             # and L = 2
  40.             if z[k] < r - i + 1:
  41.                 z[i] = z[k]
  42.  
  43.             # For example str = "aaaaaa" and i = 2,
  44.             # R is 5, L is 0
  45.             else:
  46.  
  47.                 # else start from R and check manually
  48.                 l = i
  49.                 while r < n and string[r - l] == string[r]:
  50.                     r += 1
  51.                 z[i] = r - l
  52.                 r -= 1
  53.  
  54. def checkTransposition(text, pattern, zvalues, i):
  55.     ### easier than expected
  56.     size = len(pattern)
  57.     currentIndex = i
  58.     now = 0
  59.     count = 0;
  60.     first, second = -1, -1
  61.     print(text[i:])
  62.     print(zvalues[i:])
  63.    
  64.     while now < size and currentIndex < len(text):
  65.         mn = min(zvalues[currentIndex], zvalues[now])
  66.        
  67.         now += mn
  68.         currentIndex += mn
  69.        
  70.         if now >= size:
  71.             break
  72.        
  73.         if currentIndex >= len(text):
  74.             break
  75.        
  76.         if (text[currentIndex] == pattern[now]  and mn == 0):
  77.             count+=0
  78.         else:
  79.             count+=1
  80.             if count > 2:
  81.                 break
  82.             if count == 1:
  83.                 first = currentIndex
  84.             if count == 2:
  85.                 second = currentIndex
  86.        
  87.        
  88.         print(now, currentIndex, mn, count)
  89.        
  90.        
  91.         now += 1
  92.         currentIndex += 1
  93.        
  94.        
  95.        
  96.         #print("second", now, currentIndex, mn, count)
  97.        
  98.         # if  zvalues[now] > zvalues[currentIndex]:
  99.         #     continue
  100.        
  101.    
  102.        
  103.         # while now < size and currentIndex < len(text) and zvalues[now] == 0 and text[currentIndex] == pattern[now]:
  104.         #     now += 1
  105.         #     currentIndex += 1
  106.            
  107.         #print("second", now, currentIndex, mn, count)    
  108.         # if startIndex < len(text) and  zvalues[now] <= zvalues[currentIndex] and now + zvalues[now] == size-1:
  109.         #     break
  110.            
  111.        
  112.    
  113.     #print(i, count, first, second)
  114.     if count == 2:
  115.         if text[first] == text[second-i] and text[second] == text[first-i]:
  116.             print("answer", i, i-size, first-size, second-size)    
  117.         else:
  118.             print("false", i, i-size, first-size, second-size)
  119.     else:
  120.         print(i, i-size,"false")
  121.  
  122.     print("=============")
  123.     return False
  124.  
  125.    
  126. def checkTranspositionNew(text, pattern, zvalues, i):
  127.     ### easier than expected
  128.     size = len(pattern)
  129.     currentIndex = i
  130.     now = 0
  131.     count = 0;
  132.     first, second = -1, -1
  133.     #print(text[i:])
  134.     #print(zvalues[i:])
  135.    
  136.     currentIndex +=  zvalues[currentIndex]
  137.     now += zvalues[currentIndex]
  138.     first = currentIndex
  139.     second = currentIndex + 1
  140.    
  141.     if currentIndex == i + size - 1:
  142.         return False
  143.    
  144.    
  145.    
  146.    
  147.     if currentIndex < len(text) and now < size  and text[currentIndex] == pattern[now + 1] and text[currentIndex+1] == pattern[now]:
  148.         #slide
  149.         now += 2
  150.         currentIndex += 2
  151.         while now < size and currentIndex < len(text):
  152.             if text[currentIndex] != pattern[now]:
  153.                 return False
  154.            
  155.             mn = min(zvalues[now], zvalues[currentIndex])
  156.             if mn == 0:
  157.                 now += 1
  158.                 currentIndex += 1
  159.             else:
  160.                 now += mn
  161.                 currentIndex += mn
  162.        
  163.         print(i-size, first-size)
  164.         return True
  165.        
  166.     else:
  167.         return False
  168.    
  169.     # while now < size and currentIndex < len(text):
  170.        
  171.            
  172.        
  173.    
  174.    
  175.     return False
  176.  
  177.  
  178.  
  179. # prints all occurrences of pattern
  180. # in text using Z algo
  181. def search(text, pattern):
  182.  
  183.     # Create concatenated string "P$T"
  184.     concat = pattern + "$" + text
  185.     l = len(concat)
  186.  
  187.     # Construct Z array
  188.     z = [0] * l
  189.     getZarr(concat, z)
  190.     print(z)
  191.     # now looping through Z array for matching condition
  192.     for i in range(l):
  193.  
  194.         # if Z[i] (matched region) is equal to pattern
  195.         # length we got the pattern
  196.         if z[i] == len(pattern):
  197.             print("Pattern found at index",
  198.                       i - len(pattern))
  199.             print("=============")
  200.         elif i < l - len(pattern) + 1:
  201.             checkTranspositionNew(concat, pattern, z, i)
  202.            
  203.            
  204.  
  205.  
  206. search('babbababaabbaba','abba')
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement