0% found this document useful (0 votes)
38 views1 page

08 SubhasishBagchi Day2

The document provides Python code solutions to various list problems: 1. Find the average and standard deviation of a user-input list 2. Reverse a user-input list 3. Count the occurrences of an element in a user-input list 4. Remove empty lists from a user-input list 5. Break a user-input list into chunks of a given size 6. Check if the difference between adjacent digits in a list is 1 7. Find the closest pair of elements to the kth index element in a tuple

Uploaded by

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

08 SubhasishBagchi Day2

The document provides Python code solutions to various list problems: 1. Find the average and standard deviation of a user-input list 2. Reverse a user-input list 3. Count the occurrences of an element in a user-input list 4. Remove empty lists from a user-input list 5. Break a user-input list into chunks of a given size 6. Check if the difference between adjacent digits in a list is 1 7. Find the closest pair of elements to the kth index element in a tuple

Uploaded by

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

In 

[ ]:
1. Write a program to find the average of the elements in the List. Also find the standard

deviation of these elements.

In [1]:
import math

# Taking user input for list-----------

n=int(input("enter how many elemesnts ="))

list1=[]

temp=0

for i in range(n):

list1.append(int(input("please enter the values into the list =")))

sum_of_the_list=sum(list1)

length_of_the_list=len(list1)

average=sum_of_the_list/length_of_the_list

for i in list1:

temp=temp+math.pow((i-average),2)

sd=math.pow((temp/len(list1)),0.5)

print("average of the given list is =",average)

print("standered deviation of the given list is =",sd)

enter how many elemesnts =10

please enter the values into the list =1

please enter the values into the list =2

please enter the values into the list =3

please enter the values into the list =4

please enter the values into the list =5

please enter the values into the list =6

please enter the values into the list =7

please enter the values into the list =8

please enter the values into the list =9

please enter the values into the list =10

average of the given list is = 5.5

standered deviation of the given list is = 2.8722813232690143

In [ ]:
2. Write a python program to reverse a List.

In [2]:
import ast

# Taking user input for list using while loop -----------

list1=[]

while True:

element=ast.literal_eval(input("enter the list elements one by one = "))


list1.append(element)

choice=input("want to stop? if yes enter yes or press any key! =")

if choice == "yes":

break

print("Before reverse the list =",list1)

list1.reverse()

print("After reverse the list =",list1)

enter the list elements one by one = 11

want to stop? if yes enter yes or press any key! =

enter the list elements one by one = 22

want to stop? if yes enter yes or press any key! =

enter the list elements one by one = 33

want to stop? if yes enter yes or press any key! =

enter the list elements one by one = 44

want to stop? if yes enter yes or press any key! =

enter the list elements one by one = 55

want to stop? if yes enter yes or press any key! =yes

Before reverse the list = [11, 22, 33, 44, 55]

After reverse the list = [55, 44, 33, 22, 11]

In [ ]:
3. Write a python program to count occurrences of an element in a List.

In [3]:
# Python code to count the number of occurrences

def countX(lst, x):

count = 0

for ele in lst: # using in given element search in list

if (ele == x):

count = count + 1

return count

# Driver Code

# Taking inputs from user using while-loop -----------

list1 = []

while True:

element=int(input("enter the numbers into the list ="))

list1.append(element)

choice=input("want to stop insert numbers into the list? if yes enter yes otherwise press any key! = ")

if choice == "yes":

break

x = int(input("Enter the number you want to see how many times its occur ="))

print('{} has occurred {} times'.format(x, countX(list1, x)))

enter the numbers into the list =11

want to stop insert numbers into the list? if yes enter yes otherwise press any key! =

enter the numbers into the list =22

want to stop insert numbers into the list? if yes enter yes otherwise press any key! =

enter the numbers into the list =33

want to stop insert numbers into the list? if yes enter yes otherwise press any key! =

enter the numbers into the list =22

want to stop insert numbers into the list? if yes enter yes otherwise press any key! =

enter the numbers into the list =22

want to stop insert numbers into the list? if yes enter yes otherwise press any key! =

enter the numbers into the list =22

want to stop insert numbers into the list? if yes enter yes otherwise press any key! =

enter the numbers into the list =11

want to stop insert numbers into the list? if yes enter yes otherwise press any key! = yes

Enter the number you want to see how many times its occur =22

22 has occurred 4 times

In [ ]:
4. Write a python program to remove empty List from List

In [5]:
# Taking input from user for list & empty list---------------

n=int(input("Enter the number of elements: "))


list=[]

for i in range(n):

choice=int(input("Enter 1 for insert elements into the list or Enter 2 for insert an empty list: "))

if(choice==1):

el=int(input("Enter integer: "))

list.append(el)

if(choice==2):

el=[]

list.append(el)

print("user define list: ",list)

while([] in list): # if empty list in list then using remove function we delete it

list.remove([])

print("List after empty list removal : ", list)

Enter the number of elements: 8

Enter 1 for insert elements into the list or Enter 2 for insert an empty list: 1

Enter integer: 12

Enter 1 for insert elements into the list or Enter 2 for insert an empty list: 1

Enter integer: 13

Enter 1 for insert elements into the list or Enter 2 for insert an empty list: 2

Enter 1 for insert elements into the list or Enter 2 for insert an empty list: 1

Enter integer: 14

Enter 1 for insert elements into the list or Enter 2 for insert an empty list: 2

Enter 1 for insert elements into the list or Enter 2 for insert an empty list: 1

Enter integer: 15

Enter 1 for insert elements into the list or Enter 2 for insert an empty list: 1

Enter integer: 16

Enter 1 for insert elements into the list or Enter 2 for insert an empty list: 2

user define list: [12, 13, [], 14, [], 15, 16, []]

List after empty list removal : [12, 13, 14, 15, 16]

In [ ]:
5. Write a python program to break a List into chunks of size N.

In [6]:
# takin input from user ------------

n=int(input("enter how many elemesnts ="))

l=[]

for i in range(n):

l.append(int(input("please enter the values into the list =")))

n =int(input("please enter the numbers of chunks ="))

# using list comprehension

x = [l[i:i + n] for i in range(0, len(l), n)]

print("Chunks of list: ",x)

enter how many elemesnts =10

please enter the values into the list =1

please enter the values into the list =2

please enter the values into the list =3

please enter the values into the list =4

please enter the values into the list =5

please enter the values into the list =6

please enter the values into the list =7

please enter the values into the list =8

please enter the values into the list =9

please enter the values into the list =10

please enter the numbers of chunks =2

Chunks of list: [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]

In [ ]:
6. Write a python program to check the number where the difference between every

adjacent digit should be 1 using List.

In [8]:
# taking inputs from user----

n=int(input("enter how many elemesnts ="))

t=[]

for i in range(n):

t.append(int(input("please enter the values into the list =")))

# using list comprehension

lst=[j-i for i, j in zip(t[:-1], t[1:])]

if all(ele == lst[0] for ele in lst):

print("the given list is Adjacent")

else:

print("the given list is not Adjacent")

enter how many elemesnts =4

please enter the values into the list =5

please enter the values into the list =4

please enter the values into the list =3

please enter the values into the list =2

the given list is Adjacent

In [ ]:
7. Write a python program to find the closest pair to kth index element in Tuple.

In [1]:
import sys

def nano(p):

print("Enter values with comma...")

return tuple(map(int,input().split(",")))

lis=list(map(nano,list(range(int(input("Enter the no. of element: "))))))

print(lis)

key = tuple(map(int,input("Please enter the tuple key value with comma: ").split(",")))

K = int(input("please enter k value for 1 or 2: "))

min_dif,res=sys.maxsize,None

for idx, val in enumerate(lis): #it takes one tuple

if K<=len(key) and K<=len(val):

dif = abs(key[K-1] - val[K-1]) #then kth index value of the tuple and the given tuple are substracted

if dif<=min_dif: #if the difference is less than the minimum difference

min_dif,res = dif,idx #the min diff is changed to the difference

if res!=None:

print(f'Pair closet to the {K}th index of {key} : {lis[res]}')

Enter the no. of element: 4

Enter values with comma...

11,22
Enter values with comma...

33,44
Enter values with comma...

55,66
Enter values with comma...

77,88
[(11, 22), (33, 44), (55, 66), (77, 88)]

Please enter the tuple key value with comma: 22,33

please enter k value for 1 or 2: 2

Pair closet to the 2th index of (22, 33) : (33, 44)

In [ ]:
8. Write a python program to find all pair combinations of 2 Tuples.

In [2]:
# taking user input for tuples

test_tuple1 = tuple(map(int,input("Please enter the first tuple value with comma: ").split(",")))

test_tuple2 = tuple(map(int,input("Please enter the second value with comma: ").split(",")))

# printing original tuples

print("The original tuple 1 : " + str(test_tuple1))


print("The original tuple 2 : " + str(test_tuple2))

# All pair combinations of 2 tuples

# Using list comprehension

res = [(a, b) for a in test_tuple1 for b in test_tuple2]

res = res + [(a, b) for a in test_tuple2 for b in test_tuple1]

# printing result

print("The filtered tuple : " + str(res))

Please enter the first tuple value with comma: 1,2

Please enter the second value with comma: 3,4

The original tuple 1 : (1, 2)

The original tuple 2 : (3, 4)

The filtered tuple : [(1, 3), (1, 4), (2, 3), (2, 4), (3, 1), (3, 2), (4, 1), (4, 2)]

In [ ]:
9. Write a python program to create a List of Tuples from given List having number and

its cube in each Tuple.

In [3]:
# creating a list

n=int(input("how many elements you to create in the list: "))

list1 = []

for i in range(n):

list1.append(int(input("Enter the elements into the list: ")))


# using list comprehension to iterate each

# values in list and create a tuple as specified

res = [(val, pow(val, 3)) for val in list1]

# print the result

print(res)

how many elements you to create in the list: 4

Enter the elements into the list: 2

Enter the elements into the list: 3

Enter the elements into the list: 4

Enter the elements into the list: 5

[(2, 8), (3, 27), (4, 64), (5, 125)]

In [ ]:
10. Write a python program to generate the Fibonacci Series up to Nth term and store in

the Tupple.

1 + 1 + 2 + 3 + 5 + 8 + 13 +…………+ Nth term

In [4]:
def get_fibonacci(n):

a, b = 0, 1

fibonacci_sequence = (a, b)

for i in range(n-2):

a, b = b, a+b

fibonacci_sequence += (b,)

print(fibonacci_sequence)

x=int(input("Enter the Nth term:- "))

get_fibonacci(x)

Enter the Nth term:- 10

(0, 1, 1, 2, 3, 5, 8, 13, 21, 34)

In [ ]:
11. Write a python program to find second smallest and second largest number in Tupple.

In [5]:
# Taking input from user for tuple---------

tup1=tuple(map(int,input("Please enter the tuple value with comma: ").split(",")))

min=tup1[0]

max=tup1[0]

min2=None

max2=None

for i in tup1[1:]:

if i > max:

max2=max

max=i

elif max2==None or max2 < i:

max2=i

if i < min:

min2 = min

min=i

elif min2 == None or min2 > i:

min2 = i

print("Entered tuple is: ",tup1)

print("Second Smallest is: ",min2)

print("Second Largest is: ",max2)

Please enter the tuple value with comma: 11,22,33,44,55,66,77,88,99

Entered tuple is: (11, 22, 33, 44, 55, 66, 77, 88, 99)

Second Smallest is: 22

Second Largest is: 88

In [ ]:
12. Write a program to 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 Tupple.

In [6]:
n=int(input("Enter the number of elements for the list: "))

l1=[]

for i in range(n):

l1.append(int(input("Enter the elements into the list: ")))

flag=0

key=int(input("Enter the number you want to search from list:"))

l1.sort(reverse=True)

print("Given tuple in descending order: ",tuple(l1))

for i in range(len(l1)):

if(key==l1[i]):

flag=1

break

else:

flag=0

if(flag==1):

print((f"{key} found in the list at position {i}"))

else:

print(f"{key} is not found")

Enter the number of elements for the list: 5

Enter the elements into the list: 11

Enter the elements into the list: 22

Enter the elements into the list: 33

Enter the elements into the list: 44

Enter the elements into the list: 55

Enter the number you want to search from list:33

Given tuple in descending order: (55, 44, 33, 22, 11)

33 found in the list at position 2

In [ ]:
13. Write a python program to add a list of elements to a given Set

In [2]:
temp=set()

n=int(input("Enter number of elements for set: "))

for i in range(n):

el=int(input("Enter the elements into the set: "))

temp.add(el)

# Taking inputs from user for the list ----------

n=int(input("Enter the numbers of elements into the list: "))

list1=[]

for i in range (n):

list1.append(int(input("Enter the elements into list: ")))

print("Before adding list1:",temp)

temp.update(list1) #using update function we can add list in tuple

print("After adding list:",temp)

Enter number of elements for set: 4

Enter the elements into the set: 1

Enter the elements into the set: 2

Enter the elements into the set: 3

Enter the elements into the set: 4

Enter the numbers of elements into the list: 4

Enter the elements into list: 5

Enter the elements into list: 6

Enter the elements into list: 7

Enter the elements into list: 8

Before adding list1: {1, 2, 3, 4}

After adding list: {1, 2, 3, 4, 5, 6, 7, 8}

In [ ]:
14. Write a python program to remove the duplicate item from a List using Set

In [1]:
n=int(input("Enter the numbers of elements into the list: "))

list1=[]

for i in range (n):

list1.append(int(input("Enter the elements into list: ")))

print("Before removing the duplicate items in the list: ",list1)

dup_items = set()

uniq_items = []

for x in list1:

if x not in dup_items:

uniq_items.append(x)

dup_items.add(x)

print("After removing the duplicate items in the set: ",dup_items)

Enter the numbers of elements into the list: 5

Enter the elements into list: 11

Enter the elements into list: 22

Enter the elements into list: 11

Enter the elements into list: 33

Enter the elements into list: 44

Before removing the duplicate items in the list: [11, 22, 11, 33, 44]

After removing the duplicate items in the set: {33, 11, 44, 22}

In [ ]:
15. Write a python program to returns a new set with all items from both sets by removing

duplicates.

In [2]:
# Taking input from user for set1 ----------------

temp=set()

n=int(input("Enter number of elements for set1: "))


for i in range(n):

el=int(input("Enter the elements into the set: "))

temp.add(el)

# Taking input from user for set2----------------------

temp1=set()

m=int(input("Enter number of elements for set2: "))


for i in range(m):

el=int(input("Enter the elements into the set: "))

temp1.add(el)

print("Before removing duplicate items from set1:",temp)

print("Before removing duplicate items from set2:",temp1)

# taking a new set

set3=set()

for i in temp: # iterate for first set

for y in temp1: # iterate for second set

if(i==y):

continue

else:

set3.add(i)

set3.add(y)

print("After removing duplicate items from both sets: ",set3)

Enter number of elements for set1: 4

Enter the elements into the set: 11

Enter the elements into the set: 22

Enter the elements into the set: 33

Enter the elements into the set: 44

Enter number of elements for set2: 4

Enter the elements into the set: 55

Enter the elements into the set: 66

Enter the elements into the set: 77

Enter the elements into the set: 22

Before removing duplicate items from set1: {33, 11, 44, 22}

Before removing duplicate items from set2: {66, 77, 22, 55}

After removing duplicate items from both sets: {33, 66, 11, 44, 77, 22, 55}

In [ ]:
16. Write a python program to check if a Set is a subset of another Set.

In [3]:
# Taking input from user for set1----------------------

temp=set()

n=int(input("Enter number of elements for set1: "))


for i in range(n):

el=int(input("Enter the elements into the set: "))

temp.add(el)

# Taking input from user for set2----------------------

temp1=set()

m=int(input("Enter number of elements for set2: "))


for i in range(m):

el=int(input("Enter the elements into the set: "))

temp1.add(el)

print("set1:",temp)

print("set2:",temp1)

c=0

for i in temp1: # iterate for subset

if(i in temp): # if all item of subset in super set then pass it

pass

else: # otherwise increase c value by 1

c=1

if(c==0):

print("set2 is subset of set1")

else:

print("set2 is not subset of set1")

Enter number of elements for set1: 3

Enter the elements into the set: 10

Enter the elements into the set: 20

Enter the elements into the set: 30

Enter number of elements for set2: 2

Enter the elements into the set: 10

Enter the elements into the set: 20

set1: {10, 20, 30}

set2: {10, 20}

set2 is subset of set1

In [ ]:
17. Write a python program to check for pair in the List with sum as x using Set.

In [4]:
#pre-requisite

import itertools

# all subsets of given size of a set

def findsubsets(s, n):

return list(itertools.combinations(s, n))

#input

list1=set(map(int,input("Enter list elem with commas: ").split(',')))

x=int(input("Enter the sum: "))

#geting pair equivalent to sum

n = 2 # for pair n=2

all_s=[set(i) for i in findsubsets(list1, n)]

for i in all_s:

if(sum(i)==x):

print(i)

Enter list elem with commas: 1,2,3,4,5,6,7,8,9

Enter the sum: 10

{1, 9}

{8, 2}

{3, 7}

{4, 6}

In [ ]:
18. Write a python program to make a Dictionary with n number of odd items.

In [5]:
num=int(input("Please enter the range to print odd numbers in dictionary : :"))

myDic={}

for i in range (1,num+1):

myDic[i]=2*i-1

print("Dictionary =", myDic)

Please enter the range to print odd numbers in dictionary : :10

Dictionary = {1: 1, 2: 3, 3: 5, 4: 7, 5: 9, 6: 11, 7: 13, 8: 15, 9: 17, 10: 19}

In [ ]:
19. Write a python program to make a Dictionary with n number of Prime Number.

In [6]:
def PrimeChecker(a):

# Checking that given number is more than 1

if a > 1:

# Iterating over the given number with for loop

for j in range(2, int(a/2) + 1):

# If the given number is divisible or not

if (a % j) == 0:

return 0

break

# Else it is a prime number

else:

return 1

else:

return 0

# Taking an input number from the user

num=int(input("Please enter the number of elements :"))


myDic={}

j=1

for i in range (1,num+1):

b = PrimeChecker(i)

if b==1:

myDic[j]=i

j=j+1

print("Dictionary =", myDic)

Please enter the number of elements :20

Dictionary = {1: 2, 2: 3, 3: 5, 4: 7, 5: 11, 6: 13, 7: 17, 8: 19}

In [ ]:
20. Write a python program to calculate minimum delete operations to make all elements

of List same (Input: number = [1, 3, 1, 1, 2, 1] Output: 2)

In [12]:
l1=[]

d1={}

count=0

n=int(input("Enter the number of elements in list: "))

print("Enter the elements one by one: ")

for i in range(n):

temp=int(input())

l1.append(temp) #taking inputs

for i in range(n):

d1[l1[i]]=l1[i] #creating a dictionary containing unique elements of the list

for i in d1.values():

if l1.count(i)==1:

count=count+1 #counting the minimum number of delete operation

print("Minimum delete operations: "+str(count))

Enter the number of elements in list: 6

Enter the elements one by one:

Minimum delete operations: 2

You might also like