package com.codegym.task.task09.task0930;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
Task about algorithms
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> list = new ArrayList<String>();
while (true) {
String s = reader.readLine();
if (s.isEmpty()) break;
list.add(s);
}
String[] array = list.toArray(new String[list.size()]);
sort(array);
for (String x : array) {
System.out.println(x);
}
}
public static void sort(String[] array) {
// write your code here
for ( int i=0; i<array.length-1; i++ )
{
if ( isNumber(array[i]) )
{
for (int j=i+1; j<array.length; j++ )
if ( isNumber(array[j]) )
{
int a = Integer.parseInt(array[i]);
int b = Integer.parseInt(array[j]);
if ( a<b )
{
array[i] = array[j];
array[j] = String.valueOf(a);
}
}
}
else
{
for (int j=i+1; j<array.length-1; j++)
{
if ( !isNumber(array[j]) )
{
String a = array[i];
isGreaterThan(array[j], array[i]);
array[i]=array[j];
array[j]=a;
}
}
}
}
}
// String comparison method: 'a' is greater than 'b'
public static boolean isGreaterThan(String a, String b) {
return a.compareTo(b) > 0;
}
// Is the passed string a number?
public static boolean isNumber(String s) {
if (s.length() == 0) return false;
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if ((i != 0 && c == '-') // The string contains a hyphen
|| (!Character.isDigit(c) && c != '-') // or is not a number and doesn't start with a hyphen
|| (i == 0 && c == '-' && chars.length == 1)) // or is a single hyphen
{
return false;
}
}
return true;
}
}
Struggling with isGreaterThan(String a, String b) method. Did not quite get how it works. Can someone please clarify on that. I would really appreciate it🙏
Resolved
Comments (8)
- Popular
- New
- Old
You must be signed in to leave a comment
Ntuthuko Xaba
11 April 2020, 21:48
.compareTo() , is actually a String method that compares strings. eg. Just as int 5 is > int 2,
so in strings the String ' D ' (4) is > ' B' (2). Hope that makes sense.
0
Jakhongir Ruziev QA Automation Engineer at Exadel
12 April 2020, 06:52
Yes thank you, that makes sense.
0
Ntuthuko Xaba
11 April 2020, 21:44
On line 50 - it should be:
for (int j = 0; j < array.length; j++) {
On line line 54 you made a mistake of getting
[i] but you don’t have [i] initialised in this particular
codeblock.
The [i] you are calling is from the ' if ' codeblock.
You need to insert the following before line 54:
for (int i = 0; i < array.length; i++) {
After this it should be fairly simple to figure the rest out.
0
Jakhongir Ruziev QA Automation Engineer at Exadel
12 April 2020, 06:57
i is initialized at the beginning of the for loop(line 32) if i is number it goes to first block(line 34) else goes to second(line 48). And then it checks for j if it is not number(line 52) it iterates through them. But if I enter only words it is not sorting, that is why I don't get why it is not working 🤷♂️
0
Ntuthuko Xaba
12 April 2020, 14:08solution
I'm not sure where you got it wrong but you can look at my code and compare.
public static void sort(String[] array) {
//Lets sort the numbers
for (int i = 0; i < array.length; i++) {
String a = array[i];
if(isNumber(a)){
for (int j = 0; j < array.length; j++) {
String b = array[j];
if(isNumber(b)){
int aa = Integer.parseInt(array[i]);
int bb = Integer.parseInt(array[j]);
if( aa > bb){
String temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
}
//Lets sort words
for (int i = 0; i < array.length; i++) {
String a = array[i];
if(!isNumber(a)){
for (int j = 0; j < array.length; j++) {
String b = array[j];
if(!isNumber(b)){
if(isGreaterThan(b,a)){
String temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
}
}
+3
Liliane Top
12 April 2020, 15:56
Thanks to you I did find the solution and also found what is not working in your code.
As long the following is true is keeps swapping positions.
isGreaterThan(array[i], array[j]) returns true (>0) only if the String value of array[i] is greater than array[j]. Otherwise it would return a '0' if they are equal or '-1' if it is smaller. And nothing will be changed.
So in case it's value is greater you swap their position.
0
Liliane Top
12 April 2020, 15:58
Oh i forgot to tell you that I also changed the 'array.length -1' to 'array.length' otherwise you will not get to the last item in the array. So either one of the for loops should check the last item.
0
Jakhongir Ruziev QA Automation Engineer at Exadel
13 April 2020, 02:38
Thank you guys!
0