Odd/curious behaviour in a recursive function - java

I am trying to program a function checking whether or not a string is the result of set of actions on previous string. Specifically, a string 'b' is a transformation of string 'a' if it follows the same order of characters, has only the same characters, yet may have them multiplied. For example:
"aabba" is a transformation of "aba" however not of "abaa", as that has double 'a' at the end.
The code is as follows:
public static boolean isTrans(String s, String t)
{
if(s.equals(t)){return true;} //obviously the latter is transformation
else if(s.length()==0||t.length()==0){return false;} //either is empty, can't be transformation
else if(s.charAt(0)!=t.charAt(0)){return false;} //obviously not transformation
else {return (s.length()==(isTrans(s,0,t,1)));} //recursive method below, note s.charAt(0)==t.charAt(0).
}
private static int isTrans(String s, int i, String t, int j)
{
if(i < s.length() && j < t.length()) //only as long as the indexes are within right bound
{
if(s.charAt(i) == t.charAt(j)) //character at 'transformed' string is the character at original string
{
if((j+1)<t.length()) //only as long as there are more characters
{
j++;
isTrans(s,i,t,j); //check next character at transformed string
}
}
else if((i+1)<s.length()) //ensures there is a next character at string s
{
if(s.charAt(i+1) == t.charAt(j)) //character is the next chracter at original string
{
i++;
if((j+1)<t.length()) //only as long as there are more characters
{
j++;
isTrans(s,i,t,j); //checks next characters at strings
}
}
else{i=-1;} //not transformation
}
else{i=-1;} //not transformation
}
return (i+1);
}
The program doesn't work as intended. Now here is the curious thing: running it through the debugger, it does everything as intended, however, when it reaches the "return (i+1)" command, instead of actually returning it, it starts to execute the recursive calls for some reason, meanwhile decreasing the value of i, until it reaches 0, and only then returning it, causing false negatives. More specifically, it goes up the stack and 'executes' the recursive calls of isTrans(s,i,t,j).
I would like to know why it does that, even more than a way to solve such a problem. It doesn't even enters through the iff's, but immediately enters the recursive calls, reducing the value of i all the way to 0 and only then returning it.
Appreciate any comments!
Edit: to clarify what exactly it does, according to the debugger. If I try to see if "aabba" is a transformation of "aba" (it is under the definitions above), the program reaches the desired value for i - 2. However, it then reaches the command return (i+1), and suddenly returns to line 17 in the given code, then the next recursive call in the code, and back to it - all the meanwhile reducing the value of i back to 0. Only then it executes the command outside of the function.
Edit 2: After a bit of tweaking and playing with the code, it seems that without connection to the return statement, at the end the function 'jumps' backwards, skipping through the 'if's, to the recursive calls - does not execute them, yet reduces i to 0 and only then continues. Does it have something to do with me calling isTrans(s,0,t,1)?

Your exit condition is return i+1. This will never work, even if your logic did, because the result you are returning is always one more than the length of the String.
Here is what likely happens in your code:
First if condition is satisfied
j is incremeanted.
Recursive call.
No more if conditions are satisfied, because i and j no longer point to the same letter.
It returns i+1
To do this recursively you would probably do something like this:
public class Main {
public static void main(String args[])
{
String s = "aba";
String t = "abz";
System.out.println(isTrans(0, 0, s, t));
}
public static boolean isTrans(int si, int ti,String s ,String t)
{
// we have run out of source characters, or the source character has passed the transformation character. This is not a transformation.
if (si > ti || si == s.length())
{
return false;
}
// we are at the end of the transformation string. This might be a transformation.
if(ti == t.length())
{
return si == s.length()-1; // If we're at the end of our source string, otherwise it isn't.
}
// if the current positions contain identical characters...
if (checkChar(si, ti, s, t))
{
// advance the transformation index.
ti++;
}
else
{
// otherwise, advance the source index.
si++;
}
// recursion.
return isTrans(si, ti, s, t);
}
private static boolean checkChar(int si, int ti, String s, String t)
{
return (s.charAt(si) == t.charAt(ti));
}
}
Hope you found it helpful.

Related

Does the program stop excuting once it returns something?

public int max1020(int a, int b) {
// First make it so the bigger value is in a
if (b > a) {
int temp = a;
a = b;
b = temp;
}
// Knowing a is bigger, just check a first
if (a >= 10 && a <= 20) return a;
if (b >= 10 && b <= 20) return b;
return 0;
}
so if both a and b are within the range [10,20], would it just return a, and stop excuting the next two lines of code?
so if both a and b are within the range [10,20], would it just return a, and stop executing the next two lines of code?
It first checks a, if it matches the first condition then the function is returned with the value of a. The next two lines of code will not be executed as the function has returned.
Does the program stop executing once it returns something?
No, the value is not returned by the whole program it is just returned by the particular function. Let me give you an example.
public class Main {
public static void main(String[] args) {
int result;
// The method max1020 returns the value of 'a' here
result = max1020(11,14);
System.out.println(result);
// The method max1020 returns the value of 'b' here
result = max1020(31,11);
System.out.println(result);
// The method max1020 returns the value of '0' here
result = max1020(50,60);
System.out.println(result);
}
private static int max1020(int a, int b) {
// First make it so the bigger value is in a
if (b > a) {
int temp = a;
a = b;
b = temp;
}
// Knowing a is bigger, just check a first
if (a >= 10 && a <= 20) return a;
if (b >= 10 && b <= 20) return b;
return 0;
}
}
The return keyword just gets you out of the function you are in, it does not get you out of your whole program. When you call the max1020() for the first time it returns a. See it returns the value of a but the program will still execute the other lines. It will print the value of the variable result then it'll again call the max1020() on another pair of values.
If both a and b are within the range [10,20] it will always return a.
You are correct, once a return statement is reached, it will not execute any further code.
You are correct in the assumption that the program checks if a is withing the range, and given that the result of the evaluation is true it will return the value bond to variable a.
In Java the keyword return means that the program will return the value associated with it and stops further execution.
As for your question about the void method. In Java and many other languages you have something called "state" which means that there exists some values outside of the current scope of execution, e.g in pseudocode
int a = 0
public void inc (){
a++
}
Method inc does not take any arguments (it has an arity of 0) but increments the variable a that is defined outside the scope of the function it self. Any method that has other return type than void returns the defined type.
I hope that my answer was helpful.
You seem to be mixing method return and program exit (which can also be called "program return code").
In a method, whenever the line return is reached, the method stops right there and will not execute further instructions.
When that method is the main method, it also is the entry-point of the program. Therefore, when the main calls return, the program will effectively stop, returning code 0 (which means "success").
In Java, you can also stop a program execution by calling System.exit(ret). In that particular case, the JVM process (which is "your program") will stop executing further instructions, even if return is not explicitly called (though some IDE will give you errors or warnings). The return code (seen by the caller process) will be the one you specified with ret.

How does loop in recursion work?

I'm learning recursion now, and I thought I quite understood how recursion works, and then I saw this code, and my head is about to explode.
I know this simple recursion works like
public void recursivePrint(int number){
if(number == 0{
return;
}
System.out.println(number + " ");
recursivePrint(number - 1);
}
If the parameter "number"'s value is 2.
public void recursivePrint(2){
if(number == 0{
return;
}
System.out.print(2 + " ");
recursivePrint(2 - 1);
}
public void recursivePrint(1){
if(number == 0{
return;
}
System.out.print(1 + " ");
recursivePrint(1 - 1);
}
and then stops because it meets its base case.
What about this print all permutations of a string function?
private void permute(String str, int l, int r)
{
if (l == r)
System.out.println(str);
else
{
for (int i = l; i <= r; i++)
{
str = swap(str,l,i);
permute(str, l+1, r);
str = swap(str,l,i);
}
}
}
There is a recursive call inside a for loop. If the input value is "ab", how does this recursion function work? Can you explain as I wrote above?
I got this code form geeksforgeeks, and there's a video for this, but I can't understand this since I don't know how loop works in recursion.
Using permute function you are generating strings where lth char is being replaced by one of the char following it. With the for loop inside it, you are touching onto each of those following characters one at a time.
With several call to permute, you are able to advance till the end position of the string, and that end is checked by if (l == r)
Take the case of abc.
abc
/ | \
Level 1 a(bc) b(ac) c(ba) (Here three new call to permute are made out of permute with l=1)
Goes on...
FYI, permutation isn't that simple to understand if you are new to recursion or programming. For easy understanding use pen-paper.
Recursion occurs when a method calls itself. Such a method is called recursive. A recursive method may be more concise than an equivalent non-recursive approach. However, for deep recursion, sometimes an iterative solution can consume less of a thread's finite stack space.
What is recursion:
In general, recursion is when a function invokes itself, either directly or indirectly. For example:
// This method calls itself "infinitely"
public void useless() {
useless(); // method calls itself (directly)
}
Conditions for applying recursion to a problem:
There are two preconditions for using recursive functions to solving a specific problem:
There must be a base condition for the problem, which will be the endpoint for the recursion. When a
recursive function reaches the base condition, it makes no further (deeper) recursive calls.
Each level of recursion should be attempting a smaller problem. The recursive function thus divides the problem into smaller and smaller parts. Assuming that the problem is finite, this will ensure that the recursion terminates.
In Java there is a third precondition: it should not be necessary to recurse too deeply to solve the problem;
The following function calculates factorials using recursion. Notice how the method factorial calls itself within the function. Each time it calls itself, it reduces the parameter n by 1. When n reaches 1 (the base condition) the function will recurse no deeper.
public int factorial(int n) {
if (n <= 1) { // the base condition
return 1;
} else {
return n * factorial(n - 1);
}
}

Removing a substring from a string, repeatedly

Problem:
Remove the substring t from a string s, repeatedly and print the number of steps involved to do the same.
Explanation/Working:
For Example: t = ab, s = aabb. In the first step, we check if t is
contained within s. Here, t is contained in the middle i.e. a(ab)b.
So, we will remove it and the resultant will be ab and increment the
count value by 1. We again check if t is contained within s. Now, t is
equal to s i.e. (ab). So, we remove that from s and increment the
count. So, since t is no more contained in s, we stop and print the
count value, which is 2 in this case.
So, here's what I have tried:
Code 1:
static int maxMoves(String s, String t) {
int count = 0,i;
while(true)
{
if(s.contains(t))
{
i = s.indexOf(t);
s = s.substring(0,i) + s.substring(i + t.length());
}
else break;
++count;
}
return count;
}
I am just able to pass 9/14 test cases on Hackerrank, due to some reason (I am getting "Wrong Answer" for rest of the cases). After a while, I found out that there is something called replace() method in Java. So, I tried using that by replacing the if condition and came up with a second version of code.
Code 2:
static int maxMoves(String s, String t) {
int count = 0,i;
while(true)
{
if(s.contains(t))
s.replace(t,""); //Marked Statement
else break;
++count;
}
return count;
}
But for some reason (I don't know why), the "Marked Statement" in the above code gets executed infinitely (this I noticed when I replaced the "Marked Statement" with System.out.println(s.replace(t,""));). I don't the reason for the same.
Since, I am passing only 9/14 test cases, there must be some logical error that is leading to a "Wrong Answer". How do I overcome that if I use Code 1? And if I use Code 2, how do I avoid infinite execution of the "Marked Statement"? Or is there anyone who would like to suggest me a Code 3?
Thank you in advance :)
Try saving the new (returned) string instead of ignoring it.
s = s.replace(t,"");
replace returns a new string; you seemed to think that it alters the given string in-place.
Try adding some simple parameter checks of the strings. The strings shouldn't be equal to null and they should have a length greater than 0 to allow for counts greater than 0.
static int maxMoves(String s, String t) {
int count = 0,i;
if(s == null || s.length() == 0 || t == null || t.length() == 0)
return 0;
while(true)
{
if(s.contains(t) && !s.equals(""))
s = s.replace(t,""); //Marked Statement
else break;
++count;
}
return count;
}
You might be missing on the edge cases in the code 1.
In code 2, you are not storing the new string formed after the replace function.
The replace function replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
Try this out:
public static int findCount(String s, String t){
if( null == s || "" == s || null == t || "" == t)
return 0;
int count =0;
while(true){
if(s.contains(t)){
count++;
int i = s.indexOf(t);
s = s.substring(0, i)+s.substring(i+t.length(), s.length());
// s = s.replace(t,"");
}
else
break;
}
return count;
}
String r1="ramraviraravivimravi";
String r2="ravi";
int count=0,i;
while(r1.contains(r2))
{
count++;
i=r1.indexOf(r2);
StringBuilder s1=new StringBuilder(r1);
s1.delete(i,i+r2.length());
System.out.println(s1.toString());
r1=s1.toString();
}
System.out.println(count);
First of all no logical difference in both the codes.
All the mentioned answers are to rectify the error of code 2 but none told how to pass all (14/14) cases.
Here I am mentioning a test case where your code will fail.
s = "abcabcabab";
t = "abcab"
Your answer 1
Expected answer 2
According to your code:
In 1st step, removig t from index 0 of s,
s will reduce to "cabab", so the count will be 1 only.
But actual answer should be 2
I first step, remove t from index 3 of s,
s will reduced to "abcab", count = 1.
In 2nd step removing t from index 0,
s will reduced to "", count = 2.
So answer would be 2.
If anyone know how to handle such cases, please let me know.

Recursive return error, Java

So I'm defining a recursive function that takes as an argument a value for x (like the arithmetic variable x, i.e. "x + 3 = 5") and returns the result of the arithmetic expression. The expression is taken from a Binary expression tree that looks like this:
You start at the root and keep working your way down till you hit the leaves, and once you do you come back up. The expression on the tree is then:
x * ( (x + 2) + cos(x-4) ).
My code for this function is as follows:
// Returns the value of the expression rooted at a given node
// when x has a certain value
double evaluate(double x) {
if (this.isLeaf()) {
//convert every instance of 'x' to the specified value
if (this.value.equals("x")) {
this.value = Double.toString(x);
}
//return the string-converted-to-double
return Double.parseDouble(this.value);
}
//if-else statements to work as the arithmetic operations from the tree. Checks the given node and performs the required operation
else {
if(this.value.equals("sin")) { return Math.sin(evaluate(Double.parseDouble(this.leftChild.value))); }
if(this.value.equals("cos")) { return Math.cos(evaluate(Double.parseDouble(this.leftChild.value))); }
if(this.value.equals("exp")) { return Math.pow(evaluate(Double.parseDouble(this.leftChild.value)), evaluate(Double.parseDouble(this.rightChild.value))); }
if(this.value.equals("*")) { return evaluate(Double.parseDouble(this.leftChild.value)) * evaluate(Double.parseDouble(this.rightChild.value)); }
if(this.value.equals("/")) { return evaluate(Double.parseDouble(this.leftChild.value)) / evaluate(Double.parseDouble(this.rightChild.value)); }
if(this.value.equals("+")) { return evaluate(Double.parseDouble(this.leftChild.value)) + evaluate(Double.parseDouble(this.rightChild.value)); }
if(this.value.equals("-")) { return evaluate(Double.parseDouble(this.leftChild.value)) - evaluate(Double.parseDouble(this.rightChild.value)); }
}
}
However the compiler is tossing an error telling me that my function must return a type double. Both the if and the else statements return a double- the if statement directly and the else statement through the sum of 2 doubles returned by the same function. What is the deal here? If I place a return statement outside of the if-else then the error resolves itself but to work with that would require me to keep a static or a global variable consistent through each recursion. I'd like to know what is wrong with my function as is, because it feels much more intuitive than a global variable and I think I'm missing a key concept about recursion here. Any help is appreciated- thank you!
Both the if and the else statements return a double
They actually don't. The if branch always does, but the else branch doesn't. What happens if this.value equals "Invalid", or something else which isn't in your list? Then it won't ever hit a return statement. Since it's required to always return, or throw an exception, this isn't allowed.
Even if you have your program structured in such a way that it logically always has to return a value, the compiler isn't going to be doing complex analysis on all the branches of your program to ensure that it always returns something. It just checks that each branch has a valid return.
So, for example, something like is invalid
if(x < 0) return -1;
if(x >= 0) return 1;
Because the compiler doesn't know that it always has to hit one of those two conditions (an issue which is further complicated by the fact that, depending on what x is, it might not always have to go down one of those branches).
Instead, your code should be structured like this:
if(x < 0) return -1;
else return 1;
So that every branch has a valid exit condition.

Recursive method to determine if a string is a hex number - Java

This is a homework question that I am having a bit of trouble with.
Write a recursive method that determines if a String is a hex number.
Write javadocs for your method.
A String is a hex number if each and every character is either
0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9
or a or A or b or B or c or C or d or D or e or E or f or f.
At the moment all I can see to test this is if the character at 0 of the string is one of these values he gave me then that part of it is a hex.
Any hints or suggestions to help me out?
This is what I have so far: `
public boolean isHex(String string){
if (string.charAt(0)==//what goes here?){
//call isHex again on s.substring(1)
}else return false;
}
`
If you're looking for a good hex digit method:
boolean isHexDigit(char c) {
return Character.isDigit(c) || (Character.toUpperCase(c) >= 'A' && Character.toUpperCase(c) <= 'F');
}
Hints or suggestions, as requested:
All recursive methods call themselves with a different input (well, hopefully a different input!)
All recursive methods have a stop condition.
Your method signature should look something like this
boolean isHexString(String s) {
// base case here - an if condition
// logic and recursion - a return value
}
Also, don't forget that hex strings can start with "0x". This might be (more) difficult to do, so I would get the regular function working first. If you tackle it later, try to keep in mind that 0xABCD0x123 shouldn't pass. :-)
About substring: Straight from the String class source:
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > count) {
throw new StringIndexOutOfBoundsException(endIndex);
}
if (beginIndex > endIndex) {
throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
}
return ((beginIndex == 0) && (endIndex == count)) ? this :
new String(offset + beginIndex, endIndex - beginIndex, value);
}
offset is a member variable of type int
value is a member variable of type char[]
and the constructor it calls is
String(int offset, int count, char value[]) {
this.value = value;
this.offset = offset;
this.count = count;
}
It's clearly an O(1) method, calling an O(1) constructor. It can do this because String is immutable. You can't change the value of a String, only create a new one. (Let's leave out things like reflection and sun.misc.unsafe since they are extraneous solutions!) Since it can't be changed, you also don't have to worry about some other Thread messing with it, so it's perfectly fine to pass around like the village bicycle.
Since this is homework, I only give some hints instead of code:
Write a method that always tests the first character of a String if it fulfills the requirements. If not, return false, if yes, call the method again with the same String, but the first character missing. If it is only 1 character left and it is also a hex character then return true.
Pseudocode:
public boolean isHex(String testString) {
If String has 0 characters -> return true;
Else
If first character is a hex character -> call isHex with the remaining characters
Else if the first character is not a hex character -> return false;
}
When solving problems recursively, you generally want to solve a small part (the 'base case'), and then recurse on the rest.
You've figured out the base case - checking if a single character is hex or not.
Now you need to 'recurse on the rest'.
Here's some pseudocode (Python-ish) for reversing a string - hopefully you will see how similar methods can be applied to your problem (indeed, all recursive problems)
def ReverseString(str):
# base case (simple)
if len(str) <= 1:
return str
# recurse on the rest...
return last_char(str) + ReverseString(all_but_last_char(str))
Sounds like you should recursively iterate the characters in string and return the boolean AND of whether or not the current character is in [0-9A-Fa-f] with the recursive call...
You have already received lots of useful answers. In case you want to train your recursive skills (and Java skills in general) a bit more I can recommend you to visit Coding Bat. You will find a lot of exercises together with automated tests.

Categories

Resources