Professional Documents
Culture Documents
Day2 2182002
Day2 2182002
1. Average of elements in the list. Also find the standard deviation of these elements
In [ ]:
2. Reverse a list.
In [ ]:
n=int(input("Enter size for list: "))
print("Enter elements one by one:")
l=[]
s=0
for i in range(n):
a=int(input("Enter number: "))
l.append(a)
rev=l[::-1]
print("Reversed list: ",rev)
In [ ]:
In [ ]:
n=int(input("Enter size for list: "))
print("Enter elements one by one:")
l=[]
s=0
for i in range(n):
a=int(input("Enter number: "))
l.append([])
l.append(a)
l.append([])
res=[e for e in l if e!=[]]
print("List after removing empty list:",str(res))
In [ ]:
n=int(input("Enter size for list: "))
print("Enter elements one by one:")
l=[]
s=0
for i in range(n):
a=int(input("Enter number: "))
l.append(a)
N=int(input("Enter size of N: "))
fin=[l[i:i + n] for i in range(0, len(l), n)]
print(fin)
6. Check the number where the difference between every adjacent digit should be 1 using List.
In [ ]:
n=int(input("Enter size for list: "))
print("Enter elements one by one:")
l=[]
for i in range(n):
a=int(input("Enter number: "))
l.append(a)
k=0
for i in range(n-1):
if ((abs(l[i]-l[i+1]))!=1):
print("Discrepancy in ",l[i],"and",l[i+1])
break
else:
k=1
if (k==1):
print("No discrepancy present, difference in every adjacent digit is 1")
In [ ]:
my_list=[(5,6),(66,76),(21,35),(99,8),(0,9)]
print("The list is\n ",my_list)
my_tup=(17,23)
print("The tuple is\n ",my_tup)
k=1
print("The value of k has been initialised to",k)
min_diff,my_result=999999999,None
for idx,val in enumerate(my_list):
diff=abs(my_tup[k-1]-val[k-1])
if (diff<min_diff):
min_diff,my_result=diff,idx
print("The tuple nearest to k-th index element is",end=' ')
print(my_list[my_result])
The list is
[(5, 6), (66, 76), (21, 35), (99, 8), (0, 9)]
The tuple is
(17, 23)
The value of k has been initialised to 1
The tuple nearest to k-th index element is (5, 6)
The tuple nearest to k-th index element is (21, 35)
In [ ]:
#List 1
n1=int(input("Enter size for list: "))
print("Enter elements one by one:")
l1=[]
for i in range(n1):
a=int(input("Enter number: "))
l1.append(a)
l1=tuple(l1)
#List 2
n2=int(input("Enter size for list: "))
print("Enter elements one by one:")
l2=[]
for i in range(n2):
a=int(input("Enter number: "))
l2.append(a)
l2=tuple(l2)
print("Tuple 1:\n ",l1,"\n\nTuple 2:\n ",l2)
#Pair formation using list comprehension
x=[(p,q) for p in l1 for q in l2]
x=x+[(p,q) for p in l2 for q in l1]
print("Resultant tuple:\n ",x)
Tuple 2:
(6, 7)
Resultant tuple:
[(4, 6), (4, 7), (5, 6), (5, 7), (6, 4), (6, 5), (7, 4), (7, 5)]
3. Create a List of Tuples from given List having number and its cube in each Tuple.
In [ ]:
n=int(input("Enter size for list: "))
print("Enter elements one by one:")
l=[]
for i in range(n):
a=int(input("Enter number: "))
l.append(a)
x=[(i,pow(i,3)) for i in l]
print(x)
4. Generate the Fibonacci Series up to Nth term and store in the Tuple.
In [ ]:
print("Fibonacci series\n\n\n")
n=int(input("Enter a value for n: "))
a,b,c=0,1,0
for i in range(1,n+1):
print(c,end=' ')
a,b,c=b,c,a+b
Fibonacci series
In [ ]:
def big(a):
l=len(a)
import math
def smol(a):
n=len(a)
if (n<2):
print("Invalid Input!")
return
first=second=math.inf
for i in range(n):
if (a[i]<first):
second=first
first=a[i]
elif(a[i]<second and a[i]!=first):
second=a[i]
if (second==math.inf):
print("The smallest element is",first)
print("No second smallest element here")
else:
print("The smallest element is",first,"and second smallest element is",second)
def big(a):
l=len(a)
if (l<2):
print("Invalid Input!")
return
first=second=-2454635434
for i in range(l):
first=max(first,a[i])
for i in range(l):
if (a[i]!=first):
second=max(second,a[i])
if (second==-2454635434):
print("The largest element is",first)
print("There is no second largest element")
else:
print("The largest element is",first,"and second largest element is",second)
6. Sort a set of n numbers in descending order and then search for a given number (taken from user) in the set
6. Sort a set of n numbers in descending order and then search for a given number (taken from user) in the set
of sorted numbers in the Tuple.
In [ ]:
n=int(input("Enter size of list: "))
l=[]
for i in range(n):
a=int(input("Enter a number: "))
l.append(a)
print("Original tuple:\n ",)
l.sort(reverse=True)
l=tuple(l)
print("Sorted tuple in descending order:\n ",l)
x=int(input("Enter search element: "))
f=0
for i in range(n):
if (x==l[i]):
print("Number",x,"found at position number",(i+1))
f=1
break
if (f!=1):
print("Search number",x,"not found in the tuple")
In [ ]:
sampset={1,2,3,4}
l=[5,6,7,8,9]
sampset.update(l)
print(sampset)
{1, 2, 3, 4, 5, 6, 7, 8, 9}
In [ ]:
k=[1,2,3,4,4,4,3,2,5]
print("Original list:\n ",k)
k=list(set(k))
print("List after removing duplicates:\n ",k)
Original list:
[1, 2, 3, 4, 4, 4, 3, 2, 5]
List after removing duplicates:
[1, 2, 3, 4, 5]
3. Return a new set with all items from both sets by removing duplicates.
In [ ]:
In [ ]:
#Since set is a DS that can NEVER contain duplicates, this question is #invalid!
#ERROR 404: Invalid question
In [ ]:
A = {1, 2, 3}
B = {1, 2, 3, 4, 5}
C = {1, 2, 4, 5}
# Returns True
print(A.issubset(B))
# Returns False
print(B.issubset(A))
# Returns False
print(A.issubset(C))
# Returns True
print(C.issubset(B))
True
False
False
True
In [ ]:
#INCOMPLETE PROGRAM / WRONG - SET NOT USED
def chkPair(A, size, x) :
for i in range (0, size - 1) :
for j in range (i + 1, size):
if (A[i] + A[j] == x) :
print("Pair with a given sum " , x , " is (" , A[i] , ", " , A[j] , ")")
return True
return False
if (chkPair(l, n, x)) :
print("Valid pair exists")
else :
print("No valid pair exists for " , x )
In [ ]:
class my_dictionary(dict):
def __init__(self):
self = dict()
def add(self, key, value):
self[key] = value
d=my_dictionary()
while(True):
n=int(input("Enter a positive value for n: "))
if (n>=1):
break
i=0
k=-11
while(i<=n):
if (k%2!=0):
d.add(k,"ODD")
i+=1
k+=2
print(d)
In [ ]:
from sympy import *
class my_dictionary(dict):
def __init__(self):
self = dict()
def add(self, key, value):
self[key] = value
dict_obj = my_dictionary()
while(True):
n=int(input("Enter a positive value for n: "))
if (n>=1):
break
i=0#loop control var for n
k=2#prime numbers
while(i<=n):
if (isprime(k)):
dict_obj.add(k,"PRIME")
i+=1
k+=1
print(dict_obj)
8. Calculate minimum delete operations to make all elements of List same (Input: number = [1, 3, 1, 1, 2, 1]
Output: 2)
In [ ]: