{"record":{"id":"3eb15dcb2dadf2cc","repo":"donnemartin/interactive-coding-challenges","slug":"num-cannot-be-none-3eb15d","errorCode":null,"errorMessage":"num cannot be None","messagePattern":"num cannot be None","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/flip_bit/flip_bit_solution.ipynb","lineNumber":138,"sourceCode":"    \"    MAX_BITS = 32\\n\",\n    \"    \\n\",\n    \"    def _build_seen_list(self, num):\\n\",\n    \"        seen = []\\n\",\n    \"        looking_for = 0\\n\",\n    \"        count = 0\\n\",\n    \"        for _ in range(self.MAX_BITS):\\n\",\n    \"            if num & 1 != looking_for:\\n\",\n    \"                seen.append(count)\\n\",\n    \"                looking_for = not looking_for\\n\",\n    \"                count = 0\\n\",\n    \"            count += 1\\n\",\n    \"            num >>= 1\\n\",\n    \"        seen.append(count)\\n\",\n    \"        return seen\\n\",\n    \"    \\n\",\n    \"    def flip_bit(self, num):\\n\",\n    \"        if num is None:\\n\",\n    \"            raise TypeError('num cannot be None')\\n\",\n    \"        if num == -1:\\n\",\n    \"            return self.MAX_BITS\\n\",\n    \"        if num == 0:\\n\",\n    \"            return 1\\n\",\n    \"        seen = self._build_seen_list(num)\\n\",\n    \"        max_result = 0\\n\",\n    \"        looking_for = 0\\n\",\n    \"        for index, count in enumerate(seen):\\n\",\n    \"            result = 0\\n\",\n    \"            # Only look for zeroes\\n\",\n    \"            if looking_for == 1:\\n\",\n    \"                looking_for = not looking_for\\n\",\n    \"                continue\\n\",\n    \"            # First iteration, take trailing zeroes\\n\",\n    \"            # or trailing ones into account\\n\",\n    \"            if index == 0:\\n\",\n    \"                if count != 0:\\n\",\n    \"                    # Trailing zeroes\\n\",","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/bit_manipulation/flip_bit/flip_bit_solution.ipynb#L120-L156","documentation":"Bits.flip_bit raises TypeError when num is None. The method finds the longest run of 1s achievable by flipping one bit and immediately performs bitwise operations on num, so None is rejected up front.","triggerScenarios":"Bits().flip_bit(None), or flip_bit(num) where num is an optional parameter, a failed int() parse, or a missing dict/config value.","commonSituations":"Numeric input parsed from strings (int(x) guarded by try/except that left None), optional numeric fields, or test scaffolding that forgot to set the value.","solutions":["Ensure num is an int before calling; flip_bit(num or 0) when 0 is an acceptable default","Fix the upstream parse/lookup that produced None","Catch TypeError defensively if inputs are untrusted"],"exampleFix":"# before\nresult = bits.flip_bit(user_value)\n\n# after\nif user_value is None:\n    raise ValueError('user_value is required')\nresult = bits.flip_bit(user_value)","handlingStrategy":"validation","validationCode":"if num is None:\n    raise ValueError('num is required')\nBits().flip_bit(num)","typeGuard":"def is_int(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool)","tryCatchPattern":"try:\n    result = bits.flip_bit(num)\nexcept TypeError:\n    result = 1  # default for missing input","preventionTips":["Initialize numeric variables to 0","Return parsed ints with defaults: int(s or 0)","Check optional params before bit-twiddling APIs"],"tags":["python","bit-manipulation","type-error","none-check"],"backgroundTag":"none-argument-type-error","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}