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.
- package/__init__.py +0 -0
- package/index.js +29 -0
- package/main.py +2 -0
- package/package.json +18 -0
- package/pods/DSA/all.txt +914 -0
- package/pods/DSA/lasttime.txt +66 -0
- package/pods/DSA/tg.txt +99 -0
- package/pods/backend/inventory_management_system.txt +71 -0
- package/pods/backend/simple_inventory_management_system_for_bookstore_using_tuples.txt +59 -0
- package/pods/db/mongo.txt +842 -0
- package/pods/db/mongo_MCQ.txt +216 -0
- package/pods/db/sql.txt +728 -0
- package/pods/react/book_list.txt +51 -0
- package/pods/sudo/Product Management System-ts.txt +274 -0
- package/pods/sudo/backend/managing a delivery service.txt +130 -0
- package/pods/sudo/backend/online shopping cart system.txt +200 -0
- package/pods/sudo/backend/restaurant ordering system.txt +104 -0
- package/pods/sudo/das.txt +635 -0
- package/pods/sudo/react/TravelMap Application.txt +212 -0
- package/pods/sudo/sq.txt +217 -0
- package/pods/ts/Product Management System.txt +135 -0
- package/pods/ts/RetailStore.txt +113 -0
- package/pods/ts/Transaction Entity.txt +41 -0
- package/pods/ts/books_in_library.txt +39 -0
- package/pods/ts/find avg marks.txt +7 -0
- package/pods/ts/managing product inventory.txt +12 -0
- package/pods/ts/ts_examples/examples.txt +341 -0
- package/test.js +3 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
Design a Python module for managing a restaurant ordering system
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
|
|
5
|
+
# -------------------- Abstraction --------------------
|
|
6
|
+
class MenuItem(ABC):
|
|
7
|
+
def __init__(self, name, price):
|
|
8
|
+
self.name = name
|
|
9
|
+
self.price = price
|
|
10
|
+
@abstractmethod
|
|
11
|
+
def get_price(self):
|
|
12
|
+
pass
|
|
13
|
+
|
|
14
|
+
# -------------------- Inheritance & Polymorphism --------------------
|
|
15
|
+
class VegItem(MenuItem):
|
|
16
|
+
def get_price(self):
|
|
17
|
+
return self.price
|
|
18
|
+
|
|
19
|
+
class NonVegItem(MenuItem):
|
|
20
|
+
def get_price(self):
|
|
21
|
+
return self.price + 20 # extra charge for non-veg
|
|
22
|
+
|
|
23
|
+
# -------------------- Order Class --------------------
|
|
24
|
+
class Order:
|
|
25
|
+
def __init__(self):
|
|
26
|
+
self.items = []
|
|
27
|
+
def add_item(self, item):
|
|
28
|
+
self.items.append(item)
|
|
29
|
+
def remove_item(self, name):
|
|
30
|
+
for item in self.items:
|
|
31
|
+
if item.name == name:
|
|
32
|
+
self.items.remove(item)
|
|
33
|
+
return True
|
|
34
|
+
return False
|
|
35
|
+
def calculate_total(self):
|
|
36
|
+
total = 0
|
|
37
|
+
for item in self.items:
|
|
38
|
+
total += item.get_price()
|
|
39
|
+
return total
|
|
40
|
+
|
|
41
|
+
# -------------------- Restaurant System --------------------
|
|
42
|
+
class Restaurant:
|
|
43
|
+
def __init__(self):
|
|
44
|
+
self.menu = {}
|
|
45
|
+
self.order = Order()
|
|
46
|
+
def add_menu_item(self, item_type, name, price):
|
|
47
|
+
try:
|
|
48
|
+
if item_type == "VEG":
|
|
49
|
+
self.menu[name] = VegItem(name, price)
|
|
50
|
+
elif item_type == "NONVEG":
|
|
51
|
+
self.menu[name] = NonVegItem(name, price)
|
|
52
|
+
else:
|
|
53
|
+
raise ValueError("Invalid item type")
|
|
54
|
+
except Exception:
|
|
55
|
+
pass
|
|
56
|
+
def place_order(self, name):
|
|
57
|
+
try:
|
|
58
|
+
if name not in self.menu:
|
|
59
|
+
raise KeyError("Item not found")
|
|
60
|
+
self.order.add_item(self.menu[name])
|
|
61
|
+
except Exception:
|
|
62
|
+
pass
|
|
63
|
+
def cancel_order(self, name):
|
|
64
|
+
self.order.remove_item(name)
|
|
65
|
+
def get_bill(self):
|
|
66
|
+
return self.order.calculate_total()
|
|
67
|
+
|
|
68
|
+
# -------------------- Switch Case Controller --------------------
|
|
69
|
+
def process_commands(commands):
|
|
70
|
+
restaurant = Restaurant()
|
|
71
|
+
result = []
|
|
72
|
+
for command in commands:
|
|
73
|
+
try:
|
|
74
|
+
parts = command.split()
|
|
75
|
+
action = parts[0]
|
|
76
|
+
match action:
|
|
77
|
+
case "ADD_MENU":
|
|
78
|
+
item_type = parts[1]
|
|
79
|
+
name = parts[2]
|
|
80
|
+
price = int(parts[3])
|
|
81
|
+
restaurant.add_menu_item(item_type, name, price)
|
|
82
|
+
case "ORDER":
|
|
83
|
+
name = parts[1]
|
|
84
|
+
restaurant.place_order(name)
|
|
85
|
+
case "CANCEL":
|
|
86
|
+
name = parts[1]
|
|
87
|
+
restaurant.cancel_order(name)
|
|
88
|
+
case "BILL":
|
|
89
|
+
result.append(str(restaurant.get_bill()))
|
|
90
|
+
case _:
|
|
91
|
+
pass
|
|
92
|
+
except Exception:
|
|
93
|
+
pass
|
|
94
|
+
return result
|
|
95
|
+
|
|
96
|
+
# -------------------- Main Execution --------------------
|
|
97
|
+
if __name__ == "__main__":
|
|
98
|
+
n = int(input())
|
|
99
|
+
commands = []
|
|
100
|
+
for _ in range(n):
|
|
101
|
+
commands.append(input())
|
|
102
|
+
output = process_commands(commands)
|
|
103
|
+
for value in output:
|
|
104
|
+
print(value)
|
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
dsa
|
|
2
|
+
|
|
3
|
+
1.(sum of valid numbers)
|
|
4
|
+
|
|
5
|
+
problem Description
|
|
6
|
+
You are given an integer N.
|
|
7
|
+
|
|
8
|
+
A number x (1 ≤ x ≤ N) is called a valid number if it satisfies all the following number-theoretic properties:
|
|
9
|
+
|
|
10
|
+
-----------------------CODE-------------------------
|
|
11
|
+
|
|
12
|
+
MOD = 10**9 + 7
|
|
13
|
+
|
|
14
|
+
def is_valid(x):
|
|
15
|
+
|
|
16
|
+
if x % 3 != 0:
|
|
17
|
+
|
|
18
|
+
return False
|
|
19
|
+
|
|
20
|
+
if x % 5 == 0:
|
|
21
|
+
|
|
22
|
+
return False
|
|
23
|
+
|
|
24
|
+
digit_sum = sum(map(int, str(x)))
|
|
25
|
+
|
|
26
|
+
return digit_sum % 2 == 0
|
|
27
|
+
|
|
28
|
+
def count_valid(prefix):
|
|
29
|
+
|
|
30
|
+
return prefix_valid_count[prefix]
|
|
31
|
+
|
|
32
|
+
def build_prefix(n):
|
|
33
|
+
|
|
34
|
+
for i in range(1, n + 1):
|
|
35
|
+
|
|
36
|
+
prefix_valid_count[i] = prefix_valid_count[i - 1]
|
|
37
|
+
|
|
38
|
+
prefix_valid_sum[i] = prefix_valid_sum[i - 1]
|
|
39
|
+
|
|
40
|
+
if is_valid(i):
|
|
41
|
+
|
|
42
|
+
prefix_valid_count[i] += 1
|
|
43
|
+
|
|
44
|
+
prefix_valid_sum[i] = (prefix_valid_sum[i] + i) % MOD
|
|
45
|
+
|
|
46
|
+
# Input
|
|
47
|
+
|
|
48
|
+
N, K = map(int, input().split())
|
|
49
|
+
|
|
50
|
+
prefix_valid_count = [0] * (N + 1)
|
|
51
|
+
|
|
52
|
+
prefix_valid_sum = [0] * (N + 1)
|
|
53
|
+
|
|
54
|
+
# Prefix Sum Preprocessing
|
|
55
|
+
|
|
56
|
+
build_prefix(N)
|
|
57
|
+
|
|
58
|
+
# Binary Search to find smallest X with >= K valid numbers
|
|
59
|
+
|
|
60
|
+
left, right = 1, N
|
|
61
|
+
|
|
62
|
+
answer = -1
|
|
63
|
+
|
|
64
|
+
while left <= right:
|
|
65
|
+
|
|
66
|
+
mid = (left + right) // 2
|
|
67
|
+
|
|
68
|
+
if prefix_valid_count[mid] >= K:
|
|
69
|
+
|
|
70
|
+
answer = mid
|
|
71
|
+
|
|
72
|
+
right = mid - 1 # greedy minimization
|
|
73
|
+
|
|
74
|
+
else:
|
|
75
|
+
|
|
76
|
+
left = mid + 1
|
|
77
|
+
|
|
78
|
+
# Output sum modulo
|
|
79
|
+
|
|
80
|
+
print(prefix_valid_sum[answer] % MOD)
|
|
81
|
+
|
|
82
|
+
-----------------------------------------------------------------------------------------
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
(Tourist groups and Buses)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def min_days_to_finish_trips(A, B, C, D):
|
|
89
|
+
|
|
90
|
+
M = len(A)
|
|
91
|
+
|
|
92
|
+
N = len(B)
|
|
93
|
+
|
|
94
|
+
# Sort groups descending (largest first)
|
|
95
|
+
|
|
96
|
+
A.sort(reverse=True)
|
|
97
|
+
|
|
98
|
+
# Pair buses as (capacity, cost) and sort by:
|
|
99
|
+
|
|
100
|
+
# 1) capacity descending
|
|
101
|
+
|
|
102
|
+
# 2) cost ascending
|
|
103
|
+
|
|
104
|
+
buses = sorted(zip(B, C), key=lambda x: (-x[0], x[1]))
|
|
105
|
+
|
|
106
|
+
# Early impossible check: largest group > largest bus capacity
|
|
107
|
+
|
|
108
|
+
if A[0] > buses[0][0]:
|
|
109
|
+
|
|
110
|
+
return -1
|
|
111
|
+
|
|
112
|
+
# Helper function: check if X days is possible
|
|
113
|
+
|
|
114
|
+
def can_finish_in_days(X):
|
|
115
|
+
|
|
116
|
+
# How many buses are minimally required
|
|
117
|
+
|
|
118
|
+
required_buses = (M + X - 1) // X
|
|
119
|
+
|
|
120
|
+
selected_buses = []
|
|
121
|
+
|
|
122
|
+
total_cost = 0
|
|
123
|
+
|
|
124
|
+
# Pick cheapest buses with highest capacity
|
|
125
|
+
|
|
126
|
+
for cap, cost in buses:
|
|
127
|
+
|
|
128
|
+
selected_buses.append(cap)
|
|
129
|
+
|
|
130
|
+
total_cost += cost
|
|
131
|
+
|
|
132
|
+
if len(selected_buses) == required_buses:
|
|
133
|
+
|
|
134
|
+
break
|
|
135
|
+
|
|
136
|
+
# Budget exceeded or not enough buses
|
|
137
|
+
|
|
138
|
+
if len(selected_buses) < required_buses or total_cost > D:
|
|
139
|
+
|
|
140
|
+
return False
|
|
141
|
+
|
|
142
|
+
# Each bus can serve X groups
|
|
143
|
+
|
|
144
|
+
slots = []
|
|
145
|
+
|
|
146
|
+
for cap in selected_buses:
|
|
147
|
+
|
|
148
|
+
slots.extend([cap] * X)
|
|
149
|
+
|
|
150
|
+
# Assign largest groups first
|
|
151
|
+
|
|
152
|
+
slots.sort()
|
|
153
|
+
|
|
154
|
+
idx = 0
|
|
155
|
+
|
|
156
|
+
for group in A:
|
|
157
|
+
|
|
158
|
+
# Find first slot that can fit this group
|
|
159
|
+
|
|
160
|
+
while idx < len(slots) and slots[idx] < group:
|
|
161
|
+
|
|
162
|
+
idx += 1
|
|
163
|
+
|
|
164
|
+
if idx == len(slots):
|
|
165
|
+
|
|
166
|
+
return False
|
|
167
|
+
|
|
168
|
+
idx += 1
|
|
169
|
+
|
|
170
|
+
return True
|
|
171
|
+
|
|
172
|
+
# Binary search on days
|
|
173
|
+
|
|
174
|
+
left, right = 1, M
|
|
175
|
+
|
|
176
|
+
answer = -1
|
|
177
|
+
|
|
178
|
+
while left <= right:
|
|
179
|
+
|
|
180
|
+
mid = (left + right) // 2
|
|
181
|
+
|
|
182
|
+
if can_finish_in_days(mid):
|
|
183
|
+
|
|
184
|
+
answer = mid
|
|
185
|
+
|
|
186
|
+
right = mid - 1
|
|
187
|
+
|
|
188
|
+
else:
|
|
189
|
+
|
|
190
|
+
left = mid + 1
|
|
191
|
+
|
|
192
|
+
return answer
|
|
193
|
+
|
|
194
|
+
-----------------------------------------------------------------------------------------
|
|
195
|
+
|
|
196
|
+
(Play with stacks)
|
|
197
|
+
|
|
198
|
+
def play_with_stacks(s):
|
|
199
|
+
stack = []
|
|
200
|
+
removals = 0
|
|
201
|
+
for ch in s:
|
|
202
|
+
if ch == '(':
|
|
203
|
+
stack.append(ch)
|
|
204
|
+
else:
|
|
205
|
+
if stack:
|
|
206
|
+
stack.pop()
|
|
207
|
+
else:
|
|
208
|
+
removals += 1
|
|
209
|
+
removals += len(stack)
|
|
210
|
+
return removals
|
|
211
|
+
|
|
212
|
+
# ---------------- TEST CASES ---------------- #
|
|
213
|
+
tests = [
|
|
214
|
+
"(()))(",
|
|
215
|
+
"()()",
|
|
216
|
+
"(((",
|
|
217
|
+
")))",
|
|
218
|
+
"()(()",
|
|
219
|
+
"((()))",
|
|
220
|
+
"(()()("
|
|
221
|
+
]
|
|
222
|
+
for i, t in enumerate(tests, 1):
|
|
223
|
+
print(f"Test {i}: {t} -> {play_with_stacks(t)}")
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
-----------------------------------------------------------------------------------------
|
|
227
|
+
|
|
228
|
+
(Distinct sets)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def distinct_sets(arr1, arr2):
|
|
232
|
+
set1 = set(arr1)
|
|
233
|
+
set2 = set(arr2)
|
|
234
|
+
common = set1 & set2
|
|
235
|
+
return len(common)
|
|
236
|
+
|
|
237
|
+
# ---------------- TEST CASES ---------------- #
|
|
238
|
+
tests = [
|
|
239
|
+
([1, 2, 3], [3, 4, 5]),
|
|
240
|
+
([1, 1, 2], [2, 2, 3]),
|
|
241
|
+
([5, 6], [7, 8]),
|
|
242
|
+
([1, 2, 3], [1, 2, 3]),
|
|
243
|
+
([10, 20], [20, 30]),
|
|
244
|
+
]
|
|
245
|
+
for i, (a, b) in enumerate(tests, 1):
|
|
246
|
+
print(f"Test {i}: {distinct_sets(a, b)}")
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
-----------------------------------------------------------------------------------------
|
|
250
|
+
|
|
251
|
+
(We will meet again)
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def can_meet(pos, dist, k):
|
|
255
|
+
count = 1
|
|
256
|
+
last = pos[0]
|
|
257
|
+
for i in range(1, len(pos)):
|
|
258
|
+
if pos[i] - last > dist:
|
|
259
|
+
count += 1
|
|
260
|
+
last = pos[i]
|
|
261
|
+
if count > k:
|
|
262
|
+
return False
|
|
263
|
+
return True
|
|
264
|
+
|
|
265
|
+
def we_will_meet_again(pos, k):
|
|
266
|
+
pos.sort()
|
|
267
|
+
low, high = 0, pos[-1] - pos[0]
|
|
268
|
+
ans = high
|
|
269
|
+
while low <= high:
|
|
270
|
+
mid = (low + high) // 2
|
|
271
|
+
if can_meet(pos, mid, k):
|
|
272
|
+
ans = mid
|
|
273
|
+
high = mid - 1
|
|
274
|
+
else:
|
|
275
|
+
low = mid + 1
|
|
276
|
+
return ans
|
|
277
|
+
|
|
278
|
+
# ---------------- TEST CASES ---------------- #
|
|
279
|
+
tests = [
|
|
280
|
+
([1, 2, 4, 8, 9], 3),
|
|
281
|
+
([10, 20, 30], 2),
|
|
282
|
+
([1, 100, 200], 2),
|
|
283
|
+
([5, 10, 15, 20], 4),
|
|
284
|
+
([1, 2, 3, 4, 5], 1)
|
|
285
|
+
]
|
|
286
|
+
for i, (pos, k) in enumerate(tests, 1):
|
|
287
|
+
print(f"Test {i}: {we_will_meet_again(pos, k)}")
|
|
288
|
+
|
|
289
|
+
-----------------------------------------------------------------------------------------
|
|
290
|
+
|
|
291
|
+
(Collect the prizes)
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def can_collect(prizes, k, dist):
|
|
295
|
+
collectors = 1
|
|
296
|
+
start = prizes[0]
|
|
297
|
+
for i in range(1, len(prizes)):
|
|
298
|
+
if prizes[i] - start > dist:
|
|
299
|
+
collectors += 1
|
|
300
|
+
start = prizes[i]
|
|
301
|
+
if collectors > k:
|
|
302
|
+
return False
|
|
303
|
+
return True
|
|
304
|
+
|
|
305
|
+
def collect_the_prizes(prizes, k):
|
|
306
|
+
prizes.sort()
|
|
307
|
+
low, high = 0, prizes[-1] - prizes[0]
|
|
308
|
+
ans = high
|
|
309
|
+
while low <= high:
|
|
310
|
+
mid = (low + high) // 2
|
|
311
|
+
if can_collect(prizes, k, mid):
|
|
312
|
+
ans = mid
|
|
313
|
+
high = mid - 1
|
|
314
|
+
else:
|
|
315
|
+
low = mid + 1
|
|
316
|
+
return ans
|
|
317
|
+
|
|
318
|
+
# -------- TEST CASES -------- #
|
|
319
|
+
tests = [
|
|
320
|
+
([1, 2, 4, 8, 9], 3),
|
|
321
|
+
([10, 20, 30, 40], 2),
|
|
322
|
+
([1, 100, 200], 2),
|
|
323
|
+
([5, 6, 7, 8], 1),
|
|
324
|
+
([1, 2, 3, 4], 4)
|
|
325
|
+
]
|
|
326
|
+
for i, (p, k) in enumerate(tests, 1):
|
|
327
|
+
print(f"Test {i}: {collect_the_prizes(p, k)}")
|
|
328
|
+
|
|
329
|
+
-----------------------------------------------------------------------------------------
|
|
330
|
+
|
|
331
|
+
(Fadi’s Garden)
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def can_water(plants, k, length):
|
|
335
|
+
sprinklers = 1
|
|
336
|
+
cover_end = plants[0] + length
|
|
337
|
+
for p in plants:
|
|
338
|
+
if p > cover_end:
|
|
339
|
+
sprinklers += 1
|
|
340
|
+
cover_end = p + length
|
|
341
|
+
if sprinklers > k:
|
|
342
|
+
return False
|
|
343
|
+
return True
|
|
344
|
+
|
|
345
|
+
def fadi_garden(plants, k):
|
|
346
|
+
plants.sort()
|
|
347
|
+
low, high = 0, plants[-1] - plants[0]
|
|
348
|
+
ans = high
|
|
349
|
+
while low <= high:
|
|
350
|
+
mid = (low + high) // 2
|
|
351
|
+
if can_water(plants, k, mid):
|
|
352
|
+
ans = mid
|
|
353
|
+
high = mid - 1
|
|
354
|
+
else:
|
|
355
|
+
low = mid + 1
|
|
356
|
+
return ans
|
|
357
|
+
|
|
358
|
+
# -------- TEST CASES -------- #
|
|
359
|
+
tests = [
|
|
360
|
+
([1, 2, 3, 7, 8], 2),
|
|
361
|
+
([10, 20, 30], 1),
|
|
362
|
+
([1, 5, 9], 3),
|
|
363
|
+
([2, 4, 6, 8], 2),
|
|
364
|
+
([1, 100, 200], 2)
|
|
365
|
+
]
|
|
366
|
+
for i, (p, k) in enumerate(tests, 1):
|
|
367
|
+
print(f"Test {i}: {fadi_garden(p, k)}")
|
|
368
|
+
|
|
369
|
+
-----------------------------------------------------------------------------------------
|
|
370
|
+
|
|
371
|
+
(Collecting Candies)
|
|
372
|
+
|
|
373
|
+
def can_distribute(candies, k, x):
|
|
374
|
+
|
|
375
|
+
kids = 0
|
|
376
|
+
|
|
377
|
+
for c in candies:
|
|
378
|
+
|
|
379
|
+
kids += c // x
|
|
380
|
+
|
|
381
|
+
return kids >= k
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def collecting_candies(candies, k):
|
|
385
|
+
|
|
386
|
+
low, high = 1, max(candies)
|
|
387
|
+
|
|
388
|
+
ans = 0
|
|
389
|
+
|
|
390
|
+
while low <= high:
|
|
391
|
+
|
|
392
|
+
mid = (low + high) // 2
|
|
393
|
+
|
|
394
|
+
if can_distribute(candies, k, mid):
|
|
395
|
+
|
|
396
|
+
ans = mid
|
|
397
|
+
|
|
398
|
+
low = mid + 1
|
|
399
|
+
|
|
400
|
+
else:
|
|
401
|
+
|
|
402
|
+
high = mid - 1
|
|
403
|
+
|
|
404
|
+
return ans
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
# -------- TEST CASES -------- #
|
|
408
|
+
|
|
409
|
+
tests = [
|
|
410
|
+
|
|
411
|
+
([10, 20, 30], 6),
|
|
412
|
+
|
|
413
|
+
([5, 8, 6], 5),
|
|
414
|
+
|
|
415
|
+
([100], 10),
|
|
416
|
+
|
|
417
|
+
([4, 4, 4], 4),
|
|
418
|
+
|
|
419
|
+
([1, 2, 3], 3)
|
|
420
|
+
|
|
421
|
+
]
|
|
422
|
+
|
|
423
|
+
for i, (c, k) in enumerate(tests, 1):
|
|
424
|
+
|
|
425
|
+
print(f"Test {i}: {collecting_candies(c, k)}")
|
|
426
|
+
|
|
427
|
+
-----------------------------------------------------------------------------------------
|
|
428
|
+
|
|
429
|
+
(Its snowing)
|
|
430
|
+
|
|
431
|
+
def can_clear(snow, k, d):
|
|
432
|
+
|
|
433
|
+
plows = 1
|
|
434
|
+
|
|
435
|
+
start = snow[0]
|
|
436
|
+
|
|
437
|
+
for i in range(1, len(snow)):
|
|
438
|
+
|
|
439
|
+
if snow[i] - start > d:
|
|
440
|
+
|
|
441
|
+
plows += 1
|
|
442
|
+
|
|
443
|
+
start = snow[i]
|
|
444
|
+
|
|
445
|
+
if plows > k:
|
|
446
|
+
|
|
447
|
+
return False
|
|
448
|
+
|
|
449
|
+
return True
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
def its_snowing(snow, k):
|
|
453
|
+
|
|
454
|
+
snow.sort()
|
|
455
|
+
|
|
456
|
+
low, high = 0, snow[-1] - snow[0]
|
|
457
|
+
|
|
458
|
+
ans = high
|
|
459
|
+
|
|
460
|
+
while low <= high:
|
|
461
|
+
|
|
462
|
+
mid = (low + high) // 2
|
|
463
|
+
|
|
464
|
+
if can_clear(snow, k, mid):
|
|
465
|
+
|
|
466
|
+
ans = mid
|
|
467
|
+
|
|
468
|
+
high = mid - 1
|
|
469
|
+
|
|
470
|
+
else:
|
|
471
|
+
|
|
472
|
+
low = mid + 1
|
|
473
|
+
|
|
474
|
+
return ans
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
# -------- TEST CASES -------- #
|
|
478
|
+
|
|
479
|
+
tests = [
|
|
480
|
+
|
|
481
|
+
([1, 2, 5, 8, 10], 2),
|
|
482
|
+
|
|
483
|
+
([10, 20, 30], 1),
|
|
484
|
+
|
|
485
|
+
([1, 100, 200], 2),
|
|
486
|
+
|
|
487
|
+
([5, 6, 7, 8], 4),
|
|
488
|
+
|
|
489
|
+
([1, 2, 3, 4], 1)
|
|
490
|
+
|
|
491
|
+
]
|
|
492
|
+
|
|
493
|
+
for i, (s, k) in enumerate(tests, 1):
|
|
494
|
+
|
|
495
|
+
print(f"Test {i}: {its_snowing(s, k)}")
|
|
496
|
+
|
|
497
|
+
--------------------------------------------------------------------------------------
|
|
498
|
+
|
|
499
|
+
(Vanish the multiset)
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
from collections import Counter
|
|
503
|
+
|
|
504
|
+
def vanish_multiset(arr):
|
|
505
|
+
|
|
506
|
+
freq = Counter(arr)
|
|
507
|
+
|
|
508
|
+
return len(freq)
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
# -------- TEST CASES -------- #
|
|
512
|
+
|
|
513
|
+
tests = [
|
|
514
|
+
|
|
515
|
+
[1, 2, 3],
|
|
516
|
+
|
|
517
|
+
[1, 1, 2, 2],
|
|
518
|
+
|
|
519
|
+
[5, 5, 5],
|
|
520
|
+
|
|
521
|
+
[1, 2, 2, 3, 3, 3],
|
|
522
|
+
|
|
523
|
+
[]
|
|
524
|
+
|
|
525
|
+
]
|
|
526
|
+
|
|
527
|
+
for i, t in enumerate(tests, 1):
|
|
528
|
+
|
|
529
|
+
print(f"Test {i}: {vanish_multiset(t)}")
|
|
530
|
+
|
|
531
|
+
--------------------------------------------------------------------------------------
|
|
532
|
+
|
|
533
|
+
(Stewie Loves Pancakes)
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
def can_make(pancakes, k, size):
|
|
537
|
+
|
|
538
|
+
count = 0
|
|
539
|
+
|
|
540
|
+
for p in pancakes:
|
|
541
|
+
|
|
542
|
+
count += p // size
|
|
543
|
+
|
|
544
|
+
return count >= k
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
def stewie_loves_pancakes(pancakes, k):
|
|
548
|
+
|
|
549
|
+
low, high = 1, max(pancakes)
|
|
550
|
+
|
|
551
|
+
ans = 0
|
|
552
|
+
|
|
553
|
+
while low <= high:
|
|
554
|
+
|
|
555
|
+
mid = (low + high) // 2
|
|
556
|
+
|
|
557
|
+
if can_make(pancakes, k, mid):
|
|
558
|
+
|
|
559
|
+
ans = mid
|
|
560
|
+
|
|
561
|
+
low = mid + 1
|
|
562
|
+
|
|
563
|
+
else:
|
|
564
|
+
|
|
565
|
+
high = mid - 1
|
|
566
|
+
|
|
567
|
+
return ans
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
# -------- TEST CASES -------- #
|
|
571
|
+
|
|
572
|
+
tests = [
|
|
573
|
+
|
|
574
|
+
([10, 20, 30], 6),
|
|
575
|
+
|
|
576
|
+
([5, 8, 6], 5),
|
|
577
|
+
|
|
578
|
+
([100], 10),
|
|
579
|
+
|
|
580
|
+
([4, 4, 4], 4),
|
|
581
|
+
|
|
582
|
+
([1, 2, 3], 3)
|
|
583
|
+
|
|
584
|
+
]
|
|
585
|
+
|
|
586
|
+
for i, (p, k) in enumerate(tests, 1):
|
|
587
|
+
|
|
588
|
+
print(f"Test {i}: {stewie_loves_pancakes(p, k)}")
|
|
589
|
+
|
|
590
|
+
--------------------------------------------------------------------------------------
|
|
591
|
+
|
|
592
|
+
(Number of Emirp)
|
|
593
|
+
|
|
594
|
+
def sieve(n):
|
|
595
|
+
|
|
596
|
+
prime = [True] * (n + 1)
|
|
597
|
+
|
|
598
|
+
prime[0] = prime[1] = False
|
|
599
|
+
|
|
600
|
+
for i in range(2, int(n ** 0.5) + 1):
|
|
601
|
+
|
|
602
|
+
if prime[i]:
|
|
603
|
+
|
|
604
|
+
for j in range(i * i, n + 1, i):
|
|
605
|
+
|
|
606
|
+
prime[j] = False
|
|
607
|
+
|
|
608
|
+
return prime
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
def count_emirp(n):
|
|
612
|
+
|
|
613
|
+
prime = sieve(n)
|
|
614
|
+
|
|
615
|
+
count = 0
|
|
616
|
+
|
|
617
|
+
for i in range(13, n + 1):
|
|
618
|
+
|
|
619
|
+
rev = int(str(i)[::-1])
|
|
620
|
+
|
|
621
|
+
if i != rev and prime[i] and rev <= n and prime[rev]:
|
|
622
|
+
|
|
623
|
+
count += 1
|
|
624
|
+
|
|
625
|
+
return count
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
# -------- TEST CASES -------- #
|
|
629
|
+
|
|
630
|
+
tests = [50, 100, 500, 1000, 2000]
|
|
631
|
+
|
|
632
|
+
for i, n in enumerate(tests, 1):
|
|
633
|
+
|
|
634
|
+
print(f"Test {i}: N={n} -> {count_emirp(n)}")
|
|
635
|
+
|