Coding Question 1-
Create a function check_prime() that takes a number as an argument and returns 1 if the number is prime, 0 if it composite and -1 otherwise
Create another function print_list() that takes a list of numbers as input.
- Check_prime(l[1]) and print above conditions as output
- It uses check_prime() and creates a separate list of prime numbers and composite numbers.
- It returns the third list containing both the above lists.
Input-
count = 5
list of elements : 1 6 2 4 5
Output-
0
[[2, 5],[1, 6, 4]]
Solution-
def print_list(l):
prime = []
composite = []
result = []
for i in l:
if check_prime(i) == 1:
prime.append(i)
elif (check_prime(i) == 0):
composite.append(i)
result.append(prime)
result.append(composite)
return result
if __name__ == "__main__":
count = int(input())
l = list(map(int, input().split()))
print(check_prime(l[1]))
print(print_list(l))
Coding Question 2-
Create a Class Item and initialize attributes as
-> item_id (int)
-> item_name (String)
-> item_price (int)
-> quantity_available (int)
Create a method calculate_price() that takes a number quantity as an argument
-> Check if the quantity is greater than or equal to quantity_availabe or not.
-> If available, return price as quantity*item_price otherwise return 0.
Create one more Class Store which takes a list as parameters.
-> Initialize an attribute item_list that takes list of item objects.
-> Create a method generate_bill() that takes input as a dictionary (item_name : quantity).
-> It returns the total price of the items available in the dictionary by using the method calculate_price() in the Item Class
Input-
3
1
milk
70
7
2
bread
50
5
3
jam
60
2
2
bread
3
jam
1
Output-
140
210
Solution-
class Item:
def __init__(self, a, b, c, d):
self.id = a
self.name = b
self.price = c
self.quant = d
def calc_price(self, n):
if self.quant >= n:
return self.price * n
else:
return 0
class Store:
def __init__(self, l):
self.list = l
def generate_bill(self, d):
s = 0
for a, b in d.items():
for i in range(len(self.list)):
if self.list[i].name == a:
s += self.list[i].calc_price(b)
return s
if __name__ == '__main__':
count = int(input())
l = []
for i in range(count):
id = int(input())
name = input()
price = int(input())
quant = int(input())
itm = Item(id, name, price, quant)
l.append(itm)
s = Store(l)
d = {}
num = int(input())
for i in range(num):
iname = input()
quant = int(input())
d[iname] = quant
print(l[0].calc_price(2))
print(s.generate_bill(d))
theinfinitevisions
Thursday, August 20, 2020
Related Posts