[Tutor] Pythonify this code!
Dave Angel
davea at ieee.org
Tue Jul 14 12:54:30 CEST 2009
A.T.Hofkamp wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">Muhammad
> Ali wrote:
>> def separateToList(num):
>> """
>> changes an integer into a list with 0's padded to the left if the
>> number is in tens or units
>> """
>> assert(num <= 255)
>
>
>> s = str(num)
>> li = []
>> if len(s) > 2:
>> li = [s[0:1], s[1:2], s[2:3]]
>> elif len(s) > 1:
>> li = [0, s[0:1], s[1:2]]
>> elif len(s) > 0:
>> li = [0, 0, s[0:1]]
>>
>> return map(int, li)
>
> return [int(v) for v in ("00" + str(num))[-3:]]
>
>
> Albert
>
> </div>
>
Or
def separateToList(num):
"""
changes an integer 0-255 into a list of ints, size exactly 3
"""
return map(int, list(format(num, "03d")))
More information about the Tutor
mailing list