[Tutor] Pythonify this code!

A.T.Hofkamp a.t.hofkamp at tue.nl
Tue Jul 14 11:03:20 CEST 2009


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


More information about the Tutor mailing list