Walchand Institute of Technology, Solapur Information Technology 2021-22 SEMESTER - I

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

WALCHAND INSTITUTE OF TECHNOLOGY,

SOLAPUR
INFORMATION TECHNOLOGY
2021-22 SEMESTER –I

Name-Kiran Chavan
Roll No-62

Python Assignment-5

1)Define a Python function remdup(l) that takes a nonempty list of integers l


and removes all duplicates in l, keeping only the first occurrence of each
number. For instance:

remdup([3,1,3,5]) : [3,1,5]

remdup([7,3,-1,-5]) : [7,3,-1,-5]

remdup([3,5,7,5,3,7,10]) : [3,5,7,10]

Code :
def remdup(l):

l1 = []

for item in l :

if item not in l1:

l1.append(item)

else:
continue

return l1

op1 = remdup([3,1,3,5])

op2 = remdup([7,3,-1,-5])

op3 = remdup([3,5,7,5,3,7,10])

print("Output lists are : \n {} \n {} \n {} ".format(op1,op2,op3))

Output:
2) A two dimensional matrix can be represented in Python row-wise, as a list of
lists: each

inner list represents one row of the matrix. For instance, the matrix

1234

5678

would be represented as [[1, 2, 3, 4], [5, 6, 7, 8]].

The transpose of a matrix converts each row into a column. The transpose of
the matrix

above is:

Write a Python function transpose(m) that takes as input a two dimensional


matrix m and

returns the transpose of m. The argument m should remain undisturbed by the


function.

Do not use modules like Numpy.

transpose([[1,4,9]]) : [[1], [4], [9]]

transpose([[1,3,5],[2,4,6]]) : [[1, 2], [3, 4], [5, 6]]

transpose([[1,1,1],[2,2,2],[3,3,3]]) : [[1, 2, 3], [1, 2, 3], [1, 2, 3]]

Code :
l1 = [ ]

def transpose(m):

for i in range(len(m[0])):

l2 = [ ]

for j in range(len(m)):

l2.append(m[j][i])

l1.append(l2)
return l1

op = transpose([[1,4,9]])

print("Transpose of given matrix is :\n",op)

Output:
3)Write a higher order function, sum_all() to calculate the sum of elements in a
list.

sum_all() – It accepts a list and calculates the sum of the elements in the list
based on the

conditions being checked in the lambda expressions passed to it.

Only those values satisfying the condition should be included in the sum.

Write the following lambda expressions.

greater: To check whether a given number is greater than 10.

divide: To check whether a given number is divisible by 10 and not greater


than 100.

range_of_values: To check whether a given number is between 25 and 50


(Both inclusive).

Input : [25,26,27,28,29,30,147,187]

Output : 499

30

165

Code :
def sum_all(l,lambda_expr):
print(sum(list(filter(lambda_expr,l))))
l = [25,26,27,28,29,30,147,187]
sum_all(l,lambda x : x>10)
sum_all(l,lambda x : x%10==0 and x<=100)
sum_all(l,lambda x : x>=25 and x<=50)
Output :

4) Input number (n) from user and create sample_data = range(1,n)

Create two functions: odd() and even()

The function even() returns a list of all the even numbers from sample_data

The function odd() returns a list of all the odd numbers from sample_data

Create a function sum_of_numbers() which will accept the sample data and/or
a

function.

If a function is not passed, the sum_of_numbers() should return the sum of all
the

numbers from sample_data

If a function is passed, the sum_of_numbers() should return the sum of


numbers
returned from the function passed.

Code :
def even(l):

return list(filter(lambda x : x%2==0,l))

def odd(l):

return list(filter(lambda x : x%2!=0,l))

def sum_of_numbers(n, fun_name=None):

sample_data = range(1,n)

if fun_name == even:

return sum(even(sample_data))

elif fun_name == odd:

return sum(odd(sample_data))

else:

return sum(sample_data)

n= int(input())

op = sum_of_numbers(n,even)

print("Sum of Numbers is : ",op)

Output:

You might also like