#Description

Everybody has probably heard of the animal heads and legs problem from the earlier years at school. It goes:

```“A farm contains chickens and cows. There are x heads and y legs. How many chickens and cows are there?” ```

Where x <= 1000 and y <=1000

#Task

Assuming there are no other types of animals, work out how many of each animal are there. 

```Return a tuple in Python - (chickens, cows) and an array list - [chickens, cows]/{chickens, cows} in all other languages```

If either the heads & legs is negative, the result of your calculation is negative or the calculation is a float return "No solutions" (no valid cases).

In the form:
```python
(Heads, Legs) = (72, 200)

VALID - (72, 200) =>             (44 , 28) 
                             (Chickens, Cows)

INVALID - (72, 201) => "No solutions"
```

However, ```if 0 heads and 0 legs are given always return [0, 0]``` since zero heads must give zero animals.

There are many different ways to solve this, but they all give the same answer.

You will only be given integers types - however negative values (edge cases) will be given. 

Happy coding!

Source: codewars | Difficulty: EASY
Tags: Matrices

Mathematics

Starter code: def animals(heads, legs):
	

Solutions:
def animals(heads, legs):
	(chickens, cows) = (2 * heads - legs / 2, legs / 2 - heads)
	if chickens < 0 or cows < 0 or (not chickens == int(chickens)) or (not cows == int(cows)):
		return 'No solutions'
	return (chickens, cows)


def animals(heads, legs):
	solution = (2 * heads - legs / 2, legs / 2 - heads)
	return solution if legs % 2 == 0 and solution[0] >= 0 and (solution[1] >= 0) else 'No solutions'


def animals(heads, legs):
	cows = (legs - 2 * heads) / 2
	chicken = heads - cows
	return (chicken, cows) if cows == int(cows) and cows >= 0 and (chicken >= 0) else 'No solutions'


def animals(h, l):
	co = (l - 2 * h) / 2
	ch = h - co
	return 'No solutions' if co.is_integer() == False else (h - co, co) if (co < 0 or ch < 0) == False else 'No solutions'


def animals(heads, legs):
	(ch, co) = (2 * heads - legs / 2, legs / 2 - heads)
	return (0, 0) if heads == 0 and legs == 0 else (ch, co) if ch == int(ch) and co == int(co) and (ch >= 0) and (co >= 0) else 'No solutions'


def animals(heads, legs):
	if heads == 0 and legs == 0:
		return (0, 0)
	if legs % 2:
		return 'No solutions'
	y = (legs - 2 * heads) / 2
	if int(y) != y or heads - y < 0 or y < 0:
		return 'No solutions'
	else:
		return (heads - y, y)


def animals(heads, legs):
	aus = ((4 * heads - legs) / 2, (legs - 2 * heads) / 2)
	return aus if all((v >= 0 and v.is_integer() for v in aus)) else 'No solutions'


def animals(heads, legs):
	(Chickens, Cows) = (2 * heads - legs / 2, legs / 2 - heads)
	if Chickens < 0 or Cows < 0 or (not Chickens == int(Chickens)) or (not Cows == int(Cows)):
		return 'No solutions'
	return (Chickens, Cows)


def animals(heads, legs):
	a = (4 * heads - legs) / 2
	b = (legs - 2 * heads) / 2
	if a == int(a) and 0 <= a <= 1000 and (0 <= b <= 1000) and (b == int(b)):
		return (a, b)
	else:
		return 'No solutions'


def animals(heads, legs):
	cows = abs((legs - 2 * heads) // 2)
	chicken = abs((4 * heads - legs) // 2)
	return (chicken, cows) if cows + chicken == heads else 'No solutions'


animals = lambda h, l: (lambda c=(l - h * 2) / 2: (lambda ch=h - c: (ch, c) if abs(ch) + int(abs(c)) == h else 'No solutions')())()


def animals(heads, legs):
	if heads == 0 and legs == 0:
		return (0, 0)
	elif heads <= 0 or legs <= 0:
		return 'No solutions'
	elif heads >= 1000 or legs >= 1000:
		return 'No solutions'
	elif heads * 2 == legs:
		return (heads, 0)
	elif heads * 4 == legs:
		return (0, heads)
	else:
		number_of_animals = heads
		chicken = number_of_animals * 2
		cows = (legs - chicken) / 4 * 2
		chicken = number_of_animals - cows
		if chicken <= 0 or cows <= 0:
			return 'No solutions'
		elif chicken.is_integer():
			return (chicken, cows)
		else:
			return 'No solutions'


from typing import Tuple, Union

def animals(heads: int, legs: int) -> Union[str, Tuple[int]]:
	(chickens, cows) = (heads - (legs / 2 - heads), legs / 2 - heads)
	return (chickens, cows) if not any([chickens < 0, cows < 0, not chickens.is_integer(), not cows.is_integer()]) else 'No solutions'


def animals(heads, legs):
	return (2 * heads - legs // 2, legs // 2 - heads) if not legs % 2 and 2 * heads >= legs // 2 >= heads >= 0 else 'No solutions'


def animals(heads, legs):
	if heads * 2 > legs or heads * 4 < legs or legs % 2:
		return 'No solutions'
	cows = legs // 2 - heads
	return (heads - cows, cows)


def animals(heads, legs):
	y = legs / 2 - heads
	x = heads - y
	return (x, y) if all([int(x) == x, int(y) == y, x >= 0, y >= 0]) else 'No solutions'


def animals(heads, legs):
	for x in range(heads + 1):
		if 2 * x + 4 * (heads - x) == legs:
			return (x, heads - x)
	return 'No solutions'


def animals(heads, legs):
	cows = (legs - 2 * heads) / 2
	chickens = heads - cows
	if '.5' in str(cows) or cows < 0 or chickens < 0:
		return 'No solutions'
	return (chickens, cows)


def animals(heads, legs):
	if legs < 2 * heads or 4 * heads - legs < 0 or legs * heads < 0 or legs % 2:
		return 'No solutions'
	return (int((4 * heads - legs) / 2), int((legs - 2 * heads) / 2))


animals = lambda h, l: l % 2 < 1 and h <= l // 2 <= h * 2 and (2 * h - l // 2, l // 2 - h) or 'No solutions'


animals = lambda h, l: (lambda c: 'No solutions' if c < 0 or c > h or c % 1 != 0 else (h - c, c))((l - 2 * h) / 2.0)


def animals(heads, legs):
	chickens = heads - (legs - heads * 2) / 2
	cows = (legs - heads * 2) / 2
	if heads == 0 and legs == 0:
		return (0, 0)
	elif chickens.is_integer() and chickens >= 0 and cows.is_integer and (cows >= 0):
		return (chickens, cows)
	else:
		return 'No solutions'


def animals(heads, legs):
	result = ((heads * 4 - legs) / 2, heads - (heads * 4 - legs) / 2)
	if heads < 0 or legs < 0 or legs % 2 or (min(result) < 0):
		return 'No solutions'
	return result


def animals(heads, legs):
	chickens = 2 * heads - legs / 2
	cows = legs / 2 - heads
	if legs >= heads >= 0 and chickens >= 0 and (cows >= 0) and (chickens == chickens // 1) and (cows == cows // 1):
		return (chickens, cows)
	else:
		return 'No solutions'


def animals(heads, legs):
	print((heads, legs))
	if heads == 0 and legs == 0:
		return (0, 0)
	elif heads <= 0 or legs <= 0:
		return 'No solutions'
	elif legs % 4 == 0 and legs / 4 == heads:
		return (0, heads)
	elif legs % 2 == 0 and legs / 2 == heads:
		return (heads, 0)
	else:
		cow_try = legs / 4
		chicken = (heads - cow_try) * 2
		cow = heads - chicken
		if cow // 1 + chicken // 1 == heads and cow > 0 and (chicken > 0):
			return (chicken, cow)
		else:
			return 'No solutions'


def animals(heads, legs):
	cow = (legs - 2 * heads) / 2
	if cow < 0 or cow != round(cow):
		return 'No solutions'
	chicken = heads - cow
	if chicken < 0:
		return 'No solutions'
	return (chicken, cow)


def animals(heads, legs):
	if heads == legs == 0:
		return (0, 0)
	if heads < 0 or legs < 0 or legs % 2 == 1:
		return 'No solutions'
	for i in range(heads + 1):
		cows = i * 4
		chickens = (heads - i) * 2
		if legs == chickens + cows:
			return (chickens / 2, cows / 4)
	return 'No solutions'


def animals(heads, legs):
	if 1000 >= heads < 0 or 1000 >= legs < 0:
		return 'No solutions'
	if heads < 0 and legs < 0:
		return 'No solutions'
	if heads == 0 and legs == 0:
		return (0, 0)
	if legs % 2 != 0:
		return 'No solutions'
	cows = int(legs / 2 - heads)
	chickens = int(heads - cows)
	if cows >= 0 and chickens >= 0:
		return (chickens, cows)
	else:
		return 'No solutions'


def animals(heads, legs):
	if heads == 0 and legs == 0:
		return (0, 0)
	cows = (legs - heads * 2) / 2
	if heads <= 0 or legs <= 0 or cows > heads or (cows % 1 != 0) or (cows < 0):
		return 'No solutions'
	return (heads - cows, cows)


def animals(heads, legs):
	rem = legs - 2 * heads
	cow = rem / 2
	chk = heads - rem / 2
	if heads < 0 or legs < 0 or rem < 0 or rem % 2 or (chk < 0):
		return 'No solutions'
	else:
		return (chk, cow)


def animals(heads, legs):
	x = (legs - 2 * heads) / 2
	y = heads - x
	if x < 0 or y < 0:
		return 'No solutions'
	elif x.is_integer() or y.is_integer():
		return (y, x)
	else:
		return 'No solutions'


def animals(heads, legs):
	if heads == 0 and legs == 0:
		return (0, 0)
	chickens = (4 * heads - legs) / 2
	cows = heads - chickens
	all_good = chickens.is_integer() and chickens >= 0 and (cows >= 0)
	return (chickens, cows) if all_good else 'No solutions'


def animals(heads, legs):
	i = 0
	while i <= heads:
		chicken_number = i
		cow_number = heads - chicken_number
		if chicken_number * 2 + cow_number * 4 == legs:
			return (chicken_number, cow_number)
		i += 1
	return 'No solutions'


def animals(heads, legs):
	b = legs / 2 - heads
	a = heads - b
	if int(a) < 0 or int(b) < 0:
		return 'No solutions'
	elif int(a) != float(a) or int(b) != float(b):
		return 'No solutions'
	elif int(a) == 0 and int(b) == 0:
		return (0, 0)
	else:
		return (int(a), int(b))


def animals(heads, legs):
	chickens = (heads * 4 - legs) / 2
	cows = heads - chickens
	if chickens >= 0 and cows >= 0 and (chickens != 11.5):
		return (chickens, cows)
	else:
		return 'No solutions'


def animals(h, l):
	cow = (l - 2 * h) / 2
	ch = h - cow
	return 'No solutions' if cow < 0 or ch < 0 or (not cow.is_integer()) or (not ch.is_integer()) else (ch, cow)


def animals(heads, legs):
	(a, b) = (2 * heads - legs / 2, legs / 2 - heads)
	if a < 0 or b < 0 or [a % 1, b % 1] != [0, 0]:
		return 'No solutions'
	else:
		return (a, b)


def animals(heads, legs):
	chickens = a = (4 * heads - legs) / 2
	cows = b = (legs - 2 * heads) / 2
	if heads == legs == 0:
		return (0, 0)
	if a < 0 or b < 0 or [a % 1, b % 1] != [0, 0]:
		return 'No solutions'
	else:
		return (chickens, cows)


def animals(heads, legs):
	for i in range(501):
		for j in range(251):
			if heads == i + j and i * 2 + j * 4 == legs:
				return (i, j)
	return 'No solutions'


def animals(h, l):
	return (2 * h - 1 / 2 * l, -h + 1 / 2 * l) if (2 * h - 1 / 2 * l).is_integer() and (-h + 1 / 2 * l).is_integer() and (2 * h - 1 / 2 * l >= 0) and (-h + 1 / 2 * l >= 0) else 'No solutions'


def animals(heads, legs):
	if heads == 0 and legs == 0:
		return (0, 0)
	print(heads, legs)
	if legs > heads * 4 or heads * 2 > legs or heads < 0 or (legs < 0) or ((legs - heads * 2) % 2 != 0):
		return 'No solutions'
	cows = (legs - heads * 2) // 2
	chickens = heads - cows
	return (chickens, cows)


def animals(heads, legs):
	(ch, co) = (heads * 2 - legs / 2, legs / 2 - heads)
	if ch < 0 or co < 0 or (not ch.is_integer()) or (not co.is_integer()):
		return 'No solutions'
	else:
		return (ch, co)


def animals(heads, legs):
	Chickens = (4 * heads - legs) / 2
	Cows = heads - Chickens
	return (Chickens, Cows) if Cows >= 0 and Chickens >= 0 and (int(Chickens) == Chickens) else 'No solutions'


def animals(heads, legs):
	for i in range(0, heads + 1):
		for j in range(0, heads + 1):
			if i + j == heads and legs == i * 2 + j * 4:
				return (i, j)
	return 'No solutions'


def animals(heads, legs):
	if heads < 0 or legs < 0 or heads > 1000 or (legs % 2 == 1):
		return 'No solutions'
	else:
		x = (4 * heads - legs) / 2
		y = heads - x
		if x < 0 or y < 0:
			return 'No solutions'
		else:
			return (x, y)


def animals(heads, legs):
	cows = (legs - 2 * heads) / 2
	chickens = heads - cows
	if cows < 0 or chickens < 0 or heads < 0 or (legs < 0):
		return 'No solutions'
	elif cows != int(cows):
		return 'No solutions'
	else:
		return (chickens, cows)


def animals(heads, legs):
	if heads == 0 and legs == 0:
		return (0, 0)
	elif heads < 0 or legs < 0 or heads > legs or (legs > heads * 4) or legs % 2:
		return 'No solutions'
	else:
		cows = (heads * 4 - legs) // 2
		chickens = heads - (heads * 4 - legs) // 2
		return (cows, chickens) if heads - cows - chickens == 0 else 'No solution'


def animals(heads, legs):
	cows = (legs - heads * 2) / 2
	chickens = (legs - cows * 4) / 2
	if cows % 1 != 0 or chickens % 1 != 0 or cows < 0 or (chickens < 0):
		return 'No solutions'
	return (int(chickens), int(cows))


def animals(heads, legs):
	c = (legs - 2 * heads) // 2
	if c < 0 or heads - c < 0 or c * 4 + (heads - c) * 2 != legs:
		return 'No solutions'
	else:
		return (heads - c, c)


def animals(heads, legs):
	if legs % 2 == 1 or legs > 4 * heads or legs < 2 * heads:
		return 'No solutions'
	return (heads - (legs // 2 - heads), legs // 2 - heads)


def animals(heads, legs):
	if (heads - (legs - heads * 2) / 2) % 1 != 0 or (heads - (legs - heads * 2) / 2) % 1 < 0 or int((legs - heads * 2) / 2) < 0:
		return 'No solutions'
	elif heads == 0 and legs == 0:
		return (0, 0)
	elif heads <= 0 or legs <= 0:
		return 'No solutions'
	else:
		hen = heads - int((legs - heads * 2) / 2)
		cow = int((legs - heads * 2) / 2)
		if hen < 0 or cow < 0:
			return 'No solutions'
		else:
			return (hen, cow)


def animals(h, l):
	print(h, l)
	a = (4 * h - l) / 2
	b = h - a
	return (a, b) if a >= 0 and b >= 0 and (a == int(a)) and (b == int(b)) else 'No solutions'


def animals(h, l):
	(ck, cw) = (2 * h - l // 2, l // 2 - h)
	return (ck, cw) if h >= 0 and l >= 0 and (not l % 2) and (ck >= 0) and (cw >= 0) else 'No solutions'


def animals(head, legs):
	restlegs = legs - 2 * head
	chicken = head - restlegs / 2
	cow = head - chicken
	if cow.is_integer() and cow >= 0:
		if chicken.is_integer() and chicken >= 0:
			return (int(chicken), int(cow))
		else:
			return 'No solutions'
	elif head == 0 and legs == 0:
		return [0, 0]
	else:
		return 'No solutions'


def animals(heads, legs):
	y = legs - 2 * heads
	if y < 0 or y % 2 == 1:
		return 'No solutions'
	y //= 2
	x = 2 * heads - legs // 2
	if x < 0 or legs % 2 == 1:
		return 'No solutions'
	return (x, y)


def animals(heads, legs):
	x = (heads * 4 - legs) / 2
	return (int(x), int(heads - x)) if legs >= heads and x >= 0 and (int(x) == x) else 'No solutions'


def animals(x, y):
	b = 0.5 * (y - 2 * x)
	X = (x - b, b)
	return X if all((i // 1 == i and i >= 0 for i in X)) else 'No solutions'


def animals(h, l):
	co = l / 2 - h
	ch = h - co
	return (0, 0) if h == 0 and l == 0 else 'No solutions' if h < 1 or l < 1 or co < 0 or (ch < 0) or (l % 2 != 0) else (ch, co)


def animals(heads, legs):
	if heads == 0 and legs == 0:
		return (0, 0)
	if legs % 2 == 1 or heads <= 0 or legs <= 0 or (heads * 4 < legs) or (heads * 2 > legs):
		return 'No solutions'
	else:
		ch = heads
		co = 0
		for i in range(1, heads + 1):
			if ch * 2 + co * 4 < legs:
				ch -= 1
				co += 1
			if ch * 2 + co * 4 == legs:
				break
		return (ch, co)


def animals(heads, legs):
	if heads == 0 and legs == 0:
		return (0, 0)
	elif heads < 0 or legs < 0:
		return 'No solutions'
	else:
		result = False
		chickens = 0
		cows = 0
		for i in range(heads + 1):
			if i * 2 + (heads - i) * 4 == legs:
				result = True
				chickens = i
				cows = heads - i
		if result:
			return (chickens, cows)
		else:
			return 'No solutions'


def animals(heads, legs):
	if heads < 0 or legs < 0:
		return 'No solutions'
	else:
		cows = legs / 2 - heads
		chickens = heads - cows
	if chickens < 0 or cows < 0:
		return 'No solutions'
	elif chickens.is_integer() == False or cows.is_integer() == False:
		return 'No solutions'
	else:
		return (chickens, cows)


def animals(heads, legs):
	if heads == 0 and legs == 0:
		return (0, 0)
	elif heads <= 0 or legs <= 0:
		return 'No solutions'
	elif heads > 0 and legs > 0:
		x = -legs / 2 + 2 * heads
		y = legs / 2 - heads
		if legs % 2 == 0 and x >= 0 and (y >= 0):
			return (x, y)
		else:
			return 'No solutions'
	else:
		return 'No soultions'


def animals(heads, legs):
	cows = legs / 2 - heads
	chickens = heads - cows
	chick = str(chickens)
	cow = str(cows)
	c1 = cow.split('.')
	c2 = chick.split('.')
	if c1[1] == '0' and c2[1] == '0':
		if int(chickens) >= 0 and int(cows) >= 0:
			return (int(chickens), int(cows))
	return 'No solutions'


def animals(heads, legs):
	if heads < 0 or legs < 0 or legs % 2 != 0:
		return 'No solutions'
	chicken = (4 * heads - legs) // 2
	cows = heads - chicken
	if chicken >= 0 and cows >= 0 and (chicken + cows == heads):
		return (chicken, cows)
	else:
		return 'No solutions'


def animals(heads, legs):
	cows = abs(legs // 2 - heads)
	chickens = abs(cows - heads)
	if cows * 4 + chickens * 2 != legs:
		return 'No solutions'
	return (chickens, cows)


def animals(heads, legs):
	chickens = 2 * heads - legs / 2
	cows = legs / 2 - heads
	if chickens < 0 or cows < 0 or (not chickens.is_integer()) or (not cows.is_integer()):
		msg = 'No solutions'
	else:
		msg = (chickens, cows)
	return msg


def animals(heads, legs):
	cows = (legs - heads * 2) / 2
	chicken = heads - cows
	if cows < 0 or chicken < 0 or chicken != int(chicken):
		return 'No solutions'
	return (chicken, cows)


def neg_or_float(var):
	return var < 0 or not float(var).is_integer()

def animals(heads, legs):
	cows = 0.5 * legs - heads
	chick = heads - cows
	return 'No solutions' if neg_or_float(cows) or neg_or_float(chick) else (chick, cows)


def animals(heads, legs):
	if heads == 0 and legs == 0:
		return (0, 0)
	if heads > 0 and legs > 0:
		cows = int(legs / 2 - heads)
		chi = int(heads - cows)
		if cows < 0 or chi < 0 or legs % 2 != 0:
			return 'No solutions'
		else:
			return (chi, cows)
	return 'No solutions'


def animals(heads, legs):
	x = (legs - 4 * heads) / -2
	y = heads - x
	print(x, y)
	if x < 0 or y < 0:
		return 'No solutions'
	if x + y == 0:
		return (0, 0)
	if not x.is_integer():
		return 'No solutions'
	return (x, y)