Finding Top 5 in Descending Order
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I am having trouble finding a way to display the top 5 results in descending order from this code. Any help would be appreciated.
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Welcome to the Ranch
You cannot usually sort out that sort of thing in code, but you would need to work out the algorithm with pencil and paper first. Start by working out how to go through the collection and find the largest item. That should be easy enough. Then try the next largest. What are you going to do if you have two large items the same size?
Don't use CAPITAL_LETTERS and underscores in Java® variable names. Use camelCase instead.
You cannot usually sort out that sort of thing in code, but you would need to work out the algorithm with pencil and paper first. Start by working out how to go through the collection and find the largest item. That should be easy enough. Then try the next largest. What are you going to do if you have two large items the same size? Don't use CAPITAL_LETTERS and underscores in Java® variable names. Use camelCase instead.
Tim Johns
Greenhorn
Posts: 14
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
So is it possible to accomplish want I want by manipulating this code? This is a school assignment and I am kind of at a loss.
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
i wouldn't. That code is pretty bad. There is one single method that does everything, which makes it hard to adjust. it's impossible to change one part without it affecting the entire thing.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tim Johns
Greenhorn
Posts: 14
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Unfortunately, I have to use this code for the assignment. Any ideas?
posted 9 years ago
But that code doesn't find the top 5 numbers. So you can't use that code to find the top 5 numbers.
So presumably you have to modify it in some way so that the modified version finds the top 5 numbers? If that's the case then I don't see any barrier to cleaning up the code to make it usable, as the others have already suggested.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Tim Johns wrote:Unfortunately, I have to use this code for the assignment. Any ideas?
But that code doesn't find the top 5 numbers. So you can't use that code to find the top 5 numbers.
So presumably you have to modify it in some way so that the modified version finds the top 5 numbers? If that's the case then I don't see any barrier to cleaning up the code to make it usable, as the others have already suggested.
Tim Johns
Greenhorn
Posts: 14
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
That's were I'm stuck. I can't figure out what I need to modify and add to make the code display the top 5 results in descending order.
Campbell Ritchie
Marshal
Posts: 81617
593
posted 9 years ago
Please be careful about // comments They are only intended for short comments, and they should not be used for something which is already obvious from reading the code. Look at line 30 should be a /* comment */ if you are keeping the comment at all
.Why are you looping a List to print it out? Have you tried this?
System.out.println(myList);
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I told you yesterday that you probably can't modify that code. You need separate methods to read the numbers, to find the largest, etc. Unless you have specifically been given that code and told to modify it, you will not be able to use that code again.Tim Johns wrote:. . . modify and add . . .
Please be careful about // comments They are only intended for short comments, and they should not be used for something which is already obvious from reading the code. Look at line 30 should be a /* comment */ if you are keeping the comment at all
.Why are you looping a List to print it out? Have you tried this?
System.out.println(myList);
Tim Johns
Greenhorn
Posts: 14
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I have been given this code and specifically told to modify it. That is why I am stuck on what I need to add to make this existing code print the top five results in descending order.
Tim Johns
Greenhorn
Posts: 14
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Also, this is not my code so I am not sure why it was written the way it was.
posted 9 years ago
Top 5 unique numbers or just the top 5 numbers?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Tim Johns wrote:I am having trouble finding a way to display the top 5 results in descending order from this code.
Top 5 unique numbers or just the top 5 numbers?
Tim Johns
Greenhorn
Posts: 14
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Just the top 5 in descending order.
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
So, top 5 "numbers".
Your "numbers" are actually Strings. So do you want them descending in String order or do you want to convert them to an int (or Integer) and sort by that?
Your "numbers" are actually Strings. So do you want them descending in String order or do you want to convert them to an int (or Integer) and sort by that?
Tim Johns
Greenhorn
Posts: 14
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I think I want it to sort by string order.
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You could use Collections.sort(number_list) which gives you the list in ascending order and then go to the end and count backwards for 5 indexes.
You could use Collections.sort(number_list,myComparator) which you'd have to write a comparator that gives you the list in descending order and look at the first 5 indexes.
You could use Collections.sort(number_list,myComparator) which you'd have to write a comparator that gives you the list in descending order and look at the first 5 indexes.
Tim Johns
Greenhorn
Posts: 14
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I have this now but it is saying "wordsArray cannot be resolved to a variable in lines 44, 56, and 65.
posted 9 years ago
You've defined wordsArray inside the while() loop. It will not be visible outside of the loop.
"wordsArray" is a poor name as it is not an array.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You've defined wordsArray inside the while() loop. It will not be visible outside of the loop.
"wordsArray" is a poor name as it is not an array.
Tim Johns
Greenhorn
Posts: 14
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Any advice on what needs to be changed around? I am stuck again at this point and my deadline is soon. I have been messing around with this thing for days.
Tim Johns
Greenhorn
Posts: 14
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I have this now and when I run it nothing displays. Any ideas?
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
See comments
Tim Johns
Greenhorn
Posts: 14
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
This is what I have now and nothing is displaying still. Any ideas? The scanner document is actually completely full of words so I need the program to display the top 5 words and their frequence in the console.
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Now I get it. You want to display the 5 most popular movies.
First off, uncomment and fix line 24. This is required to populate your list.
First off, this is a BIG red flag. You have two arrays, 'number' and 'freq', whose indexes must be kept in sync. This is a recipe for disaster, and in your case the disaster happens when you go to sort on 'freq'. 'freq' will be sorted but then its indicies will no longer coincide with those of 'number'.
The only way around this is to create an inner class that holds the 'number' and 'freq' as a pair. Something like:
I hate to say it but this requires some major refactoring of your code. I'll try and walk you through it.
So now we have to replace your number/freq arrays with something that makes use of the new WordFreq. We could use an array but then we'd constantly be scanning it for the matching word. A better tool for this is a Map where the key is the 'word' (movie) and the value is a WordFreq object.
Now, as this map is populated only unique words held in the keys and their associated WordFreq objects, held as values will be kept. Maps do not allow duplicate keys.
So far so good. Now we have to sort the WordFreq object based on freq. This requires the creation of a inner Comparator class because WordFreq has no natural ordering. And while we're at it we might as well sort them in descending frequency order.
So now to sort them we can make a List out of the wordFreqMap values (which are WordFreq objects).
And lastly we have to display the sorted list, which is made a bit easier because we've added a toString() method to WordFreq.
Done.
Sorry this is so involved but I didn't see any cleaner way of dealing with your parallel array issue. I'm hoping that throwing Maps and Comparators at you isn't too much.
First off, uncomment and fix line 24. This is required to populate your list.
First off, this is a BIG red flag. You have two arrays, 'number' and 'freq', whose indexes must be kept in sync. This is a recipe for disaster, and in your case the disaster happens when you go to sort on 'freq'. 'freq' will be sorted but then its indicies will no longer coincide with those of 'number'.
The only way around this is to create an inner class that holds the 'number' and 'freq' as a pair. Something like:
I hate to say it but this requires some major refactoring of your code. I'll try and walk you through it.
So now we have to replace your number/freq arrays with something that makes use of the new WordFreq. We could use an array but then we'd constantly be scanning it for the matching word. A better tool for this is a Map where the key is the 'word' (movie) and the value is a WordFreq object.
Now, as this map is populated only unique words held in the keys and their associated WordFreq objects, held as values will be kept. Maps do not allow duplicate keys.
So far so good. Now we have to sort the WordFreq object based on freq. This requires the creation of a inner Comparator class because WordFreq has no natural ordering. And while we're at it we might as well sort them in descending frequency order.
So now to sort them we can make a List out of the wordFreqMap values (which are WordFreq objects).
And lastly we have to display the sorted list, which is made a bit easier because we've added a toString() method to WordFreq.
Done.
Sorry this is so involved but I didn't see any cleaner way of dealing with your parallel array issue. I'm hoping that throwing Maps and Comparators at you isn't too much.
| Well don't expect me to do the dishes! This ad has been cleaned for your convenience: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |










