Beruflich Dokumente
Kultur Dokumente
Problem No# 1
Create a function with one argument that accepts a list which contains strings, integers or floats, and then
returns two lists one containing only strings and other list containing only integers or floats from the given list.
In [1]:
# Solution
def separator(list):
str_list =[]
int_list= []
for i in list:
if isinstance(i,str):
str_list.append(i)
else:
int_list.append(i)
print('list of integers or floats is', int_list)
print('list of strings is', str_list)
l = [3,'one','two',2,'three',1]
separator(l)
In [2]:
# Alternative Solution
def separator(list):
str_list =[]
int_list= []
for i in list:
if type(i)==str:
str_list.append(i)
else:
int_list.append(i)
print('list of integers or floats is', int_list)
print('list of strings is', str_list)
l = [3,'one','two',2,'three',1]
separator(l)
Problem No#2
Create a function with name “rem” that accepts two arguments and returns the remainder when 1st argument is
divided by the 2nd argument.
In [23]:
def rem(m,n):
r=(m/n-m//n)*n
return int(r)
In [24]:
print(rem(5,2))
print(rem(4,7))
print(rem(-5,4))
1
4
3
Problem No#3
Create a function with name “maxx” that accepts a list of real numbers and returns the maximum number in it.
In [30]:
def maxx(list):
mx=list[0]
for i in list:
if i>mx:
mx=i
return mx
In [31]:
l = [3,5,-4,0,2]
maxx(l)
Out[31]:
Problem No#4
Create a function with name “nthrt” that take two arguments (positive real numbers), where 1st argument is a
number you want to calculate the root of and the 2nd number is to be the nth root of the 1st number.
In [33]:
def nthrt(a,b):
return a**(1/b)
In [35]:
nthrt(4,3)
Out[35]:
1.5874010519681994
Problem No#5
Create a python function which implements the piece-wise function given below, with name “f” and print f(0),
f(1), and f(-1) in the main program.
𝑓(𝑥) = { 𝑠𝑖𝑛(𝑥)/𝑥 𝑖𝑓 𝑥 ≠ 0
{1 𝑖𝑓 𝑥 = 0
In [38]:
def f(x):
from math import sin
if x!=0:
return sin(x)/x
else:
return 1
In [43]:
print(f(0))
print(f(1))
print(f(-1))
1
0.8414709848078965
0.8414709848078965
Problem No#6
Create a function with argument of a string and then create two separate lists of distinct vowels and distinct
consonants.
In [50]:
def sep(s):
v = []
c = []
vowels = 'aeiouAEIOU'
for i in s:
if i in vowels:
v.append(i)
else:
c.append(i)
print('list of vowels is', v)
print('list of consonants is', c)
In [51]:
sep('Education')
Problem No#7
Write a function that takes two lists as arguments and then returns a list so that its elements are the product of
corresponding elements of the lists in arguments and prints an error message if length of both lists differ.
In [5]:
def list_prod(list1,list2):
if len(list1)!=len(list2):
print('Error. Length of both list need to be same.')
else:
new_list=[]
j=0
for i in list1:
while j<len(list2):
new_list.append(i*list2[j])
break
j+=1
return new_list
In [6]:
l1 = [3,5,7,-2]
l2 = [-4,3,6,1]
list_prod(l1,l2)
Out[6]:
Bonus Question:
Write a function that take a non-negative number as argument, say “n”, and returns a list of n keith numbers.
In [4]:
def keith_number(n):
c = str(n)
a = list(map(int, c))
b = sum(a)
while b < n:
a = a[1:] + [b]
b = sum(a)
In [ ]: