频道栏目
首页 > 资讯 > Python > 正文

python各种进制求值的方法(代码实例)

17-12-04        来源:[db:作者]  
收藏   我要投稿

python各种进制求值的方法(代码实例)

def checkio(str_number, radix):
    str_int = dict(map(lambda x,y:(y, x), [ i for i in range(10, 36) ], [ chr(i)  for i in range(97, 123) ]))
    int_int = dict(map(lambda x,y:(str(x),y), [ i for i in  range(10) ], [ i for i in range(10) ]))
    sum = 0
    times = 0
    for i in reversed(str_number):
        v = int_int.get(i, None)
        if v == None:
            v = str_int.get(i.lower(), None)
        if v >= radix:
            return -1
        else:
            sum += v if times == 0 else v*radix**times
        times += 1
    return sum
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    assert checkio("AF", 16) == 175, "Hex"
    assert checkio("101", 2) == 5, "Bin"
    assert checkio("101", 5) == 26, "5 base"
    assert checkio("Z", 36) == 35, "Z base"
    assert checkio("AB", 10) == -1, "B > A = 10"
    print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
相关TAG标签
上一篇:React学习之nodejs的下载安装教程
下一篇:Specialized Four-Digit Numbers:进制“编程题”
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站