-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
359 lines (301 loc) · 8.16 KB
/
main.py
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
# ## vanilla binary search
# #
# # high = len(arr)-1
# # low = 0
# # index=-1
# # target =7
# # while(low<=high):
# # mid = (high+low)//2
# # if(arr[mid]==target):
# # index = mid
# # break
# # elif(arr[mid]>target):
# # high = mid-1
# # else:
# # low = mid+1
# # print(index)
# ## lower bound target=>index
# # arr = [1,2,3,4,5,6,7,8,9]
# # i = 0
# # j = len(arr)-1
# # target = 6
# # index = -1
# # while(i<=j):
# # mid = (i+j)//2
# # if(target<=arr[mid]):
# # j= mid-1
# # index = mid
# # else:
# # i = mid+1
# # print(index)
# # first position of true
# # arr = [False,False,False,False,True,True,True]
# # i=0
# # j=len(arr)-1
# # index=-1
# # while(i<=j):
# # mid = (i+j)//2
# # if(arr[mid]):
# # j = mid-1
# # index = mid
# # else:
# # i = mid+1
# # print(index)
# ## element in rotated sorted array
# # arr = [7,8,9,1,2,3,4,5,6]
# # i =0
# # j = len(arr)-1
# # index=-1
# # target = 9
# # while(i<=j):
# # mid = (i+j)//2
# # if(arr[mid]<arr[j]):
# # ## this portion is sorted
# # if(arr[mid]==target):
# # index = mid
# # break
# # elif(arr[mid]>target>arr[j]):
# # i = mid+1
# # else:
# # j = mid-1
# # else:
# # if(arr[mid]==target):
# # index = mid
# # break
# # elif(arr[mid]<target<arr[i]):
# # j = mid-1
# # else:
# # i = mid+1
# # print(index)
# # rotated sorted with duplicates
# # arr = [7,8,1,2,3,3,3,4,5,6]
# # i = 0
# # j = len(arr)-1
# # index = -1
# # target = 8
# # while(i<=j):
# # mid = (i+j)//2
# # number = int(input())
# # i =0
# # j=float('inf')
# # index = -1
# # def func(mid,target):
# # return mid*mid<=target
# # while(i<=j):
# # mid =(i//2)+(j//2)
# # print(mid)
# # if(func(mid,number)):
# # index = mid
# # i = mid+1
# # else:
# # j = mid-1
# # print(index)
# # number = int(input())
# # power = int(input())
# # i=0
# # j = number
# # index = -1
# # def func(mid,power,number):
# # result =1
# # while(power!=0):
# # if(power%2!=0):
# # result = result*mid
# # power-=1
# # else:
# # mid = mid*mid
# # power/=2
# # return result<=number
# # while(i<=j):
# # mid = i+(j-i//2)
# # if(func(mid,power,number)):
# # index = mid
# # j = mid-1
# # else:
# # i = mid+1
# # print(index)
# # piles = [3,6,7,11], h = 8
# # i = 0
# # j = sum(piles)
# # hr questions
# # introduction
# # dbms
# # javascript+ reactjs
# # oops+js oops
# # python - revision, js revision
# # shallow vs deep copy
# # importing
# # sorting
# ## lambda
# ## python file handeling
# ## file operations
# ## map and reduce
# # a=4
# # b=5
# # x = lambda a,b:a+b
# # print(x(a,b))
# # from abc import ABC, abstractmethod
# ## python file handleing
# # f = open("hello.txt",'w')
# # # for lines in f:
# # # print(lines)
# # f.write("harshdeepsingh")
# # f.close()
# # try:
# # f= open("hello.txt",'a')
# # f.write("Harshsdeep singh is good ")
# # except:
# # print("error occured")
# # else:
# # print("done")
# # finally:
# # print("file operations completed")
# ## total number of employee in each city
# # select city,count(*) from employee
# # group by city;
# ## second highest ssalary
# # select min(salary) from(
# # select * from employee
# # order by salary desc
# # limit 2
# # );
# # select department,min(salary) as minimum_of_dept from employee
# # group by department
# # select department, min(salary) as second_ghigest from (
# # select department,salary from employee
# # order by department
# # )
# # arr=[4,5,6,7,0,1,2]
# # i=0
# # j=len(arr)-1
# # index=-1
# # while(i<j):
# # mid = (i+j)//2
# # if(arr[mid]>arr[j]):
# # if(arr[mid]<arr[index]):
# # index=mid
# # j=mid-1
# # else:
# # if(arr[i]<arr[index]):
# # index = i
# # i=mid+1
# # print(arr[index])
# # // bubble sorting
# # arr=[9,8,7,6,5,4,3,2,1]
# # for j in range(len(arr)-1,i,-1):
# # print(i,j,j-1)
# # if(arr[j]<arr[j-1]):
# # arr[j],arr[j-1] = arr[j-1],arr[j]
# # print(arr)
# ## selection sorting
# # for i in range(0,len(arr)-1):
# # minidx=0
# # for j in range(i+1,len(arr-1)):
# # if (arr[minidx]>arr[j]):
# # arr[minidx] = j
# # arr[minidx],arr[i] = arr[i],arr[minidx]
# # def merge(arr,low,high,mid):
# # i=low
# # j=mid
# # while(i<=mid and j<=len(arr)-1):
# # temp=float('inf')
# # def mergesorting(low,high,arr):
# # if(low>=high):
# # return
# # mid = (low+high)//2
# # mergesorting(low,mid,arr)
# # mergesorting(mid+1,high,arr)
# # merge(arr,low,high,mid)
# # def dfs(node):
# # if not node.left and not node.right
# # return 0
# # l = dfs(node.left)
# # r = dfs(node.right)
# # return 1+max(l,r)
# # class Node:
# # def __init__(self,val=None):
# # self.val=None
# # self.left=left
# # self.right=right
# # class Tree:
# # def __init__(self):
# # self.root = None
# # def dfs(node):
# # if not node.left and not node.right:
# # return
# # dfs(node.left)
# # dfs(node.right)
# # def bfs(node):
# # queue=[node]
# # while(queue):
# # nlevel=[]
# # for cnode in queue:
# # ##do the operation
# # if(cnode.left):
# # nlevel.append(cnode.left)
# # if(cnode.right):
# # nlevel.append(cnode.right)
# # queue=nlevel
# ## treee views
# seti =set()
# seti.add()
# seti.remove()
# len(seti)
# combined = zip(arr1,arr2)
# arr = [[0]*3 for x in range(0,3)]
# print(arr)
# lower upper isalnum is num
# file = open("input.txt",'w')
# file.write(" ")
# try:
# file = open("input.input.txt")
# else:
# string = ""
# print(string.split('e'))
import copy
# Example class to demonstrate copying
class Person:
def __init__(self, name, details):
self.name = name
self.details = details
def __repr__(self):
return f"Person(name={self.name}, details={self.details})"
def demonstrate_copy_types():
# Original complex object
original_list = [1, [2, 3], {'a': 4}]
# 1. Assignment (Not a Copy)
print("1. Assignment (Reference)")
assigned_list = original_list
assigned_list[1][0] = 'X'
print("Original list after modification:", original_list)
print("Assigned list:", assigned_list)
print("Are they the same object?", original_list is assigned_list)
# 2. Shallow Copy
print("\n2. Shallow Copy")
shallow_copy = copy.copy(original_list)
shallow_copy[1][0] = 'Y' # Modifies nested mutable object
print("Original list after shallow copy modification:", original_list)
print("Shallow copy:", shallow_copy)
print("Are they the same object?", original_list is shallow_copy)
# 3. Deep Copy
print("\n3. Deep Copy")
deep_copy = copy.deepcopy(original_list)
deep_copy[1][0] = 'Z' # Does not affect original
print("Original list after deep copy modification:", original_list)
print("Deep copy:", deep_copy)
print("Are they the same object?", original_list is deep_copy)
# 4. Complex Object Example with Custom Class
print("\n4. Custom Class Copy Demonstration")
# Create an original person with nested dictionary
original_person = Person("Alice", {"age": 30, "hobbies": ["reading", "coding"]})
# Shallow copy
shallow_person = copy.copy(original_person)
shallow_person.details["hobbies"].append("swimming")
print("Original person after shallow copy modification:", original_person)
print("Shallow copy:", shallow_person)
# Deep copy
deep_person = copy.deepcopy(original_person)
deep_person.details["hobbies"].append("painting")
print("Original person after deep copy modification:", original_person)
print("Deep copy:", deep_person)
# Uncomment to run the demonstration
# demonstrate_copy_types()