-Done
Printable View
-Done
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.
Code: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;
}
}
EDIT...derp, I posted the first question read your response and realized it was for the first question lol my bad finished that one >.<
Here is what I got for #1 and #2
So now all I need help with is #3 and #4
http://i.imgur.com/Yrvcz2P.png?1
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 ;)
java Code:
java Code: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
}
}
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:
java Code:
java Code: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
java Code:
java Code: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
}
}
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!
ah, fixed them too at http://www.compileonline.com/compile_java_online.php :p
java Code: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
java Code: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
Very impressive lol, mine looks sloppy cause its long so I may swap it out with yours!