私は就職の面接の質問を読んで、次のコードを書きました。
文字列内の最初の繰り返されない文字を見つけるための効率的な関数を記述します。たとえば、「total」の最初の繰り返されない文字は「o」であり、「teeter」の最初の繰り返されない文字は「r」です。アルゴリズムの効率について話し合います。
私はPythonでこのソリューションを思いついた。しかし、それを行うにはもっと美しい方法があると確信しています。
word="googlethis"
dici={}
#build up dici with counts of characters
for a in word:
try:
if dici[a]:
dici[a]+=1
except:
dici[a]=1
# build up dict singles for characters that just count 1
singles={}
for i in dici:
if dici[i]==1:
singles[i]=word.index(i)
#get the minimum value
mini=min(singles.values())
#find out the character again iterating...
for zu,ui in singles.items():
if ui==mini:
print zu
より簡潔で効率的な答えはありますか?