python tiny_uri
基于26个字母的tiny_uri,类似sinat.cn功能
#coding=utf-8 import os, sys import string import random import re Dict = string.ascii_lowercase MAXLEN = len(Dict) def tiny_uri(uri="t.cn/zzzz", domain="t.cn"): """tiny uri base on ascii_lowercase carry""" macth = re.match(r"%s/(.+)" % domain, uri) macths = macth.groups(0) if macths: uri = macths[0] else: return None loop_index = 0 carry = 0 stack = [] for i in reversed(uri): stack.append(i) for u in stack: index = Dict.find(u) if index < 0: continue if index >= (MAXLEN - 1): #carry bit stack[loop_index] = Dict[0] carry = 1 else: #add on bit stack[loop_index] = Dict[index + 1] carry = 0 break loop_index += 1 if carry > 0: stack.append(Dict[0]) return "".join(stack)[::-1] if __name__ == "__main__": #test for i in xrange(100): uri = Dict[random.randint(0, MAXLEN - 1)] print " * " * 20 print "before uri:", uri result = tiny_uri("t.cn/%s" % uri) print "after uri:", result assert uri != result print print "result:", tiny_uri("t.cn/ccaad") * * * * * * * * * * * * * * * * * * * * befor uri: r after uri: s result: ccaae
相关推荐
houjinkai 2020-03-01
xiaouncle 2020-01-23
gaoyubotaili 2014-06-18
心丨悦 2019-12-24
luofuIT成长记录 2019-12-15
Lincain 2019-11-16
killmice 2016-07-14
GoAheadY 2011-07-18
RickyHuo成长之路 2012-06-20
xiyf0 2018-05-30
yongzhang 2019-05-24
AbitGo 2019-04-12
haoxun0 2019-04-12
loveyy 2018-04-16
finnaxu 2016-04-29
87921432 2016-03-15
GimmeS 2016-03-01
LiTOPPPP 2015-06-09
QAnyang 2013-10-08