Mini-Project - Python FSP - Sprint1 Case Study - Data Structures
Welcome to Python Data Structures
Question 1: Basics of Python
Solution 1:
# Enter your code here. Read input from STDIN. Print output to STDOUT
import ast
def numbers(num1,num2,num3,num4):
num_list=[num1,num2,num3,num4]
'''
Find the sum of all the numbers and store it in variable sum_of_numbers
'''
def sum_of_number(input_list):
count =0
for i in input_list:
count +=i
return count
sum_of_numbers = sum_of_number(num_list)
'''
Find the sum of float numbers among the numbers and store it in variable sum_of_float_numbers
'''
y= [i for i in num_list if isinstance(i, float)]
sum_of_float_numbers = sum_of_number(y)
'''
Find the smallest integer number among the numbers and store it in variable min_number
'''
def filter_list_int(mix_list):
int_list= []
for items in mix_list:
if isinstance(items , int):
int_list.append(items)
return int_list
def find_small_num(a):
smallest_int= []
for i in a:
if len(smallest_int)<=0 :
smallest_int.append(i)
else:
if i<smallest_int[0] and type(i)!=float:
smallest_int.pop()
smallest_int.append(i)
return smallest_int[0]
min_number = find_small_num( filter_list_int(num_list))
'''
Find the count of prime numbers among the numbers and store it in variable prime_count
'''
def prime(item_list):
prime_list=[]
for item in item_list:
is_prime = True
for index_item in range(2,int(item)):
if item % index_item==0:
is_prime=False
if is_prime and type(item)!= float and item>1:
prime_list.append(item)
return len(prime_list)
prime_count = prime(num_list)
'''
Check for zeros. If there are zeros present among the numbers, store True else store False in the variable zero_check
'''
def find_zeros(input_list):
zeros=0 in input_list
return zeros
zero_check= find_zeros(num_list)
print(sum_of_numbers)
print(sum_of_float_numbers)
print(min_number)
print(prime_count)
print(zero_check)
# Refer '__main__' method code which is given below if required.
if __name__ == '__main__':
# Starting from here:
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)
Question 2: Objects and Data Structures.
Solution 2:
# 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
'''
def split_string(string):
string_list = string.strip().split()
string_list_cap = [ words.capitalize() for words in string_list if words.isalpha() and len(words)>2]
return string_list_cap
list1= split_string(string1)
'''
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= split_string(string2)
'''
Concatinate the lists list1,list2 and store it in variable list3
Type of list3 - List
'''
list3 = [*list1, *list2]
# list3= [single_list_item for x in [list1, list2] for single_list_item in x] # it will also work
# list3 = list1+list2 # it will also work
list4= list3.copy()
list4.sort()
'''
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 = { key:list4.count(key) for key in list4 }
'''
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
'''
list4_copy = list4.copy()
list3_uni = list(set(list4_copy))
list3_uni.sort()
'''
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
'''
unique_list1 = set(list1)
unique_list2 = set(list2)
fun = lambda x:x in unique_list2
common_words = tuple(filter(fun, unique_list1))
common_tuple= tuple(sorted(common_words))
'''
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
'''
s = [ i for i in list4 if i.endswith("s")]
s = list(set(s))
ends_with = tuple(sorted(s))
print(list3)
print(count_dictionary)
print(list3_uni)
print(common_tuple)
print(ends_with)
# Refer '__main__' method code which is given below if required.
if __name__ == '__main__':
# Starting from here:
if __name__=='__main__':
string1 = input()
string2 = input()
data_oper(string1,string2)
Question 3: Functions and Methods.
Solution 3:
import ast
# 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 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( num_list, sqr_cub_list):
# l2 = list(set(l2))
squ= True
cub= True
for i in num_list:
if squ:
if i**2 not in sqr_cub_list :
squ= False
if cub:
if i**3 not in sqr_cub_list:
cub = False
if squ and cub:
return "Squares and Cubes are present"
if squ:
return "Squares are only present"
if cub:
return "Cubes are only present"
if not squ and not cub:
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_num = []
for num in range(num1 , num2+1):
num_len = len(str(num))
str_num_list= list(str(num))
int_num_list = list(map( lambda x: int(x), str_num_list))
q=[]
for j in int_num_list :
q.append(j ** num_len)
if sum(q) == num:
amstrong_num.append(num)
return amstrong_num
'''
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_list= string.split()
unique_char = []
for word in string_list:
for character in word :
if character in unique_char:
string = string.replace(word, '').replace(" "," ")
break
else:
unique_char.append(character)
unique_char.clear()
return string.strip()
# Alternative Solution
# Comment line from 66 to 76
# Uncomment only line 80, it will also work.
# return " ".join(word for word in ss.split() if len(set(word)) == len(word))
'''
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_list = string.split()
rev_str = ""
for word in string_list:
rev_str+= "".join(word[::-1]+" ")
return rev_str
# Alternative Solution
# Comment line 95 to 101
# Uncomment only line 105, it will also work.
# return " ".join("".join(list(reversed(ss))).split()[::-1])
# Refer '__main__' method code which is given below if required.
if __name__ == '__main__':
# Starting from here:
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))
Question 4: Hierarchical inheritance.
Solution 4:
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 ' '.join([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, fist_name, last_name, rollno,mark1,mark2):
super().__init__(fist_name, last_name)
self.first_name = fist_name
self.last_name = last_name
self.rollno = rollno
self.mark1 = mark1
self.mark2 = mark2
self.count = Student.count+1
Student.count = self.count
def GetStudent(self):
return Person.Name(self)+","+ 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, fist_name, last_name, staffnum):
super().__init__(fist_name, last_name)
self.first_name = fist_name
self.last_name = last_name
self.staffnum = staffnum
self.count = Staff.count + 1
Staff.count = self.count
def GetStaff(self):
return Person.Name(self)+","+ str(self.staffnum)
# Refer '__main__' method code which is given below if required.
if __name__ == '__main__':
# Starting from here:
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)