Created
June 17, 2015 14:16
-
-
Save sumeetpareek/4a0b5044fe5793129c8b to your computer and use it in GitHub Desktop.
working on codeeval challenge - https://www.codeeval.com/open_challenges/190/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
import operator | |
# have the allowed operators in an array | |
def get_operator_fn(op): | |
return { | |
'+' : operator.add, | |
'-' : operator.sub, | |
'*' : operator.mul, | |
}[op] | |
# do the math operation from operator as string | |
def eval_binary_expr(op1, operator, op2): | |
op1,op2 = int(op1), int(op2) | |
return get_operator_fn(operator)(op1, op2) | |
# get string input as integer iteratables | |
# TODO replace this later as IO from file and all of that | |
input_string = "44 6 1 49 47" | |
input_array = input_string.split() | |
# have operators as iteratable | |
operands_string = "* + -" | |
operands_array = operands_string.split() | |
my_flag = False | |
# for all permutations of numbers drawn, do a dirty brute force check | |
for number_subset in itertools.permutations(input_array, 5): | |
# TODO replace this later with something that is not dirty | |
for op1 in operands_array: | |
val1 = eval_binary_expr(number_subset[0], op1, number_subset[1]) | |
for op2 in operands_array: | |
val2 = eval_binary_expr(val1, op2, number_subset[2]) | |
for op3 in operands_array: | |
val3 = eval_binary_expr(val2, op3, number_subset[3]) | |
for op4 in operands_array: | |
val4 = eval_binary_expr(val3, op4, number_subset[4]) | |
if (val4 == 42): | |
my_flag = True | |
print "Yes" if (my_flag == True) else "No" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import permutations | |
from itertools import combinations_with_replacement as cartesian_product | |
import sys | |
# read in the supplied input file | |
lines = open(sys.argv[1], 'r') | |
# have the allowed operators in an array | |
signs = ['+', '-', '*'] | |
# put together the expression to be evaluated at once | |
# using a given permutation of numbers drawn and math signs | |
def make_test_str(perm, sp): | |
test_str = "" | |
for i in xrange(0, 5): | |
test_str += perm[i] | |
if i < 4: | |
test_str += sp[i] | |
return test_str | |
# print if there is a possible 42 solution for a given number set | |
def has_solution(num_set): | |
# get all permutations of the set | |
perm_set = permutations(num_set, 5) | |
# for each permutation try all math sign combinations | |
for p in perm_set: | |
sp = cartesian_product(signs, 4) | |
for s in sp: | |
test_str = make_test_str(p, s) | |
print "test_str is", test_str | |
result = eval(test_str) | |
if result is 42: | |
return 'YES' | |
return 'NO' | |
# get each line from supplied input file | |
for line in lines: | |
num_set = line.replace("\n", "").split(" ") | |
print has_solution(num_set) | |
lines.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment