Last active
July 17, 2020 13:59
-
-
Save max747/d81f120772153494415417a054d850f6 to your computer and use it in GitHub Desktop.
ソート実装案
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
class ItemOrdering: | |
regular_items = { | |
'爪': 10, | |
'心臓': 20, | |
'逆鱗': 30, | |
'根': 40, | |
} | |
# TODO 実際にはここは動的に設定することになるだろう | |
event_items = { | |
'チップ': 10010, | |
'トランプ': 10020, | |
'コイン': 10030, | |
} | |
def priority(self, name): | |
if name in self.regular_items: | |
return self.regular_items[name] | |
if name in self.event_items: | |
return self.event_items[name] | |
if name == 'QP': | |
# 適当 | |
return 99999 | |
OrderingMaster = ItemOrdering() | |
class Item: | |
def parse_name(self, name): | |
idx = name.find('(') | |
if idx == -1: | |
return name, '' | |
trunk, suffix = name[:idx], name[idx:] | |
if not suffix.endswith(')'): | |
raise ValueError('closing parenthesis not found') | |
# ( ) に加えて演算記号 x, + もこの時点で落とす | |
return trunk, suffix[2:-1] | |
def normalize_name(self, name): | |
# TODO 実装する | |
return name | |
def __init__(self, name): | |
self.name = name | |
trunk, suffix = self.parse_name(name) | |
self.canonical_name = self.normalize_name(trunk) | |
self.suffix = suffix | |
@property | |
def priority(self): | |
# 礼装は考慮してない | |
prio = OrderingMaster.priority(self.canonical_name) | |
# 辞書式ソートするためにゼロ埋め必須 | |
# TODO ゼロ埋めする桁数は実データの桁数に合わせて要調整 | |
return f'{prio:0>6}-{self.suffix:0>6}' |
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 unittest | |
from collections import Counter | |
from operator import attrgetter | |
import main | |
class ItemOrderingTest(unittest.TestCase): | |
def test_ordering(self): | |
input = ['チップ(x3)', 'コイン(x3)', 'チップ(x3)', 'コイン(x4)', '爪', '爪', '心臓', 'QP(+9600)', 'QP(+12000)'] | |
item_dict = Counter(input) | |
items = [main.Item(name) for name in item_dict] | |
items.sort(key=attrgetter('priority')) | |
names = [e.name for e in items] | |
self.assertEqual(names, [ | |
'爪', | |
'心臓', | |
'チップ(x3)', | |
'コイン(x3)', | |
'コイン(x4)', | |
'QP(+9600)', | |
'QP(+12000)', | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment