Anjeev Singh Academy

Question – 5: Write a function that receives two strings arguments and check whether they are same-length strings. (returns True in this case otherwise False)

Answer: #Function to check the given two strings have equal lengths

#Que - 5 : Compare length of two strings

def compare_length(string1, string2):
    if len(string1) == len(string2):
        return True
    else:
        return False

print('compare_length("hello", "welcome") =>', compare_length("hello", "welcome"))
print('compare_length("school", "anjeev") =>',compare_length("school", "anjeev"))

Output:

compare_length(“hello”, “welcome”) => False
compare_length(“school”, “anjeev”) => True

Question – 6: Write a function namely nthRoot() that receives two parameters x and n and returns nth root of x i.e. x to the power 1/n.

Answer: # Function to calculate and return the nthroot() of the given number.

#Que - 6 : Function to calculate and return the nthroot() of the given number.


def nthRoot(x, n = 2):
    return x**(1/n)

print("nthroot(4) =>",nthroot(4))
print("nthroot(7,4) =>", nthroot(7, 4))

Output:

nthroot(4) => 2.0
nthroot(7,4) => 1.6265765616977856

Question – 7: Write a function that takes a number n and then returns a randomly generated number having exactly n digits. (not starting with zero).

Answer: # Function to generate random number having equal to ‘n’ number of digits.

#Que 7 - Function to generate random number having equal to 'n' digits

import random
def generate_random(n):
    start = 10 **(n-1)
    end = 10**n - 1
    return random.randint(start, end)

print("generate_random(1)=>", generate_random(1))
print("generate_random(2) =>", generate_random(2))
print("generate_random(3) =>", generate_random(3))
print("generate_random(4) =>", generate_random(4))

Output:

generate_random(1)=> 5
generate_random(2) => 45
generate_random(3) => 640
generate_random(4) => 5754

Question – 8: Write a function that takes two numbers and returns the number that has minimum one’s digit.

Answer: Function that takes two numbers and returns the number that has minimum one’s digit.

#Que 8 - Function that takes two numbers and returns the number that has minimum one's digit.

def compare_minimum_onesdigit(n1, n2):
    if (n1%10) < (n2%10):
        return n1
    else:
        return n2

print("compare_minimum_onesdigit(494, 272) =>", compare_minimum_onesdigit(494, 272))
print("compare_minimum_onesdigit(54, 27) =>", compare_minimum_onesdigit(54, 27))
print("compare_minimum_onesdigit(6598, 19) =>", compare_minimum_onesdigit(6598, 19))

Output:

compare_minimum_onesdigit(494, 272) => 272
compare_minimum_onesdigit(54, 27) => 54
compare_minimum_onesdigit(6598, 19) => 6598

Question – 9: Write a program that generates a series using a function which takes first and last values of the series and then generates four terms that are equidistant.

Answer: Function generate equidistant four terms between given two numbers.

def equidistant_fourterms(a, b):
    
    d = ( ( b - a ) / 3 )
    print("Series are = " , a , a + d , a + 2*d , b )

first = int(input("Enter first Term = "))
last = int(input("Enter last Term = "))

equidistant_fourterms(first , last )

Output:

Enter first Term = 100
Enter last Term = 1000
Series = 100 400 700 1000

Enter first Term = 9
Enter last Term = 1
Series are = 9 6.333333333333334 3.666666666666667 1

Sorry! You cannot copy content of this page. Please contact, in case you want this content.

Scroll to Top