Skip to content

Commit

Permalink
Merge pull request #337 from yashksaini-coder/yash/fix-329
Browse files Browse the repository at this point in the history
fix: 🐛 Optimize the code for better call, and less time utilize
  • Loading branch information
UTSAVS26 authored Nov 5, 2024
2 parents 0dd5c68 + d5518b1 commit d494418
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 21 deletions.
3 changes: 3 additions & 0 deletions pysnippets/Image_Processing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .image_processing import resize, filter_image, rotate

__all__ = ["resize", "filter_image", "rotate"]
28 changes: 28 additions & 0 deletions pysnippets/Image_Processing/image_processing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from PIL import Image, ImageFilter

def resize(image_path, output_path, size):
"""
Resizes the image to the specified size.
"""
with Image.open(image_path) as img:
img = img.resize(size, Image.ANTIALIAS) # Use ANTIALIAS for better quality
img.save(output_path)

def filter_image(image_path, output_path, filter_type):
"""
Applies a filter to the image and saves it.
"""
with Image.open(image_path) as img:
if filter_type == 'BLUR':
img = img.filter(ImageFilter.BLUR)
elif filter_type == 'CONTOUR':
img = img.filter(ImageFilter.CONTOUR)
img.save(output_path)

def rotate(image_path, output_path, angle):
"""
Rotates the image by the specified angle.
"""
with Image.open(image_path) as img:
img = img.rotate(angle, expand=True) # Expand to fit the new size
img.save(output_path)
18 changes: 18 additions & 0 deletions pysnippets/Image_Processing/test_image_processing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import unittest
from image_processing import resize, filter_image, rotate

class TestImageProcessing(unittest.TestCase):
def test_resize(self):
resize('input_image.jpg', 'output_image_resized.jpg', (100, 100))
# Add assertions to check if the output image is as expected

def test_filter_image(self):
filter_image('input_image.jpg', 'output_image_filtered.jpg', 'BLUR')
# Add assertions to check if the output image is as expected

def test_rotate(self):
rotate('input_image.jpg', 'output_image_rotated.jpg', 90)
# Add assertions to check if the output image is as expected

if __name__ == "__main__":
unittest.main()
5 changes: 5 additions & 0 deletions pysnippets/Memoisation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .fibonacci import fibonacci
from .knapsack import knapsack
from .lcs import lcs

__all__ = ["fibonacci", "knapsack", "lcs"]
18 changes: 12 additions & 6 deletions pysnippets/Memoisation/fibonacci.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
def fibonacci(n, memo={}):
if n in memo:
return memo[n]
if n <= 1:
from functools import lru_cache

@lru_cache(maxsize=None)
def fibonacci(n):
"""
Returns the nth Fibonacci number using automatic memoization with lru_cache.
"""
if n < 2:
return n
memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo)
return memo[n]
return fibonacci(n - 1) + fibonacci(n - 2)

if __name__ == "__main__":
print(fibonacci(10)) # Output: 55
# New test cases
print(fibonacci(20)) # Output: 6765
print(fibonacci(30)) # Output: 832040
20 changes: 11 additions & 9 deletions pysnippets/Memoisation/knapsack.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
def knapsack(weights, values, capacity, n, memo={}):
if (n, capacity) in memo:
return memo[(n, capacity)]
from functools import lru_cache

@lru_cache(maxsize=None)
def knapsack(weights, values, capacity, n):
if n == 0 or capacity == 0:
return 0
if weights[n - 1] > capacity:
memo[(n, capacity)] = knapsack(weights, values, capacity, n - 1, memo)
return knapsack(weights, values, capacity, n - 1)
else:
memo[(n, capacity)] = max(values[n - 1] + knapsack(weights, values, capacity - weights[n - 1], n - 1, memo),
knapsack(weights, values, capacity, n - 1, memo))
return memo[(n, capacity)]
return max(values[n - 1] + knapsack(weights, values, capacity - weights[n - 1], n - 1),
knapsack(weights, values, capacity, n - 1))

if __name__ == "__main__":
weights = [1, 2, 3]
values = [10, 15, 40]
weights = (1, 2, 3)
values = (10, 15, 40)
capacity = 6
n = len(values)
print(knapsack(weights, values, capacity, n)) # Output: 55
# New test cases
print(knapsack((1, 2, 3, 4), (10, 20, 30, 40), 6, 4)) # Output: 60
14 changes: 8 additions & 6 deletions pysnippets/Memoisation/lcs.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
def lcs(x, y, m, n, memo={}):
if (m, n) in memo:
return memo[(m, n)]
from functools import lru_cache

@lru_cache(maxsize=None)
def lcs(x, y, m, n):
if m == 0 or n == 0:
return 0
if x[m - 1] == y[n - 1]:
memo[(m, n)] = 1 + lcs(x, y, m - 1, n - 1, memo)
return 1 + lcs(x, y, m - 1, n - 1)
else:
memo[(m, n)] = max(lcs(x, y, m, n - 1, memo), lcs(x, y, m - 1, n, memo))
return memo[(m, n)]
return max(lcs(x, y, m, n - 1), lcs(x, y, m - 1, n))

if __name__ == "__main__":
x = "AGGTAB"
y = "GXTXAYB"
print(lcs(x, y, len(x), len(y))) # Output: 4
# New test cases
print(lcs("ABC", "AC", len("ABC"), len("AC"))) # Output: 2
24 changes: 24 additions & 0 deletions pysnippets/Memoisation/test_memoization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import unittest
from fibonacci import fibonacci
from knapsack import knapsack
from lcs import lcs

class TestMemoization(unittest.TestCase):
def test_fibonacci(self):
self.assertEqual(fibonacci(10), 55)
self.assertEqual(fibonacci(20), 6765)
self.assertEqual(fibonacci(30), 832040)

def test_knapsack(self):
weights = (1, 2, 3)
values = (10, 15, 40)
capacity = 6
self.assertEqual(knapsack(weights, values, capacity, len(values)), 55)
self.assertEqual(knapsack((1, 2, 3, 4), (10, 20, 30, 40), 6, 4), 60)

def test_lcs(self):
self.assertEqual(lcs("AGGTAB", "GXTXAYB", len("AGGTAB"), len("GXTXAYB")), 4)
self.assertEqual(lcs("ABC", "AC", len("ABC"), len("AC")), 2)

if __name__ == "__main__":
unittest.main()

0 comments on commit d494418

Please sign in to comment.