-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArray and list in dsa
More file actions
102 lines (76 loc) · 2.04 KB
/
Array and list in dsa
File metadata and controls
102 lines (76 loc) · 2.04 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
# List , it can store any data element , it stores the reference of the data
li=[1,2,2.5,6]
print(li)
min_element=li[0]
max_element=li[0]
for ele in li:
if(ele<min_element):
min_element = ele
if(ele>max_element):
max_element = ele
print(min_element)
print(max_element)
res=[]
for i in range(-1,-len(li)-1,-1):
res.append(li[i])
print(res)
# Array , it can only store same datatype element , it stores the actual data
import array as ar
a = ar.array('f',[1,2,3,4,55,1,1,1,1,1,1,11,1,1])
print(a)
print(*a)
# adding the element in the last
a.append(100)
# insert the element in the index which we will mention
a.insert(2,1098.00)
print(*a)
# default value of pop is -1
# pop will remove the element from the last , in pop we have to mention the index
a.pop()
a.pop(2)
print(*a)
# remove will remove the element direct
a.remove(55)
print(*a)
# printing using indexing
print(a[1])
# reverse the element
a.reverse()
print(a)
# count the number of the element present in the array
print(a.count(1))
# to get the index of the element
print(*a)
print(a.index(11.0))
# to extend the array
a.extend([100,200,300,400])
print(*a)
#Subarray -> continuous manner , order matters
# ab = [2,5,1,6,7,4]
# j=0
# for i in range(len(ab)+1):
# print(ab[0:i])
# print(ab[1:i])
# print(ab[2:i])
# print(ab[3:i])
# print(ab[4:i])
# print(ab[5:i])
ab = [2,5,1,6,7,4]
# for i in range(1, len(ab)+1): # start from 1 so slices aren’t empty
# print(ab[0:i])
# print(ab[1:i]) if i > 1 else None
# print(ab[2:i]) if i > 2 else None
# print(ab[3:i]) if i > 3 else None
# print(ab[4:i]) if i > 4 else None
# print(ab[5:i]) if i > 5 else None
for i in range(len(ab)):
for j in range(i,len(ab)):
print(ab[i:j+1])
li = [2,5,4,3,1,6]
for i in range(0,len(li)):
for j in range(i,len(li)):
for k in range(i,j+1):
print(li[k],end="")
print()
# Subsequence -> No need of continous , order matters
# subarray-> to find the number of subarray we use the formula n*(n+1)/2