Skip to content

Instantly share code, notes, and snippets.

@iogi
Created May 7, 2014 11:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save iogi/138aa4c16f1a91449341 to your computer and use it in GitHub Desktop.
# -*- 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