presudo 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,66 @@
1
+ Perfect subarray
2
+
3
+
4
+ def perfect_subarrays(arr):
5
+ MOD = 10**9 + 7
6
+ n = len(arr)
7
+ seen = set()
8
+ prefix = 0
9
+ left = 0
10
+ result = 0
11
+ seen.add(0)
12
+ for right in range(n):
13
+ prefix += arr[right]
14
+ while prefix in seen:
15
+ seen.remove(sum(arr[left:right+1]))
16
+ left += 1
17
+ seen.add(prefix)
18
+ result += (right - left + 1)
19
+ return result % MOD
20
+
21
+ ----------------------------------------------------------------------------------------------------
22
+
23
+ Divisors count
24
+
25
+
26
+ def count_good_sets(A):
27
+ MOD = 10**9 + 7
28
+ m = sum(A)
29
+ A = list(set(A)) # distinct elements only
30
+ dp = [0] * (m + 1)
31
+ dp[0] = 1
32
+ for x in A:
33
+ for s in range(m, x - 1, -1):
34
+ dp[s] = (dp[s] + dp[s - x]) % MOD
35
+ ans = 0
36
+ for i in range(1, m + 1):
37
+ if m % i == 0:
38
+ ans = (ans + dp[i]) % MOD
39
+ return ans
40
+
41
+ ----------------------------------------------------------------------------------------------------
42
+
43
+ Nearly lucky
44
+
45
+ from itertools import combinations
46
+
47
+ arr = [1, 1, 2, 4]
48
+ m = sum(arr)
49
+
50
+ result = []
51
+ seen = set()
52
+
53
+ for r in range(1, len(arr) + 1):
54
+ for combo in combinations(arr, r):
55
+ key = tuple(combo)
56
+
57
+ if key in seen:
58
+ continue
59
+
60
+ s = sum(combo)
61
+
62
+ if s != 0 and m % s == 0 and 0 < s <= m:
63
+ seen.add(key)
64
+ result.append(list(combo))
65
+
66
+ print(result)
@@ -0,0 +1,99 @@
1
+ (Telegram DSA)
2
+
3
+ Max permutations
4
+
5
+ @classmethod
6
+ def maxpermutationvalue(cls, input1, input2):
7
+ '''
8
+ input1 : string[]
9
+ input2 : int
10
+ Expected return type : int
11
+ '''
12
+ def fact(n):
13
+ if n <= 1:
14
+ return 1
15
+ return n * fact(n - 1)
16
+
17
+ val = 0
18
+ for word in input1:
19
+ res = ""
20
+ for char in word.lower():
21
+ if char not in 'aeiou':
22
+ res += char
23
+ if len(res) != 0:
24
+ val = max(val, fact(len(res)))
25
+ return val
26
+
27
+ -------------------------------------------------------------------------------------
28
+
29
+ Xor sum
30
+
31
+
32
+ class UserMainCode(object):
33
+ @classmethod
34
+ def sumOR(cls, inputs, inputz):
35
+ '''
36
+ inputs : int[]
37
+ inputz : int
38
+ Expected return type : int
39
+ '''
40
+ s = 0
41
+ x = 0
42
+ arr = inputs
43
+
44
+ for i in range(inputz):
45
+ if i % 2 == 0:
46
+ x = x ^ arr[i]
47
+ else:
48
+ s += arr[i]
49
+
50
+ return s - x
51
+
52
+ -------------------------------------------------------------------------------------
53
+
54
+ Maximize Pair Product
55
+
56
+ class UserMainCode(object):
57
+ @classmethod
58
+ def FindPair(cls, input1, input2):
59
+ '''
60
+ input1 : int
61
+ input2 : int[]
62
+ Expected return type : int[]
63
+ '''
64
+ max_pro = 0
65
+ arr = input2
66
+ res = []
67
+
68
+ for i in range(input1 - 1):
69
+ for j in range(i + 1, input1):
70
+ temp = arr[i] * arr[j]
71
+ if temp >= max_pro:
72
+ max_pro = temp
73
+ res = sorted([arr[i], arr[j]], reverse=True)
74
+
75
+ return res
76
+
77
+ -------------------------------------------------------------------------------------
78
+
79
+ Pair Product
80
+
81
+ class UserMainCode(object):
82
+ @classmethod
83
+ def countPairs(cls, input1, input2):
84
+ '''
85
+ input1 : int
86
+ input2 : int[]
87
+ Expected return type : int
88
+ '''
89
+ x = set()
90
+ input2.sort()
91
+
92
+ for i in range(input1):
93
+ for j in range(i + 1, input1):
94
+ if (input2[i] * input2[j]) % 3 == 0:
95
+ x.add((input2[i], input2[j]))
96
+
97
+ return len(x)
98
+
99
+ -------------------------------------------------------------------------------------
@@ -0,0 +1,71 @@
1
+ # src/main.py
2
+
3
+ from abc import ABC, abstractmethod
4
+ from typing import List
5
+
6
+
7
+ # Product class - base class for different product categories
8
+ class Product(ABC):
9
+ def __init__(self, product_id: int, name: str, price: float, stock_level: int):
10
+ self.product_id = product_id
11
+ self.name = name
12
+ self.price = price
13
+ self.stock_level = stock_level
14
+
15
+ @abstractmethod
16
+ def display(self):
17
+ pass
18
+
19
+
20
+ # Specialized product categories
21
+ class Smartphone(Product):
22
+ def __init__(self, product_id: int, name: str, price: float, stock_level: int, brand: str):
23
+ super().__init__(product_id, name, price, stock_level)
24
+ self.brand = brand
25
+
26
+ def display(self):
27
+ print(f"Smartphone {self.name} by {self.brand} - {self.stock_level} units, ${self.price}")
28
+
29
+
30
+ class Laptop(Product):
31
+ def __init__(self, product_id: int, name: str, price: float, stock_level: int, ram_size: str):
32
+ super().__init__(product_id, name, price, stock_level)
33
+ self.ram_size = ram_size
34
+
35
+ def display(self):
36
+ print(f"Laptop {self.name} with {self.ram_size} RAM - {self.stock_level} units, ${self.price}")
37
+
38
+
39
+ # Singleton Design Pattern for Inventory Management System
40
+ class InventoryManager:
41
+ _instance = None
42
+
43
+ def __new__(cls):
44
+ if cls._instance is None:
45
+ cls._instance = super().__new__(cls)
46
+ cls._instance.products = []
47
+ return cls._instance
48
+
49
+ def add_product(self, product: Product):
50
+ self.products.append(product)
51
+
52
+ def update_stock(self, product_id: int, quantity: int):
53
+ for product in self.products:
54
+ if product.product_id == product_id:
55
+ product.stock_level += quantity
56
+ return
57
+ print("Product not found.")
58
+
59
+ def generate_report(self):
60
+ print("Inventory Report:")
61
+ for product in self.products:
62
+ product.display()
63
+
64
+ def alert_low_stock(self, threshold: int):
65
+ for product in self.products:
66
+ if product.stock_level < threshold:
67
+ print(f"ALERT: {product.name} has low stock ({product.stock_level} left).")
68
+
69
+
70
+
71
+
@@ -0,0 +1,59 @@
1
+ # src/main.py
2
+
3
+ def display_menu():
4
+ print("1. Add a book to inventory")
5
+ print("2. Display all books in inventory")
6
+ print("3. Search for a book")
7
+ print("4. Remove a book from inventory")
8
+ print("5. Exit")
9
+
10
+ def add_book(inventory):
11
+ title = input("Enter book title: ")
12
+ author = input("Enter author name: ")
13
+ quantity = int(input("Enter quantity: "))
14
+ price = float(input("Enter price: "))
15
+ book = (title, author, quantity, price)
16
+ inventory.append(book)
17
+
18
+ def display_books(inventory):
19
+ if not inventory:
20
+ print("No books in inventory.")
21
+ return
22
+ for book in inventory:
23
+ print(f"Title: {book[0]}, Author: {book[1]}, Quantity: {book[2]}, Price: {book[3]}")
24
+
25
+ def search_book(inventory):
26
+ title = input("Enter the title of the book to search: ")
27
+ for book in inventory:
28
+ if book[0].lower() == title.lower():
29
+ print(f"Book found: Title: {book[0]}, Author: {book[1]}, Quantity: {book[2]}, Price: {book[3]}")
30
+ return
31
+ print("Book not found.")
32
+
33
+ def remove_book(inventory):
34
+ title = input("Enter the title of the book to remove: ")
35
+ for book in inventory:
36
+ if book[0].lower() == title.lower():
37
+ inventory.remove(book)
38
+ print("Book removed.")
39
+ return
40
+ print("Book not found.")
41
+
42
+ def main():
43
+ inventory = []
44
+ while True:
45
+ display_menu()
46
+ choice = int(input("Choose an option: "))
47
+ if choice == 1:
48
+ add_book(inventory)
49
+ elif choice == 2:
50
+ display_books(inventory)
51
+ elif choice == 3:
52
+ search_book(inventory)
53
+ elif choice == 4:
54
+ remove_book(inventory)
55
+ elif choice == 5:
56
+ print("Exiting the program.")
57
+ break
58
+ else:
59
+ print("Invalid option. Please try again.")