1. Basics of Python
Function Numbers
Write the function numbers that accepts four numbers as parameters and performs operation specified below
- Find the sum of all the numbers and store it in variable sum_of_numbers
- Find the sum of float numbers among the numbers and store it in variable sum_of_float_numbers
- Find the smallest integer number among the numbers and store it in variable min_number
- Find the count of prime numbers among the numbers and store it in variable prime_count
- Check for zeros. If there are zeros present among the numbers, store True else store False in the variable zero_check
Sample Case 0
Sample Input
2
3.5
0
1
Sample Output
6.5
3.5
0
1
True
Explanation
Sum of all the numbers are 6.5
Sum of float numbers are 3.5
Smallest integer number is 0
Count of prime numbers are 1. 2 is the only prime number among them.
Zero is present among the numbers. So the output is True.
Solution
import ast
# Enter your code here. Read input from STDIN. Print output to STDOUT
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def numbers(*args):
'''
Find the sum of all the numbers and store it in variable sum_of_numbers
'''
sum_of_numbers = sum(args)
'''
Find the sum of float numbers among the numbers and store it in variable sum_of_float_numbers
'''
sum_of_float_numbers = sum(i for i in args if isinstance(i, float))
'''
Find the smallest integer number among the numbers and store it in variable min_number
'''
min_number = min(i for i in args if isinstance(i, int))
'''
Find the count of prime numbers among the numbers and store it in variable prime_count
'''
prime_count = sum(1 for i in args if not isinstance(i, float) and is_prime(i))
'''
Check for zeros. If there are zeros present among the numbers, store True else store False in the variable zero_check
'''
zero_check = bool(sum(1 for i in args if i == 0 ))
print(sum_of_numbers)
print(sum_of_float_numbers)
print(min_number)
print(prime_count)
print(zero_check)
if __name__=='__main__':
num1 = ast.literal_eval(input())
num2 = ast.literal_eval(input())
num3 = ast.literal_eval(input())
num4 = ast.literal_eval(input())
numbers(num1,num2,num3,num4)
2. Objects and Data Structures
Function Data_oper
Write the function data_oper which accepts two strings as inputs and performs operations that are specified below
- Split the string string1 with whitespace as delimiter Remove the words that contain characters that are not alphabets Remove the words whose length is less than 3 Convert first letter of each word in to Upper case Store it in variable list1
- Split the string string2 with whitespace as delimiter Remove the words that contain characters that are not alphabets Remove the words whose length is less than 3 Convert first letter of each word in to Upper case Store it in variable list2
- Concatenate the lists list1,list2 and store it in variable list3
- Find the count of each word in the list list3 and store it in variable count_dictionary Type of count_dictionary - Dictionary Keys have to be words Dictionary must be ordered in ascending order based on Keys
- Find the unique words from words of list3 and store it in variable list3_uni Type of list3_uni - list list3_uni must be sorted in ascending order
- Find the common words in list1,list2 and store it in variable common_tuple Type of common_tuple - tuple Words have to unique and sorted in ascending order If there are no common words in list1,list2 store an empty tuple
- Find the words of list3 that ends with 's' and store it in variable ends_with Type of ends_with - tuple Words have to be unique and sorted in ascending order If there are no words in list3 that ends with 's', store an empty tuple
Sample Input
String1 : A glance at the outline map on page 4 may shows the location of theseancient works.
String2 : To begin with the Mounds and Earthworks themselves, it may be said that there are many thousands of them.
Sample Output
['Glance', 'The', 'Outline', 'Map', 'Page', 'May', 'Shows', 'The', 'Location', 'Theseancient', 'Begin', 'With', 'The', 'Mounds', 'And', 'Earthworks', 'May', 'Said', 'That', 'There', 'Are', 'Many', 'Thousands']
{'And': 1, 'Are': 1, 'Begin': 1, 'Earthworks': 1, 'Glance': 1, 'Location': 1, 'Many': 1, 'Map': 1, 'May': 2, 'Mounds': 1, 'Outline': 1, 'Page': 1, 'Said': 1, 'Shows': 1, 'That': 1, 'The': 3, 'There': 1, 'Theseancient': 1, 'Thousands': 1, 'With': 1}
['And', 'Are', 'Begin', 'Earthworks', 'Glance', 'Location', 'Many', 'Map', 'May', 'Mounds', 'Outline', 'Page', 'Said', 'Shows', 'That', 'The', 'There', 'Theseancient', 'Thousands', 'With']
('May', 'The')
('Earthworks', 'Mounds', 'Shows', 'Thousands')
Solution
# Enter your code here. Read input from STDIN. Print output to STDOUT
def data_oper(string1,string2):
'''
Split the string string1 with whitespace as delimiter
Remove the words that contain characters that are not alphabets
Remove the words whose length is less than 3
Convert first letter of each word in to Upper case
Store it in variable list1
Type of list1 - List
'''
list1 = string1.split()
list1 = list(filter(lambda x: x.isalpha() and len(x) >= 3, list1))
list1 = list(map(lambda x: x.title(), list1))
'''
Split the string string2 with whitespace as delimiter
Remove the words that contain characters that are not alphabets
Remove the words whose length is less than 3
Convert first letter of each word in to Upper case
Store it in variable list2
Type of list2 - List
'''
list2 = string2.split()
list2 = list(filter(lambda x: x.isalpha() and len(x) >= 3, list2))
list2 = list(map(lambda x: x.title(), list2))
'''
Concatinate the lists list1,list2 and store it in variable list3
Type of list3 - List
'''
list3 = list(list1 + list2)
'''
Find the count of each word in the list list3 and store it in variable count_dictionary
Type of count_dictionary - Dictionary
Keys have to be words
Dictionary must be ordered based on Keys
'''
count_dictionary = {}
for word in set(list3):
count_dictionary[word] = list3.count(word)
count_dictionary = dict(sorted(count_dictionary.items()))
'''
Find the unique words from words of list3 and store it in variable list3_uni
Type of list3_uni - list
list3_uni must be sorted in ascending order
'''
list3_uni = list(sorted(count_dictionary.keys()))
'''
Find the common words in list1,list2 and store it in variable common_tuple
Type of common_tuple - tuple
Words have to unique and sorted in ascending order
If there are no common words in list1,list2 store an empty tuple
'''
common_tuple = tuple(sorted(set(list1).intersection(set(list2))))
'''
Find the words of list3 that ends with 's' and store it in variable ends_with
Type of ends_with - tuple
Words have to be unique and sorted in ascending order
If there are no words in list3 that ends with 's', store an empty tuple
'''
ends_with = tuple(sorted(set(filter(lambda x: x.endswith('s'), list3))))
print(list3)
print(count_dictionary)
print(list3_uni)
print(common_tuple)
print(ends_with)
if __name__=='__main__':
string1 = input()
string2 = input()
data_oper(string1,string2)
3. Functions and Methods
Functions and Methods
Read the Instructions and code for the questions given.
- Write the function list_oper that takes two list as input. Function should return a string after performing the operations specified below:
- Check for the squares and cubes of elements in list1 are present in list2
- Return "Squares and Cubes are present" if squares and cubes of all elements in list1 are present in list2.
- Return "Squares are only present" if squares of all elements in list1 are only present in list2.
- Return "Cubes are only present" if cubes of all the elements in list1 are only present in list2.
- Return "No such pattern is present" if there are squares and cubes of any element in list1 is not present in list2
- Write the function armstrong which takes two numbers (num1,num2) as input. Function should return the list of armstrong numbers between num1 and num2.
- Write the function string_oper which takes a string as input and return another string after removing words that have characters repeated.
- Write a function string_reverse which takes a string as input and return another string with each word in reversed order.
Sample Case 0
Sample Input
[1,2,3,4]
[12,13,20,1,8,16,9,2,4,27,10,64]
10
200
HAMMER AND TONGS
Here is a characteristic letter
Sample Output
Squares and Cubes are present
[153]
AND TONGS
ereH si a citsiretcarahc rettel
Explanation
Squares and cubes of numbers 1,2,3,4 are present in list2. So the function list_oper should return "Squares and Cubes are present"
153 is the armstrong number present between the numbers 10 and 200. So the function armstrong should return [153]
'M' is repeated twice in the word "HAMMER" . So the function string_oper should return "AND TONGS" after removing the word "HAMMER" from the string.
Function string_reverse should return "ereH si a citsiretcarahc rettel" with each word in the string "Here is a characteristic letter" in reversed order
Solution
import ast
import sys
# Enter your code here. Read input from STDIN. Print output to STDOUT
'''
Write the function list_oper that takes two list as input.
Function should return a string after performing the operations specified below
- Check for the squares and cubes of elements in list1 is present in list2
- Return "Squares and Cubes are present" if squares and cubes of all elements in list1 are present in list2.
- Return "Squares are only present" if squares of all elements in list1 are only present in list2.
- Return "Cubes are only present" if cubes of all the elements in list1 are only present in list2.
- Return "No such pattern is present" if there are squares and cubes of any element in list1 is not present in list2
'''
def list_oper(list1,list2):
if all(i**2 in list2 and i**3 in list2 for i in list1):
return "Squares and Cubes are present"
elif all(i**2 in list2 for i in list1):
return "Squares are only present"
elif all(i**3 in list2 for i in list1):
return "Cubes are only present"
else:
return "No such pattern is present"
'''
Write the function amstrong which takes two numbers(num1,num2) as input. Function should return the list of amstrong numbers between num1 and num2
Example : [0,1,..]
'''
def amstrong(num1,num2):
amstrong_list = []
for i in range(num1,num2+1):
temp = i
sum = 0
length = len(str(i))
while temp != 0:
last_digit = temp % 10
sum += last_digit**length
temp //= 10
if sum == i:
amstrong_list.append(i)
return amstrong_list
'''
Write the function string_oper which takes a string as input and return another string after removing words that have characters repeated
Input : "This is an example"
Output : "This is an"
'''
def string_oper(string):
string = string.split()
string = [i for i in string if len(set(i)) == len(i)]
return ' '.join(string)
'''
Write a function string_reverse which takes a string as input and return another string with each word in reversed order.
Example : "START OF THE PROJECT"
Output : "TRATS FO EHT TCEJORP"
'''
def string_reverse(string):
string = string.split()
string = [i[::-1] for i in string]
return ' '.join(string)
if __name__=='__main__':
list1 = ast.literal_eval(input())
list2 = ast.literal_eval(input())
num1 = ast.literal_eval(input())
num2 = ast.literal_eval(input())
string1 = input()
string2 = input()
print(list_oper(list1,list2))
print(amstrong(num1,num2))
print(string_oper(string1))
print(string_reverse(string2))
4. Hierarchical Inheritance
Read the Instructions and code for the questions specified below
- Create a class Person
Initialize the class with first_name,last_name
Create a member function "Name" that returns the Full name of the person
Type of first_name - str
Type of last_name - str - Create another class Student that inherits the proporties of class Person
Initialize the class variable "count" with value 0
Initialize the class with fist_name,last_name,rollno,mark1,mark2
class variable count should have the number of students
Create a member function "GetStudent" that returns the fullname,rollno,total marks seperated by commas
Type of first_name - str
Type of last_name -str
Type of rollno - int
Type of mark1 - int
Type of mark2 - int - Create another class Staff that inherits the proporties of class Person
Initialize the class variable "count" with value 0
Initialize the class with fist_name,last_name,staffnum
class variable count should have the number of staffs
Create a member function "GetStaff" that returns the fullname and staffnumber seperated by comma
Type of first_name - str
Type of last_name -str
Type of staffnum - int
Sample Case 0
Sample Input
[["Joseph", "Paul",1, 100,30],["Mathew","John",2,100,0]]
[["Abin", "Joy", 1832],["Liza","Clare",1003]]
Sample Output
Abin Joy,1832
Liza Clare,1003
2
Joseph Paul,1,130
Mathew John,2,100
2
Solution
import ast
# Enter your code here. Read input from STDIN. Print output to STDOUT
'''
Create class Person
Initialize the class with first_name,last_name
Create a member function "Name" that returns the Full name of the person
Type of first_name - str
Type of last_name - str
'''
class Person:
def __init__(self,first_name,last_name):
self.first_name = first_name
self.last_name = last_name
def Name(self):
return self.first_name + " " + self.last_name
'''
Create another class Student that inherits the proporties of class Person
Initialize the class variable "count" with value 0
Initialize the class with fist_name,last_name,rollno,mark1,mark2
class variable count should have the number of students
Create a member function "GetStudent" that returns the fullname,rollno,total marks seperated by commas
Type of first_name - str
Type of last_name -str
Type of rollno - int
Type of mark1 - int
Type of mark2 - int
'''
class Student(Person):
count = 0
def __init__(self,first_name,last_name,rollno,mark1,mark2):
super().__init__(first_name,last_name)
self.rollno = rollno
self.mark1 = mark1
self.mark2 = mark2
Student.count += 1
def GetStudent(self):
return self.Name() + "," + str(self.rollno) + "," + str(self.mark1 + self.mark2)
'''
Create another class Staff that inherits the proporties of class Person
Initialize the class variable "count" with value 0
Initialize the class with fist_name,last_name,staffnum
class variable count should have the number of staffs
Create a member function "GetStaff" that returns the fullname and staffnumber seperated by comma
Type of first_name - str
Type of last_name -str
Type of staffnum - int
'''
class Staff(Person):
count = 0
def __init__(self,first_name,last_name,staffnum):
super().__init__(first_name,last_name)
self.staffnum = staffnum
Staff.count += 1
def GetStaff(self):
return self.Name() + "," + str(self.staffnum)
if __name__=='__main__':
students = ast.literal_eval(input())
staff = ast.literal_eval(input())
t = []
s = []
for i in staff:
t.append(Staff(i[0],i[1],i[2]))
for i in students:
s.append(Student(i[0],i[1],i[2],i[3],i[4]))
for i in t:
print(i.GetStaff())
print(Staff.count)
for i in s:
print(i.GetStudent())
print(Student.count)
0 comments:
Post a Comment