# Code to calculate the average of number list
def avg(numbers):
sum=0
for i in numbers:
sum=sum+i
n=len(numbers)
avg=sum/n
return (avg)
# Code to calculate the maximum of number list
def max(numbers):
x=numbers[0]
for i in numbers:
if i>x:
x=i
return(x)
#Code to calculate the frequency of a number
def frequency(numbers,value):
x=0
for i in numbers:
if i==value:
x=x+1
return(x)
#Code to find median of a list
def median(lis):
lists=sorted(lis)
n=len(lists)
if n%2!=0:
x=n//2
return(lists[x])
else:
x=(n+1)//2
y=(n+1)//2-1
return(lists[y],lists[x])
def sort(list):
# Sorted to a new list
sorted=[]
while len(list)>0:
min=list[0]
for i in list:
if i<min:
min=i
sorted.append(min)
list.remove(min)
return sorted
def sort2(lists):
#Sort in the same list
a=0
min=lists[a]
for i in range(a,len(lists)-1):
if i<min:
min=lists[i]
lists.remove(min)
lists.insert(a,min)
a=a+1
return lists
2. MATRICES
def determin(A):
#determinant of a matrix
D=0
i=0
while i<len(A[0]):
if len(A[0])==2:
D=A[0][0]*A[1][1]-A[1][0]*A[0][1]
break
else:
B=reduced(A,0,i)
D=D+A[0][i]*((-1)**(i+1))*determin(B)
i+=1
return D
def reduced(A,k,i):
#reduces the matrix by removing the row k and column i
B=[]
n=0
for p in range(len(A)):
newrow=[]
for q in range(len(A)):
if p==k or q==i:
n=n+1
else:
newrow.append(A[p][q])
B.append(newrow)
del B[k]
return B
def trans(A):
#transpose of a matrix method 1
def trans(m1):
r=len(m1)
c=len(m1[0])
tran= [ r*[0] for i in range(c) ]
for k in range(r):
for j in range(c):
tran[j][k]=m1[k][j]
return(tran)
def transpose(B):
#transpose of a matrix method 2
result=[]
c=0
l=len(B[0])
while l>0:
vec=[]
for row in B:
vec.append(row[c])
c=c+1
l=l-1
result.append(vec)
return result
def inver(A):
#inverse of a matrix
if determin(A)!=0:
B=[]
for i in range(len(A)):
newline=[]
for k in range(len(A[0])):
newline.append(((-1)**(k+i+1))*determin(reduced(A,i,k))/determin(A))
B.append(newline)
C=trans(B)
return C
else:
print('this matrix is singular')
def multmat(A,B):
#matrix product
nrowsA=len(A)
ncolsA=len(A[0])
nrowsB=len(B)
ncolsB=len(B[0])
mat=[]
row=[]
ele=0
for i in range(0,nrowsA,1):
row=[]
for k in range(0,ncolsB,1):
ele=0
for j in range(0,ncolsA,1):
ele=ele+A[i][j]*B[j][k]
row.append(ele)
mat.append(row)
return mat
def addmat(A,B):
#matrix addition
nrows=len(A)
ncol=len(A[0])
addmat=[]
for i in range(0,nrows,1):
addrow=[]
for k in range(0,ncol,1):
addrow.append(A[i][k]+B[i][k])
addmat.append(addrow)
return addmat
3.ADDRESS BOOK
def add_contact(contact,name,tag,number):
#To add a contact to a dictionary
if name in contact:
contact[name][tag]=number
else:
contact[name]={tag:number}
def del_contact(contact,name,tag=None):
#To delete a contact from the dictionary
if name in contact:
if tag!=None:
if tag in contact[name]:
del(contact[name][tag])
else:
print('not found')
else:
del(contact[name])
else:
print('not found')
def print_contact(contact,name,tag=None):
#To print a contact from the dictionary
if name in contact:
if tag!=None:
if tag in contact[name]:
print(contact[name][tag])
else:
print('not found')
else:
print(contact[name])
else:
print('not found')
def find_contact(contac,name):
#To find a contact from the dictionary
x=False
for i in contact:
if name in i:
print(i,contact[i])
x=True
if x==False:
print ('not found')
4. UTILITIES
fo=open('c:/Python31/phone numbers')
def list_to_phone_book(file):
#To convert list to a dictionary
phone_book={}
for l in file.readlines():
name,number=l.split()
phone_book[name]=number
return(phone_book)
file.close()
f=open('c:/Python31/phone book')
def load_contacts(file):
#To load contacts from a file to dictionary
phone_book={}
for l in file.readlines():
name,tag,number=l.split()
if name in phone_book:
phone_book[name][tag]=number
else:
phone_book[name]={tag:number}
return(phone_book)
file.close()
No comments:
Post a Comment