The program intends to find the "longest compounded word" alongwith the "second longest compounded word" from a text file given by user and mentions the time taken by the program to do so.
- Clone the github repository
git clone https://github.com/Abhigyan-Sh/LongestCompoundedWord.git
- Navigate to the project directory
cd impledge_technologies/
- Execute the program
node longestCompoundWord.js
- After that now it says, Enter the file name:
Enter the file name: Input_01.txt
// OR
Enter the file name: Input_02.txt
starting-off with the file_name, as a developer its a wrong practice to hardcode your input or if someone has to provide file_name they shouldn't be doing so by writing into the code itself. So I use readline module which allows the program to read input from user via terminal or command line interface.
File Path Handling: also I have kept relative file path rather than absolute file path as then all your programs/code, assets seems to be in one place and moreover in future if we shift the folder to some other directory, still the code would work as intended and nothing would break as relative path is still the same.
-
We take the filename as input from the user. For this we use readline module which helps to read the input line by line.
-
We start counting the time as soon as program starts after taking input from user.
-
Read the input file content considering utf8 encoding, we extract out words into an array which we call words.
-
Now as this array contains the longest compounded words if exists any so we need to iterate it but the catch here is that if we need to get the longest possible we need to store the current longest compounded word in some variable while iterating words array until a compounded word of greater length is found or we can instead sort the array in decreasing order of length of words instead which seems to be more easily done as now the very first compounded word which we get is surely the longest.
-
We will pick words one by one from the array words and will have to search for each possible prefix and suffix which can be made out of this single word but in doing that we encounter another challenge as the word can match to itself when
prefix == word
so for that we can use a SET data-structure which also serves as an alternate collection from which we will delete the current word for which we are finding that whether it could be the longest compounded word or not, and so that one of the possible prefix or suffix doesn't match with the word itself and that is where we employ the line
for (const word of words) {
unique_words.delete(word);
...
}
-
If there doesn't exists any possible compounded word we return a message saying "couldn't locate ๐ for ... longest compound word".
-
After the process has been done, get the current time as end_time and return
end_time - start_time
alongwith the results obtained for longest and second longest compounded words.