现在的位置: 首页 > 综合 > 正文

Python 26进制计算

2014年11月02日 ⁄ 综合 ⁄ 共 482字 ⁄ 字号 评论关闭
# -*- coding:utf-8 -*-
'''
Created on 2013-3-29

@author: naughty
'''

dict={}
dict['A']=1
dict['B']=2
dict['C']=3
dict['D']=4
dict['E']=5
dict['F']=6
dict['G']=7
dict['H']=8
dict['I']=9
dict['J']=10
dict['K']=11
dict['L']=12
dict['M']=13
dict['N']=14
# .....其他字母

def count_(s):
    p=len(s)-1
    count=0
    for x in xrange(len(s)):
        count+=get(x)*dict[s[p-x]]
    return count

def get(p):
    return 26**p

print count_('AC')

题目是这样的:

假设A=1,B=2,C=3...AA=27,AB=28...AAA=xxx(表示某个数字),写一个函数统计一个字符串的值是多少

在上面的代码中,利用的字典来映射每个字母的含义。当然也可以每次计算每个字母代表的数字。但是会有效率问题。

抱歉!评论已关闭.