Friday, 31 January 2014

PYTHON QUESTION 2 FROM: MOTUNRAYO AFOLABI

Assume that there are n tyres that could be used by car, tricyle and bicyle. Write a program to accept n tyres and generate all the combinations (i.e the number) of car(s), tricycle(s) and bicyle(s) that could exhaust the n tyres. Ensure that all the objects have fair share where possible

SOLUTION

>>> car = 4
>>> tricycle = 3
>>> bicycle = 2
>>> def lessThanNineTyres(n,c,t,b):
     if n == 4:
           c += 1
     elif n == 3:
           t += 1
     elif n == 2:
           b +=1
     else:
           if (n == ( car + tricycle)):
                c += 1
                t += 1
           elif (n == (car + bicycle)):
                c += 1
                b += 1
           elif (n == (tricycle + bicycle)):
                t += 1
                b += 1
           else:
                if (b > t):
                     t += 2
                     b += 1
                else:
                     b += 2
                     c += 1
     countList = [c,t,b]
     return countList

>>> c = 0
>>> t = 0
>>> b = 0
>>> allTogether = 9
>>> n = input("enter numbers of tyres :")
 if n >= 9 :
     a = n / allTogether
     t += a
     c += a
     b += a
     k = n % allTogether
     if k > 1:
           countList =[]
           countList = lessThanNineTyres(k,c,t,b)
           c += countList[0]
           t += countList[1]
           b += countList[2]
else:
     result = lessThanNineTyres(n,c,t,b)
     c += result[0]
     t += result[1]
     b += result[2]
    

>>> print "cars :",c,"\t tricycle :",t,"\t bicycle :",b

No comments:

Post a Comment