Python Proctored Assessment Programming solution 25th January

Question 1


Make a function check_prime() that takes a number as argument. It returns 1 if the number is prime, 0 if it is composite and -1 otherwise.

Make another function composite_prime_list() that takes a list of numbers as input. It uses check_prime() and creates seperate lists of prime and composite numbers. It returns a third list containing both the above lists

 Solution:

def isprime(a):
    if a<=1:
        return -1
   else:
        for i in range(2,int(a/2)+1):
            if a%i==0:
                return 0
        return 1

def printlist(l):
    p=[]
    c=[]
    r=[]
    for i in l:
        if isprime(i)==1:
            p.append(i)
       elif isprime(i)==0:
            c.append(i)
    r.append(p)
    r.append(c)
    return r

if name=='_main_':
    l=[]
    for _ in range(int(input())):
        l.append(int(input()))
    print(isprime(l[1])
    print(printlist(l))



Question 2

Make a Class Item having 4 attributes- item_id, item_name, item_price and quantity_available.


It has a Method calc_price() that takes a number 'quantity' is argument and checks if the provided quantity is greater than or equal to quantity_available attribute. If so, it returns the total price as (quantity * item_price). Otherwise, it returns 0.

Create another class Store that has one attribute item_list (list of Item objects).
It has a method generate_bill() that takes a dictionary having (item_name: quantity). It returns total price for the items in the dictionary if available in store. It uses calc_price() method of item class

Solution:

class Item:
  def __init__(self,a,b,c,d):
      self.iid=a
      self.name=b
      self.price=c
      self.quant=d
  def calc(self,n):
     if self.quant>=n:
       return self.price*n
     else:
       return 0

class Store:
  def __init__(self,l):
    self.list1=l
    
  def gen_bill(self,d):
    s=0
    for a,b in d.items():
      for i in range(len(self.list1)):
        if self.list1[i].name==a:
          s+= self.list1[i].calc(b)
    return s
    
if __name__=='__main__':
  l=[]
  c=int(input())
  for _ in range (c):
    i=Item(int(input()),input(),int(input()),int(input()))
    l.append(i)
  s=Store(l)
  d={}
  for _ in range(int(input())):
    n=input()
    q=int(input())
    d[n]=q
  print(l[0].calc(2))
  print(s.gen_bill(d))

 

©2018 by The real one. Proudly created with Wix.com