Back Forum Reply New

reverseDigits() ???

Post Last Edit by ePrasart at 19-3-2009 14:03

Post Last Edit by ePrasart at 19-3-2009 13:53

Hi,
I have a hard time with my data structure class.
I can't do the reverseDigits().

Here is the instruction:
Write a recursive method, reverseDigits, that takes an integer as a paramenter and return the number with the digits reverse.


Sample Run:
System.out.print(reverseDigits(734));   // would print 437

Any idea?
I appreciate the code in any languages, exept Assembly though. lol.
ePrasart.
1# ePrasart
I see. Since you study data structure, I provide quite a number of data structure solution. Here are my ideas. 1 i use recursive, 2 I use StringBuffer, 3 I use Iterative for loop, 4 I use stack.
To my opinion, use recursive to reverse String is not really good idea , however it can help us to improve our recursive knowledge.

Here you go :

/* Khmer IT Forum*/
   import java.util.*;
    public class reverse{
       public static void main(String [] args){
         reverse1("KhmerITForum");
         System.out.println();
         System.out.println(reverse2("KhmerITForum"));
         reverse3("KhmerITForum");
         System.out.println();
         reverse4("KhmerITForum");
      }
       public static void reverse1(String s){// recursive
         if(s.length()==1) {
            System.out.print(s);
            return;
         }
         reverse1(s.substring(1));
         System.out.print(s.charAt(0));
      }
       public static String reverse2(String s){//java doc        
         return new StringBuffer(s).reverse().toString();
      }
       public static void reverse3(String s){//iterative
         for(int i=s.length()-1;i>=0;i--) System.out.print(s.charAt(i));
      }
       public static void reverse4(String s){
         Stack st=new Stack();
         int i=0;
         while(i<s.length()){st.push(new Character(s.charAt(i))); i++;        }
         while(!st.isEmpty()) System.out.print(st.pop());  
      }
   }
that is cool solution
Thanks a lot psv_jupiter. Nice tool!
Well, if I'm not wrong, all of your reverse methods deal with string.
What I'm asking is the recursive method that takes an interger as a parameter and return a reverse integer number.
So, it's should be:
  1. public int reverseDigits(int n){
  2. // codes here
  3. // return here
  4. }
Copy Code
And if we call this method, e.g.
  1. System.out.print(reverseDigits(925));
Copy Code
It would print out 529. // Number, not the string.

Thanks again,
ePrasart.
Oh sorry sorry, didn't read your instruction carefully. Now I do.
I suggest you one recursive method :

    public class reverseInt{
       public static void main(String[] args){
         System.out.println(reverseDigit(1024));
             
      }
       public static int reverseDigit(int n){
         return reverseDigit(n,0);
      }
       public static int reverseDigit(int n, int result){
         if(n==0)   return result;
         result*=10;
         result+=n%10;
         return reverseDigit(n/10,result);
      }
Wow.
I've been spening 3 week already, but I could not find it.
You must be very good at programming.
Thanks for your help.
ePrasart.
By the way, can you just need one method to do this?
ePrasart.
Er... I can't find a better solution of using only one method beside this one.

       public static int reverse2(int n){// use one method
         if(n<10)    return n;
         int result=1, m=n;
         while(n>=10) {n=n/10;result*=10;} // this line is to count digit of n
         return result*(m%10)+reverse2(m/10); //result = 10^(digits of n)
      }

I think the shortest solution is :

public static int reverse3(int n){  return Integer.parseInt(new StringBuffer(Integer.toString(n)).reverse().toString());    }

You just need a line. However it is not recursive.

I came up with an Iterative also, not in instruction, just in case u interested :
public static int reverseDigit2(int n){ //non recursive
         int result=0;
         while(n>0){
            result*=10;          result+=n%10;   n=n/10;
         }
         return result;
      }


Hope it helps this time. No I'm not yet good programmers just started java this semester , But i plan to be good programmer. Nice to know you
Thanks a lot.
Me too.
ePrasart.
psv_jupiter:
I just get the solution from my prof.:
  1. class ReverseDigits
  2. {     
  3.    public static int reverseDigits(int n)
  4.    {
  5.       boolean positive = !(n < 0);
  6.       if (Math.abs(n) < 10)
  7.          return n;
  8.       else
  9.          if (positive)
  10.             return Math.abs(n)%10 * (int)Math.pow(10,(int)Math.floor(Math.log10(Math.abs(n)))) + reverseDigits(n/10);
  11.          else
  12.             return -1 * Math.abs(n)%10 * (int)Math.pow(10,(int)Math.floor(Math.log10(Math.abs(n)))) + reverseDigits(n/10);
  13.    }
  14. }
Copy Code
ePrasart.
I see.
I feel it is even complicated that mine , what do you think? . I will try to understand the logic behind it.
Anyway thank for sharing.
Yeah. It works, but it's a bit messy.
ePrasart.
Back Forum