Sie sind auf Seite 1von 5

10/13/2019 ES Assignment-III Solution

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)

list of integers or floats is [3, 2, 1]


list of strings is ['one', 'two', 'three']

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)

list of integers or floats is [3, 2, 1]


list of strings is ['one', 'two', 'three']

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.

[Note: Do not use the built-in function (modulus %)]

localhost:8888/notebooks/ES Assignment-III Solution.ipynb 1/5


10/13/2019 ES Assignment-III Solution

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.

[Note: Do not use the built-in function “max”.]

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)

localhost:8888/notebooks/ES Assignment-III Solution.ipynb 2/5


10/13/2019 ES Assignment-III Solution

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)

localhost:8888/notebooks/ES Assignment-III Solution.ipynb 3/5


10/13/2019 ES Assignment-III Solution

In [51]: 

sep('Education')

list of vowels is ['E', 'u', 'a', 'i', 'o']


list of consonants is ['d', 'c', 't', 'n']

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.

[Note: Do not use built-in function “np.multiply(list1,list2)”]

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]:

[-12, 15, 42, -2]

Bonus Question:

Write a function that take a non-negative number as argument, say “n”, and returns a list of n keith numbers.

localhost:8888/notebooks/ES Assignment-III Solution.ipynb 4/5


10/13/2019 ES Assignment-III Solution

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)

return (b == n) & (len(c) > 1)

n = int(input("\nHow many keith numbers? "))


N = 5
for i in range(N):
a, b = 0, 10**(i + 1)
list1=([i for i in filter(keith_number, range(a, b))])
list2 = list1[0:n]
print(list2)

How many keith numbers? 7


[14, 19, 28, 47, 61, 75, 197]

In [ ]: 

localhost:8888/notebooks/ES Assignment-III Solution.ipynb 5/5

Das könnte Ihnen auch gefallen