-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_test.py
More file actions
128 lines (107 loc) · 3.29 KB
/
Copy pathcode_test.py
File metadata and controls
128 lines (107 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Imports
from itertools import permutations
# Sorting Algorithms
class Sorting:
@staticmethod
def bubble_sort(arr):
n = len(arr)
for i in range(n):
swapped = False
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
swapped = True
if not swapped:
break
@staticmethod
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >=0 and key < arr[j]:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
@staticmethod
def quick_sort(arr):
if len(arr) <= 1:
return arr
else:
pivot = arr.pop()
greater = [x for x in arr if x > pivot]
lesser = [x for x in arr if x <= pivot]
return Sorting.quick_sort(lesser) + [pivot] + Sorting.quick_sort(greater)
# Matrix Manipulations
class MatrixManipulations:
@staticmethod
def rotate_matrix(matrix):
n = len(matrix)
for layer in range(n // 2):
first, last = layer, n-layer-1
for i in range(first, last):
top = matrix[layer][i]
matrix[layer][i] = matrix[-i-1][layer]
matrix[-i-1][layer] = matrix[-layer-1][-i-1]
matrix[-layer-1][-i-1] = matrix[i][-layer-1]
matrix[i][-layer-1] = top
@staticmethod
def spiral_order(matrix):
return matrix and [*matrix.pop(0)] + MatrixManipulations.spiral_order([*zip(*matrix)][::-1])
# String Manipulations
class StringManipulations:
@staticmethod
def reverse_string(s):
return s[::-1]
@staticmethod
def is_palindrome(s):
cleaned = ''.join(filter(str.isalnum, s.lower()))
return cleaned == cleaned[::-1]
@staticmethod
def string_compression(s):
compressed = []
count = 1
for i in range(1, len(s)+1):
if i < len(s) and s[i] == s[i-1]:
count += 1
else:
compressed.append(s[i-1] + (str(count) if count > 1 else ''))
count = 1
return ''.join(compressed)
@staticmethod
def check_anagram(s1, s2):
return sorted(s1) == sorted(s2)
@staticmethod
def permutations_string(s):
return [''.join(p) for p in permutations(s)]
# Searching Algorithms
class Searching:
@staticmethod
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] < target:
low = mid + 1
elif arr[mid] > target:
high = mid - 1
else:
return mid
return -1
# Set Manipulations
class SetManipulations:
@staticmethod
def union(s1, s2):
return s1 | s2
@staticmethod
def intersection(s1, s2):
return s1 & s2
@staticmethod
def difference(s1, s2):
return s1 - s2
@staticmethod
def symmetric_difference(s1, s2):
return s1 ^ s2
# Testing
if __name__ == "__main__":
# Functions can be tested by invoking them here with example input.
pass