{"record":{"id":"bd20db8af5730c7a","repo":"donnemartin/interactive-coding-challenges","slug":"num-cannot-be-0-or-negative","errorCode":null,"errorMessage":"num cannot be 0 or negative","messagePattern":"num cannot be 0 or negative","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/get_next/get_next_solution.ipynb","lineNumber":120,"sourceCode":"   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Code\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"class Bits(object):\\n\",\n    \"\\n\",\n    \"    def get_next_largest(self, num):\\n\",\n    \"        if num is None:\\n\",\n    \"            raise TypeError('num cannot be None')\\n\",\n    \"        if num <= 0:\\n\",\n    \"            raise ValueError('num cannot be 0 or negative')\\n\",\n    \"        num_ones = 0\\n\",\n    \"        num_zeroes = 0\\n\",\n    \"        num_copy = num\\n\",\n    \"        # We'll look for index, which is the right-most non-trailing zero\\n\",\n    \"        # Count number of zeroes to the right of index\\n\",\n    \"        while num_copy != 0 and num_copy & 1 == 0:\\n\",\n    \"            num_zeroes += 1\\n\",\n    \"            num_copy >>= 1\\n\",\n    \"        # Count number of ones to the right of index\\n\",\n    \"        while num_copy != 0 and num_copy & 1 == 1:\\n\",\n    \"            num_ones += 1\\n\",\n    \"            num_copy >>= 1\\n\",\n    \"        # Determine index and set the bit\\n\",\n    \"        index = num_zeroes + num_ones\\n\",\n    \"        num |= 1 << index\\n\",\n    \"        # Clear all bits to the right of index\\n\",\n    \"        num &= ~((1 << index) - 1)\\n\",\n    \"        # Set bits starting from 0\\n\",","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/bit_manipulation/get_next/get_next_solution.ipynb#L102-L138","documentation":"Bits.get_next_largest raises ValueError('num cannot be 0 or negative') for num <= 0. The algorithm rearranges set bits of a positive integer to find the next larger number with the same popcount; zero and negatives have no meaningful 'next largest' under that definition.","triggerScenarios":"get_next_largest(0), get_next_largest(-5), or calling with a computed difference/subtraction result that came out zero or negative.","commonSituations":"Delta computations, array indices that underflow, or unvalidated numeric input from users/config flowing into the call.","solutions":["Guard with if num <= 0 before calling and handle that case separately","Fix the upstream arithmetic that produced a non-positive value","If 0 input is legitimate in your domain, special-case it (answer would be 1-bit numbers)"],"exampleFix":"# before\nnxt = bits.get_next_largest(count - used)\n\n# after\nbase = count - used\nif base <= 0:\n    base = 1\nnxt = bits.get_next_largest(base)","handlingStrategy":"validation","validationCode":"if num is None or num <= 0:\n    raise ValueError('num must be a positive integer')\nbits.get_next_largest(num)","typeGuard":"def is_positive_int(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v > 0","tryCatchPattern":"try:\n    nxt = bits.get_next_largest(num)\nexcept ValueError as e:\n    if '0 or negative' in str(e):\n        nxt = 1  # smallest 1-bit number\n    else:\n        raise","preventionTips":["Check num > 0 before next-largest computations","Treat non-positive deltas as logic errors to fix upstream","Validate numeric ranges at input boundaries"],"tags":["python","value-error","bit-manipulation","non-positive-input"],"backgroundTag":"invalid-argument-value","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}