Starting from:

$29.99

CPSC319 Assignment 1- Searching & Sorting Solution



GOAL

The goal of this assignment is to write a Java program that arranges a list of words into separate lists of anagrams. Your program should be named “asgmt1" and will be called from the command line. The only input to your program is a file containing a list of words to be sorted into anagrams and is read by your program through standard input. The number of words in the input is arbitrary. Your program should print to standard output the lists of anagrams in the following way:

For example, this input text file:

car
mega bed stop game pots arc tops  Should yield this output text file:

arc car bed
game mega
pots stop tops

1. All the words that are anagrams of each other are printed on one line of output.
2. The words on each line should be in alphabetic order.
3. The groups should be ordered alphabetically by the string that results by sorting the characters in each word.
4. Exactly one space between words on the same line, and no other spaces.


Input File Expected Output
example_1--8_words example_1_out
example_1--13_words example_2_out
example_1--19_words example_3_out
example_1--267_words example_4_out
on D2L) as shown in the right:


ALGORITHMS & DATA STRUCTURES

Step #1. The data from the input structured in a 1-D array of words (e text file “example_1--8_words”):



0 car
1 mega
2 bed
3 stop
4 game
5 pots
6 arc
7 tops


Input text file
text file should be xample: input
LIST A

1-D array of words Step #2. “LIST A” should then be sorted:


0 arc
1 bed
2 car
3 game
4 mega
5 pots
6 stop
7 tops
LIST A

Sorted
1-D array of words
Step #3. Traverse LIST A to generate the required output text file format of found anagrams exactly as shown in the example below for all the 4 input files.


arc car
bed  game mega
pots stop tops

Output
text file for “example_1_out”



Finding Anagrams: A good way to determine if two words are anagrams is to sort the letters in both words. If the two sorted words are the same, then the original two words are anagrams. For example, array entries #5, #6, and #7 (i.e., “pots”, “stop”, and “tops”) from the sorted LIST A of words (i.e., step #2) are anagrams:

‘o’ ‘p’ ‘s’ ‘t’
0 1 2 3

‘o’ ‘p’ ‘s’ ‘t’
0 1 2 3

‘o’ ‘p’ ‘s’ ‘t’
0 1 2 3
‘p’ ‘o’ ‘t’ ‘s’
0 1 2 3

‘s’ ‘t’ ‘o’ ‘p’
0 1 2 3

‘t’ ‘o’ ‘p’ ‘s’
0 1 2 3
𝒂𝒂𝒂𝒂𝒂𝒂𝒂𝒂𝒂𝒂 𝒔𝒔𝒔𝒔𝒂𝒂𝒂𝒂𝒔𝒔𝒔𝒔𝒔𝒔
⎯⎯⎯⎯⎯⎯⎯⎯⎯

𝒂𝒂𝒂𝒂𝒂𝒂𝒂𝒂𝒂𝒂 𝒔𝒔𝒔𝒔𝒂𝒂𝒂𝒂𝒔𝒔𝒔𝒔𝒔𝒔
⎯⎯⎯⎯⎯⎯⎯⎯⎯

𝒂𝒂𝒂𝒂𝒂𝒂𝒂𝒂𝒂𝒂 𝒔𝒔𝒔𝒔𝒂𝒂𝒂𝒂𝒔𝒔𝒔𝒔𝒔𝒔 ⎯⎯⎯⎯⎯⎯⎯⎯⎯



HAND-IN

• Submit your source code electronically to D2L dropbox.
• Please name your source code file as follows: “asgmt-1-your-last name-UCID” • Late assignments will not be accepted.

MARKING

Source code that does not compile or produces run-time errors will receive a grade of 0%

Input File Expected Output # Points
for correct output (Step #3) for partially correct output (Step #2)
example_1--8_words example_1_out 2 1
example_1--13_words example_2_out 2 1
example_1--19_words example_3_out 2 1
example_1--267_words example_4_out 2 1
Total 8 4
















2



Asgmt #1 – Searching & Sorting Page 3 of 3
COLLABORATION


• The assignment must be done individually so you must write up the solutions on your own.


END OF THE ASSIGNMENT

3

More products