TCS Python Hackathon - Python Hands-on
Q1. Write a python code to take a string as input and count the number of vowels in the string. Your code is expected to print the count value as output. Count should include vowels in lower and upper cases.
def countVowels(inp):
count = 0
vowels = ['a','e','i','o','u','A','E','I','O','U']
for character in inp:
if character in vowels:
count = count + 1
return count
if __name__=='__main__':
inp = input()
output = countVowels(inp)
print(output)
Q2. Write a python code to count how many prime integers are there in a given list of integers.
def countPrimeNumbers(numbers):
count =
0
for
elements in numbers:
if(elements == 0 or elements == 1):
continue
else:
for i in range(2,elements//2+1):
if(elements % i == 0):
break
else:
count = count +1
return count
if __name__ == '__main__':
numbers=[]
count=int(input())
for i
in range(count):
numbers.append(int(input()))
print(countPrimeNumbers(numbers))
Q3. Write a python code to which will calculate the percentage and grade of a student based on the percentage of marks.
Define a class Student with attributes roll(number), student name(name) and a list which stores the marks scored by the student(mark_list).
Provide following methods to the class:
1. calculate_percentage()
2. find_grade()
class Student:
def
__init__(self,roll,name,marks_list):
self.roll = roll
self.name = name
self.marks_list = marks_list
self.percent = None
def
calculate_percentage(self):
summ = sum(self.marks_list)
count = len(self.marks_list)
percentage = summ//count
self.percent = percentage
return percentage
def
find_grade(self):
percent = self.percent
if(percent >= 80):
grade = 'A'
elif(percent >= 60):
grade = 'B'
elif(percent >= 40):
grade = 'C'
else:
grade = 'F'
return grade
if __name__ == '__main__':
roll=int(input())
name=input()
count=int(input())
marks=[]
for i
in range(count):
marks.append(int(input()))
s=Student(roll,name,marks)
print(s.calculate_percentage())
print(s.find_grade())
Q4. Write a python code as per below prototype to fulfill the below requirements.
a. Which will add couples of employee to the organization based on the count.
b. Display the employee age,whose id is given on the input.
c. Display the count of employees whose age is greater than the given age as input.
Define a class Employee with the attributes name, id , age, gender.
Define a class Organisation with attributes to have a name for the organisation, and a list to store list of employees.
Provide following methods to the class:
1. addEmployee()
2. getEmployee()
3. findEmployeeAge()
4. countEmployee()
class Employee:
def
__init__(self,name,Id,age,gender):
self.name = name
self.Id = Id
self.age = age
self.gender = gender
class Organisation:
def
__init__(self,organizationName,employee_list):
self.organizationName = organizationName
self.employee_list = employee_list
def
addEmployee(self,name,Id,age,gender):
empObj
= Employee(name,Id,age,gender)
self.employee_list.append(empObj)
def
getEmployeeCount(self):
count = len(self.employee_list)
return count
def
findEmployeeAge(self,Id):
for
i in self.employee_list:
if(i.Id == Id):
return i.age
return -1
def
countEmployees(self,Age):
countEmp = 0
for
i in self.employee_list:
if(i.age > Age):
countEmp = countEmp + 1
return countEmp
if __name__ == '__main__':
employees=[]
o =
Organisation('XYZ',employees)
n=int(input())
for i
in range(n):
name=input()
id=int(input())
age=int(input())
gender=input()
o.addEmployee(name,id,age,gender)
id=int(input())
age=int(input())
print(o.getEmployeeCount())
print(o.findEmployeeAge(id))
print(o.countEmployees(age))
Please share your opinion/view/experience on the contents through comment section. Your comments are valuable to serve you better.
Great work Thank you so much
ReplyDelete