-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatastructure.py
More file actions
272 lines (202 loc) · 5.3 KB
/
Copy pathdatastructure.py
File metadata and controls
272 lines (202 loc) · 5.3 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
# ## LIST
# a = 12
# b = 13
# c = 14
# d = 15
# e = 16
# for creating list you have to use square
##brackets([])
#l = [12,13,14,15,16]
##Special powers
##1 - hetrogeneous nature
## means - it can store any kind of data type at once
##eg
#l = [12,"hello",12.67,True,print()]
##2nd --ordered
## every element in a list has a designated Position
## 3rd-- mutable nature
## you can change anything inside the list at any point of time
## 4th -- duplicates
## you can store duplicates element inside list
# ## reading a list
# #a = [10,20,30,40,50]
# #you will use indexing
# # print(a)
# # print(a[4],a[-1])
# ## updating a list
# # a = [10,20,30,40,70]
# # a [-1] = 50
# # print(a)
# ## delete -- you can delete a single element and entire list
# # a = [10,20,30,40,50]
# # del a[-1]
# # print(a)
# ## creating loops on list
# #a = [10,20,30,40,50]
# ##based on values
# # for i in a:
# # print(a)
# ## here you will access all the value 10,20,30,.......
# ##based on index
# # for i in range(0,len(a)):
# # print(a[i])
# ## this loop can access your index as well as your values and it gives more control over your list
# ##methods
# # a = [1,2,3,4]
# # #a.append([5,6,7])
# # a.append(5)
# # print(a)
# ####
# l = []
# for i in range(10,51,10):
# l.append(i)
# print(l)
# ## insert a value between list
# a = [10,20,40,50]
# a.insert(2,30)
# print(a)
# ##delete
# a = [10,20,30]
# a.clear()
# print(a)
# ##
# a = [10,20,30,40,50]
# #a.pop(1)
# saved= a.pop(1) ## this will remove via index/it remove the value of list using index
# a.remove(10) ##-- this will remove via value/it will remove the value of list using value...means it use first occurence of value means it remove only first value from list
# print(a)
### 21/04/026
##
##reverse function
# a = [10,20,30,40,50]
# a.reverse()
# print(a)
# ## sort
# a = [40,70,60,90,80,30,20]
# a.sort()
# print(a)
##shorting the list
# a = [56,12,89,23,56,90,13]
# for j in range(len(a)-1):
# for i in range(0,len(a)-1):
# if a[i] > a[i+1]:
# a[i],a[i+1] = a[i+1],a[i]
# print(a)
# ## find the greatest
# a = [12,56,23,89,23,1,45,7,8,4,23]
# largest = a[0]
# index = 0
# for i in range(len(a)):
# if a[i]> largest:
# largest = a[i]
# index = i
# print(f"largest element is {largest} at {index}")
# l = [1,16,17,23,2,89,45]
# largest = l[0]
# s_largest = l[0]
# for i in l:
# if i >largest:
# s_largest = largest
# largest = i
# elif i > s_largest:
# s_largest = i
# print(largest)
# print(s_largest)
###
# l = [1,16,17,23,2,89,45]
# largest_index = 0
# largest = l[0] ##loat(inf)
# s_largest = l[0]
# second_largest_index = 0
# for i in range(1,len(l)):
# if l[i] >largest:
# s_largest = largest
# largest = l[i]
# second_largest_index = largest_index
# largest_index = i
# elif l[i] > s_largest:
# s_largest = l[i]
# print(largest,largest_index)
# print(s_largest,second_largest_index)
# ## smallest and second smallest
# l = [1,16,17,23,2,89,45]
# smallest = 12
# smallest_index = 0
# s_smallest = 12
# second_smallest_index = 0
# for i in range(0,len(l)):
# if l[i] < smallest:
# s_smallest = smallest
# smallest = l[i]
# second_smallest_index = smallest_index
# smallest_index = i
# elif l[i] < s_smallest:
# s_smallest = l[i]
# second_smallest_index = i
# print(smallest,smallest_index)
# print(s_smallest,second_smallest_index)
###47) Check if List is sorted or not.
# l = [1,2,3,4,5,6,7]
# for i in range(len(l)-1):
# if l[i] > l[i+1]:
# print("list is not sorted")
# break
# else:
# print('list is sorted')
###palindrom --> left to right or right to left must be equal
#l = [2,3,15,15,3,2]
#print(l[len(l)]-1)
# l = [2,3,15,15,3,2]
# for i in range(len(l)//2):
# if l[i] != l[len(l)-1-i]:
# print('list is not pallindrome')
# break
# else:
# print("list is pallindrome")
## task
#1 take n as input from user
#2.create an list of size n
#3.take input for each element
#1 take n as input from user
# n = int(input("number of element you want"))
# l = []
# sum = 0
# for i in range(n):
# inputs = int(input(f"enter the element of index at index {i}:"))
# sum += inputs``
# l.append(inputs)
# print(l)
# print(sum)
## map function
# lst = list(map(int,input('enter elements:').split()))
# print(lst)
## map(data_type,input)
#split(seperate all the values and digits)
#list(converts the value in the form of list data structure)
"""sabse pahle inputs accept--. har input split hoga --> inputs will be type casted in the form of int--
>we stored all the int values inside a list"""
## 27 april
## Rotate a list by k elements
# l = [1,2,3,4,5] ##o//p = k = 2, [1,2,3,4,5]
# k = 2
# for i in range(k): # i ..> 0,1
# last = l [-1] #3 last value 5
# for j in range(len(l) -1,0,-1): # j --> 4,3,2,1
# l[j] = l[j-1]
# l[0] = last
# print(l)
## Assign all the zero at the end of the list
l = [0, 1, 0,3,12]
j = 0
for i in range(len(l)):
if l [i ] != 0:
l[i],l[j] = l[j],l[i]
j = j+ 1
print(l)
### orrr
l = [0,1,0,5,3,0,12]
l.sort()
print("sorted list:-",l)
l.reverse()
print("reverse list:-",l)
### 28 april 2026