Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

NEED in JAVA You can use sorting functions like quicksort, insertion sort For your poetry class, you would like to tabulate a list of rhyming

NEED in JAVA

You can use sorting functions like quicksort, insertion sort

For your poetry class, you would like to tabulate a list of rhyming words. For example, if you had the following words (electrics, ethnic, clinic, coughed, laughed, metrics), your program should produce the following output:

 [ cli|nic, eth|nic ] [ elec|trics, me|trics ] [ co|ughed, la|ughed ] 

Write a program Rhyming.java that reads in a sequence of words from standard input and prints them out in the order specified above. Specifically, we define here rhyming words as a group of words sharing a rhyme, and we define that rhyme as being the longest possible suffix common to all words in the group. For instance, in the example above, we do not consider "ic", "ics", or "ed" to be rhymes because they are suffixes of longer common suffixes.

The output order is defined as follows.

Words are listed in increasing order of suffix length

Among suffixes of the same length, words are listed in alphabetical suffix order

Words with common suffix (rhyming words) are listed in alphabetical order

Looking back at the previous example: the suffixes are nic (length=3), trics (length=5), and ughed(length=5). They appear in that order as a result of rule 1 (length 3 before length 5) and rule 2 (trics comes before ughed alphabetically). Finally, each set of rhyming words are listed alphabetically (rule 3). In addition, groups of rhyming words are separated by a newline. Note that the same word can rhyme with different groups of words. For instance: (friendly, lucky, murky, rapidly) would lead to the following output:

 [ friendl|y, luck|y, murk|y, rapidl|y ] [ luc|ky, mur|ky ] [ frien|dly, rapi|dly ] 

Again, while "y" is not a rhyme for the group (lucky, murky) (it is a suffix of the actual rhyme "ky"), it is a rhyme for the group (friendly, lucky, murky, rapidly), as it is the longest common suffix.

Input / Output

Your program should read from standard input, using StdIn.java. It should process the input using the following command line:

 %java -cp .:stdlib.jar:algs4.jar Rhyming < list1.txt 

The file contains a list of strings, one string per line. Your program should output to standard output all the words sorted as the example stated above. To facilitate the visual assessment of your result, start each new line (corresponding to a series of rhyming words in alphabetical order) with an opening square bracket followed by a space character and, symmetrically, terminate each new line with a space character followed by a closing square bracket. In addition, each word should contain a pipe symbol ("|") that indicates the beginning of the identified suffix. Again, refer to the examples above and to the solution files provided below.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions