Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

#p2.

py
import statistics
li=[]

def stat(n):
for i in range(n):
ele=int(input("enter element:"))
li.append(ele)
print("The given list is",li)
print("mean:",statistics.mean(li))
print("median:",statistics.median(li))
if len(li)==len(set(li)): #set() is used to create a list with unique vaues
print("In the given list all elements are unique")
else:
print("mode:",statistics.mode(li))
n=int(input("enter no of terms:"))
stat(n)

Output:
enter no of terms:5
enter element:35
enter element:76
enter element:90
enter element:23
enter element:43
The given list is [35, 76, 90, 23, 43]
mean: 53.4
median: 43
In the given list all elements are unique
____________________________________________________________________________
____________________________________________

#p3.py
def fact(n):
f=1
for i in range(1,n+1):
f*=i
return f

def fibo(n):
f1,f2=-1,1
for i in range(n):
f3=f1+f2
print(f3,end=" ")
f1=f2
f2=f3

while True:
print(''' MENU DRIVEN CODE - FACTORIAL/FIBONACCI NUMBER
1.Factorial Of A Number
2.Generate Fibonacci Series upto 'n' numbers
3.Exit''')

ch=int(input("Enter your choice:"))


if ch==1:
num=int(input('Enter the number to find its factorial :'))
print('The Factorial of',num,'is ',fact(num))
elif ch==2:
num=int(input('Enter the number of terms to genarate fibonacci series:'))
fibo(num)
elif ch==3:
break
else: print("Invalid choice")

output:
MENU DRIVEN CODE - FACTORIAL/FIBONACCI NUMBER
1.Factorial Of A Number
2.Generate Fobonacci Series upto 'n' numbers
3.Exit
Enter your choice:1
Enter the number to find its factorial :5
The Factorial of 5 is 120
MENU DRIVEN CODE - FACTORIAL/FIBONACCI NUMBER
1.Factorial Of A Number
2.Generate Fobonacci Series upto 'n' numbers
3.Exit
Enter your choice:2
Enter the number of terms to genarate fibonacci series:5
0 1 1 2 3 MENU DRIVEN CODE - FACTORIAL/FIBONACCI NUMBER
1.Factorial Of A Number
2.Generate Fobonacci Series upto 'n' numbers
3.Exit
Enter your choice:3
____________________________________________________________________________
_____________________________________________
#p4.py
def rev_str(s):
s1=''
if len(s)==0:
return s
else:
for i in range(len(s)-1,-1,-1):
s1+=s[i]
return s1

def binary_search(alist,num):
start=0
end=len(alist) - 1
mid = (start + end)//2
while start<=end:
if num==alist[mid]:
print(num,"is found at ",mid+1,"position")
break
if num>=alist[mid]:
start=mid+1
mid=(start+end)//2
else:
end=mid-1
mid=(start+end)//2
else:
print(num,"is not present in the given list")

print(''' MENU
1.Binary Search in a list of integers
2.Reversing a string
3.Exit''')
while True:
ch=int(input('Enter your choice : '))
if ch==1:
alist=eval(input('Enter numbers for the list in ascending order: '))
n=int(input("Enter the number to be searched:"))
binary_search(alist,n)
elif ch==2:
str=input("Enter a string")
print("original string",str)
print("Reversed string",rev_str(str))
elif ch==3:
break
else:
print('Invalid choice')

Output:
MENU
1.Binary Search in a list of integers
2.Reversing a string
3.Exit
Enter your choice : 1
Enter numbers for the list in ascending order: [-23,-12,-1,5,10,12,17]
Enter the number to be searched:-1
-1 is found at 3 position
Enter your choice : 2
Enter a string: vacation
original string: vacation
Reversed string: noitacav
Enter your choice : 3
____________________________________________________________________________
________________________________________
#p5
import random
def findprime(l,n):
flag,l1=0,[]
for i in l:
if i==0 or i==1: print(i,"Neither prime nor composite")
else:
for j in range(2,i//2+1):
if i%j==0: break
else:
l1.append(i)
flag=1
if flag==0: print('No prime number in the list')
else: print('The prime numbers are',l1)
#first and second largest element
def firstsecond(l,n):
l=list(set(l))
l.sort()
print("The Largest Element in this List is : ", l[-1])
print("The Second Largest Element in this List is : ",l[-2])
#generating random number
def genrandom():

n=int(input("enter no of terms:"))
x=int(input('Enter x:'))
y=int(input('Enter y:'))
l=[]
for i in range(n):
a=random.randint(x,y)
l.append(a)
print(l)
findprime(l,n)
firstsecond(l,n)
#main
genrandom()

Output:
enter no of terms:10
Enter x:30
Enter y:300
[184, 203, 83, 203, 44, 60, 114, 277, 241, 110]
The prime numbers are [83, 277, 241]
The Largest Element in this List is : 277
The Second Largest Element in this List is : 241
____________________________________________________________________________
_______________________________________

You might also like