WhatsApp Documents - FDS Unit 2 Notes
WhatsApp Documents - FDS Unit 2 Notes
1 8
Definition of an array
An array is a set of consecutive memory location with contains similar data elements.
Or
One dimension
The one dimensional array 'a' is declared as int ai0].
Value storeI Ín the a r r a
40
1
Index used to nd e l e e n t
2.Traversing List
The loop is used in list for traversing purpose. The for loop is used to traverse the list
element. Syntax - for variable in List:
Body
Example- a-['a, b'c'd','e]
for i in a:
print(i)
We can traverse the list using range() function. We can access each element of the list
using index of list.
If we want to increment each element of the list by one then we must pass index as
argument to for loop. This can be done using range() function as follows.
a-[10,20,30,40]
for iin range(len(a):
a[i] = a[i]+1
print(a) # (11,21,31,41]
3.Deleting an elements from array
hecause have to shift the
Dcleting an clemcnts fromn array is complex activity
an clement sizc gcts decremented by
clements to previous position. That mcans after Deleting
2
Onc.
10 20 30 35 40
al-)
n=int(input("Enter how many elements you want insert in first array"))
for i inrange(0,n):
item=int(input("Enter element in array")
al.append(item)
a2=[]
m=int(input("Enter how many elements you want insert in second array"))
for i in range(0,m):
item=int(input("Enter element in array")
a2.append(item)
def mergeArray(al,a2,n,m):
a3-[None]*(n+m)
i-0
j-0
k=0
while i<n and j<m:
if al[i}<a2[iI:
a3[k]-al[i]
k=k+|
i=itl
else:
a3[k]-a2[i]
k=k+|
while i<n:
a3[k]=al[ij
k=k+l
i=itl
while /<n:
a3{k]-a2|il
k-k+|
print("Merge array")
for iin range(n tm):
print(str(a3{i),cnd =" ")
mergeArray(al,a2,n,m)
Transpose of Matrix
def Transpose():
C=[[0 for i in range(0,row)] for j in range(0,col)]
for iin range(0,row):
forj in range(0,col):
C[iJ] = AjJ[]
print(" Transpose of Matrix==>".C)
row-int(input("Enter no. of row for matrix")
col=int(input("Enter no. of column for matrix")
A=[[0 for iin range(0,row)] for j in range(0,col)]
print("Enter element of matrix")
for iin range(0,row):
for j in range(0,col):
A[JÜ] = int(input(nEnter element A{}:.format(i, i))
print("Matrix A ==> ",A)
Transpose()
Concept of Ordered List
sometimes called as linear
Ordered List is nothing but a set of clements such as list
list. Example 1-list of one digit number [0,1.2,3,4,......9)!)
Example 2- Days ina week
"Saturday"]
T"Sunday", "Monday", "Tuesday"" Wednesday", "Thursday", "Friday.
Withthis concept in mind let us fomally define the ordered list.
Definition
Ordered List is set of elements where set may be emepty or it can be written as a
sort()
The sort method arrange elements in increasing order.
a-[40.50.10,20,80,90|
print("Original clemcnts in the list", a)
a.sort()
print("List after sort function", a)
output-Original elements in the list 140, 50, 10, 20. 80, 90]
List after sort function [10, 20, 40, 50, 80, 90]
Insert()
desired position in the list..
This insert method allows us to insert the element at
a-[10, 20, 40, 50, 60, 70]
print("Original elements in the list", a)
a.insert(2,30)
print("List after insert 30 at index 2 function", a)
40, 50, 60, 701
m" output-Original elements in the list [10, 20.
30, 40, S0, 60, 70] "nn
List after insert 30at index 2 function [10, 20.,
delete) like
is carried out using various function
The deletion of any element from the list
pop, remove, del.
function.. the
element to be deleted then just use pop
If we know the index of the
function.
index as an argunent to the pop
a=[10, 20, 40, 50, 60, 70]
print("Original clements in the list", a)
a.pop(2) function", a)
print("List after pop 30 from index 2
40, 50, 60, 70]
output-Original elements inthe list [10, 20, 20, 50, 60, 70]
function |10,
List after pop 30 from index 2
argument to pop function then last element of the list
If you do not provide any
will be deleted.
a-[10, 20, 40, 50, 60, 70]
print("Originalelements in the list", a)
a.pop() no argument ", a)
print("List after pop function with
20, 40, 50, 60, 701
output-Originalelements in the list |10,
argument [10, 20, 40, 50,60]
Listafter pop function with no
3 2#
# 50))
used. be a.index(50) ",a.copy()
to funetion a.count( True False
is is ",a) using by
function
that 70] 50] 70] # 70] # obtained
List "Sham","Ram""V
"Pankaj",
value del 60, ", 60, ", [10,20,30,40,50] a= List 60, all(a))
a= a [10,20,30,40,50] 60, all(b) list
remove using 50, 50 50, 50 of a 50, 50, ", new
actuat of
40, ofIndex40, Count of 40, ",print("returns
print("Reverse is
print("Copy0, print("returns defineelement
tine deleted 20, 20, a.reverse) a-[10,20, 20,
the the a-[10,
print(" b-[10,
a a=[10,print(" &
the is at be createcachsequence.
deletedfunction element will wherc
70| 70| the
70| passed to
way
elegant
clement 60, if True list anotlher
60, item
60,
be remove 50, 70) one ".a) 50. or
to than 40,
20., matched items true returns new "ishwar""Rahul""
name-
40, 60, index list
clement element
20, [10, of the list are a of
the a)
20, 40,
more numberin the list function an make member
to |10,20,
clement", a) 3
and
|10,clement first is list|
the passed a) 4 items of the comprehension
to
list", list |10, remove list", to list the copy useful each the
2 2 Methods of of this
of
valueparameter he the thei.c. from the4 of of
shallowelements
in
element 3 in to indeX count order then to item
in 50 in to 70| in indexindex 2
elements Built-in
Functions:
List argument
an
as comprehension
List many operation
the list.
701 elementsremoveclements possibleelements from List
Python
the the the empty for compression
without
know the
60, 50 60, 4-1 del index Returns ReturnsReverse Returns
a the List is
This
List|=expression
name
the from 50, remove 50, # after
print("Originaloutput-Original all is some
after output-Original
is list
40, print("Original
a.remove(50) it 40, del If list.
applying Example
we means rname-
in
print("Listafier python
20, del print("List
a[2:4]
If removed 20, after reverse() existing title
|10, a=[10, index() count) copy)
That List In List for
all()
a
if titlc.startswith(R'):
name.apend(itle)
print("Another List rom name "rname)
with compression
name=
["ishwar""Rahul"."Rohit".if "Ramesh" "Pankaj""Sham", "Ram"."Vitthal"|
rname-[ title for title in name
title.startswith(R') |
print("Another List from name ",rname)
Coeticients of
polvnomials
copy the coefficient at position ifrom A polynomial into coefficient field at position k
of array C copy the exponent pointed by iinto the exponent field at
position k in array C.
Increment i, k pointers.
else
while i< t
copy the coefficient at position iof into the coefficient field at position k in C.
copy the exponent pointed by iinto the exponent field at position k in C
array,
Increment iand k to point to the next position in arrays A and C.
while j < t2
Copy the cocfficient at posit ion j of B into the coctficicnt ficld at position kin C.
Copy thc exponent pointed by jinto exponcnt ficld at position k in C.
Increment j, k to point to the next position in Band C arrays.
8. Display the complete array Cas the addition of two given
9. Stop. polynomials.
ipointer
5x3 |7x BPolynomial
Tipointer
CPolynomial
As the term pointed by i& j shows that both term have equal exponent. We can
perform the addition of the terms & we will store the result in the polynomial C.
3x2x* x 1 APolynomial
ipointer
Sx*7x B Polynomial
Tipointer
CPolynomial
k pointer
Tipointer
5x7xBPolynomial
i pointer
8 x3 C Polynomial
Tkpointer
2x*>7%. We will copy 2x* in
Now point Ipoints to 2x* & pointer jpoints to 7x as
polynomial C & we will increment i&k
APolynonial
ipointer
B Polynomial
Tipointer
2x C Polynomial
k pointer
As the term pointed by i &jshows that both term have equal exponent. We can pertorm the
addition of the terms & we will store the result in the polynomial C. Now increment 1j,k
pointer.
A Polvnomial
ipointer
73 B Polynomial
i pointer
2x² 8x
C Polynomial
k pointer
Now term in polynomial B is over. Hence we will copy remaining term polynomial Ato
polynomial C.
3x³2x x
A Polynomial
7x| BPolvnomial
8 x³ 2x+ 8x 1 C Polynonial
Polynomial multiplication
Example - if two polynomials given as
3x'+ 2x+x+1
Sx'+7x
Then we will perform multiplication by multiplying each term of Polynomial Aby each
term of Polynomial B
3x'+ 2x' +x +1
5x'+ 7%
Sparse Matrix
A matriX can be defined as a two dimensional array having 'm' column & 'n' rows
representing m * n matrix.
Sparse Matrix are those matrices that have the majority of their elements equal to
zero. In other words, the Sparse Matrix can be defined as the matrix that has greater number
of zero elements than the non-zero element. 1
Example 0
Fig-Sparse Matrix
Sparse matrix representation using array
The representation of Sparse Matrix will be a triplet only. That means it store rows,
column and values.
The 0" row will store that rows of the matrix , total column of the matrix & total
number of non-zero values.
Suppose a matrix is 4 x 5 & number of non-zero term are say 7.
Row- index of the row where non-zero element is located.
Column - index of the column where non-zero element is located.
Value - value of the non-zero element is located at index.
0
Row 0 0 3
Colunn 2 3 1
() value 7 3 6 3
2
Algorithm
1.Obtain the triplet form both Sparse matrix.
2. Create a new triplet to store result matrix.
3. Copy number of rows & column from any sparse matrix to result matrix.
4.Let i,j,k be the indices of sparse matrix 1,2,3 respectively.
5. Initialize i,j,k to I
6. Traverse both matrices from second row
else
Compare row of both sparse matrix & which ever has less row value copy that to
result matrix by incrementing respectively indices.
7. Copy stop 6 till end of any matrix triplet.
8. Copy the reaming term of sparse matrix (if any) to resultant matrix.
B= 1 10
1
2 3
6 1
2 6
1 1
3
2
1
I.ocaion
Row Colunn
1 0 1
Types of Tradeoff
1. Lookup tables vs. Recalculation
A common situation in an algorithm involving a lookup tables an implementation can include
the entire table which reduces computing time, but increases the amount of memory needed,
or it can compute table entries as needed, increasing computing time, but reducing memory
requirements
Rendering the image when the page is changed and storing the rendered images
would be trading space for time; more space used, but less time. This technique is more
generally known as caching.