Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
24,646 views
in Service-based-companies by Expert (109,130 points)
retagged by | 24,646 views

3 Answers

0 like 0 dislike
Best answer

Python Problem : 

Solution :

 

 

 

by Expert (109,130 points)
selected by
1 0
My solution:>>>

class Blood:
    def __init__(self,bg,units):
        self.bloodGroup=bg.upper()
        self.unitInHand=units
        
class BloodBank:
    def __init__(self,bl):
        self.bloodList=bl

    def isBloodAvailable(self,b,u):
        b=b.upper()
        y = False
        for i in self.bloodList:
            if i.bloodGroup==b:
                if i.unitInHand>=u:
                    y = True
        return y            
        
        
    def findMinBloodStock(self):
        minlst=[]
        minn=self.bloodList[0].unitInHand
        for i in self.bloodList:
            if i.unitInHand<minn:
                minn=i.unitInHand
        for i in self.bloodList:
            if i.unitInHand==minn:
                print(i.bloodGroup)
                
        return ''

if __name__=='__main__':                
        
    bl=[]
    n=int(input())
    for i in range(n):
        bg=input()
        units=int(input())
        bl.append(Blood(bg,units))
    
    o=BloodBank(bl)
    b=input()
    u=int(input())
    ba=o.isBloodAvailable(b,u)
    if ba==True:
        print("Blood Available")
    else:
        print("Blood not Available")
    print(o.findMinBloodStock())
0 like 0 dislike

Another Python Solution : 

class Blood:
  def __init__(self,group,unit):
    self.group=group
    self.unit=unit
class BloodBank:
  def __init__(self,bloodList):
    self.bloodList=bloodList
  def available(self,group_given,unit_given):
    for i in self.bloodList:
      if i.group.upper()==group_given.upper() and i.unit>=unit_given:
        return True
    return False
  def minBlood(self):
    min=self.bloodList[0].unit
    l=[]
    for i in self.bloodList:
      if i.unit<min:
        min=i.unit
    for i in self.bloodList:
      if i.unit==min:
        l.append(i.group)
    return l
if __name__=='__main__':
  n=int(input())
  bloodList=[]
  for i in range(n):
    grp=input().upper()
    unit=int(input())
    bloodList.append(Blood(grp,unit))
  ob=BloodBank(bloodList)
  group_given=input()
  unit_given=int(input())
  p=ob.available(group_given,unit_given)
  if p==True:
    print('Blood Available')
  else:
    print('Blood Not Available')
  j=ob.minBlood()
  for i in j:
    print(i)
by Expert (109,130 points)
1 like 0 dislike
class Blood:
    def __init__(self, bloodGroup, unitinHand):
        self.bloodGroup = bloodGroup
        self.unitinHand = unitinHand

class BloodBank:
    def __init__(self,bloodList):
        self.bloodList = bloodList

    def isBloodAvailable(self, blood_grp, units):
        for blood in self.bloodList:
            if blood.bloodGroup==blood_grp and units<=blood.unitinHand:
                blood.unitinHand-=units
                return True
        return False

    def findMinBloodStock(self):
        minBloodCounts = self.bloodList[0].unitinHand
        minBlood = self.bloodList[0].bloodGroup
        for i in range(1,len(self.bloodList)):
            if(self.bloodList[i].unitinHand < minBloodCounts):
                minBlood = self.bloodList[i].bloodGroup
                minBloodCounts = self.bloodList[i].unitinHand
        minBloodList = []
        for blood in self.bloodList:
            if blood.unitinHand == minBloodCounts:
                minBloodList.append(blood.bloodGroup)
        return minBloodList

if __name__ == '__main__':
    bloodList = []
    for _ in range(int(input())):
        blood_grp = input("Enter the blood group: ").upper()
        units = int(input("Units present in the blood bank: "))
        bloodList.append(Blood(blood_grp,units))
    
    blood_req = input("Enter required blood group: ").upper()
    units = int(input("Enter units of blood required: "))
    print("Blood is available" if BloodBank(bloodList).isBloodAvailable(blood_req,units) else "Blood is not available")
    print(*BloodBank(bloodList).findMinBloodStock(), sep = ",")
by