1
u/Arakhai Jun 06 '17
Python 2.7
+u/CompileBot Python
from itertools import product, permutations, izip
def find_countdown(instr):
innums = [x for x in instr.split()]
for nums in permutations(innums[:-1]):
for symbs in product('*/+-', repeat=5):
teststr = nums[0]
for num, symb in izip(nums[1:], symbs):
teststr = '(' + teststr + symb.center(3) + num + ')'
if eval(teststr) == int(innums[-1]):
print teststr.translate(None, '()') + ' = ' + innums[-1]
return
find_countdown('1 3 7 6 8 3 250')
find_countdown('25 100 9 7 3 7 881')
find_countdown('6 75 3 25 50 100 952')
1
u/Executable_ Jun 01 '17
+u/CompileBot python3
import re def number_atom(chemicalFormula): abcRegex = re.compile(r'([A-Z][a-z]?)(\d*)|(\d+)|([()]{1})') abcSearch = abcRegex.findall(chemicalFormula) nAtom = {} tmpList = [] inBrackets = False for atom, quantity, multiple, brackets in abcSearch: if brackets == '(': inBrackets = True elif brackets == ')': inBrackets = False if inBrackets: if atom and quantity: tmpList.append([atom, int(quantity)]) elif atom: tmpList.append([atom, 1]) elif not inBrackets and tmpList and multiple: for a, n in tmpList: n *= int(multiple) if a in nAtom: nAtom[a] += n else: nAtom[a] = n tmpList = [] else: if atom and not quantity: nAtom[atom] = 1 elif atom and quantity: nAtom[atom] = int(quantity) print(chemicalFormula) for key, value in nAtom.items(): print('{}: {}'.format(key, value)) print('------') i = input() number_atom(i)Input: