Now we will confect a reagent. There are eight materials to choose from, numbered 1,2,..., 8 respectively.

We know the rules of confect:
```
material1 and material2 cannot be selected at the same time
material3 and material4 cannot be selected at the same time
material5 and material6 must be selected at the same time
material7 or  material8 must be selected(at least one, or both)
```

# Task

You are given a integer array `formula`. Array contains only digits 1-8 that represents material 1-8. Your task is to determine if the formula is valid. Returns `true` if it's valid, `false` otherwise.


# Example

For `formula = [1,3,7]`, The output should be `true`.

For `formula = [7,1,2,3]`, The output should be `false`.

For `formula = [1,3,5,7]`, The output should be `false`.

For `formula = [1,5,6,7,3]`, The output should be `true`.

For `formula = [5,6,7]`, The output should be `true`.

For `formula = [5,6,7,8]`, The output should be `true`.

For `formula = [6,7,8]`, The output should be `false`.

For `formula = [7,8]`, The output should be `true`.


# Note

- All inputs are valid. Array contains at least 1 digit. Each digit appears at most once. 

- Happy Coding `^_^`

Difficulty: EASY
Tags: Fundamentals

Starter code:
def isValid(formula):
	

Solutions:
def isValid(formula):
	return not (1 in formula and 2 in formula or (3 in formula and 4 in formula) or (5 in formula and (not 6 in formula)) or (not 5 in formula and 6 in formula) or (not 7 in formula and (not 8 in formula)))


def isValid(formula):
	if not 7 in formula and (not 8 in formula):
		return False
	if 1 in formula and 2 in formula:
		return False
	if 3 in formula and 4 in formula:
		return False
	if 5 in formula and (not 6 in formula) or (6 in formula and (not 5 in formula)):
		return False
	return True


def isValid(formula):
	(a, b, c, d, e, f, g, h) = (v in formula for v in range(1, 9))
	return not (a and b) and (not (c and d)) and e ^ f ^ 1 and (g or h)


def isValid(formula):
	f = set(formula)
	return not {1, 2} <= f and (not {3, 4} <= f) and ((5 in f) == (6 in f)) and bool({7, 8} & f)


def isValid(formula):
	if {1, 2} <= set(formula) or {3, 4} <= set(formula):
		print(1)
		return False
	elif 5 in formula and 6 not in formula or (6 in formula and 5 not in formula):
		print(3)
		return False
	elif 7 not in formula and 8 not in formula:
		print(4)
		return False
	else:
		return True


def isValid(formula):
	if not any((el in formula for el in (7, 8))):
		return False
	elif all((el in formula for el in (1, 2))):
		return False
	elif all((el in formula for el in (3, 4))):
		return False
	elif 5 in formula and 6 not in formula:
		return False
	elif 6 in formula and 5 not in formula:
		return False
	else:
		return True


def isValid(f):
	if 1 in f and 2 in f:
		return False
	elif 3 in f and 4 in f:
		return False
	elif 5 in f and 6 in f:
		if 7 in f or 8 in f:
			return True
		else:
			return False
	elif 5 in f or 6 in f:
		return False
	elif 7 in f or 8 in f:
		return True
	else:
		return False


def isValid(formula):
	s = ''.join(sorted(map(str, formula)))
	return not any(['12' in s, '34' in s, ('5' in s) + ('6' in s) == 1, ('7' in s) + ('8' in s) == 0])


from typing import List

def isValid(f: List[int]):
	return all([(1 in f) ^ (2 in f) if 1 in f or 2 in f else True, (3 in f) ^ (4 in f) if 3 in f or 4 in f else True, all([5 in f, 6 in f]) if 5 in f or 6 in f else True, any([7 in f, 8 in f])])


def isValid(formula):
	material = [m + 1 in formula for m in range(8)]
	r1 = not (material[0] and material[1])
	r2 = not (material[2] and material[3])
	r3 = not material[4] ^ material[5]
	r4 = material[6] or material[7]
	return r1 and r2 and r3 and r4


def isValid(formula):
	mat = [m + 1 in formula for m in range(8)]
	return all((not (mat[0] and mat[1]), not (mat[2] and mat[3]), not mat[4] ^ mat[5], mat[6] or mat[7]))


def isValid(formula):
	test1 = not (1 in formula and 2 in formula)
	test2 = not (3 in formula and 4 in formula)
	test3 = 5 in formula and 6 in formula or (5 not in formula and 6 not in formula)
	test4 = 7 in formula or 8 in formula
	print((test1, test2, test3, test4))
	return test1 and test2 and test3 and test4


def isValid(formula):
	list = []
	for x in range(8):
		list.append(9)
	for x in formula:
		list[x - 1] = x
	if list[0] != 9 and list[1] != 9:
		return False
	elif list[2] != 9 and list[3] != 9:
		return False
	elif list[4] == 9 and list[5] != 9 or (list[4] != 9 and list[5] == 9):
		return False
	elif list[6] + list[7] == 18:
		return False
	else:
		return True


def isValid(formula):
	m = [0, 1, 2, 3, 4, 5, 6, 7, 8]
	t = []

	def mat1_mat2():
		count = 0
		for i in formula:
			if i == 1:
				count += 1
			if i == 2:
				count += 1
		if count == 0:
			t.append(True)
		elif count == 1:
			t.append(True)
		else:
			t.append(False)

	def mat3_mat4():
		count = 0
		for i in formula:
			if i == 3:
				count += 1
			if i == 4:
				count += 1
		if count == 0:
			t.append(True)
		elif count == 1:
			t.append(True)
		else:
			t.append(False)

	def mat5_mat6():
		count = 0
		for i in formula:
			if i == 5:
				count += 1
			if i == 6:
				count += 1
		if count == 0:
			t.append(True)
		elif count == 1:
			t.append(False)
		else:
			t.append(True)

	def mat7_mat8():
		count = 0
		for i in formula:
			if i == 7:
				count += 1
			if i == 8:
				count += 1
		if count == 1:
			t.append(True)
		elif count == 2:
			t.append(True)
		else:
			t.append(False)
	mat1_mat2()
	mat3_mat4()
	mat5_mat6()
	mat7_mat8()
	return all(t)


def isValid(f):
	return all((7 in f or 8 in f, (5 in f) == (6 in f), not (3 in f and 4 in f), not (1 in f and 2 in f)))


def isValid(formula):
	formula = sorted(formula)
	if len(formula) < 1:
		return False
	elif 1 in formula and 2 in formula or (3 in formula and 4 in formula) or (7 not in formula and 8 not in formula):
		return False
	elif 5 in formula and 6 not in formula or (6 in formula and 5 not in formula):
		return False
	else:
		return True


def isValid(formula):
	print(formula)
	f = list(map(str, formula))
	f1 = str(''.join(f))
	b = 0
	if f1.find('7') != -1:
		for i in f1:
			b = i
			if b == '1':
				if f1.find('2') == -1:
					continue
				else:
					return False
			elif b == '3':
				if f1.find('4') == -1:
					continue
				else:
					return False
			elif b == '5':
				if f1.find('6') != -1:
					return True
				else:
					return False
			elif b == '6':
				if f1.find('5') != -1:
					return True
				else:
					return False
	elif f1.find('8') != -1:
		for i in f1:
			b = i
			if b == '1':
				if f1.find('2') == -1:
					continue
				else:
					return False
			elif b == '3':
				if f1.find('4') == -1:
					continue
				else:
					return False
			elif b == '5':
				if f1.find('6') != -1:
					return True
				else:
					return False
			elif b == '6':
				if f1.find('5') != -1:
					return True
				else:
					return False
	else:
		return False
	return True


def isValid(formula):
	if formula.count(1) and formula.count(2):
		return False
	if formula.count(3) and formula.count(4):
		return False
	if formula.count(5) + formula.count(6) == 1:
		return False
	if formula.count(7) + formula.count(8) == 0:
		return False
	return True


def isValid(a):
	for (x, y) in [[1, 2], [3, 4]]:
		if x in a and y in a:
			return False
	return (5 in a) == (6 in a) and any((x in a for x in (7, 8)))


def isValid(formula):
	rules = [not (1 in formula and 2 in formula)]
	rules.append(not (3 in formula and 4 in formula))
	rules.append(5 in formula and 6 in formula or (5 not in formula and 6 not in formula))
	rules.append(7 in formula or 8 in formula)
	print(rules)
	return all(rules)


def isValid(formula):
	valid_formula = True
	if all((x in formula for x in [1, 2])):
		valid_formula = False
	if all((x in formula for x in [3, 4])):
		valid_formula = False
	if 5 in formula and 6 not in formula:
		valid_formula = False
	if 6 in formula and 5 not in formula:
		valid_formula = False
	if 7 not in formula and 8 not in formula:
		valid_formula = False
	return valid_formula


def isValid(formula=[1, 3, 7]):
	fr = not (1 in formula and 2 in formula)
	sr = not (3 in formula and 4 in formula)
	tr = not (5 in formula and 6 not in formula or (5 not in formula and 6 in formula))
	four = 7 in formula or 8 in formula
	return fr and sr and tr and four


def isValid(a):
	b = not (1 in a and 2 in a)
	c = not (3 in a and 4 in a)
	d = (5 in a) == (6 in a)
	e = 7 in a or 8 in a
	print([b, c, d, e])
	return all([b, c, d, e])


def isValid(formula):
	(a, b) = (sum([1 for c in formula if c in [1, 2]]), sum([1 for c in formula if c in [3, 4]]))
	(c, d) = (sum([1 for c in formula if c in [5, 6]]), sum([1 for c in formula if c in [7, 8]]))
	return True if (a in range(2) and b in range(2)) and ((c == 0 or c == 2) and d in range(1, 3)) else False


def isValid(formula):
	r1 = not (1 in formula and 2 in formula)
	r2 = not (3 in formula and 4 in formula)
	r3 = (5 in formula) == (6 in formula)
	r4 = 7 in formula or 8 in formula
	return all([r1, r2, r3, r4])


def isValid(formula: list) -> bool:
	if 7 in formula or 8 in formula or set([7, 8]).issubset(formula):
		if set([1, 2]).issubset(formula):
			return False
		if set([3, 4]).issubset(formula):
			return False
		if 5 in formula or 6 in formula:
			return set([5, 6]).issubset(formula)
		if 7 in formula or 8 in formula or set([7, 8]).issubset(formula):
			return True
	return False


def isValid(formula):
	rule_1 = 1 not in formula or 2 not in formula
	rule_2 = 3 not in formula or 4 not in formula
	rule_3 = 5 in formula and 6 in formula or (5 not in formula and 6 not in formula)
	rule_4 = 7 in formula or 8 in formula
	return rule_1 and rule_2 and rule_3 and rule_4


def isValid(formula):
	if len(set(formula).intersection(set([1, 2]))) > 1 or len(set(formula).intersection(set([3, 4]))) > 1 or len(set(formula).intersection(set([5, 6]))) == 1 or (len(set(formula).intersection(set([7, 8]))) < 1):
		return False
	return True


def isValid(formula):
	print(formula)
	if 1 in formula and 2 in formula:
		return 0
	if 3 in formula and 4 in formula:
		return 0
	if 5 in formula and (not 6 in formula):
		return 0
	if 6 in formula and (not 5 in formula):
		return 0
	if 7 in formula or 8 in formula:
		return 1
	else:
		return 0


def isValid(formula):
	rules = list()
	rules.append(1) if 7 in formula or 8 in formula else rules.append(0)
	if 5 in formula or 6 in formula:
		rules.append(2) if 5 in formula and 6 in formula else rules.append(0)
	if 3 in formula or 4 in formula:
		rules.append(0) if 3 in formula and 4 in formula else rules.append(3)
	if 1 in formula or 2 in formula:
		rules.append(0) if 1 in formula and 2 in formula else rules.append(4)
	return True if min(rules) != 0 else False


def isValid(formula):
	check1 = 1 in formula and 2 in formula
	check2 = 3 in formula and 4 in formula
	check3 = 5 in formula and 6 not in formula
	check4 = 6 in formula and 5 not in formula
	check5 = 7 in formula or 8 in formula
	if check1 == True or check2 == True or check3 == True or (check4 == True) or (check5 == False):
		return False
	else:
		return True


def isValid(formula):
	d = {1: 1, 2: 1, 3: 2, 4: 2, 5: 3, 6: 3, 7: 4, 8: 4}
	r = [d[i] for i in formula]
	l = {}
	for x in r:
		if x == 4 and r.count(4) == 1 or (x == 4 and r.count(4) == 2):
			l[4] = True
		if x == 1 and r.count(1) == 1:
			l[1] = True
		if x == 2 and r.count(2) == 1:
			l[2] = True
		if x == 3 and r.count(3) == 2:
			l[3] = True
		if r.count(4) == 0:
			l[4] = False
		if x == 1 and r.count(1) != 1:
			l[1] = False
		if x == 2 and r.count(2) != 1:
			l[2] = False
		if x == 3 and r.count(3) != 2:
			l[3] = False
	result = [k for k in list(l.values())]
	return sum(result) == len(result)


def isValid(formula):
	exclusive = [[1, 2], [3, 4]]
	implies = {5: 6, 6: 5}
	required = {7, 8}
	for (m1, m2) in exclusive:
		if m1 in formula and m2 in formula:
			return False
	for m in formula:
		if m in implies and (not implies[m] in formula):
			return False
	if not any((m in formula for m in required)):
		return False
	return True


def isValid(f):
	y = ''.join(map(str, sorted(f)))
	return '12' not in y and '34' not in y and ('56' in y or ('5' not in y and '6' not in y)) and ('7' in y or '8' in y)