How can we remove all distinct words of length 16 letters or more using python
Question: How can we remove all distinct words of length 16 letters or more. Reduce the
size of these words to fifteen letters while keeping them distinct. (hint
removing postfix, suffix, and or infix)

so far i have done the following code:

    
    
    fo = open("anyFile.txt","wb")
    words = set(w.lower() for w in open('distinct_words_only.txt').read().split())
    for item in english_words:
       if len(item) > 15:
           fo.write(item)
    fo.close()
    


Answer: Well I gave a quick shot to your question. I shall explain how I accomplished
this task. There could be a better approach too. As far as what I understood
from your question is:

> Your major concern is to keep the length of a word to 15 and also to keep
> them distinct.

The code snippet below does that exactly. I first read the file `anyFile.txt`
which shall contain all the words having 15 letters or more only. I assume
from your question that you have already created this kind of file. I kept the
contents of this file into `english_words`.

Then I declared a `charList = ['a','b','c','d','e','f']`. Well in my code I am
simply using these `6` letters for creating distinct words. You can use more
if you like. More is better.

Then I loop over every word using a for loop. In loop first I strip the word
to keep its length up to 15 letters. Then I call my `checker()` to do the
heavy stuff.

The `checker()` will first check if the word is already present in
`oldWordList` if yes then it will generate a new distinct word. I used
recursion to double check if the newly created word is distinct or not. If the
word is not in the `oldWordList` then I simply append it to a `newWorldList`.

This `newWorldList` will contain all the distinct words at last.

How are distinct words being generated? Well the code is simple. First I am
converting the word which is a string to a List `tempList = list(item)`, so
that I can modify it. Then here `tempList[randomIndex] = charList[randomChar]`
I am selecting a random letter from `charList = ['a','b','c','d','e','f']` and
then assigning it to a random position in the `tempList`.

Code snippet: _Please see: The code snippet is quick and dirty feel free to
improve it further._

    
    
    import random
    
    fo = open("anyFile.txt","wb")
    english_words = set(w.lower() for w in open('distinct_words_only.txt').read().split())
    
    for item in english_words:
        if len(item) > 15:
            fo.write(item+'\n')
    fo.close()
    
    
    def checker(item):
        randomIndex = random.randrange(0,15)#Random number to decide which position in word
        randomChar = random.randrange(0,6)#Random number to pick a letter from charList
        if item in oldWordList:
            print 'Yes %s is present already'%item
            tempList = list(item)#Convert string to list
            tempList[randomIndex] = charList[randomChar]#Generate random word
            item = "".join(tempList)#Covert list back to string
            checker(item)#Recursion to double check
            print 'Newly created word %s'%item
        else:
            print '%s is distinct'%item
            newWordList.append(item)
    
    english_words = set(w.lower() for w in open('anyFile.txt').read().split())
    charList = ['a','b','c','d','e','f']
    oldWordList = []
    newWordList = []
    for item in english_words:
        item = item[0:15]
        print 'Append item to oldWordList %s '%item
        checker(item)
        oldWordList.append(item)
    
    print oldWordList#Will contain the original 15 letters words 
    print newWordList#Will contain distinct 15 letteres  words
    

**Testing the code:** Create a file named as `'distinct_words_only.txt'` with
the following content and execute the script code:

    
    
    averybigblackdogwithdots
    averysmallwhitecatwithatail
    averytinygreymousewithanose
    averytinygreymousewithabignose
    averybigblackdogwithdots
    averybigblackdogwithdotts
    averysmallwhitecatwithalongtail
    averytinygreymousewithanosee
    aaaaaaaaaaaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaaaaaabbbbb
    

When the script is executed you shall see the `print newWordList` will show
you a list of distinct words with length of 15 letters in console.

**Limitation:** I just wanted to point out that there is a limitation in
creating distinct words too using replacement method like what I did. Eg:
Consider a word of 2 letters and if you are allowed to use 2 letters for
creating all the possible words then you can only generate 4 different words.

**Eg:** Word = IN, replacement letters are A,B

Distinct words could only be: IA, IB, AN, BN. Because in replacement method we
can only replace a letter without changing their position. We can only replace
I or N with A or B. We can't do something like NA, NB, AI, BI etc. using
replacement method.

I hope this was a bit helpful.

