0% found this document useful (0 votes)
30 views

Cps Ass

The document discusses various functions and recursion problems including prime number checking, factorials, sums, differences, recursive sums, factorials, finding maximum, minimum and average of GDP data of countries, binary search, and various pattern printing problems using recursion. It also contains problems on files involving reading and writing data to new files.

Uploaded by

vimal007.x
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Cps Ass

The document discusses various functions and recursion problems including prime number checking, factorials, sums, differences, recursive sums, factorials, finding maximum, minimum and average of GDP data of countries, binary search, and various pattern printing problems using recursion. It also contains problems on files involving reading and writing data to new files.

Uploaded by

vimal007.x
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

FUNCTIONS AND RECURSION

1)

def prime(n):
for i in range(2,n+1):
if n%i==0:
return 1
else:
return 0
n=int(input('enter an integer'))
print(prime(n))

2)

def fac(i):
l=[]

for i in range(1,i+1):
if i%k==0:
l.append(k)
return l
i=int(input('Enter an integer:'))
print(fac(i))

3)

def fact(no):
s=1
for i in range(1,no+1):
s=s*i
return s
no=int(input('Enter an integer:'))

print(fact(no))

4)

def word(no):
S=''
s=str(no)
l=['zero','one','two','three','four','five','six','seven','eight','nine','ten']
for i in s:

index=int(i)
S=S+l[index]+' ‘
return S
no=int(input('Enter an integer:'))
print(word(no))

5)

def sum(l):
s=0
for i in l:
s=s+i
return s
l=eval(input('Enter an array:'))
print(sum(l))

6)
def diff(l):
d=0
sm=l[0]
la=l[0]
for i in l:
if i<sm:
sm=i
for j in l:
if j>la:
la=j
d=la-sm
return d
l=eval(input('Enter an array:'))
print(diff(l))

7)
def recsum(N):
s=0
if N<=0:
return 0
else:
s= N+recsum(N-1)
return s
N=int(input('Enter a number:'))
print(recsum(N))

8)
def recfac(N):
if N<=0:
return 1
else:
s=N*recfac(N-1)
return s
N=int(input('Enter a number:'))
print(recfac(N))

9)
def sum(l,n):
if n<=0:
return 0
else:
s=sum(l,n-1)+l[n-1]
return s
l=eval(input('Enter an array:'))
n=len(l)
print(sum(l,n))

10)
def recsum(no):
if no==0:
return 0
else:
return no%10 + recsum(no//10)
no=int(input('enter an integer'))
print(recsum(no))

11)
def recgcd(A,B):
if B==0:
return A
else:
return recgcd(B,A%B)
A=int(input('Enter an int:'))
B=int(input('Enter an int:'))
print(recgcd(A,B))

12)
def recpow(m,n):
if n==0:
return 1
else:
return m*recpow(m,n-1)
m=int(input('Enter an int for base:'))
n=int(input('Enter an int for exponent:'))
print(recpow(m,n))

13)
def recsm(l,n):
if n==1:
return l[0]
le=l[n-1]
m=recsm(l,n-1)
if le<m:
return le
else:
return m
l=eval(input('Enter an array:'))
n=len(l)
print('smallest is')
print(recsm(l,n))

14 to 19
def binarysearch(arr,low,up,n):
if low>up:
return -1
else:
mid=(low+up)//2
if arr[mid]==n:
return mid
elif n<arr[mid]:
return binarysearch(arr,low,mid-1,n)
else:
return binarysearch(arr,mid+1,up,n)
array=eval(input('enter a sorted array'))
n=int(input('Element to be searched'))
lower=0
upper=len(array)-1
print(binarysearch(array,lower,upper,n))
20) a
def pat(n):
if n>0:
pat(n-1)
print(n*'*')
pat(4)

20) b
def pat(n):
if n>0:
pat(n-1)
for i in range(1,n+1):
print(i,end=' ')
print()
pat(4)

20) c
def pat(n):
if n>0:
pat(n-1)
for i in range(1,n+1):
if i==n:
print((str(i)+" ")*i)
pat(4)

21)
def find(no):
if no== 0:
return 0
else:
return (no% 2 + 10 *find(int(no// 2)))
no=int(input('Enter a number'))
print(find(no))

22)
def rev(no):
if no==0:
return ''
else:
return str(no%10)+str(rev(no//10))
no=int(input('Enter a number:'))
print(rev(no))

23)
n=int(input('Enter number of countries:'))
l=[]
for i in range(n):
c=input('Enter country:')
yr1=float(input('Enter gdp of 1st year:'))
yr2=float(input('Enter gdp of 2nd year:'))
yr3=float(input('Enter gdp of 3rd year:'))
l.append([c,yr1,yr2,yr3])
print(l)

a)
def max(l):
maxi=l[0][1]
ind=0
for country in l:
if country[1]>maxi:
maxi=country[1]
ind=l.index(country)
return l[ind][0]
print(max(l))

b)
def min(l):
mani=l[0][2]
ind=0
for country in l:
if country[2]<mani:
mani=country[2]
ind=l.index(country)
return l[ind][0]
print(min(l))

c)
def avg(l):
L=[]
for country in l:
av=(country[1]+country[2]+country[3])/3
L.append([country[0],av])
best=L[0][1]
ind=0
for country in L:
if country[1]>best:
best=country[1]
ind=L.index(country)
return l[ind][0]
print(avg(l))

24)

S=[]
s1=[1,’VimalHariHar',14,'Annanagar']
s2=[2,'Darun',11,'omr']
s3=[3,'Rishikesh',14,'kottayam']
s4=[4,'Sathyaroopan',11,'mettupalayam']
s5=[5,'kapilan',12,'theni']
s6=[6,'balaji',12,'kochin']
s7=[7,'keshav',13,'hosur']
s8=[8,'abinav',13,'hosur']
s9=[9,'anushanth',12,'Bangalore']
s10=[10,'ashish',14,'Mumbai']
s11=[11,'pradeep',13,'Punjab']
S.extend([s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11])
#a)
def age(S):
print('Students whose age is 14 are')
for i in S:
if i[2]==14:
print(i[1])
age(S)

#b)
def roll(S):
print('Students whose roll no is even are')
for i in S:
if i[0]%2==0:
print(i[1])
roll(S)

#c)
def giv(S):
print('The details of given roll no is')
for i in S:
if i[0]==n:
print(i)
n=int(input('Enter roll no:'))
giv(S)

25)
s1=[1,’VimalHariHar',14,'Annanagar']
s2=[2,'Darun',11,'omr']
s3=[3,'Rishikesh',14,'kottayam']
s4=[4,'Sathyaroopan',11,'mettupalayam']
s5=[5,'kapilan',12,'theni']
s6=[6,'balaji',12,'kochin']
s7=[7,'keshav',13,'hosur']
s8=[8,'abinav',13,'hosur']
s9=[9,'anushanth',12,'Bangalore']
s10=[10,'ashish',14,'Mumbai']
s11=[11,'pradeep',13,'Punjab']
S.extend([s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11])

def bal(S):
print('The names of customers whose balance is less than $200 is')
for i in S:
if i[2]<200:
print(i[0])
bal(S)

def add(S):
print('Incremented values')
for i in S:
if i[2]>1000:
i[2]=i[2]+100
print(i)
add(S)

26)
f=open(‘pg.txt','r')
alp=0
con=0
vow=0
wor=0
sen=-1
r=f.read()
for letter in r:
if letter.isalpha():
alp+=1
if letter.lower() in 'aeiou':
vow+=1
else:
con+=1
for words in r.split():
wor+=1
for sent in r.split('.'):
sen+=1
print('Total alphabets is ',alp)
print('Number of consonants is ',con)
print('Number of vowels is ',vow)
print('Number of words is ',wor)
print('Number of sentences is ',sen)
f.close()

27)
f=open('pg.txt','r')
r=f.read()
a)
f1=open('new.txt','w')
f1.write(r)
f1.close()

b)
f2=open('new1.txt','w')
l=[]
for i in r:
if i.isalpha():
l.append(i)
for j in range(0,len(l),2):
f2.write(l[j])
f2.close()

c)
f3=open('new2.txt','w')
L=[]
for i in r.split():
if i.isalpha():
L.append(i)
for j in range(0,len(L),2):
f3.write(L[j])
f3.close()

d)
f4=open('new3.txt','w')
for i in r:
if i.isalpha():
if i.lower() not in 'aeiou':
f4.write(i)
f4.close()
f.close()

You might also like