-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrays.py
309 lines (211 loc) · 5.28 KB
/
Arrays.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
# Arrays are similar to list, but need to have all the values of same type
# when creating an array at the start we have to mention the type code like ; int, float, double
#Refer this youtube video before any interview, it'll be useful for array
# https://www.youtube.com/watch?v=6a39OjkCN5I&list=PLsyeobzWxl7poL9JTVyndKe62ieoN-MZ3&index=31
from array import *
# val=array('i',[3,4,5,6,7,4])
# print(val.buffer_info())
# print(val.typecode)
# val.reverse()
# print(val[0])
# for i in range(len(val)):
# print(val[i])
# different way to print the array one by one
# for i in val:
# print(i)
#=======================================================
# val=array('u',['a','U','e'])
#
# for i in val:
# print(i)
#======================================================
# val=array('i',[4,5,6,7,8,9])
#
# new=array(val.typecode,(a for a in val)) # creating a new array with the same type and values of old array
# # new1=array(val.typecode,(a*a for a in val)) # creating new array with the square value of the old array
#
# for i in new:
# print(i)
#=========================================================
# also doing the same with while loop instead of for loop ( better is for loop )
# val=array('i',[4,5,6,7,8,9])
#
# i=0
# while i<len(val):
# print(val[i])
# i+=1
#======================================================
#-----------Array values from user--------------------
# ar=array('i',[])
#
# n=int(input('Enter the Length of the Array : '))
#
# for i in range(n):
# x=int(input('Enter the values : '))
# ar.append(x)
#
# print(ar)
# searching the index by getting input value array value from the user
# val=int(input('Enter the value for search : '))
#
# k=0
#
# for e in ar:
# if e==val:
# print(k)
# break
# k+=1
#===================================================
# ar=array('i',[])
#
# x=int(input('Enter the length of the array : '))
#
# for i in range(x):
# z=int(input('Enter the values : '))
# ar.append(z)
#
# print(ar)
#
# m=int(input('Enter the search value : '))
#
# v=0
#
# for e in ar:
# if e==m:
# print(v)
# break
# v += 1
# else:
# print('Invalid array value')
#--------------------------------------------------
# print(ar.index(m)) # getting index with the function
#===========================================================
# defautly array doesn't support multidimensional array so we are using Numpy
#---------------------------------------------------------------------------------------
# ay=array('i',[]) # ay=array('x',[]) || ValueError: bad typecode (must be b, B, u, h, H, i, I, l, L, q, Q, f or d) || so the things in the bracket
#
#
#
# m=int(input('Enter the limit of the array : '))
#
# for i in range(m):
# z=int(input('Enter the array value : '))
# ay.append(z)
# print(ay)
#
# n=int(input('Enter the value to find the index : '))
#
# r=0
#
# for j in ay:
# if j==n:
# print(r)
# break
# r+=1
# else:
# print('Invalid Index value')
# ar=array('i',[])
#
# limit=int(input('Enter the limit for the array : '))
#
# for i in range(limit):
# z=int(input('Enter the array value : '))
# ar.append(z)
#
# print(ar)
#
# random=int(input('Enter the value to find the Index : '))
#
# ind=0
#
# for j in ar:
# if j==random:
# print(ind)
# break
# ind+=1
# else:
# print('Invalid Index value')
#---------------------------------------
# val=array('i',[2,3,4,6])
# val.reverse() # reversing the array
# print(val)
# for i in range(len(val)): # printing the array value according to it's length
# print(val[i])
# for j in val: # another way for printing the array value according to it's length
# print(j)
#---------------------------------------------
# v=array('i',[1,2,3,4,5,6])
# # v.reverse()
# # print(v)
#
# for i in v:
# print(i)
#-----------------------------------------
# arr=array('u',['a','e','i','o','u'])
#
# for i in arr:
# print(i)
#----------------------------------------
# val=array('i',[2,3,4,6])
#
# newar=array(val.typecode,(a*a for a in val))
#
# for i in newar:
# print(i)
#
# j=0
# while j<len(val):
# print(val[j])
# j+=1
#---------------------------------------
z=array('i',[])
x=int(input('Enter the limit : '))
for i in range(x):
m=int(input('Enter the value : '))
z.append(m)
print(z)
n=int(input('Enter the search element : '))
v=0
for j in z:
if j==n:
print(v)
break
v+=1
else:
print('Invalid index ')
#------------------------------------------
# v=array('i',[1,2,3,4,5])
# for i in range(len(v)):
# print(v[i])
#
# print('----------------')
#
# ne=array(v.typecode,(e**2 for e in v)) # deriving a new array with copying old array's type and elements
#
# for j in ne:
# print(j)
# m=0
# while m<len(v):
# print(v[m])
# m+=1
#---------------------------------------------------
# l=[]
#
# m=int(input('Limit : '))
#
# for i in range(m):
# x=int(input('Enter the value : '))
# l.append(x)
# print('My list :',l)
#
# n=int(input('Enter the value to find the index : '))
#
# v=0
# for j in l:
# if n==j:
# print(v)
# break
# v+=1
# else:
# print('Invalid Index')
# print(l.index(n)) # this is a function which we can use to find the index instead of manual method