The inhabitants of Planet Linear Chaos give their children sorting tasks instead of Rubik's cubes. Here's a pastime enjoyed by small ordered isomorphs. Knock yourself out: after the user enters a list of words (and numbers) from the keyboard, the program should display the words in ascending order, and the numbers in descending order.
Task about algorithms
- 12
Locked
Comments (20)
- Popular
- New
- Old
You must be signed in to leave a comment
Naughtless
1 June 2021, 08:44
Well I brute-forced this one with an array and two ArrayLists.
1. One identifier Integer array of 0 and 1.
2. Two ArrayLists containing numbers and strings.
3. I used a regular for loop and a try-catch block to fill in both the identifier array and the two ArrayLists.
See, if it's not parse-able into an Integer, then it will throw an Exception which when caught, calls the catch block which adds the array entry into the strings array instead of the integers.
4. I sorted both with collections.
5. Then reassembled them into an ordinary String array, using the identifier array as guide which entries were numbers and which entries were strings.
Hope that can help people who are struggling, since the completion numbers seems to just plummet on this task. +5
Karas Java Developer
22 March 2021, 22:01
I am of the opinion that definitielly using collections was better, faster.
Mine was for loop twice, one to split the numbers and strings into two arrays then again to put them back according to the entered sequence, that is a small detail is not mentioned but if you see the output 22 and zero are one after the other, a smal if else statement to check for the requirements and poof.
0
Sinisa
18 March 2021, 12:13
Lets just say the proposed way of solving this task isn't the most intuitive one...😁
+1
Jurij Thmsn
31 January 2021, 14:21
I always love the Tasks about algorithms 😍
+1
George
2 October 2020, 21:21
Apparently, I overcomplicated this. I was almost certain I needed to use hashmaps in this one and use the array's indeces as keys. After checking the Help section it seems there was a faster way without wasting memory space.
Oh well, it worked so I'm still happy with the result.
0
Lucas Hoage
6 June 2020, 18:18
I thought I was so slick converting the int values using String.ValueOf and then running IsGreaterThan to do the comparison, and then pipe the results in reverse order back into the array. I can't believe CodeGym wouldn't allow that but they allowed us to run Collections to fiddle with the numbers.
Come onnn, that was a great way to do it. No need to go through and create a whole new sorting system for the numbers when we could have just converted and utilized what we had.
Yeah Collections does the job either way but man I really would have liked to pass with the other method.
0
Sam
18 March 2020, 03:21
I kept using the isGreaterThan method for the numbers in the array in the final task, then I realize it was only meant for the strings in the array. would have saved a lot of time if I just read the requirements carefully.
0
Kent Hervey Software Engineer/Consult at Zeal IT ConsultantsExpert
12 March 2020, 01:48
Clearly this is the hardest problem so far since completion number is the least
+2
Kent Hervey Software Engineer/Consult at Zeal IT ConsultantsExpert
10 March 2020, 16:26
So I think the output should be
first word
last number
next word
number previous to last
etc
0
Alex
21 August 2019, 07:45
In the sort method:
1. Make a for(int i = 0; i < array.length - 1; i++)
2. check if the array[i] is number. If it is -> Step 3, else -> Step 7.
3. Make a for(int j = i + 1; j < array.length; j++)
4. Check if array[j] is number. If it is, Step 5, else Step 6
5. Make 2 int variables and convert the array[i] and array[j] to ints and compare them. Wen you make the swap, make between the strings, not between the ints.
6. Do nothing.(else { })
7. Because the array[i] is not number, we iterate over j to check if is not number as well: for(int j = i + 1; j < array.length; j++).
8. Check if array[j] is not number. If it is not, compare array[i] and array[j] with isGreaterThan method and swap them. If array[j] is number -> step 9
9. Do nothing.
Hope this helps you. This is the easiest way for me. I didn't provide the code so you can put yourselves. :P
If you need any assistance, please let me know. Good luck!
+21
Nikolaos
4 September 2019, 16:01
Thank you Alex! I have just followed the algorithm you suggest!
0
Alex
4 September 2019, 16:21
Anytime! :)
0
shaan mohd khan QA Automation Engineer at mphasis Ltd
12 December 2019, 09:19
Hey Alex, My code is throwing null pointer exception error on the 18th line. not able to find a way to resolve it.Can you help
+1
Alex
18 December 2019, 07:23
Hi,
Sorry for the late.
It would be helpful if you would give me in private, your full source code so I can help you with the error.
0
shaan mohd khan QA Automation Engineer at mphasis Ltd
20 December 2019, 08:52
I got it.. Its solved now.. Btw thanx for help
0
Jeanine Kimball
8 July 2020, 14:13
This was beautifully explained. This saved my life. Thank you!
0