SRL Archive - General

Java Recursion Help


[XoL]

2013-11-17 14:59:48

-Done

NKN

2013-11-17 15:22:40

Here's what I whipped up for the first one.

It's really ugly, but I didn't have much time. Someone can probably do this in half the lines I did.

public class main{

public static void main(String[] args){
System.out.println(shortestInt(new int[]{9,4,1,7,0},0,0));
}


private static int shortestInt(int[] intArray, int lowestNumber, int count){
if(count == 0)
lowestNumber = intArray[count];
if(count < intArray.length){
if(intArray[count] < lowestNumber)
lowestNumber = intArray[count];
return shortestInt(intArray, lowestNumber, ++count);
}
return lowestNumber;
}
}

[XoL]

2013-11-17 15:27:56

EDIT…derp, I posted the first question read your response and realized it was for the first question lol my bad finished that one >.<

[XoL]

2013-11-17 15:35:13

Here is what I got for #1 and #2
So now all I need help with is #3 and #4

[XoL]

2013-11-17 15:44:33

From: Sin
Is recursion absolutely necessary? I can do it without it, and it'd be simpler :s


Yeah I know this would be so much easier without recursion but its a recursion assignment pretty sure shed be pissed if i did it another way.

masterBB

2013-11-17 15:49:32

I don't have a java compiler, nor did I wrote any java code in the last years, but this works fine with my internal compiler ;)

public static String reverseString(String s){
int l;
l = s.length();

if (l = 0) {
return '';
} else if (l = 1) {
return s[0];
} else {
return s[l-1] + reverseString(s.substring(1, l - 1)) + s[0];
}
}

public static boolean isPalindrome(String s){
int l;
l = s.length();

if (l <= 1) {
return True;
} else if(s[0] == s[l-1]){
return True && isPalindrome(s.substring(1, l - 1));
} else {
return False; //Not sure if needed in Java
}
}

[XoL]

2013-11-17 16:09:54

From: masterBB
I don't have a java compiler, nor did I wrote any java code in the last years, but this works fine with my internal compiler ;)

public static String reverseString(String s){
int l;
l = s.length();

if (l = 0) {
return '';
} else if (l = 1) {
return s[0];
} else {
return s[l-1] + reverseString(s.substring(1, l - 1)) + s[0];
}
}

public static boolean isPalindrome(String s){
int l;
l = s.length();

if (l <= 1) {
return True;
} else if(s[0] == s[l-1]){
return True && isPalindrome(s.substring(1, l - 1));
} else {
return False; //Not sure if needed in Java
}
}


Lol I am impressed thank you!

masterBB

2013-11-17 16:16:28

From: [XoL]
Lol I am impressed thank you!


Does it work?

I might rewrite your rearrange in a bit. But I am having second diner first.

[XoL]

2013-11-17 16:22:31

so for q3, I get an invalid character constant on the ' '
and for q4, I am getting errors on else if(s[0] == s[l-1]){

s[0] is "Type of expression must be array type but it resolved to string"
and s[l-1] is "Type of expression must be array type but it resolved to string"

also sounds good thanks!

masterBB

2013-11-17 16:34:40

From: [XoL]
so for q3, I get an invalid character constant on the ' '
and for q4, I am getting errors on else if(s[0] == s[l-1]){

s[0] is "Type of expression must be array type but it resolved to string"
and s[l-1] is "Type of expression must be array type but it resolved to string"

also sounds good thanks!


This makes me all feel like being on school again :p I don't think java strings are made '' but instead with "". And I looked it up, java doesn't treat strings as arrays. You might want to try this:

public static String reverseString(String s){
int l;
l = s.length();

if (l = 0) {
return "";
} else if (l = 1) {
return s.charAt(0);
} else {
return s.charAt(l - 1) + reverseString(s.substring(1, l - 1)) + s.charAt(0);
}
}

public static boolean isPalindrome(String s){
int l;
l = s.length();

if (l <= 1) {
return True;
} else if(s.charAt(0) == s.charAt(l - 1)){
return True && isPalindrome(s.substring(1, l - 1));
} else {
return False; //Not sure if needed in Java
}
}

e: Now I think about it, if java doesn't treat strings as arrays of char you might need to use StringBuilder

public static String reverseString(String s){
int l;
l = s.length();

if (l = 0) {
return "";
} else if (l = 1) {
return String.valueOf(s.charAt(0));
} else {
return new StringBuilder(s.charAt(l - 1)).append(reverseString(s.substring(1, l - 1))).append(s.charAt(0)).toString();
}
}

public static boolean isPalindrome(String s){
int l;
l = s.length();

if (l <= 1) {
return True;
} else if(s.charAt(0) == s.charAt(l - 1)){
return True && isPalindrome(s.substring(1, l - 1));
} else {
return False; //Not sure if needed in Java
}
}

[XoL]

2013-11-17 16:48:14

Yeah lol I figured when I took a look at the code it might just be a small syntax error not actual logic problems! Fixed them thank you very much!

masterBB

2013-11-17 16:50:51

ah, fixed them too at http://www.compileonline.com/compile_java_online.php :p

public class HelloWorld{

public static void main(String []args){
System.out.println(reverseString("Hello World"));
System.out.println(isPalindrome("OLABLO"));
System.out.println(isPalindrome("OLAALO"));
}

public static String reverseString(String s){
int l;
String b;
l = s.length();

if (l == 0) {
return "";
} else if (l == 1) {
return String.valueOf(s.charAt(0));
} else {
return s.charAt(l - 1) + reverseString(s.substring(1, l - 1)) + s.charAt(0);
}
}

public static boolean isPalindrome(String s){
int l;
l = s.length();

if (l <= 1) {
return true;
} else if(s.charAt(0) == s.charAt(l - 1)){
return true && isPalindrome(s.substring(1, l - 1));
} else {
return false; //Not sure if needed in Java
}
}
}

e: with import java.util.Arrays

public static String reverseString(String s){
return (s.length() == 0) ? "" : ((s.length() == 1) ? String.valueOf(s.charAt(0)) : s.charAt(s.length()-1) + reverseString(s.substring(1, s.length()-1)) + s.charAt(0));
}

public static boolean isPalindrome(String s){
return (s.length() <= 1) ? true : ((s.charAt(0) == s.charAt(s.length()-1)) ? true && isPalindrome(s.substring(1, s.length()-1)) : false);
}

public static void arrange(int[] a){
int l = a.length;

if(l <= 1){ return; }

int[] b = Arrays.copyOfRange(a, 1, l);
arrange(b);

if((a[0] & 1) == 1){
a[l-1] = a[0];
System.arraycopy(b, 0, a, 0, l-1);

}else {
System.arraycopy(b, 0, a, 1, l-1);
}
}

e: shorter

public static void arrange(int[] a){
if(a.length <= 1){ return; }

int[] b = Arrays.copyOfRange(a, 1, a.length);
arrange(b);

a[a.length-1] = a[0];
System.arraycopy(b, 0, a, 1 - (a[0] & 1), a.length-1);
}

[XoL]

2013-11-17 21:20:16

Very impressive lol, mine looks sloppy cause its long so I may swap it out with yours!