<jupyter_start><jupyter_text>Add Two NumbersYou are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.You may assume the two numbers do not contain any leading zero, except the number 0 itself.Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 0 -> 8See [The leetcode example problem](https://leetcode.com/problems/add-two-numbers/description/)<jupyter_code>debugging = False
#debugging = True
debug2 = False

logging = True

def dbg(f, *args):
    if debugging:
        print(('  DBG:' + f).format(*args))

def dbg_cont(f, *args):
    if debugging:
        print(('  DBG:' + f).format(*args), end='')

def dbg2(f, *args):
    if debug2:
        print((' -DBG2:' + f).format(*args))
        
def log(f, *args):
    if logging:
        print((f).format(*args))
        
def logError(f, *args):
    if logging:
        print(('*** ERROR:' + f).format(*args))
        
def className(instance):
    return type(instance).__name__<jupyter_output><empty_output><jupyter_text>The TestSet Mechanism<jupyter_code># %load TestHarness.py
class TestCase(object):
    def __init__(self, name, method, inputs, expected, catchExceptions=False):
        self.name = name
        self.method = method
        self.inputs = inputs
        self.expected = expected
        self.catchExceptions = catchExceptions
        
    def run(self):
        if self.catchExceptions:
            try:
                return self.method(*self.inputs)
            except Exception as x:
                return x
        else:
                return self.method(*self.inputs)

import time
from datetime import timedelta

class TestSet(object):
    def __init__(self, cases):
        self.cases = cases
    
    def run_tests(self):
        count = 0
        errors = 0
        total_time = 0
        for case in self.cases:
            count += 1
            start_time = time.time()
            result = case.run()
            elapsed_time = time.time() - start_time
            total_time += elapsed_time
            if callable(case.expected):
                if not case.expected(result):
                    errors += 1
                    logError("Test {0} failed. Returned {1}", case.name, result)
            elif result != case.expected:
                errors += 1
                logError('Test {0} failed. Returned "{1}", expected "{2}"', case.name, result, case.expected)
        if errors:
            logError("Tests passed: {0}; Failures: {1}", count-errors, errors)
        else:
            log("All {0} tests passed.", count)
        log("Elapsed test time: {0}", timedelta(seconds=total_time))
        
class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: list
        :type l2: list
        :rtype: list
        """
        
        def toInt(n):
            v = 0
            scale = 1
            for d in n:
                v += int(d) * scale
                scale *= 10
            return v

        def toList(v):
            n = []
            if v == 0:
                n = [0]
            else:
                while v > 0:
                    d = v % 10
                    v //= 10
                    n.append(d)
            return n
        
        return toList(toInt(l2) + toInt(l1))
        
class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        
        def toInt(node):
            v = 0
            scale = 1
            while node:
                d = node.val
                v += int(d) * scale
                scale *= 10
                node = node.next
            return v

        def toList(v):
            if v < 10:
                return ListNode(v)
            else:
                startNode = ListNode(v % 10)
                node = startNode
                while v >= 10:
                    v //= 10
                    d = v % 10
                    node.next = ListNode(d) 
                    node = node.next
            return startNode
        
        return toList(toInt(l2) + toInt(l1))
# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None
        
    def __iter__(self):
        node = self
        while node:
            yield node
            node = node.next
            
    def __str__(self):
        #text = '[ '
        #node = self
        #while node.next:
        #    text += str(node.val) + ', '
        #    node = node.next
        #text += str(node.val) + ' ]'
        text = str([x.val for x in self])
        return text
            
    def append(self, v):
        node = self
        while node.next is not None:
            node = node.next
        node.next = ListNode(v)
        
    @staticmethod
    def toList(v):
        node = ListNode(v % 10)
        v //= 10
        while v > 0:
            d = v % 10
            v //= 10
            node.append(d)
        return node

zz = ListNode.toList(123445)
[x.val for x in zz]
str(zz)<jupyter_output><empty_output><jupyter_text>The Unit Under Test<jupyter_code>class Solution(object):
    def myAtoi(self, str):
        """
        :type str: str
        :rtype: int
        """    
        MAXINT = 2147483647
        MININT = -2147483648
        neg = False
        n = len(str)
        v = 0
        i = 0
        while i<n and str[i] == ' ': i+=1
        if i<n and (str[i] == '+' or str[i] == '-'):
            neg = str[i] == '-'
            i += 1
        while i<n and str[i] >= '0' and str[i] <= '9':
            d = int(str[i])
            v = v * 10 + d
            i += 1
        v = -v if neg else v
        if v > MAXINT: return MAXINT
        if v < MININT: return MININT
        return v<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1120834735779199, 'fraction_numerical': 0.018512285425782565, 'mean_word_length': 1.4148720032507112, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 24, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '327548', 'license': 'Unlicense', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1749, 'n_words': 518, 'path': 'AddTwoNumbers.ipynb', 'repo_name': 'jfogarty/python-code-practice', 'size': '5942'}