如果你有这个字符集:
abc
排列将是
0 = a
1 = b
2 = c
3 = aa
4 = ab
5 = ac
6 = ba
7 = bb
8 = bc
9 = ca
10 = cb
11 = cc
依此类推。有没有一种方法在python中取n,一个数字代表它是哪个排列,并返回一个相应的字符串?有没有办法反过来?
示例:
get_word(10, "abc")
#returns cb
get_n("cb", "abc")
#returns 10
答案 0 :(得分:4)
可能有更有效的方法来执行此操作,但这些功能可以满足您的需求。使用固定长度的字符串进行转换很容易;要处理这些可变长度的字符串,我们只需要合并一个偏移量,该偏移量可以解释比当前长度短的所有字符串。我们可以轻松地为前向转换做到这一点;对于反向转换,使用sum of a geometric progression的标准公式很方便。
def gpsum(base, terms):
return (base ** (terms + 1) - 1) // (base - 1)
def ntos(n, bstring):
m = base = len(bstring)
length = 1
while m <= n:
n -= m
m *= base
length += 1
s = []
for i in range(length):
n, d = divmod(n, base)
s.append(bstring[d])
return ''.join(s)[::-1]
def ston(s, digits):
base = len(digits)
n = 0
for c in s:
n *= base
n += digits[c]
return n + gpsum(base, len(s) - 1) - 1
# Test
bstring = 'abc'
digits = {c:i for i,c in enumerate(bstring)}
maxchars = 4
for i in range(gpsum(len(bstring), maxchars) - 1):
s = ntos(i, bstring)
j = ston(s, digits)
print('{0:>3} {1:>{3}} {2:>3}'.format(i, s, j, maxchars))
<强>输出强>
0 a 0
1 b 1
2 c 2
3 aa 3
4 ab 4
5 ac 5
6 ba 6
7 bb 7
8 bc 8
9 ca 9
10 cb 10
11 cc 11
12 aaa 12
13 aab 13
14 aac 14
15 aba 15
16 abb 16
17 abc 17
18 aca 18
19 acb 19
20 acc 20
21 baa 21
22 bab 22
23 bac 23
24 bba 24
25 bbb 25
26 bbc 26
27 bca 27
28 bcb 28
29 bcc 29
30 caa 30
31 cab 31
32 cac 32
33 cba 33
34 cbb 34
35 cbc 35
36 cca 36
37 ccb 37
38 ccc 38
39 aaaa 39
40 aaab 40
41 aaac 41
42 aaba 42
43 aabb 43
44 aabc 44
45 aaca 45
46 aacb 46
47 aacc 47
48 abaa 48
49 abab 49
50 abac 50
51 abba 51
52 abbb 52
53 abbc 53
54 abca 54
55 abcb 55
56 abcc 56
57 acaa 57
58 acab 58
59 acac 59
60 acba 60
61 acbb 61
62 acbc 62
63 acca 63
64 accb 64
65 accc 65
66 baaa 66
67 baab 67
68 baac 68
69 baba 69
70 babb 70
71 babc 71
72 baca 72
73 bacb 73
74 bacc 74
75 bbaa 75
76 bbab 76
77 bbac 77
78 bbba 78
79 bbbb 79
80 bbbc 80
81 bbca 81
82 bbcb 82
83 bbcc 83
84 bcaa 84
85 bcab 85
86 bcac 86
87 bcba 87
88 bcbb 88
89 bcbc 89
90 bcca 90
91 bccb 91
92 bccc 92
93 caaa 93
94 caab 94
95 caac 95
96 caba 96
97 cabb 97
98 cabc 98
99 caca 99
100 cacb 100
101 cacc 101
102 cbaa 102
103 cbab 103
104 cbac 104
105 cbba 105
106 cbbb 106
107 cbbc 107
108 cbca 108
109 cbcb 109
110 cbcc 110
111 ccaa 111
112 ccab 112
113 ccac 113
114 ccba 114
115 ccbb 115
116 ccbc 116
117 ccca 117
118 cccb 118
119 cccc 119