# # For example, when the list is sorted into alphabetical order, COLIN, # which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the # list. So, COLIN would obtain a score of 938 × 53 = 49714. # # What is the total of all the name scores in the file? import eutils.strings as strings import eutils.vectorize as vec import re import time start = time.time() val = dict(zip(list('ABCDEFGHIJKLMNOPQRSTUVWXYZ'), list(range(1, 27)))) f = open('../data/names.txt', 'r') n = "" for line in f: n += line.rstrip() f.close() names = [re.sub("[^A-Z]", "", x) for x in n.split(',')] names.sort() ans = vec.v_sum([vec.v_sum(strings.map_vals(x, val)) * (i + 1) for i, x in enumerate(names)]) end = time.time() print("The answer to Problem 22 is: %s" % ans) print("<< Returned in %s seconds >>" % (end - start))
# # Using words.txt (right click and 'Save Link/Target As...'), a 16K text # file containing nearly two-thousand common English words, how many are # triangle words? import eutils.strings as strings import eutils.vectorize as vec import re import time start = time.time() tri = dict(zip([int(0.5 * n * (n + 1)) for n in list(range(1, 25))], list(range(1, 25)))) val = dict(zip(list('ABCDEFGHIJKLMNOPQRSTUVWXYZ'), list(range(1, 27)))) f = open('../data/words.txt', 'r') n = "" for line in f: n += line.rstrip() f.close() words = [re.sub("[^A-Z]", "", x) for x in n.split(",")] wvals = [vec.v_sum(strings.map_vals(x, val)) for x in words] ans = len([x for x in wvals if x in tri]) end = time.time() print("The answer to Problem 42 is: %s" % ans) print("<< Returned in %s seconds >>" % (end - start))