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 394063
In Process

The Archive Base Latest Questions

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

def solve(numLegs, numHeads): for numChicks in range(0, numHeads + 1): numPigs = numHeads –

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

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

I’m learning Python and came across this example, can someone please explain in plain english (or pseudo-code) what this is doing line by line.

Many 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:19:09+00:00Added an answer on May 12, 2026 at 4:19 pm

    solve is computing how many chicks (1 head, 2 legs) and how many pigs (1 head, 4 legs) it takes to total up to the given numbers of heads and legs.

    It uses a “brute force”, that is, maximally simple, approach:

    • it tries even possible number of
      chicks from none at all to as many as
      was specified as number of heads
      (that’s the role of the loop for
      numChicks in range(0, numHeads +
      1):
      , since range gives integers
      from the starting value included
      to the ending value excluded);
    • for each given numChicks it computes
      how many pigs there would be to give
      the requested number of heads, by the
      statement numPigs = numHeads - numChicks
    • then it computes how many total legs
      those chicks and pigs would have, by
      totLegs = 4*numPigs + 2*numChicks
    • then it checks if the totLegs equal
      the requested number: if so, it returns
      a list with two items, the numbers of
      chicks and pigs that solve the problem
    • lastly, if it “falls of the bottom” of
      the for loop without having returned
      a value yet, it knows there’s no solution,
      and signifies that by returning a list
      each of whose two items is None.

    barnYard just delegates the solution to solve, and prints it out in a nice readable way, either as “no solution” or as nicely decorated numbers of chicks and pigs.

    Now, to keep progressing, ask yourself if solve could be written more efficiently. Clearly there is no solution if the number of legs is less than twice the number of heads, or more than four times the number of heads, or odd — maybe solve could test for those case and return [None, None] immediately. Could you code that…?

    It may not be obvious, but every other combination of numbers of heads and legs has a solution — and there IS a way to find it just by arithmetic, without looping. Think about it, maybe with the help of elementary middle-school algebra…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

# Barn yard example: counting heads and legs def solve(numLegs, numHeads): for numChicks in
def solve(numLegs, numHeads): for numSpiders in range(0, numHeads + 1): for numChicks in range(0,
def answer_solve_sudoku(__grid): res = check_sudoku(__grid) if res is None or res is False: return
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 download_if_dne(href, filename): if os.path.isfile(filename): # print 'already downloaded:', href return False else: if
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 want to solve this problem in Scala. My code: def dividers(n: Int) =
I have the following code: def causes_exception(lamb): try: lamb() return False except: return True

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.