Alternative Tip/Trick

# Personal Agility - Thinking Outside Your Own Box

, 8 Apr 2010 CPOL
 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);

<pre>
// 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&lt;int&gt; list = new List&lt;int&gt;(digits);

for (int i = digits - 1; i &gt;= 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

 First Prev Next
 Last Visit: 31-Dec-99 19:00     Last Update: 1-Apr-15 9:31 Refresh 1