Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 395977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:30:25+00:00 2026-05-12T16:30:25+00:00

def solve(numLegs, numHeads): for numSpiders in range(0, numHeads + 1): for numChicks in range(0,

  • 0
   def solve(numLegs, numHeads):
        for numSpiders in range(0, numHeads + 1):
            for numChicks in range(0, numHeads - numSpiders + 1):
                numPigs = numHeads - numChicks - numSpiders
                totLegs = 4*numPigs + 2*numChicks + 6*numSpiders 
                if totLegs == numLegs:
                    return [numPigs, numChicks, numSpiders]
        return [None, None, None]

    def barnYard(heads, legs):
        pigs, chickens, spiders = solve(legs, heads)
        if pigs == None:
            print "There is no solution."
        else:
            print 'Number of pigs: ', pigs
            print 'Number of Chickens: ', chickens
            print 'Number of Spider: ', spiders

    barnYard(20,56) # 8 pigs - 12 chickens
    barnYard(21,62) # 10 pig - 11 chickens

20 heads and 56 legs returns 8 pigs and 12 chickens, so I made it 21 and 62 to add a spider, but it still returns pigs and chickens, whats wrong in the code?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:30:25+00:00Added an answer on May 12, 2026 at 4:30 pm

    Your code is correct – in the first iteration of the outermost for loop, numChicks is 0. Since solve returns as soon as it finds a valid match, another possible valid match won’t be attempted.

    You could change the return statement into a yield statement and iterate over solve‘s results to get all possible combinations.

    For instance:

    def solve(numLegs, numHeads):
         for numBees in range(0, numHeads + 1):
                 for numChicks in range(0, numHeads - numBees + 1):
                         numPigs = numHeads - numChicks - numBees
                         totLegs = 4*numPigs + 2*numChicks + 6*numBees 
                         if totLegs == numLegs:
                                 yield [numPigs, numChicks, numBees]
    
    def barnYard(heads, legs):
        for pigs, chickens, bees in solve(legs, heads):
                 print 'Number of pigs: ', pigs
                 print 'Number of chickens: ', chickens
                 print 'Number of bees: ', bees
    
    barnYard(20,56)
    

    will output:

    Number of pigs:  8
    Number of chickens:  12
    Number of bees:  0
    
    Number of pigs:  6
    Number of chickens:  13
    Number of bees:  1
    
    Number of pigs:  4
    Number of chickens:  14
    Number of bees:  2
    
    Number of pigs:  2
    Number of chickens:  15
    Number of bees:  3
    
    Number of pigs:  0
    Number of chickens:  16
    Number of bees:  4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

def solve(numLegs, numHeads): for numChicks in range(0, numHeads + 1): numPigs = numHeads -
# Barn yard example: counting heads and legs def solve(numLegs, numHeads): for numChicks in
I'm trying to solve this newbie puzzle: I've created this function: def bucket_loop(htable, key):
Trying to solve a problem with templatetags. I have two templatetags: @register.inclusion_tag('directory/_alphabet.html') def alphabet_list(names):
def self.get(server) return unless server server = server.to_s if klass = @handlers[server] obj =
given this code: class Foo def bar return Bar.new end end class Bar ...
def recursive_insert(arr) return arr if arr.size<=1 recursive_insert(arr[0,arr.size-1]) i=arr.size-1 while arr[i-1]>arr[i] and i>0 arr[i],arr[i-1] =
I've seen a lot of posts regarding this, but none seem to solve my
I'm desperately trying to solve the following: trait Access[Res[_]] { def access[C]: Res[C] }
I have test() as shown below: def test(arg1, arg2=None, arg3=None): Now, I tries to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.