Reference Question-ST3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Q1. Create a Python module with global variables that can be accessed by other programs.

Help user
understand how to define and use global variables within this module.

Input:

● input the radius of the circle.

Output:

● The program will display the global variables from the module.
● It will compute the area of the circle and check if it falls within the valid range defined by the
global variables.

Q2.

Saurav, a first-year student at Technoville University studying software engineering, has recently started
learning Python programming. As part of his assignment, his instructor has tasked him with creating a
Python program that generates a random number within a user-specified range. The program needs to
handle invalid inputs, such as non-numeric entries or cases where the lower bound of the range exceeds
the upper bound. To ensure the program runs correctly, Saurav needs to include exception handling and
validate user input until it's correct. Can you assist Saurav with solving this issue?

Q3.

Two friends, Meera and Simran, were chatting one day about interesting mathematical patterns. Meera,
who was quite intrigued, asked Simran if she could assist her in generating a well-known pattern called
Pascal's Triangle, which Meera had learned about recently in her Python class. Simran explained how to
construct the triangle using binomial coefficients. Can you help Meera write a Python program that
allows users to input the number of rows and then outputs Pascal's Triangle up to that row, using the
binomial coefficient to calculate the values?

Input Format
The first line contains an integer N denoting the total number of rows to be printed. Each row should
contain a similar number of integers, e.g., row 5 should contain 5 integers.

Output Format:
Refer to the sample output for formatting specifications.

Q. A teacher assigns tasks to students based on their ID numbers, ranging from 1 to 100. If the ID is a
multiple of 2, the student prints "Rajya"; if it's a multiple of 5, they print "Sabha"; and if it's a multiple of
both, they print "RajyaSabha".

Input Format
The 1st line contains an integer N denoting the total number of student IDs.
The 2nd line is a string user input for characters to be printed instead of 2’s multiples.
The 3rd line is also a string user input for characters to be printed instead of 5’s multiples.

Output Format:
Refer to the sample output.
Sample case:
Input
13
hello
ji
Output:
1
hello
3
hello
ji
hello
7
hello
9
hello ji
11
hello
13

Sample case:
Input
7
rabhi
bhai

Output:
1
rabhi
3
rabhi
bhai
rabhi
7

Sample code:
# user inputs for ID numbers
n = int(input())
# user inputs for a and b
a = input()
b = input()
for i in range(1, n+1):
if (i%10==0):
print(a, b)
continue
elif (i%2==0):
print(a)
continue
elif (i%5==0):
print(b)
continue
print(i)
Q. Ayaan and Vikram recently started learning Python programming. They both have a collection of rare
stamps. They wanted to write a Python program to find the common stamps they both own.
Additionally, the program should also display the stamps Ayaan has that Vikram doesn't, and the stamps
Vikram has that Ayaan doesn't. Can you help them write this Python code?

Sample case:
Input:
5 10 20 21
2 4 10 20
Output:
10 and 20 are common to both the person
5 and 21 are This is only with Ayaan
2 and 4 are This is only with Vikram

Sample case:
Input:
12345
23456
Output:
2 3 4 and 5 None are common to both the person
1 is This is only with Ayaan
6 is This is only with Vikram

Sample code:
# Python program for this question
s1 = set(map(int, input().split()))
s2 = set(map(int, input().split()))
def disply_lst(l):
for i in l:
print(i, end = " ")
if l.index(i) == (len(l)-2):
print("and", end = " ")
def sing_plu(l):
if len(l) == 1:
print("is", end = " ")
else:
print("None are", end = " ")
l3 = sorted(list(s1.intersection(s2)))
l4 = sorted(list(s1.difference(s2)))
l5 = sorted(list(s2.difference(s1)))
disply_lst(l3)
sing_plu(l3)
print("common to both the person")
disply_lst(l4)
sing_plu(l4)
print("This is only with Ayaan")
disply_lst(l5)
sing_plu(l5)
print("This is only with Vikram")
Q. Addition and Difference of matrix
Q. Write a Program that returns the maximum value from a given list of integers using the recursive
function.

Input Format
The first line contains an integer N denoting the total number of elements in the list.
The next N lines contain N integers.

Output Format:
An integer which is the maximum value in the list.

Sample case:
Input:
7
21
-4
8
7
5
3
76

Output:
76

Q. Aryan is exploring the concept of lambda functions in Python. He wants to write a program that
removes all the numbers from a list that are greater than a given limit.
Input Format
The first line contains an integer N indicating the number of elements in the list. The next N lines contain
one integer each. The final line contains an integer L, which is the threshold value.
Output Format
Output the list of integers that are less than or equal to L.

Sample case:
Input:
5
10
11
12
13
12
15
Output:
[10, 11, 12, 13, 12]

Sample case:
Input:
5
10
11
12
13
12
11
Output:
[10, 11]

Sample code:
def filter_less_than_or_equal(nums, limit):
return list(filter(lambda x: x <= limit, nums))
n = int(input())
nums = [int(input()) for _ in range(n)]
limit = int(input())
print(filter_less_than_or_equal(nums, limit))

Q. Ankit is learning about filtering and processing lists in Python. He wants to write a program that
removes duplicate values from a list while keeping the original order of the elements intact.
Input Format
The first line contains an integer N representing the number of elements in the list. The next N lines each
contain one integer.
Output Format
Output a list of integers with duplicates removed, maintaining their original order.

Sample case:
Input:
5
10
11
10
11
12
Output:
[10, 11, 12]

Q. Write a program to calculate the sum of elements at even indices in a given list.

Input Format
The first line contains an integer N denoting the total number of elements in the list.
The next N lines contain N integers.

Output Format:
An integer which is the sum of elements at even indices in the list.

Sample case:
Input:
5
2
4
6
8
10
Output:
18

Sample code:
def sum_even_indices(num_list):
return sum(num_list[i] for i in range(0, len(num_list), 2))
li = []
n = int(input())
for i in range(n):
e = int(input())
li.append(e)
print(sum_even_indices(li))

You might also like