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
# -*- coding: utf-8 -*- | |
class DomainScore(object): | |
KEYMAP = [ | |
'1234567890-', | |
'qwertyuiop', | |
'asdfghjkl', | |
'zxcvbnm,.' | |
] | |
def __init__(self): | |
self.generate_position_data() | |
def generate_position_data(self): | |
self.pos_data = {} | |
for y, line in enumerate(self.KEYMAP): | |
for x, char in enumerate(line): | |
self.pos_data[char] = (x, y) | |
def get_distance(self, pair): | |
a = pair[0] | |
b = pair[1] | |
a_x, a_y = self.pos_data[a] | |
b_x, b_y = self.pos_data[b] | |
if a == b: | |
score = 0 | |
# 上下左右はスコア1 | |
elif (a_x == b_x) and abs(a_y - b_y) == 1: | |
score = 1 | |
elif (a_y == b_y) and abs(a_x - b_x) == 1: | |
score = 1 | |
# 左下、右上もスコア1 | |
elif (a_y - b_y) == 1 and (a_x - b_x) == -1: | |
score = 1 | |
elif (a_y - b_y) == -1 and (b_x - a_x) == 1: | |
score = 1 | |
# 右下、左上はスコア3 | |
elif (a_y - b_y) == 1 and (b_x - a_x) == 1: | |
score = 3 | |
elif (a_y - b_y) == -1 and (b_x - a_x) == -1: | |
score = 3 | |
# それ以外はスコア5とする | |
else: | |
score = 5 | |
#print '%s to %s is score %d' % (a, b, score) | |
return score | |
def get_score(self, domain): | |
domain = domain.lower() | |
sequence = [domain[i:i+2] for i in range(len(domain) - 1)] | |
return sum(map(self.get_distance, sequence)) | |
# load file | |
domains = [] | |
with open('domain.txt') as f: | |
for line in f: | |
domains.append(line.rstrip()) | |
domainscore = DomainScore() | |
# calculate score | |
for domain in domains: | |
score = domainscore.get_score(domain) | |
print score, domain | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment