Click here for details of the problem. http://qiita.com/Nabetani/items/cbc3af152ee3f50a822f
I wrote it in python with reference to the implementation of ruby. Since the character string representing the rank is one character other than 10, first replace 10 with one character'T', and take out only the rank characters arranged every other character to make rank. Next, use the set function to extract the types of rank characters, and create a pair that checks how many ranks there are by count. Finally, sort the pairs, quantify them, and look up the dictionary you created in advance to find the answer.
def solve(text):
rank = text.replace('10','T')[1::2]
pair = [rank.count(x) for x in set(rank)]
key = reduce(lambda a,b: a*10+b, sorted(pair))
return {14:'4K', 23:'FH', 113:'3K', 122:'2P', 1112:'1P', 11111:'--'}[key]
def test(text, answer):
result = solve(text)
print 'OK' if result == answer else 'NG', result, answer, text
if __name__ == '__main__':
test('D3C3C10D10S3', 'FH')
test('S8D10HJS10CJ', '2P')
test('DASAD10CAHA', '4K')
test('S10HJDJCJSJ', '4K')
test('S10HAD10DAC10', 'FH')
test('HJDJC3SJS3', 'FH')
test('S3S4H3D3DA', '3K')
test('S2HADKCKSK', '3K')
test('SASJDACJS10', '2P')
test('S2S10H10HKD2', '2P')
test('CKH10D10H3HJ', '1P')
test('C3D3S10SKS2', '1P')
test('S3SJDAC10SQ', '--')
test('C3C9SAS10D2', '--')
Recommended Posts