sa 12 cs chapter 3

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 e.g., if two numbers passed are 1 and 7 then function returns 1 3 5 7.

# Code def func(x, y): d = (y-x)/3 # divides the difference in 3 equal parts return [x, x+d, x+2*d, y] # returns the series as a list print(func(1, 9)) print(func(4, 8)) print(func(4, 10)) print(func(1, 7)) # Output [1, 3.6666666666666665, 6.333333333333333, 9] [4, 5.333333333333333, 6.666666666666666, 8] [4, 6.0, 8.0, 10] [1, 3.0, 5.0, 7]

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 e.g., if two numbers passed are 1 and 7 then function returns 1 3 5 7. Read More »

7. Write a function that takes a number n and then returns a randomly generated number having exactly n digits (not starting with zero) e.g., if n is 2 then function can randomly return a number 10-99 but 07, 02 etc. are not valid two digit numbers.

# Code from random import randint def ran(n): x = int(‘9’*(n-1))+1 # first n-digit number, string is converted back to int y = int(‘9’*n) # last n-digit number return randint(x, y) print(‘Random number of digit 3 is’, ran(3)) print(‘Random number of digit 5 is’, ran(5)) # Output Random number of digit 3 is 883 Random

7. Write a function that takes a number n and then returns a randomly generated number having exactly n digits (not starting with zero) e.g., if n is 2 then function can randomly return a number 10-99 but 07, 02 etc. are not valid two digit numbers. Read More »