Results 1 to 13 of 13

Thread: Java Recursion Help

  1. #1
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default Java Recursion Help

    -Done
    Last edited by [XoL]; 11-17-2013 at 09:10 PM.




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  2. #2
    Join Date
    Mar 2012
    Location
    127.0.0.1
    Posts
    3,383
    Mentioned
    95 Post(s)
    Quoted
    717 Post(s)

    Default

    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;
        }
    }

  3. #3
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default

    EDIT...derp, I posted the first question read your response and realized it was for the first question lol my bad finished that one >.<
    Last edited by [XoL]; 11-17-2013 at 08:35 PM.




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  4. #4
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default

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




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  5. #5
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default

    Quote Originally Posted by Sin View Post
    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.




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  6. #6
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    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:
    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];
      }
    }

    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
      }
    }
    Last edited by masterBB; 11-17-2013 at 08:55 PM.
    Working on: Tithe Farmer

  7. #7
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default

    Quote Originally Posted by masterBB View Post
    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:
    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];
      }
    }

    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
      }
    }
    Lol I am impressed thank you!




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  8. #8
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by [XoL] View Post
    Lol I am impressed thank you!
    Does it work?

    I might rewrite your rearrange in a bit. But I am having second diner first.
    Working on: Tithe Farmer

  9. #9
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default

    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!




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  10. #10
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by [XoL] View Post
    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 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:
    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);
      }
    }

    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:
    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();
      }
    }

    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
      }
    }
    Last edited by masterBB; 11-17-2013 at 09:42 PM.
    Working on: Tithe Farmer

  11. #11
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default

    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!




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  12. #12
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

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

    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

    java Code:
    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);
        }
    Last edited by masterBB; 11-18-2013 at 12:15 AM.
    Working on: Tithe Farmer

  13. #13
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default

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




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •