Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
37 views

Coding Question of Python

The document contains 6 problem statements related to Python programming. Problem 1 asks to print positive numbers from a user-input list using while loops. Problem 2 involves reversing the words in a user-input sentence. Problem 3 prints an inverted pyramid number pattern given the number of lines. Problem 4 prints an asterisk pattern that increases then decreases in size. Problems 5 and 6 involve defining classes to analyze student data, such as calculating totals, percentages, comparing heights.

Uploaded by

Parth Tuteja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Coding Question of Python

The document contains 6 problem statements related to Python programming. Problem 1 asks to print positive numbers from a user-input list using while loops. Problem 2 involves reversing the words in a user-input sentence. Problem 3 prints an inverted pyramid number pattern given the number of lines. Problem 4 prints an asterisk pattern that increases then decreases in size. Problems 5 and 6 involve defining classes to analyze student data, such as calculating totals, percentages, comparing heights.

Uploaded by

Parth Tuteja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

1.

Problem Statement1 : Greatest Common divisor of two integers

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:

n = No. of elements in a list

l = list where input elements should be taken from the user

Output Format:

• Print the list

• Print only the positive numbers from the list “l”.

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:

[-10, 4, -1, -40, -56, 20]

Sample Output:

4, 20

Solution:
n = int(input("Enter number of elements : "))

# Below line read inputs from user using map() function

l = list(map(int, input().split()))[:n]

print(l)

i=0

while(i < len(l)):

if l[i] >= 0:

print(l[i], end = " ")

i += 1

Test case 1 Input Output

Sample Test case 6

-10 4 -1 -40 -56 20

[-10, 4, -1, -40, -56, 20]

4, 20

Test Case1 4

-10 6 9 -20

[-10, 6, 9, -20]

69

Test Case2 3

10 -12 45

[10, -12, 45]

10 45

Test Case3 5

1 2 -4 -5 7
[1, 2, -4, -5, 7]

127

Test Case4 6

-10 4 -1 -40 -56 20

[-10, 4, -1, -40, -56, 20]

4, 20

Test Case5 7

-1 -5 12

[-1, -5, 12]

12

Problem Statement2 : Greatest Common divisor of two integers

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

position of the word should be same.

Constraints:

0 <= N <= 100

Where N is the length of the input string.

Sample Input:

Welcome To Chitkara

Sample Output:

emocleW oT araktihC
Solution:

def rev_sentence(sentence):

words = sentence.split(" ")

new_words = [word[::-1] for word in words]

new_sentence = " ".join(new_words)

return new_sentence

sentence = input()

print(rev_sentence(sentence))

Test case 1 Input Output

Sample Test case Welcome To Chitkara emocleW oT araktihC

Test Case1 Hello World! olleH !dlroW

Test Case2 How are you woH era uoy

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

Test Case5 Nice to know that you will

help me

eciN ot wonk taht uoy lliw

pleh em

TOPIC : NESTED LOOPS AND PATTERNS

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Coding Question (5 Marks)

Problem Statement3 : Print the following inverted pyramid number pattern:

12345

1234
123

12

Input Format

The number of lines

Constraints

NIL

Output Format

The aforesaid pattern

Sample Input

Sample Output

12345

1234

123

12

………………………………………………………………………………………………

SOLUTION:

#taking input from user

n = int(input(“Enter the number of lines: ”))

#displaying the pattern

for i in range(n, 0, -1):

for k in range(0, n-i):

print(“ ”, end=” “)
for j in range(1, i+1):

print(j, end=” “)

print()

CODE STUB:

n = int(input(“Enter the number of lines: ”))

// Write your code here

TEST CASES:

Test case 1 Input Output

Sample Test case 6

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

The number of lines

Constraints

NIL
Output Format

The aforesaid pattern

Sample Input

Sample Output

**

***

****

*****

****

***

**

………………………………………………………………………………………………

SOLUTION:

#taking input from user

n = int(input(“Enter value to print stars sequence:”))

#start with the loop to print the first five rows

for i in range(n):

for j in range(i):

print(‘* ’, end= “”)

print(‘’)

#decreasing order of stars from the middle

for i in range(n, 0,-1):


for j in range(i):

print(‘* ’, end= “”)

print(‘’)

CODE STUB:

number = int(input(“Enter number to print star sequence”)

// write your code here

TEST CASES:

Test case 1 Input Output

Sample Test case 5 *

**

***

****

*****

****

***

**

Test Case1 6 *

**

***

****

*****

******

*****

****
***

**

Test Case2

**

***

****

***

**

Test Case3

**

***

***

Test Case4

**

TOPIC : OOPS in Python


Problem Statement 5: Student Result Using Class

A faculty wants to consider "5" different marks of a student in a different subject and then

examine the Status of the student as PASS, COMPARTMENT, FAIL.

The 4 class methods are for getting student details, Total Marks, Percentages, and Result

The result is to be calculated as per the conditions:

The Method has to count the Number of Subjects in which a student marks are >50

If Count==5 Then Result PASS

If Count>=3 Then Result COMPARTMENT

else Result FAIL

Input: Input from users for the roll number, name, and marks of five subjects.

Output: Roll No., Name, Total Marks, Percentage and Result.

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:

1 ABC 411 82.2 PASS

Code Stub:

class Student:

def __init__(self):

#Initilisation
def Studentdetails(self):

#Input

def Totalmarks(self):

#Calculate

def Percentage(self):

#Calculate

def Result(self):

#Count Subjects to get result

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 Input Output

Test Case1 10

Jatin

78

90

87

67

10 Jatin 331 66.2 COMPARTMENT

Test Case2 11

Aashim

67

89

90
97

85

11 Aashim 428 85.6 PASS

Test Case3 12

Varun

34

89

67

12 Varun 207 41.4 FAIL

Test Case4 14

Avi

45

14 Avi 377 75.4 COMPARTMENT

89

67

98

78

Test Case5 15

Rashi

51

50

67

89
90

15 Rashi 347 69.4 PASS

Problem Statement 6: Student Height Comparison Using Class

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

heights and return the student's information with more height.

Sample Input:

Abhay

45

Arun

67

Sample Output:

Larger Student and its Height is:

Arun 67

class Student:

def GetStudent(self):

self.__name=input()

self.__height_cm=int(input())

def PutStudent(self):

print(self.__name,self.__height_cm)

def __gt__(self, T):

if (self.__height_cm > T.__height_cm):


return True

else:

return False

def __lt__(self, T):

if (self.__height_cm < T.__height_cm):

return True

else:

return False

P1=Student()

P2=Student()

P1.GetStudent()

P2.GetStudent()

if(P1>P2):

print("Larger Student and its Height is: ")

P1.PutStudent()

elif(P1<P2):

print("Larger Student and its Height is:")

P2.PutStudent()

else:

print("Both Are Equal: ")

P1.PutStudent()

P2.PutStudent()

Test Case1 Input Output

Test Case1 Kuldeep

45
Mayank

43

Larger Student and its Height is:

Kuldeep 45

Test Case2 Asha

47

Ahana

48

Larger Student and its Height is:

Ahana 48

Test Case3 Varun

54

Rahul

54

Both Are Equal:

Varun 54

Rahul 54

Test Case4 Avi

45

Rashi

60

Larger Student and its Height is:

Rashi 60

Test Case5 Abhi

51
Ravi

50

Larger Student and its Height is:

Abhi 51

You might also like