私の質問は単純で、半分はすでに機能しています。順序付き単語順列の生成について助けが必要です。
私のコード:
from os.path import isfile
from string import printable
def loadRuleSet(fileLocation):
rules = {}
assert isfile(fileLocation)
for x in open(fileLocation).read().split('\n'):
if not len(x) == 0:
data = x.split(':')
if not len(data[0]) == 0 or not len(data[1]) == 0:
rules[data[0]] = data[1]
return rules
class deform:
def __init__(self, ruleSet):
assert type(ruleSet) == dict
self.ruleSet = ruleSet
def walker(self, string):
spot = []
cnt = 0
for x in string:
spot.append((x, cnt))
cnt += 1
return spot
def replace_exact(self, word, position, new):
cnt = 0
newword = ''
for x in word:
if cnt == position:
newword += new
else:
newword += x
cnt+= 1
return newword
def first_iter(self, word):
data = []
pos = self.walker(word)
for x in pos:
if x[0] in self.ruleSet:
for y in self.ruleSet[x[0]]:
data.append(self.replace_exact(word, x[1], y))
return data
print deform({'a':'@A'}).first_iter('abac')
私の現在のコードは仕事の半分を行いますが、「ライターズブロック」に達しました
>>>deform({'a':'@'}).first_iter('aaa')
['@aa', 'a@a', 'aa@']
これが、現在作成しているスクリプトの結果です。
コードがすべきことは - 単語を取り、置換で他の文字で並べ替えます。1 キャラクターで成功させましたが、すべての結果を出すには助けが必要です。例えば:
['@aa', 'a@a', 'aa@', '@@a', 'a@@', '@a@']