I have algorithm that translate six types of XPath queries into SQL queries. so, my code contains If-elseif-else statement (multiple if). I read from the internet that the time complexity of the If-elseif-else statement is the worst-case time of one one of the if that has more processing. I need to know what is is the time complexity for this code:
} else if (Query_Type == 5){
for (int i = strXPathQuery.length()-1; i > 0; i--) {
if (strXPathQuery.charAt(i) == '/') {
position = i;
break;
}
} // end for loop
Last_Node = strXPathQuery.substring(position+1);
strAncestor_Path = "";
int bracket_pos=0;
for (int i = 0; i < position; i++) {
if (strXPathQuery.charAt(i) == '[') {
bracket_pos = i;
break;
} else if (strXPathQuery.charAt(i) == '/' && strXPathQuery.charAt(i+1) == '/') {
strAncestor_Path = strAncestor_Path + "%";
}
else {
strAncestor_Path = strAncestor_Path + strXPathQuery.charAt(i);
} // end if statement
} // end for
int operator_pos = 0;
String Node_condition="";
for (int i = bracket_pos+1; i < position-2; i++) {
if ((strXPathQuery.charAt(i) == '<') || (strXPathQuery.charAt(i) == '>') || (strXPathQuery.charAt(i) == '=') || (strXPathQuery.charAt(i) == '!')) {
operator_pos = i;
break;
}
else {
Node_condition = Node_condition + strXPathQuery.charAt(i);
} // end if }
String Value_condition="";
for (int i = operator_pos; i < position-1; i++) {
Value_condition = Value_condition + strXPathQuery.charAt(i);
} // end for loop
strSQLQuery = "SELECT L2.Node_Value \n" +
"FROM Leaf_Node L1, Leaf_Node L2, Ancestor_Path P\n" +
"WHERE P.Ances_PathExp LIKE '" + strAncestor_Path + "'\n" +
"AND L1.Ances_PathID = P.Ances_PathID \n" +
"AND L1.Node_Name = '" + Node_condition + "'\n" +
"AND L1.Node_Value '".replace("'", "") + Value_condition + "'\n".replace("'", "") +
"AND L2.Node_Name = '" + Last_Node + "'\n" +
"AND L1.Ances_PathID = L2.Ances_PathID \n" +
"AND L1.Ances_Pos = L2.Ances_Pos " ;
txtSQLQuery.setText(strSQLQuery);
}
}
You have three looks there that could be O(N^2). For example.
for (int i = 0; i < position; i++) {
...
strAncestor_Path = strAncestor_Path + strXPathQuery.charAt(i);
...
}
Assume (worst case) that for that loop, the value of position is strXPathQuery.length() ... or N. That means that you are appending a character to the same string N times. Since appending a character to a string is an O(N) operation. (The append is creating a new string, copying all characters in the existing string.) The complexity of doing that N times is O(N^2).
The average complexity could be better than that, but it will depend on the input.
(And I don't have the patience to get my head around what you are actually trying to here. The your code's crap style is making my eyes hurt.)
If you want to perform the performance, don't build strings like that. Use a StringBuilder.
StringBuilder path = new StringBuilder();
for (int i = 0; i < position; i++) {
...
path.append(strXPathQuery.charAt(i));
...
}
Related
I'm trying to create a confusion matrix however I'm not sure how to properly return my method where I can index both predicted and actual values, here's what I have tried thus far:
int[][] matrixConfusion(int[] predicted, int[] actual){
int[] count = new int [10];
int counts = 0;
int counts1 = 0;
int counts2 = 0;
int counts3 = 0;
for(int i = 0; i < count.length; i++) {
if(actual[i] == count[i] && predicted[i] == count[i]) {
counts++;
}else if(actual[i] != count[i] && predicted[i] != count[i]) {
counts1++;
}else if(actual[i] != count[i] && predicted[i] == count[i]) {
counts2++;
}else if(actual[i] == count[i] && predicted[i] != count[i]) {
counts3++;
}
}
System.out.println("\t"+ "Actual = 0:"+"\t"+"Actual = 1:"+"\n" + "Predic = 0:" + "\t" + counts + "\t" + counts2 + "\n" + "Predic = 1:" + "\t" + counts3 + "\t" + counts1);
return null;
}
The code works by printing out the statements above however, I cannot find a way to properly return the values from print,so that I can return the values rather than print them.
Based from the accepted answer from this post, I have this code:
if (authors.length >= 1) {
System.out.print(authors[0]);
}
for (int i = 1; i < authors.length; i++) {
System.out.print("; " + authors[i]);
}
So the output of this is author1; author2; author3
How can I change this into author1; author2 & author3? If there are only 2 authors, the output should be author1 & author2. Thanks in advance.
You'd just need to add in one conditional to your loop to handle the last case:
for (int i = 1; i < authors.length; i++) {
if(i == authors.length - 1)
System.out.print("& " + authors[i]);
else
System.out.print("; " + authors[i]);
}
One way to do it would be changing the structure of the code to use a loop and a boolean flag instead of a conditional, like this:
boolean isFirst = true;
for (int i = 0 ; i != authors.length ; i++) {
if (!isFirst) {
System.out.print(i == authors.length-1 ? "& " : "; ");
} else {
isFirst = false;
}
System.out.print(authors[i]);
}
Demo.
You can do it recursively to separate cases clearly. Seems like other answers lack that.
This is the proxy function:
public static String doIt(String[] authors){
if (authors == null || authors.length == 0){
return "";
}
if (authors.length == 1){
return authors[0];
}
return authors[0] + doHelper(authors, 1);
}
And the helper function:
public static String doItHelper(String[] authors, int index){
if (index == authors.length - 1){
return " & " + authors[index];
}
return "; " + authors[index] + doItHelper(authors, index + 1);
}
As mentioned in comments (Thanks #JNYRanger) this is not optimal when performance is an issue.
Can't test it now, so I hope the idea is clear.
Try it this way:
String[] authors = { "1", "2", "3", "4", "5" };
StringBuffer sb = new StringBuffer();
for (int i = 0; i < authors.length; i++) {
sb.append(authors[i]);
if (i + 2 < authors.length) {
sb.append(";");
} else if (i + 2 == authors.length) {
sb.append("&");
}
}
System.out.print(sb.toString());
for (int i = 0; i < authors.length; i += 1) {
if (i > 0) {
System.out.print(i < authors.length - 1 ? "; " : " & ");
}
System.out.print(authors[i]);
}
String[] authors = {"a", "b", "c", "d"};
for (int i = 0; i < authors.length; i++) {
System.out.print((i != 0 ? (i == authors.length - 1 ? " & " : "; ") : "") + authors[i]);
}
I am coding a simple chatbot, and my method seems to be stuck in a loop.
This below is the method, and I suspect there is a problem with the while loop, but I cannot find where I am messing up. No problems with compiling and running other than the stuck loop.
The loop below takes in a complete string, statement, loops for specific keywords, goal, and the starts looking through the string at startPos
private int findKeyword(String statement, String goal, int startPos)
{
String phrase = statement.trim();
int psn = phrase.toLowerCase().indexOf(goal.toLowerCase(), startPos);
while (psn >= 0)
{
String before = " ", after = " ";
if (psn > 0)
{
before = phrase.substring(psn-1, psn).toLowerCase();
}
if (psn + goal.length() < phrase.length())
{
after = phrase.substring(psn + goal.length(), psn + goal.length() + 1).toLowerCase();
}
if (((before.compareTo("a") < 0) || (before.compareTo("z") > 0)) && ((after.compareTo("a") < 0) || after.compareTo("z") > 0))
{
return psn;
}
psn = phrase.indexOf(goal.toLowerCase(), psn + 1);
}
return -1;
}
psn = phrase.indexOf(goal.toLowerCase(), psn + 1);
will never rich to (psn < 0) thats the problem
I am trying to print a polynomial from a given number.
I did the example below, but for something like 100 it will print 1x^2+, when I want just x^2. What I'm looking for is how can I make it to not print + and at the same time get rid of coefficients that are 1.
Edit: I did it, it prints perfectly. Feel free to use it.
private static String S_frumos(int poli) {
String s = "";
for (int i = 0; i < String.valueOf(poli).length(); i++) {
int nr = Character.getNumericValue(S_GetCoefs(poli, i));
if (nr != 0) {
if (i == String.valueOf(poli).length() - 1) {
s = s + nr;
} else if (i == String.valueOf(poli).length() - 2) {
if ((S_zero(poli, i + 1) == 1)) {
if (nr != 1) {
s = s + nr + "x";
} else {
s = s + "x";
}
} else {
if (nr != 1) {
s = s + nr + "x" + "+";
} else {
s = s + "x" + "+";
}
}
} else if ((S_zero(poli, i + 1) == 1)) {
if (nr != 1) { s = s + nr + "x^" + (String.valueOf(poli).length() - i - 1);}
else { s = s + "x^" + (String.valueOf(poli).length() - i - 1);}
} else {
if (nr != 1){ s = s + nr + "x^" + (String.valueOf(poli).length() - i - 1) + "+";}
else { s = s + "x^" + (String.valueOf(poli).length() - i - 1) + "+";}
}
}
}
return s;
}
private static int S_GetCoefs(int poli, int x) {
return String.valueOf(java.lang.Math.abs(poli)).charAt(x);
}
To store something of an unknown length... then you can still use an int/double array, just gets slightly more complicated.
public static void main(String[] args)
{
// Say the size is given in a command line argument.
int coefficientNumber = Integer.parseInt(args[0]);
int[] poly = new int[coefficientNumber];
for (int i = 0; i < poly.length; i++)
{
poly[i] = 0;
}
// Set the highest coeffient to 1 (if there is 3 coefficients, this is coefficient
// of x^2, if 4 coefficients, this is coefficient of x^3
poly[0] = 1;
printPoly(poly);
}
// To print a polynomial of unknown length.
// If the coefficient is 0, don't print it.
private static void printPoly(int[] poly)
{
String output = "";
for (int index = 0; index < poly.length; index++)
{
if (poly[index] != 0)
{
// If this is the first coefficient with a value
if (output.length() == 0)
output = poly[index] + "x^" + (poly.length - (index + 1));
// Else if there is already some coefficient with values printed.
else
output += " + " + "x^" + (poly.length - (index + 1));
} // if
} // for
System.out.println(output);
} // printPoly
First of all, storing a polynomial in one variable isn't a great idea as if you have coefficients of more than 9 you'll get confused.
A better method imo (without making a polynomial class) is to store the polynomial in an int/double array.
public static void main(String[] args)
{
// To store the polynomial x^2, you could do the following:
int[] poly = new int[3];
poly[0] = 1;
poly[1] = 0;
poly[2] = 0;
printPoly(poly);
}
// To print it:
private static void printPoly(int[] poly)
{
String output = "";
if (poly[0] != 0)
output += poly[0] + "x^2"
if (poly[1] != 0)
{
if (output.size() > 0)
output += " + " + poly[1] + "^x";
else
output += poly[1] + "x";
}
if (poly[2] != 0)
{
if (output.size() > 0)
output += " + " + poly[2];
else
output += poly[2];
}
}
Hi im trying to count in a if/else but it wont work.
Every time you try to find the number 32 it should count +1. This is not working for me..
So... i want it to count all the try's ive done to find number 32 so it shows me how much times i tried it.
Can anyone help me out?
String antwoord = null;
int getal = 32;
int count = 0;
if((Integer.parseInt(txt_input.getText().toString()) ) == getal)
{
antwoord = "Goed! in: " + count + " keer";
}
else if((Integer.parseInt(txt_input.getText().toString()) ) <= getal)
{
antwoord = "Hoger... ";
count++;
}
else if((Integer.parseInt(txt_input.getText().toString()) ) >= getal)
{
antwoord = "Lager... ";
count++;
}
count++;
lbl_hoger_lager.setText(antwoord);
You are mixing your logic in the if(condition).
It should be
if(number is equal){
// some operation
}
else if(number is greater){
// some operation
}
else if(number is lesser than X ){
// some operation
}
Hope this helps.
I think you wanted to do this :
String antwoord = null;
int getal = 32;
int count = 0;
if ((Integer.parseInt(txt_input.getText().toString())) == getal) {
count++;
antwoord = "Goed! in: " + count + " keer";
} else if ((Integer.parseInt(txt_input.getText().toString())) < getal) {
antwoord = "Hoger... ";
// count++;
}
else if ((Integer.parseInt(txt_input.getText().toString())) > getal) {
antwoord = "Lager... ";
// count++;
}
lbl_hoger_lager.setText(antwoord);
Some tipps:
You should avoid long strings of common code. For example (Integer.parseInt(txt_input.getText().toString()) ) appears three times. It's a long, complicated expression. How about evaluating this only once and storing the result in a local variable?
int userInput = (Integer.parseInt(txt_input.getText().toString()) );
(and the .toString() is probably not necessary, too)
If you want to count always, count outside of the if.
count is a local variable. It will be 0 every time the code is executed. If you want to preserve the value from previous attempts, you must use a field.
I just now tested with your code and it looks ok to me , obviously with some changes :
public class A
{
public static void main(String [] args)
{
String antwoord = null;
int getal = 32;
int count = 0;
String k ="32";
if((Integer.parseInt(k) ) == getal)
{
antwoord = "Goed! in: " + (count+1) + " keer";
}
else if((Integer.parseInt(k) ) <= getal)
{
antwoord = "Hoger... ";
count++;
}
else if((Integer.parseInt(k) ) >= getal)
{
antwoord = "Lager... ";
count++;
}
// count++;
System.out.println(antwoord);
//lbl_hoger_lager.setText(antwoord);
}
}
Output is :
Goed! in: 1 keer
The way you have it set up, count will increment if the input is greater than or less than 32.
This should work for you:
String antwoord = null;
int getal = 32;
int count = 0;
if((Integer.parseInt(txt_input.getText().toString()) ) == getal)
{
antwoord = "Goed! in: " + count + " keer";
}
else if((Integer.parseInt(txt_input.getText().toString()) ) < getal)
{
antwoord = "Hoger... ";
count++;
}
else if((Integer.parseInt(txt_input.getText().toString()) ) > getal)
{
antwoord = "Lager... ";
count++;
}
lbl_hoger_lager.setText(antwoord);