Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Go to top

Personal Agility - Thinking Outside Your Own Box

, 8 Apr 2010
Rate this:
Please Sign up or sign in to vote.
Here's my answer:1. satisfies all five requirements2. does not require reversing the list (which, BTW, is technically another loop in the answer The Outlaw supplied).3. Also uses Math functions as the original requirements stated.static List digits(int value){ // get the...
Here's my answer:
 
1. satisfies all five requirements
2. does not require reversing the list (which, BTW, is technically another loop in the answer The Outlaw supplied).
3. Also uses Math functions as the original requirements stated.
 
static List<int> digits(int value)
{
    // get the sign of the value
    int sign = Math.Sign(value);
 
    // make the value positive
    value *= sign;
 
    // get the number of digits in the value
    int digits = (int)Math.Truncate(Math.Log10(value)) + 1;
 
    // reserve enough capacity for the digits
    List<int> list = new List<int>(digits);
 
    for (int i = digits - 1; i >= 0; --i)
    {
        // truncate to get the next value
        int nextValue = value / 10;
 
        // fill the list in reverse with the current digit on the end
        list[i] = value - nextValue * 10;
 
        value = nextValue;
    }
 
    // make the first digit the correct sign
    list[0] *= sign;
 
    return list;
}
 
==========
 
From JSOP: Once again, you've missed the point. The tip isn't as much about the code as it it is about being able to look at a problem from several perspectives. Did you even READ the last section of the tip?




From AHZ: Yes, JSOP, I did? And how is my example not thinking outside the box or from different perspectives?. Did you even READ my example?



License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ahmed zahmed

United States United States
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 1 There was no question in this tip th... PinmemberNeil Searle7-Sep-10 23:24 
Reason for my vote of 1
There was no question in this tip that required an answer. The appropriate place to post this would have been the original post quoted by the tip author in the C# forum.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 8 Apr 2010
Article Copyright 2010 by ahmed zahmed
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid