[go: up one dir, main page]

0% found this document useful (0 votes)
5 views6 pages

Function

The document contains solutions to programming problems in Python. Each problem solution includes the code with inputs and outputs. The solutions cover topics like functions, conditionals, loops, recursion, prime numbers, Armstrong numbers etc.

Uploaded by

shreyas soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Function

The document contains solutions to programming problems in Python. Each problem solution includes the code with inputs and outputs. The solutions cover topics like functions, conditionals, loops, recursion, prime numbers, Armstrong numbers etc.

Uploaded by

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

Program Write a Python function that takes a list of words and returns

1 the length of the longest one.


Solution print('Shreyas soni\n0827CS191224')
def find_longest_word(words_list):
word_len = []
for n in words_list:
word_len.append((len(n), n))
word_len.sort()
return word_len[-1][0], word_len[-1][1]
result = find_longest_word(["PHP", "Exercises", "Backend"])
print("\nLongest word: ",result[1])
print("Length of the longest word: ",result[0])

Output Shreyas soni


(Screen 0827CS191224
Shot)
Longest word: Exercises
Length of the longest word: 9

Program Write a function x(n) for computing an element in the


2 sequence xn=n^2+1. Call the function for n=4 and write out
the result.
Solution print('Shreyas soni\n0827CS191224')
def x(n):
return(n**2)+1;
n=int(input("enter the valueof n:"))
print(x(n))

Output Shreyas soni


(Screen 0827CS191224
Shot) enter the valueof n:5
26

Program Take the following Python code that stores a string: ‘str = 'Y-
3 tatata-acropolis: 0.8475'. Use find and string slicing to extract
the portion of the string after the colon character and then use
the float function to convert the extracted string into a floating
point number.
Solution print('Shreyas soni\n0827CS191224')
str="Y-tatata-acropolis: 0.8475"
pos=str.find(':')
num=str[pos+1:]
ans=float(num)
print(ans)

Output Shreyas soni


(Screen 0827CS191224
Shot) 0.8475

Program Write a function that returns the middle value among three
4 integers. (Hint: make use of min() and max()). Write code to
test this function with different inputs.
Solution print('Shreyas soni\n0827CS191224')
a=int(input("Enter 1st no:"))
b=int(input("Enter 2nd no:"))
c=int(input("Enter 3rd no:"))
def mid_num(num1,num2,num3):
max_num=max(num1, num2, num3)
min_num=min(num1, num2, num3)
return num1+num2+num3-max_num-min_num
print("middle no:",mid_num(a,b,c))

Output Shreyas soni


(Screen 0827CS191224
Shot) Enter 1st no:10
Enter 2nd no:20
Enter 3rd no:30
middle no: 20

Program Write a function that computes the area of a rectangle. Then,


5 write a second function that calls this function three times to
compute the surface area of a rectangular solid.
Solution print('Shreyas soni\n0827CS191224')
def rect(l,b):
return l*b
def area(l,b,h):
c1=rect(l,b)
c2=rect(b,h)
c3=rect(h,l)
sum=c1+c2+c3
print("surface area:",2*sum)
area(2,4,6)

Output Shreyas soni


(Screen 0827CS191224
Shot) surface area: 88

Program Create an outer function that will accept three parameters, a,


6 b and c. Create an inner function inside an outer function that
will calculate the addition of a, b and c. At last, an outer
function will add 5 into addition and return it
Solution print('Shreyas soni\n0827CS191224')
def outer(a,b,c):
def inner(a, b, c):
return a+b+c
return 5+inner(a,b,c)
n1=int(input("Enter 1st no:"))
n2=int(input("Enter 2nd no:"))
n3=int(input("Enter 3rd no:"))
print("output is:",outer(n1,n2,n3))

Output Shreyas soni


(Screen 0827CS191224
Shot) Enter 1st no:1
Enter 2nd no:2
Enter 3rd no:3
output is: 11

Program Write a program to create a recursive function to calculate the


7 product of numbers from 10 to 100.
Solution print('Shreyas soni\n0827CS191224')
def product(a,b):
if(a<b):
return product(b,a)
elif(b!=0):
return(a+product(a,b-1))
else:
return 0
a=int(input("Enter first number: "))
b=int(input("Enter second number: "))
print("Product is: ",product(a,b))

Output Shreyas soni


(Screen 0827CS191224
Shot) Enter first number: 10
Enter second number: 12
Product is: 120

Program Write a Python function to calculate the factorial of a number


8 (a non-negative integer). The function accepts the number as
an argument.
Solution print('Shreyas soni\n0827CS191224')
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
n=int(input("Input a number to compute the factiorial : "))
print(factorial(n))

Output Shreyas soni


(Screen 0827CS191224
Shot) Input a number to compute the factiorial : 5
120
Program Write a Python function to display all the multiples of 7 & 9
9 within the range 100 to 500.
Solution print('Shreyas soni\n0827CS191224')
def mult():
for i in range(100,500):
if(i%7==0) and (i%9==0):
print(i)
mult()

Output Shreyas soni


(Screen 0827CS191224
Shot) 126
189
252
315
378
441

Program Write a Python function to display all the multiples of 7 & 9


10 within the range 100 to 500.
Solution print('Shreyas soni\n0827CS191224')
def mult():
for i in range(100,500):
if(i%7==0) and (i%9==0):
print(i)
mult()

Output Shreyas soni


(Screen 0827CS191224
Shot) 126
189
252
315
378
441

Program Write a Python function to check whether the given integer is


11 a prime number or not.
Solution print('Shreyas soni\n0827CS191224')
num = 29
flag = False
if num > 1:
for i in range(2, num):
if (num % i) == 0:
flag = True
break
if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")

Output Shreyas soni


(Screen 0827CS191224
Shot) 29 is a prime number

Program Write a Python function that checks whether a passed


12 interger is armstrong or not.
Solution print('Shreyas soni\n0827CS191224')
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

Output Shreyas soni


(Screen 0827CS191224
Shot) Enter a number: 371
371 is an Armstrong number

Program Program to return a function from another function.


13

Solution print('Shreyas soni\n0827CS191224')


def B():
print("Inside the method B.")
def A():
print("Inside the method A.")
return B
returned_function = A()
returned_function()

Output Shreyas soni


(Screen 0827CS191224
Shot) Inside the method A.
Inside the method B.

Program First, def a function, start_process, that takes one argument p.


14 Then, if the start_process function receives an p equal to
"Yes", it should return "Start Process" Alternatively, elif p is
equal to "No", then the function should return "Start Process
Aborted". Finally, if start_process gets anything other than
those inputs, the function should return "Sorry for the input".
Solution print('Shreyas soni\n0827CS191224')
def start(p):
if p=="yes" or p=="Yes":
print("start process")
elif p == "no" or p == "No":
print("start process aborted")
else:
print("sorry for input")
ip=input("enter yes or no:")
start(ip)

Output Shreyas soni


(Screen 0827CS191224
Shot) enter yes or no:yes
start process

Program First, def a function called calculate_distance, with one


15 argument (choose any argument name you like). If the type of
the argument is either int or float, the function should return
the absolute value of the function input. Otherwise, the
function should return "No value". Check if it works by calling
the function with 9.6 and "what?".
Solution print('Shreyas soni\n0827CS191224')
def calculate_distance(n):
if (type(n)== int or type(n)== float):
print(abs(n))
else:
print("No value")
calculate_distance(9.6)
calculate_distance("what?")

Output Shreyas soni


(Screen 0827CS191224
Shot) 9.6
No value

You might also like