Skip to content

reject bool and float as polygon sides; cover negative-breadth cuboid#14790

Closed
HrachShah wants to merge 14 commits into
TheAlgorithms:masterfrom
HrachShah:fix/area-cuboid-and-reg-polygon-input-validation
Closed

reject bool and float as polygon sides; cover negative-breadth cuboid#14790
HrachShah wants to merge 14 commits into
TheAlgorithms:masterfrom
HrachShah:fix/area-cuboid-and-reg-polygon-input-validation

Conversation

@HrachShah

Copy link
Copy Markdown

Two real bugs in maths/area.py:

  1. area_reg_polygon()'s isinstance(sides, int) check was passing for bool, because bool is a subclass of int in Python. area_reg_polygon(True, 5) would silently compute cot(pi/1) for a '1-sided regular polygon' and return a real number, when the function is meant to reject anything that is not a genuine integer count of sides >= 3. Added an explicit isinstance(sides, bool) check and a docstring note explaining the subclass relationship.

Also split the original if ... elif ... so the type check on sides and the non-negative check on length are no longer mutually exclusive branches.

Added doctest cases for area_reg_polygon(True, 5) and area_reg_polygon(3.0, 5) so the new behavior is locked in.

  1. surface_area_cuboid()'s existing test coverage happened to miss triples where the negative argument is the middle one (length=1, breadth=-1, height=1). Added a doctest case for that exact input.

Verified: python3 -m doctest maths/area.py -v reports '116 passed and 0 failed'.

Zo Bot added 14 commits April 20, 2026 21:35
…ix Python 3 multi-exception syntax in instagram_crawler
…xception — int() raises ValueError on non-numeric input, not a generic Exception
…emoved in Python 3, use parentheses instead
…oat*float can only raise TypeError for incompatible types; the bare Exception was masking real bugs from the caller
The bare 'except Exception' around mass * acceleration was catching
RecursionError, MemoryError, KeyboardInterrupt, and any unrelated bug
in surrounding code. The multiplication of two Python objects can only
raise TypeError (wrong operand types) or OverflowError (very large
floats), so narrowing to TypeError matches the documented error path
and lets other exceptions propagate as designed.

Also adds a doctest that exercises the error path with a None
argument, so the new behavior is locked in by the test suite.
Two real bugs in maths/area.py:

1. area_reg_polygon()'s isinstance(sides, int) check was passing for bool,
   because bool is a subclass of int in Python. area_reg_polygon(True, 5)
   would silently compute cot(pi/1) for a '1-sided regular polygon' and
   return a real number, when the function is meant to reject anything
   that is not a genuine integer count of sides >= 3. Added an explicit
   isinstance(sides, bool) check and a docstring note explaining the
   subclass relationship.

   Also split the original  so the type check on sides
   and the non-negative check on length are no longer mutually exclusive
   branches. Previously, sides=True and length=-1 would silently pass the
   type guard, then hit the elif and raise the wrong error.

   Added doctest cases for area_reg_polygon(True, 5) and
   area_reg_polygon(3.0, 5) so the new behavior is locked in.

2. surface_area_cuboid()'s existing test coverage happened to miss
   triples where the negative argument is the *middle* one
   (length=1, breadth=-1, height=1). Added a doctest case for that
   exact input. The if-statement itself was already correct (short-
   circuit-or with three comparands binds tighter than the
   not-yet-existing precedence issue), but the missing test meant
   nothing was actually exercising the breadth < 0 branch in
   isolation. The new case locks it in.

Verified: `python3 -m doctest maths/area.py -v` reports
'116 passed and 0 failed'.
@HrachShah HrachShah requested a review from cclauss as a code owner June 10, 2026 07:39
@algorithms-keeper

Copy link
Copy Markdown

Closing this pull request as invalid

@HrachShah, this pull request is being closed as none of the checkboxes have been marked. It is important that you go through the checklist and mark the ones relevant to this pull request. Please read the Contributing guidelines.

If you're facing any problem on how to mark a checkbox, please read the following instructions:

  • Read a point one at a time and think if it is relevant to the pull request or not.
  • If it is, then mark it by putting a x between the square bracket like so: [x]

NOTE: Only [x] is supported so if you have put any other letter or symbol between the brackets, that will be marked as invalid. If that is the case then please open a new pull request with the appropriate changes.

@algorithms-keeper algorithms-keeper Bot removed the request for review from cclauss June 10, 2026 07:39
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed and removed awaiting reviews This PR is ready to be reviewed labels Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant