Python counting number of livestock -


this question has answer here:

to illustrate have 2 type of animals 1 pig , other chicken. user inputs number of heads , legs 2 particular type of animals. example if run , input 20 heads , 56 legs 12 chicken , 8 pigs. have @ here:

def solve(numlegs, numheads):     numchicks in range(0, numheads + 1): # how 12 chickens here?         numpigs = numheads - numchicks # numpigs = 20 - 12 = 8 ?         totlegs = 4 * numpigs + 2* numchicks # 4 * 8 + 2 * 12 ?         if totlegs == numlegs:            return [numpigs, numchicks]     return[none, none]  def barnyard():     heads = int(raw_input('enter number of heads:'))     legs = int(raw_input('enter number of legs:'))     pigs, chickens = solve(legs, heads)     if pigs = none:        print 'there no solution'     else:         print 'number of pigs:' , pigs         pirnt 'number of chickes:', chickens 

we calculate totlegs = 4 * numpigs + 2* numchicks knowing pigs have 4 legs , chickens have 2 , later verify if matches input number of legs right? query @ point for numchicks in range(0, numheads + 1): how finds number of chicken 12? if number of chicken not found can not proceed next lines if not wrong. can explain this? confuses me lot. algebra version can found here. link

don't make life more complicated is, use print command see intermediate values, simple algebra. way, have many errors in code , doesn't compile (mostly typos such pirnt instead of print). see here fixed code works:

def solve(numlegs, numheads):     numchicks in range(0, numheads + 1): #we have got 12 chickens here?         print "numchicks = ", numchicks         numpigs = numheads - numchicks         print "numpigs = ", numpigs         totlegs = 4 * numpigs + 2* numchicks         print "totlegs = ",totlegs         if totlegs == numlegs:             print "numlegs = ",numlegs             print "numpigs = ",numpigs             print "numchicks = ",numchicks             return [numpigs, numchicks]     return[none, none]  def barnyard():     heads = int(raw_input('enter number of heads:'))     legs = int(raw_input('enter number of legs:'))     pigs, chickens = solve(legs, heads)     if pigs == none:        print 'there no solution'     else:         print 'number of pigs:' , pigs         print 'number of chicken:', chickens    if __name__ == "__main__":     barnyard() 

output

enter number of heads:20 enter number of legs:56 numchicks =  0 numpigs =  20 totlegs =  80 numchicks =  1 numpigs =  19 totlegs =  78 numchicks =  2 numpigs =  18 totlegs =  76 numchicks =  3 numpigs =  17 totlegs =  74 numchicks =  4 numpigs =  16 totlegs =  72 numchicks =  5 numpigs =  15 totlegs =  70 numchicks =  6 numpigs =  14 totlegs =  68 numchicks =  7 numpigs =  13 totlegs =  66 numchicks =  8 numpigs =  12 totlegs =  64 numchicks =  9 numpigs =  11 totlegs =  62 numchicks =  10 numpigs =  10 totlegs =  60 numchicks =  11 numpigs =  9 totlegs =  58 numchicks =  12 numpigs =  8 totlegs =  56 numlegs =  56 numpigs =  8 numchicks =  12 number of pigs: 8 number of chicken: 12 

it simple math. you have 20 heads, 56 legs. every pigs has 4 legs, every chicken 2 legs

so

56 = 4*x+2*y    x+y=20 

which means x = 20 - y

then 56 = 4*(20-y)+2*y => 56 = 80 -4y+2y => 2y = 80 - 56 => 2y = 24 => y = 12 chicken

then x + 12 = 20 => x = 8 pigs

:d

hope helps !!


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -