Each letter stands for a 1-digit number. No two letters may stand for the same number. Find a value for each letter from the following set: 0, 1, 4, 5, 6, 7, 8, 9.

'' ASK

+ GAVE

-------

' USEDThe answer can be found here.

The concept is simple but the code is more verbose than what I thought... here is my python solution. Can anyone achieve one that is less verbose?

def permutation(num_list):

if (len(num_list) == 1 or len(num_list) == 0):

return [num_list];

permutation_list = [];

for i in range(len(num_list)):

list_copy = num_list * 1 # deep copy

item = list_copy.pop(i)

one_less_perm_list = permutation(list_copy)

for j in range(len(one_less_perm_list)):

permutation_list.append([item]+ one_less_perm_list[j])

return permutation_list

def solve_puzzle():

combination = permutation([0, 1, 4, 5, 6, 7, 8, 9])

var_list = ['A', 'S', 'K', 'G', 'V', 'E', 'D', 'U']

for i in range(len(combination)):

assignment = combination[i]

set_of_answer = dict([(var_list[j], assignment[j]) for j in range(len(var_list))])

if set_of_answer['G'] + 1 != set_of_answer['U']:

continue

if (set_of_answer['A']*2)%10 != set_of_answer['S'] and (set_of_answer['A']*2)%10 != set_of_answer['S']-1 :

continue

if set_of_answer['K']==0 or set_of_answer['E']==0:

continue

ret = set_of_answer['K'] + set_of_answer['E']

if (ret%10 != set_of_answer['D']):

continue

carryover = ret/10

ret = set_of_answer['S'] + set_of_answer['V'] + carryover

if (ret%10 != set_of_answer['E']):

continue

carryover = ret/10

ret = set_of_answer['A']*2 + carryover

if (ret%10 != set_of_answer['S']):

continue

print set_of_answer

print solve_puzzle()

But amazingly the PowerShell solution is so elegant! Too bad that it's not yet available to Vista and...wouldn't be open-sourced to Unix. Or else we can get ride of Bash or C-shell! Guess that this day won't come in near future...

## No comments:

Post a Comment