This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def encode(string): | |
| result = [] | |
| for c in string: | |
| result.append(ord(c)) | |
| return result | |
| def compress(string): | |
| dictionary = {chr(i):i for i in range(97,123)} | |
| last = 256 | |
| result = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def merge(left,right): | |
| result = [] | |
| while len(left) > 0 and len(right) > 0: | |
| if left[0] < right[0]: | |
| result.append(left[0]) | |
| left = left[1:] | |
| else: | |
| result.append(right[0]) | |
| right = right[1:] | |
| if len(left) > 0: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def search(string,pattern): | |
| string_len = len(string) | |
| pattern_len = len(pattern) | |
| for i in range(0,string_len - pattern_len + 1): | |
| for j in range(0,pattern_len): | |
| if string[i+j] != pattern[j]: | |
| break | |
| if j == pattern_len - 1: | |
| print("Pattern found at index",i) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from hashlib import md5 | |
| def rabinkarp(string,pattern): | |
| string_len = len(string) | |
| pattern_len = len(pattern) | |
| pattern_bytes = pattern.encode() | |
| pattern_hash = md5(pattern_bytes).hexdigest() | |
| for i in range(0,string_len-pattern_len+1): | |
| substring = string[i:i+pattern_len] | |
| substring_bytes = substring.encode() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class vertex(): | |
| def __init__(self,name): | |
| self.name = name | |
| self.neighbours = [] | |
| def connected_to(self,vertex,distance): | |
| self.neighbours.append((vertex,distance)) | |
| class graph(): | |
| def __init__(self): | |
| self.vertices = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def prefix_table(pattern): | |
| result = [0] | |
| for i in range(1,len(pattern)): | |
| val = 0 | |
| for j in range(i,0,-1): | |
| prefix = pattern[0:j]#Generate a prefix | |
| suffix = pattern[i-j+1:i+1]#Generate a suffix | |
| if prefix == suffix: | |
| val = len(prefix) | |
| break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def quicksort(arr,start,stop): | |
| if stop > start: | |
| left = start | |
| right = stop | |
| pivot = arr[start] | |
| while left <= right: | |
| while arr[left] < pivot: | |
| left += 1 | |
| while arr[right] > pivot: | |
| right -= 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| def shuffle(arr): | |
| for i in range(len(arr)-1,0,-1): | |
| temp = random.randint(0,i) #random integer N such that 0<=N<=i | |
| arr[i],arr[temp] = arr[temp],arr[i] | |
| arr = [1,2,3,4,5,6] | |
| shuffle(arr) | |
| print(arr) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def selectionsort(arr): | |
| for i in range(0,len(arr)): | |
| min_index = i | |
| for j in range(i+1,len(arr)): | |
| if arr[j] < arr[min_index]: | |
| min_index = j | |
| arr[min_index],arr[i] = arr[i],arr[min_index] | |
| print(arr) | |
| arr = [4,3,1,6,2,5] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def insertionsort(arr): | |
| for i in range(1,len(arr)): | |
| val = arr[i] | |
| j = i - 1 | |
| while j >= 0 and arr[j]>val: | |
| arr[j+1] = arr[j] | |
| j -= 1 | |
| arr[j+1] = val | |
| print(arr) |
OlderNewer