-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-basic.py
More file actions
332 lines (230 loc) · 5.94 KB
/
array-basic.py
File metadata and controls
332 lines (230 loc) · 5.94 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
#sampriti banerjee--ece--41--j.
# Python program to swap first
# and last element of a list
# Swap function
def swapList(newList):
size = len(newList)
# Swapping
temp = newList[0]
newList[0] = newList[size - 1]
newList[size - 1] = temp
return newList
# Driver code
newList = [12, 35, 9, 56, 24]
print(swapList(newList))
#Q1 Testcase2
def swapList(newList):
size = len(newList)
# Swapping
temp = newList[0]
newList[0] = newList[size - 1]
newList[size - 1] = temp
return newList
# Driver code
newList = [1, 2, 3]
print(swapList(newList))
#Q2 Test case 1
# Python program to swap elements
# at given positions
# Swap function
def swapPositions(list, pos1, pos2):
list[pos1], list[pos2] = list[pos2], list[pos1]
return list
# Driver function
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3
print(swapPositions(List, pos1 - 1, pos2 - 1))
#Q2 Test case 1
# Python program to swap elements
# at given positions
# Swap function
def swapPositions(list, pos2, pos5):
list[pos2], list[pos5] = list[pos5], list[pos2]
return list
#Q2 Teset case 2
# Driver function
List = [1, 2, 3, 4, 5]
pos2, pos5 = 1, 3
print(swapPositions(List, pos1 - 2, pos2 - 5))
#Q3 Test 1
from collections import Counter
# declaring the list
l = [15, 6, 7, 10, 12, 20, 10, 28, 10]
# driver program
x = 10
d = Counter(l)
print('{} appeares {} times in the given array'.format(x, d[x]))
#Q3 Test case 2
from collections import Counter
# declaring the list
l = [8, 6, 8, 10, 8, 20, 10, 8, 8]
# driver program
x = 16
d = Counter(l)
print('{} appeares {} times in the given array'.format(x, d[x]))
#Q4 Test Case1
# Python3 code to calculate
# average of array elements
# Function that return
# average of an array.
def average(a, n):
# Find sum of array element
sum = 0
for i in range(n):
sum += a[i]
print("The sum of the number is ", sum)
return sum / n;
# Driver code
arr = [4, 5, 1, 2, 9, 7, 10, 8]
n = len(arr)
print(average(arr, n))
#Q4Test case 2
# Python3 code to calculate
# average of array elements
# Function that return
# average of an array.
def average(a, n):
# Find sum of array element
sum = 0
for i in range(n):
sum += a[i]
print("The sum of the number is ", sum)
return sum / n;
# Driver code
arr = [15, 9, 55, 41, 35, 20, 62, 49]
n = len(arr)
print(average(arr, n))
#Q5testcase1
# Python program to multiply all values in the
# list using traversal
def multiplyList(myList):
# Multiply elements one by one
result = 1
for x in myList:
result = result * x
return result
# Driver code
list = [1, 2, 3]
print(multiplyList(list))
#Q5 test 2
# Python program to multiply all values in the
# list using traversal
def multiplyList(myList):
# Multiply elements one by one
result = 1
for x in myList:
result = result * x
return result
# Driver code
list = [3, 2, 4]
print(multiplyList(list))
#Q6Testcase1
# Python program to print Even Numbers in a List
# list of numbers
list1 = [2, 7, 5, 64, 14]
# iterating each number in list
for num in list1:
# checking condition
if num % 2 == 0:
print(num, end=" ")
#Q6Testcase2
# Python program to print Even Numbers in a List
# list of numbers
print(" ")
list2 = [12, 14, 95, 3]
# iterating each number in list
for num1 in list2:
# checking condition
if num1 % 2 == 0:
print(num1, end=" ")
#Q7 test case 1
# Python program to print odd Numbers in a List
# list of numbers
print(" ")
list1 = [2, 7, 5, 64, 14]
# iterating each number in list
for num in list1:
# checking condition
if num % 2 != 0:
print(num, end=" ")
#Q7testcase2
# Python program to print odd Numbers in a List
# list of numbers
print(" ")
list1 = [12, 14, 95, 3, 73]
# iterating each number in list
for num in list1:
# checking condition
if num % 2 != 0:
print(num, end=" ")
#Q8testcase1
# Python program to count Even
# and Odd numbers in a List
# list of numbers
print(" ")
list1 = [2, 7, 5, 64, 14]
even_count, odd_count = 0, 0
# iterating each number in list
for num in list1:
# checking condition
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
#Q8testcase2
# Python program to count Even
# and Odd numbers in a List
# list of numbers
list1 = [12, 14, 95, 3]
even_count, odd_count = 0, 0
# iterating each number in list
for num in list1:
# checking condition
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
#Q9Testcase1
# Python program to print positive Numbers in a List
# list of numbers
print(" ")
list1 = [12, -7, 5, 64, -14]
# iterating each number in list
for num in list1:
# checking condition
if num >= 0:
print(num, end=" ")
#Q9Testcase2
# Python program to print positive Numbers in a List
# list of numbers
print(" ")
list2 = [12, 14, -95, 3]
# iterating each number in list
for num in list2:
# checking condition
if num >= 0:
print(num, end=" ")
#Q10Testcase1
# Python program to print negative Numbers in a List
# list of numbers
print(" ")
list1 = [12, -7, 5, 64, -14]
# iterating each number in list
for num in list1:
# checking condition
if num < 0:
print(num, end=" ")
#Q10Testcase2
# Python program to print negative Numbers in a List
# list of numbers
print(" ")
list1 = [12, 14, -95, 3]
# iterating each number in list
for num in list1:
# checking condition
if num < 0:
print(num, end=" ")