create a typo with python

Solutions on MaxInterview for create a typo with python by the best coders in the world

showing results for - "create a typo with python"
Beth
17 Apr 2016
1import string
2import random
3
4new_phrase = []
5words = phrase.split(' ')
6for word in words:
7    outcome = random.random()
8    if outcome <= p:
9        ix = random.choice(range(len(word)))
10        new_word = ''.join([word[w] if w != ix else random.choice(string.ascii_letters) for w in range(len(word))])
11        new_phrase.append(new_word)
12    else:
13        new_phrase.append(word)
14
15new_phrase = ' '.join([w for w in new_phrase])