Skip to content

Instantly share code, notes, and snippets.

@jaymcgrath
Created October 28, 2016 18:05
Show Gist options
  • Save jaymcgrath/ede53be86129524b9d216fc795993ea7 to your computer and use it in GitHub Desktop.
Save jaymcgrath/ede53be86129524b9d216fc795993ea7 to your computer and use it in GitHub Desktop.
Solving Chinese Puzzle of Rabbits and Chickens with a list comprehension in Python
"""
Given a farm with rabbits and chickens, you count 35 heads and 94 legs. How many of each animal do you have?
Assume all rabbits and chickens have 4 and 2 legs respectively.
"""
from itertools import combinations_with_replacement
print([x for x in combinations_with_replacement([2,4],35) if sum(x) == 94])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment