InstructionsEach word in your lexicon should also store a list of neighbour words.From the assignment brief, a neighbour of a word w is a word that:Is of the same length as w and,Differs from w by only one letter.For example, if the word w refers to "cat", then:"cathy" is not w's neighbour, for they are not of the same length."man" is not w's neighbour, for they differ by more than one letter."can" is a neighbour of w"fat" is a neighbour of w"cot" is a neighbour of wIn this task, you should define a function add_neighbours() that takes one argument, your lexicon, and for every word in your lexicon, populates the list of neighbours that meet the criteria above.When adding neighbours, you should also ensure that the list of neighbours for each word is in sorted order.REMINDER: You are NOT allowed to use any of the builtin sorting functions/methods in Python (e.g. sorted() or .sort()).Once you have written this function, call it on your lexicon to populate the neighbours for each word in your lexicon.
Question
InstructionsEach word in your lexicon should also store a list of neighbour words.From the assignment brief, a neighbour of a word w is a word that:Is of the same length as w and,Differs from w by only one letter.For example, if the word w refers to "cat", then:"cathy" is not w's neighbour, for they are not of the same length."man" is not w's neighbour, for they differ by more than one letter."can" is a neighbour of w"fat" is a neighbour of w"cot" is a neighbour of wIn this task, you should define a function add_neighbours() that takes one argument, your lexicon, and for every word in your lexicon, populates the list of neighbours that meet the criteria above.When adding neighbours, you should also ensure that the list of neighbours for each word is in sorted order.REMINDER: You are NOT allowed to use any of the builtin sorting functions/methods in Python (e.g. sorted() or .sort()).Once you have written this function, call it on your lexicon to populate the neighbours for each word in your lexicon.
Solution
The instructions are asking you to create a function in Python that will add 'neighbour' words to each word in your lexicon. A 'neighbour' word is defined as a word that is the same length as the original word and only differs by one letter.
Here is a step-by-step guide on how to do this:
- Define your function: Start by defining your function
add_neighbours(). This function should take one argument, which is your lexicon.
def add_neighbours(lexicon):
- Loop through the lexicon: For each word in your lexicon, you need to find its neighbours. You can do this by creating a nested loop that goes through the lexicon again for each word.
for word in lexicon:
for potential_neighbour in lexicon:
- Check if the potential neighbour is a neighbour: In this step, you need to check if the potential neighbour is a neighbour of the word. This means that it should be the same length and only differ by one letter. You can do this by comparing the lengths of the words and then checking the number of differences.
if len(word) == len(potential_neighbour):
differences = sum(a != b for a, b in zip(word, potential_neighbour))
if differences == 1:
- Add the neighbour to the word's list of neighbours: If the potential neighbour is a neighbour, you should add it to the word's list of neighbours.
word.neighbours.append(potential_neighbour)
- Sort the list of neighbours: After adding all neighbours, you need to sort the list of neighbours. Since you are not allowed to use built-in sorting functions, you can implement a simple bubble sort algorithm.
for i in range(len(word.neighbours)):
for j in range(len(word.neighbours) - 1):
if word.neighbours[j] > word.neighbours[j + 1]:
word.neighbours[j], word.neighbours[j + 1] = word.neighbours[j + 1], word.neighbours[j]
- Call the function on your lexicon: Finally, you need to call the function on your lexicon to populate the neighbours for each word.
add_neighbours(lexicon)
This is a basic implementation and might need to be adjusted based on the specifics of your lexicon structure.
Similar Questions
From the assignment brief, along with the spelling of the word, a word in your lexicon needs to store the following information:The frequency: How many times the word appears in the input files.The list of neighbours: A neighbour of a word w is a word that is of the same length and differs from w by only one letter.The best way this can be implemented is as a Word class, where you can populate your lexicon with Word objects.Your task in this section is to create a class Word that is used to represent a Word in your lexicon.When you do this, you will need to:Think about what instance variables should be defined (and how they should be initialized)Think about what methods you need to implement for this classIn a future task, you will need to sort your lexicon full of Word objects. In the labs you saw a similar example where you needed to sort a collection of Person objects. It may be useful to refer back to this to see what methods were required.You may find that once you attempt the following tasks, you need to come back to this class and add additional methods.
In a certain code language the word ATMOSPHERE is written as BSLPROGFQF. How will beword NEIGHBOURS be written in that language ?
correct answerIf all the letters in the word NEIGHBOUR are rearranged as they appear in the english alphabets , the position of how many letters will remain unchanged after the rearrangement ?OptionsMore than threeNoneOneThreeTwo
define 'word'and write an essay on 'word' as a syntactic unit
If all the letters in the word NEIGHBOUR are rearranged as they appear in the english alphabets , the position of how many letters will remain unchanged after the rearrangement ?OptionsMore than threeNoneThreeTwoOne
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.