E.G. - If Input Is (12, 54, 49, 86, 23, 36) Then Modified List Is (32, 18, 16, 106, 43, 12)
E.G. - If Input Is (12, 54, 49, 86, 23, 36) Then Modified List Is (32, 18, 16, 106, 43, 12)
A) Write the following programs with the help of user-defined functions only:
1. Find the sum of all the odd integers within a given range. Use def sum_odd(min,max)
2. Display the Fibonacci series up to a given number of terms. Use def fib_series(n)
3. Check whether a number is Amstrong or not. Return True/False from the function.
4. Find the reverse of a given string without using any built-in function. def rev(String)
6. Input a string and return a tuple of integers which are actually numeric Unicode values of
each characters.
e.g. - If input is ‘Welcome’ then output is (87, 101, 108, 99, 111, 109, 101)
7. Consider the dictionary dic_Food = { 'Breakfast' : 'Milk', 'Lunch' : 'Rice' , 'Dinner' : 'Bread' }
i. Display the keys only of dic_Food after adding two elements {'M_Snack' : 'Vada’} and
{'E_Snack' : 'Pizza'}. Use def key_add()
ii. Display the values only of dic_Food after deleting elements {'Lunch' : 'Rice'}.
Use def value_remove()
8. Write a function to input a list of real numbers in ascending order of values. def input_list()
Write another function to a search whether a given value is present in the list using binary
search technique. def binary_search(value)
9. Input 3 sides of a scalene triangle with one side having default value 5 unit. Define individual
functions for calculating perimeter and area of that triangle.
10. Input a point from user i.e. a tuple of 2 integers (X, Y) and display all the adjacent points:
(X+1, Y), (X-1, Y), (X, Y+1), (X, Y-1), (X+1, Y+1), (X-1, Y+1), (X+1, Y-1), (X-1, Y-1) inside a
single function.
1.Write a function inch_to_cm() which accepts the value in inch and return the value in
centimeter, where 1 inch = 2.54 cm.
2. Input a radius r and height h from the user where both the values are positive real numbers
and define function for each of the following purpose:
i. Calculate area of the circle of radius r.
ii. Calculate total surface area of the cylinder of radius r and height h.
iii. Calculate volume of a sphere with radius r.
3. Define a function Max_Vowel() which accepts a tuple of strings as argument and return the
string with highest number of vowels in it. If more than one string have same highest count then
return the last occurrence string only.
4. Input 3 numbers as parameters and return twice of the original number if it is divisible by 4
or 9, otherwise no change.
e.g. If x=20, y=15 and z = 81 then Twice_4_9(x ,y, z) will return 40, 15, 162
5. Input a year in the function Check_Leap_Year() and return Boolean value depending on
whether that year is leap year.
6. Input length, breadth of a rectangular cuboid where default value of breadth is 2 in the
function Calc_area(length, breadth) which returns its surface area. Define height=3 as global
variable.
7. Input two real numbers in the function Display_random(). Generate 5 random real numbers
within this range and store in the list R. Display the median value of the list.
8. Input a tuple of strings and display 2 strings from the tuple randomly.
C) Re-write the following program code after rectifying errors and underline the corrections:
1. sum = 0
def series x, n
for i range[n]:
sum = sum + power(x,i)
return n : sum
sum(2,5) = a : b
print("Sum of term", a, " = ", b)
3. gb = 2
def func1(m=10):
gb = m
print(gb)
def func2(m=10, n):
global gb = m
print(gb+n)
func1(5)
func2(15)
func1(5, 15)
func2(5, 15)
print(gb)
4. sum(a = 10, b = a)
if a > b:
a-b+2
else return a+b-2
sum[10]
sum(20,30,40)
2. If the user gives input as hallucination then find the output displayed by the following code:
def crop(text):
return text[:3:1] + text[-2:7:-1]
def hlen(text):
return len(text)//2
tx = input("Enter a text : ")
t1 = crop(tx)
print(t1)
t2 = tx[::-2]
print(t2)
p = hlen(tx)
print(tx[:p:3]*3)
4. def toggle_tuple(L):
T, Temp = list(L), []
for i in range(0, len(T), 2):
Temp.extend([L[i+1], L[i]])
L=tuple(Temp)
print(L)
Tp = (9, 12, 47, 52, 73, 10)
toggle_tuple(Tp)
print(Tp)
Lst = list(Tp[::-1])
toggle_tuple(Lst)
print(Lst)
def func_sum(R):
sum = 0
for i in range(1, R, 3):
sum = sum%5 + func_prod(i,2)
print(i, sum, sep = '#')
7. DIGIT = 10
def mod_num(N):
global DIGIT
m=4
DIGIT = N//m + 1
print("DIGIT", DIGIT)
DIGIT -= 2
def ch_num():
DIGIT = 7 #STATEMENT 1
print("TWICE DIGIT", DIGIT*2)
DIGIT = DIGIT - 3
ch_num()
mod_num(23)
DIGIT += 5
print("Final DIGIT", DIGIT)
Write output of the code above if #STATEMENT 1 is replaced with ‘global DIGIT’ inch_num().
8. def dic_ch(D):
if 'Name' in D:
D['Name'] = D['Name'] + ' ' + "KV DRDO"
D['Marks'] = (90, 79, 87)
if D['Fee'] >= 1000:
D['Fee'] += round(D['Fee']*0.1)
if 'City' in D:
del D['City']
return
Dict1 = { 'Roll' : 1, 'Name' : 'Sonam Dutta', 'Fee' : 500, 'City' : 'Bangalore'}
Dict2 = { 'Roll' : 2, 'Fee' : 1500, 'Marks' : 100}
dic_ch(Dict1)
dic_ch(Dict2)
print(Dict1)
print(Dict2)