Rather than simply list all of the commonly useful PDB commands, we're going to instead debug a simple function. Our function – is_palindrome()
– takes in an integer and determines if the digits of the integer are a palindrome or not. A palindrome is a sequence which is the same both forwards and backwards.
The first thing we'll do is create a new file, palindrome.py
, with this code:
import unittest def digits(x): """Convert an integer into a list of digits. Args: x: The number whose digits we want. Returns: A list of the digits, in order, of ``x``. >>> digits(4586378) [4, 5, 8, 6, 3, 7, 8] """ digs = [] while x != 0: div, mod = divmod(x, 10) digs.append(mod) x = mod return digs def is_palindrome(x): """Determine if an integer is a palindrome. Args: x: The number to check for palindromicity. Returns: True if the digits of ``x`` are a palindrome, False otherwise....