Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    gusgol is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default Inverting integer

    Hi people,

    This is the problem:

    I need to put the last digit from an integer number to its front.
    Examples: 1234 --> 4123
    5678 --> 8567

    But is allowed only the use of integer variables in this transformation.

    Please, anybody can help me?

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    use a loop or two and convert it to a string. use modulus 10 (%10) to get the last number and then concat the remaining numbers to that.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    That modulus idea is pretty slick...
    Who Cares... As Long As It Works...

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Using only integer methods:

    1234 % 10 = 4
    1234 / 10 = 123
    10 ^ lg 1234 = 1000

    123 + 1000*4 = 4123

    There isn't an integer implementation of lg, but you can use (int)Math.log10()
    Last edited by OrangeDog; 04-17-2009 at 11:05 PM.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    That is even more slick...
    Who Cares... As Long As It Works...

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    It's called computer programming
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  7. #7
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    It's all computer programming... It's just that some are better than others...
    Who Cares... As Long As It Works...

  8. #8
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    How about this? cast your integers to strings, manipulate those strings and then cast them back to integers.
    Tshegofatso Manakana
    a.k.a Untouchable

  9. #9
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default

    hey mate, the previous suggestions made will work perfectly fine, however I think the most efficient way is to covert your integer to a char array and reverse the array, i.e.
    public static void main(String[] args) {
    Integer a = 1234;
    char[] b = a.toString().toCharArray();
    String temp="";
    for(int i = 0; i <b.length;i++)
    temp += b[b.length - 1 - i];
    System.out.println(temp);
    }

    Hope this helps,

    David

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  11. #11
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    David and Tshegofatso,
    As stated in the question, you're only allowed to use integer variables, and the task isn't to reverse the number, it's to move the last digit to the front.

    And 6 mathematical operations is always going to be more efficient than mass of object manipulation David suggests. You haven't even used a StringBuffer to avoid the cost of repeated string concatenation, nor have you arranged you loop to count down and eliminate unneeded index calculation. If you wanted to reverse a char array (which isn't even the question) you can do it in place using either a single temp char variable or using XORs to swap the values.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  12. #12
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Quote:
    Originally Posted by markw8500

    That is even more slick...
    Quote:
    Originally Posted by Eranga:

    Then what's your suggestion here?
    Eranga... I think you might have misunderstood Mark's comment. He is indicating that the suggestion by OrangeDog is a good one, neat, ingenious, etc.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  13. #13
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    Eranga,
    Chris is correct... At the time I posted that I was about to offer an answer that was long and cumbersome, but it worked... But right before I submitted it I checked to see that no one was doing the same thing I was offering... Then I saw OrangeDog's solution and was impressed... In my opinion, no one could create a better solution...
    Who Cares... As Long As It Works...

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by OrangeDog View Post
    David and Tshegofatso,
    As stated in the question, you're only allowed to use integer variables, and the task isn't to reverse the number, it's to move the last digit to the front.

    And 6 mathematical operations is always going to be more efficient than mass of object manipulation David suggests.
    Just my 2c worth...

    I thought when this was first posted that the question was ok as an exercise in using operators that a student may be unfamiliar with (%, eg), but that the restriction to integer variables was arbitrary and a bit silly. (basically the teacher's laziness in not coming up with a more robust question.)

    The operation of forming a new numeral from a given one by moving the last digit to the beginning is essentially a String - or CharacterSequence - operation. The operation can be performed alike on the decimal form of a integer, a phone "number", or even a person's name.

    Expressing data using a reasonable type is more important than efficiency, imho. And numerals are strings, not numbers.

  15. #15
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Being able to do this sort of thing when you have to program in restricted situations - either or all of memory, time and instruction limitations - is a valuable skill.
    Don't criticise interesting intellectual questions just because they don't match up to your practical experience.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  16. #16
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Two roads to Rome?

    If I'd been the teacher, I would had requested both integer and string implementations... just to show that there's more than one way to get the same result.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  17. #17
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    How do you know they didn't? Just because it didn't get posted here...
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by CJSLMAN View Post
    Eranga... I think you might have misunderstood Mark's comment. He is indicating that the suggestion by OrangeDog is a good one, neat, ingenious, etc.

    CJSL
    Yes Chris I got what he trying to say.... that he wants to say that' the best of best, slick.

    What I want to say is, if he has another explanation why don't he post it here, just commenting in that way. May be it's not fair to say that by me, but I don't like that. He's not the thread starter, but he's commenting twice, nothing else. I don't like that. ;)

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by markw8500 View Post
    Eranga,
    Chris is correct... At the time I posted that I was about to offer an answer that was long and cumbersome, but it worked... But right before I submitted it I checked to see that no one was doing the same thing I was offering... Then I saw OrangeDog's solution and was impressed... In my opinion, no one could create a better solution...
    Sorry if I make my comment is mess-up to you. What I want to say is, just commenting what's the best is not make sense. In your first three posts what you say only is, this is better than the other and so on... :rolleyes:

  20. #20
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default

    OrangeDog,

    I misread the question, hope I didn't step on any toes!, yes for an integer of logarithmic order n, the modulus operator coupled with the multiplication operator to yield 2(n-1) operations would be the most efficient approach.

    Just out of curiosity, consider string operations were permitted do you think this would hold for integers with orders > 1e10 or higher, would it not be more efficient to cast to a string and 'rearrange' the string and cast back to an int?

    Once again, I don't mean to offend - just curious,

    David

Page 1 of 2 12 LastLast

Similar Threads

  1. How to convert Integer[] to int[]
    By Nithya in forum New To Java
    Replies: 26
    Last Post: 02-11-2010, 05:41 PM
  2. Integer to String
    By zervine in forum Forum Lobby
    Replies: 3
    Last Post: 09-12-2008, 12:07 PM
  3. Integer length
    By jithan in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 03:35 PM
  4. How to get Integer from table
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-10-2008, 11:56 AM
  5. Integer vs int
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-14-2007, 09:13 PM

Posting Permissions

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