Write the recursive version of the function decimal which takes in n, a number, and returns a list representing the decimal representation of the number.
def decimal(n): """Return a list representing the decimal representation of a number. >>> decimal(55055) [5, 5, 0, 5, 5] >>> decimal(-136) ['-', 1, 3, 6] """
def decimal(n): """Return a list representing the decimal representation of a number. >>> decimal(55055) [5, 5, 0, 5, 5] >>> decimal(-136) ['-', 1, 3, 6] """ if n < 0: return ['-'] + decimal(-n) elif n < 10: return [n] else: return decimal(n // 10) + [n % 10] # Testing the function here. ignore/remove the code below if not required if __name__ == '__main__': print(decimal(55055)) print(decimal(-136))
Get Answers For Free
Most questions answered within 1 hours.