-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily.py
More file actions
490 lines (413 loc) · 11.9 KB
/
Copy pathdaily.py
File metadata and controls
490 lines (413 loc) · 11.9 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# 1.print all prime factor (g4g)
# class Solution:
# def primeFac(self, n):
# # code here
# factor = []
# i = 2
# while n>1:
# if n%i == 0:
# if(i not in factor):
# factor.append(i)
# n=n//i
# else:
# i+=1
# return factor
# 2. Array Duplicate
# def arraydup(arr):
# duplicates = []
# for x in arr:
# index = abs(x) - 1
# if arr[index] < 0:
# duplicates.append(abs(x))
# else:
# arr[index] *= -1
# return duplicates
# 3.Missing and Duplicate
# class Solution:
# def findTwoElement(self, arr):
# dup = []
# seen = set()
# n = len(arr)
# for num in arr:
# if num in seen:
# dup.append(num)
# else:
# seen.add(num)
# total = (n * (n + 1)) // 2
# actual = sum(arr)
# missing = total - (actual - dup[0])
# dup.append(missing)
# return dup
# 4.Counts Digits
#User function Template for python3
# class Solution:
# def evenlyDivides(self, n):
# count = 0
# temp = n
# while temp > 0:
# digit = temp % 10
# if digit != 0 and n % digit == 0:
# count += 1
# temp //= 10
# return count
# 5.Write a program to print multiplication table of n using for loops in reversed order.
# def multiplication_table(n):
# for i in range(1, 11):
# print(f"{n} x {11-i} = {n * (11-i)}")
# n = int(input("Enter any number: "))
# multiplication_table(n)
# 6.Move all zero to the end (g4g)
# class Solution:
# def pushZerosToEnd(self, arr):
# count = 0
# for i in range(len(arr)):
# if arr[i] != 0:
# arr[count] = arr[i]
# count += 1
# while count < len(arr):
# arr[count] = 0
# count += 1
# 7.Sum 1 to n divisor
# class Solution:
# def sumOfDivisors(self, n):
# total_sum = 0
# for i in range(1, n + 1):
# total_sum += (n // i) * i
# return total_sum
# 8
# first occurence in sorted
# class Solution:
# def fo(self, arr, k):
# try:
# return arr.index(k)
# except ValueError:
# return -1
# 9
# Equillibrium point
# class Solution:
# def findEquilibrium(self, arr):
# # code here
# total=sum(arr)
# left_sum=0
# for i in range(len(arr)):
# right_sum=total-left_sum-arr[i]
# if(left_sum == right_sum):
# return i;
# left_sum+=arr[i]
# return -1
#10
# count vovel
# def count_vowels(text):
# count = 0
# for ch in text:
# if ch in "aeiouAEIOU":
# count += 1
# return count
# 11
# Union of two array (g4g)
# class Solution:
# def findUnion(self, a, b):
# # code here
# i=0
# res=[]
# seen=set()
# while(i<len(a) or i<len(b)):
# if(i<len(a) and a[i] not in seen):
# seen.add(a[i])
# res.append(a[i])
# if(i<len(b) and b[i] not in seen):
# seen.add(b[i])
# res.append(b[i])
# i+=1
# res.sort()
# return res
# 12
# Armstrong number
# class Solution:
# def armstrongNumber (self, n):
# # code here
# arm=0
# num=n
# while(num!=0):
# digit= num%10
# num=int(num/10)
# arm+=digit*digit*digit
# if(arm==n):
# return True
# else:
# return False
# 13
# Third Largest
# class Solution:
# def thirdLargest(self,arr):
# # code here
# lens=len(arr)
# if(lens<3):
# return -1
# arr.sort()
# return arr[lens-3]
# 14
# sum of subarray
# class Solution:
# def subarraySum(self, arr):
# # code here
# sum=0
# n=len(arr)
# i=0
# for i in range(0,n):
# sum+=arr[i]*(i+1)*(n-i)
# i+=1
# return sum
# 15
# You are given a String S, you need to print its characters at even indices(index starts at 0).
# s=input("Enter strring")
# print(s[::2])
# 16
# Capacity To Ship Packages Within D Days ( leetcode 1011)
# class Solution:
# def shipWithinDays(self, weights: List[int], days: int) -> int:
# l , r = max(weights),sum(weights)
# res=r
# def canShip(cap):
# ships, currCap = 1, cap
# for w in weights:
# if currCap - w < 0:
# ships += 1
# currCap=cap
# currCap -= w
# return ships <= days
# while l <= r:
# cap = (l + r) // 2
# if canShip(cap):
# res = min(res,cap)
# r = cap-1
# else:
# l = cap + 1
# return res
# 17
# Check if a String Is an Acronym of Words (leetcode 2828)
# class Solution:
# def isAcronym(self, words: List[str], s: str) -> bool:
# if not len(words)==len(s) : return False
# i=0
# for word in words:
# if not (word.startswith(s[i])):
# return False
# i+=1
# return True
# 18 Sort vovel in string
# class Solution(object):
# def sortVowels(self, s):
# vowels = []
# s_list = list(s)
# for i in s_list:
# if i in "AEIOUaeiou":
# vowels.append(i)
# if vowels == []:
# return s
# vowels.sort()
# count = 0
# for j in range(len(s)):
# if s_list[j] in "AEIOUaeiou":
# s_list[j] = vowels[count]
# count += 1
# return "".join(s_list)
# 19
# Digit freq score (leetcode)
# class Solution:
# def digitFrequencyScore(self, n: int) -> int:
# digits = str(n)
# freq = {}
# for ds in digits:
# if ds in freq:
# freq[ds] += 1
# else:
# freq[ds] = 1
# total_score = 0
# for ds, count in freq.items():
# total_score += int(ds) * count
# return total_score
# 20 Kth Missing Positive Number
# class Solution:
# def findKthPositive(self, arr: List[int], k: int) -> int:
# a,i,l=1,0,len(arr)
# count = 0
# while count < k and i < l:
# if not (arr[i]==a):
# count += 1
# else:
# i += 1
# a+=1
# if count < k:
# return a + (k-count) - 1
# return a-1
# 21 Trim trailing vowels
# class Solution:
# def trimTrailingVowels(self, s: str) -> str:
# vowels=['a','e','i','o','u']
# return s.rstrip("".join(vowels))
# 22 Index of an extra element
# class Solution:
# def findExtra(self,a,b):
# #add code here
# extra = sum(arr1) - sum(arr2)
# return arr1.index(extra)
# 22 Max sum subarray of size k
# class Solution:
# def maxSubarraySum(self, arr, k):
# # code here
# l=0
# csum=0
# ans=0
# n=len(arr)
# for r in range(n):
# csum+=arr[r]
# while r-l+1>k:
# csum -= arr[l]
# l+=1
# ans = max(ans, csum)
# return ans
# 23 Percentage of Letter in String ( leetcode 2278)
# class Solution:
# def percentageLetter(self, s: str, letter: str) -> int:
# count = s.count(letter)
# percentage = (count * 100 ) // len(s)
# return int(percentage)
# 24 Jewels and Stones (leetcode 771)
# class Solution:
# def numJewelsInStones(self, jewels: str, stones: str) -> int:
# count = 0
# for stone in stones:
# if stone in jewels:
# count += 1
# return count
# 25 Decode XORed Array
# class Solution:
# def decode(self, encoded: List[int], first: int) -> List[int]:
# i=0
# arr = [0] * (len(encoded) + 1)
# arr[0]=first
# for num in encoded:
# arr[i+1] = encoded[i] ^ arr[i]
# i += 1
# return arr
# 26 Partition Array According to Given Pivot
# class Solution:
# def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
# arr1 = []
# same = []
# arr2 = []
# for num in nums:
# if num < pivot:
# arr1.append(num)
# elif num == pivot:
# same.append(num)
# else:
# arr2.append(num)
# res = arr1+same+arr2
# return res
# 27 How Many Numbers Are Smaller Than the Current Number (leetcode 1365)
# class Solution:
# def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
# arr = nums.copy()
# arr.sort()
# res = []
# for num in nums:
# res.append(arr.index(num))
# return res
# 28 check the balanced string
# class Solution:
# def isBalanced(self, num: str) -> bool:
# even=[]
# odd=[]
# i = 0
# for nums in num:
# if (i % 2 == 0):
# even.append(int(nums))
# else:
# odd.append(int(nums))
# i+=1
# if(sum(even) == sum(odd)):
# return True
# else:
# return False
# 29 Count triplet
# class Solution:
# def countTriplet(self, arr):
# setarr = set(arr)
# count = 0
# l = len(arr)
# for i in range(l - 1):
# for j in range(i + 1, l):
# if (arr[i] + arr[j]) in setarr:
# count += 1
# return count
# 30 Check good integer
# class Solution:
# def checkGoodInteger(self, n: int) -> bool:
# dig = [int(digit) for digit in str(n)]
# sq = [x**2 for x in dig]
# if ( sum(sq) - sum(dig) >= 50):
# return True
# else:
# return False
# 31 Two sum II
# def two_sum(nums, target):
# seen = {}
# for i in range(len(nums)):
# complement = target - nums[i]
# if complement in seen:
# return [seen[complement], i]
# seen[nums[i]] = i
#32 Josephus problem
# class Solution:
# def josephus(self, n, k):
# # code here
# res=0
# for i in range (2,n+1):
# res = (res+k) % i;
# return res+1
# 33 Product of Array Except Self
# def productExceptSelf(nums: list[int]) -> list[int]:
# n = len(nums)
# answer = [1] * n
# prefix = 1
# for i in range(n):
# answer[i] = prefix
# prefix *= nums[i]
# suffix = 1
# for i in range(n - 1, -1, -1):
# answer[i] *= suffix
# suffix *= nums[i]
# return answer
# 34 Given an integer array nums, return an array answer such that:
# answer[i] is equal to the product of all the elements of nums except nums[i].
# Do not use the division operator.
# def product_except_self(nums):
# n = len(nums)
# ans = [1] * n
# prefix = 1
# for i in range(n):
# ans[i] = prefix
# prefix *= nums[i]
# suffix = 1
# for i in range(n - 1, -1, -1):
# ans[i] *= suffix
# suffix *= nums[i]
# return ans
# 35 leetcode 3783 mirror distance
# class Solution:
# def mirrorDistance(self, n: int) -> int:
# if n<10:
# return 0
# rev = int(str(n)[::-1])
# return abs(rev - n)
# 36. Count the Number of Vowel Strings in Range (leetcode)
# class Solution:
# def vowelStrings(self, words: List[str], left: int, right: int) -> int:
# count = 0
# v = ['a','e','i','o','u']
# for i in range(left,right+1):
# if(words[i][0] in v and words[i][-1]in v):
# count+=1
# return count