11,641,971 members (64,530 online)
Alternative Tip/Trick

# Personal Agility - Thinking Outside Your Own Box

, 8 Apr 2010 CPOL 1.7K 3
 Rate this:
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...

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?

## Share

 United States
No Biography provided

## You may also be interested in...

 First Prev Next
 Reason for my vote of 1 There was no question in this tip th... Neil Searle7-Sep-10 23:24 Neil Searle 7-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.
 Last Visit: 31-Dec-99 18:00     Last Update: 2-Aug-15 1:41 Refresh 1