Coding Question of Python
Coding Question of Python
Write a Python program to print positive numbers in a List using if and while list. The list
should be created by taking input from the user (For loop not allowed).
Input Format:
Output Format:
Constraints:
• For input in “l”, if “n” exceeds the declared value of n, then list will display only up to
declared “n” values, e.g., n = 3 and input for l = [10 -12 45 -78 90], the printed l = [10
-12 45].
• If length of “l” < n, the code will continue with that list.
Sample Input:
Sample Output:
4, 20
Solution:
n = int(input("Enter number of elements : "))
l = list(map(int, input().split()))[:n]
print(l)
i=0
if l[i] >= 0:
i += 1
4, 20
Test Case1 4
-10 6 9 -20
[-10, 6, 9, -20]
69
Test Case2 3
10 -12 45
10 45
Test Case3 5
1 2 -4 -5 7
[1, 2, -4, -5, 7]
127
Test Case4 6
4, 20
Test Case5 7
-1 -5 12
12
Kirti has been asked to give a sentence in the form of a string as a function parameter. The task
is to print the sentence such that each word in the sentence is reversed.
Input Format:
The only line of input contains a string that represents the sentence given by Kirti.
Output Format:
The only line of output prints the sentence such that each word in the sentence is reversed. The
Constraints:
Sample Input:
Welcome To Chitkara
Sample Output:
emocleW oT araktihC
Solution:
def rev_sentence(sentence):
return new_sentence
sentence = input()
print(rev_sentence(sentence))
Test Case3 I'm not in a good condition m'I ton ni a doog noitidnoc
Test Case4 Would you like to help me? dluoW uoy ekil ot yats pleh
?em
help me
pleh em
12345
1234
123
12
Input Format
Constraints
NIL
Output Format
Sample Input
Sample Output
12345
1234
123
12
………………………………………………………………………………………………
SOLUTION:
print(“ ”, end=” “)
for j in range(1, i+1):
print(j, end=” “)
print()
CODE STUB:
TEST CASES:
123456
12345
1234
123
12
Test Case1 5 1 2 3 4 5
1234
123
12
Test Case2 4 1 2 3 4
123
12
Test Case3 7 1 2 3 4 5 6 7
123456
12345
1234
123
12
Test Case4 3 1 2 3
12
Test Case5 2 1 2
Problem Statement 4: Print the following pattern using nested for loops
**
***
****
*****
****
***
**
Input Format
Constraints
NIL
Output Format
Sample Input
Sample Output
**
***
****
*****
****
***
**
………………………………………………………………………………………………
SOLUTION:
for i in range(n):
for j in range(i):
print(‘’)
print(‘’)
CODE STUB:
TEST CASES:
**
***
****
*****
****
***
**
Test Case1 6 *
**
***
****
*****
******
*****
****
***
**
Test Case2
**
***
****
***
**
Test Case3
**
***
***
Test Case4
**
A faculty wants to consider "5" different marks of a student in a different subject and then
The 4 class methods are for getting student details, Total Marks, Percentages, and Result
The Method has to count the Number of Subjects in which a student marks are >50
Input: Input from users for the roll number, name, and marks of five subjects.
INPUT:
1 #Roll No
ABC #Name
67 #Marks of Subject 1
89 #Marks of Subject 2
76 #Marks of Subject 3
89 #Marks of Subject 4
90 #Marks of Subject 5
OUTPUT:
Code Stub:
class Student:
def __init__(self):
#Initilisation
def Studentdetails(self):
#Input
def Totalmarks(self):
#Calculate
def Percentage(self):
#Calculate
def Result(self):
def showStudent(self):
self.Totalmarks()
self.Percentage()
self.Result()
print(self.__roll,self.__name,self.__total,self.__per,self.__result)
def main():
s=Student()
s.Studentdetails()
s.showStudent()
if __name__=="__main__":
main()
Code:
class Student:
def __init__(self):
self.__roll=0
self.__name=""
self.__marks=[]
self.__total=0
self.__per=0
self.__result=""
def Studentdetails(self):
self.__roll=int(input())
self.__name=input()
for i in range(5):
self.__marks.append(int(input()))
def Totalmarks(self):
for x in self.__marks:
self.__total+=x
def Percentage(self):
self.__per=self.__total/5
def Result(self):
count=0
for x in self.__marks:
if x>=50:
count+=1
if count==5:
self.__result="PASS"
elif count>=3:
self.__result="COMPARTMENT."
else:
self.__result="FAIL"
def showStudent(self):
self.Totalmarks()
self.Percentage()
self.Result()
print(self.__roll,self.__name,self.__total,self.__per,self.__result)
def main():
#Student object
s=Student()
s.Studentdetails()
s.showStudent()
if __name__=="__main__":
main()
Test Case1 10
Jatin
78
90
87
67
Test Case2 11
Aashim
67
89
90
97
85
Test Case3 12
Varun
34
89
67
Test Case4 14
Avi
45
89
67
98
78
Test Case5 15
Rashi
51
50
67
89
90
Python program to find the Larger Height (cm) of two students using a Class. Get input from
users for the name and height of two students and then return the larger student's name and
height. Create a class named Student with methods to get data, print data and return the older
student's details. GetStudent() method takes input from users i.e. name and age of person.
PutStudent() method prints the data of the object. For the student's information, compare their
Sample Input:
Abhay
45
Arun
67
Sample Output:
Arun 67
class Student:
def GetStudent(self):
self.__name=input()
self.__height_cm=int(input())
def PutStudent(self):
print(self.__name,self.__height_cm)
else:
return False
return True
else:
return False
P1=Student()
P2=Student()
P1.GetStudent()
P2.GetStudent()
if(P1>P2):
P1.PutStudent()
elif(P1<P2):
P2.PutStudent()
else:
P1.PutStudent()
P2.PutStudent()
45
Mayank
43
Kuldeep 45
47
Ahana
48
Ahana 48
54
Rahul
54
Varun 54
Rahul 54
45
Rashi
60
Rashi 60
51
Ravi
50
Abhi 51