{"record":{"id":"512df82e1f2e8c42","repo":"TheAlgorithms/Python","slug":"invalid-input-needed-sum-must-be-between-1-and-100","errorCode":null,"errorMessage":"Invalid input\nneeded_sum must be between 1 and 1000, power between 2 and 10.","messagePattern":"Invalid input\nneeded_sum must be between 1 and 1000, power between 2 and 10\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"backtracking/power_sum.py","lineNumber":80,"sourceCode":"    >>> solve(20, 2)\n    1\n    >>> solve(15, 10)\n    0\n    >>> solve(16, 2)\n    1\n    >>> solve(20, 1)\n    Traceback (most recent call last):\n        ...\n    ValueError: Invalid input\n    needed_sum must be between 1 and 1000, power between 2 and 10.\n    >>> solve(-10, 5)\n    Traceback (most recent call last):\n        ...\n    ValueError: Invalid input\n    needed_sum must be between 1 and 1000, power between 2 and 10.\n    \"\"\"\n    if not (1 <= needed_sum <= 1000 and 2 <= power <= 10):\n        raise ValueError(\n            \"Invalid input\\n\"\n            \"needed_sum must be between 1 and 1000, power between 2 and 10.\"\n        )\n\n    return backtrack(needed_sum, power, 1, 0, 0)[1]  # Return the solutions_count\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":62,"sourceCodeEnd":92,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/backtracking/power_sum.py#L62-L92","documentation":"Raised by center_of_mass() in physics/center_of_mass.py when the particles list is empty. The center of mass R = sum(m_i*r_i)/sum(m_i) is undefined with no particles (0/0), so the function refuses empty input before dividing. The check is 'if not particles', so any empty sequence (list, tuple) triggers it.","triggerScenarios":"center_of_mass([]); center_of_mass(particles[:0]); passing a list produced by a filter comprehension that matched nothing, e.g. [p for p in particles if p.mass > 100] on data with no such particles.","commonSituations":"Batch processing of simulation frames where some frames contain zero particles after filtering/culling; loading a dataset whose parser returned an empty list on a malformed file.","solutions":["Skip or special-case empty particle sets before calling: if not particles: continue / return None.","Check the upstream filter/loader that produced the list to see why it is empty.","Catch ValueError at the batch level to skip bad frames without aborting the run."],"exampleFix":"# before\ncom = center_of_mass([p for p in frame if p.mass > 100])  # may be []\n\n# after\nsel = [p for p in frame if p.mass > 100]\ncom = center_of_mass(sel) if sel else None","handlingStrategy":"validation","validationCode":"if not particles:\n    return None  # or skip frame\ncom = center_of_mass(particles)","typeGuard":"def is_nonempty_particle_list(ps: object) -> bool:\n    return isinstance(ps, list) and len(ps) > 0 and all(hasattr(p, 'mass') for p in ps)","tryCatchPattern":"try:\n    com = center_of_mass(frame_particles)\nexcept ValueError:\n    continue  # skip empty frames in batch processing","preventionTips":["Guard filtered comprehensions that can return [].","Treat empty particle sets as a skip, not an error, in frame loops.","Validate loader output: an empty list often means a parse failure upstream."],"tags":["physics","empty-collection","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}