-
Notifications
You must be signed in to change notification settings - Fork 45
Add countZero.py to count the number of zeros in an integer array #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,75 @@ | ||||||||||||||
| """ | ||||||||||||||
| countZero.py | ||||||||||||||
|
|
||||||||||||||
| Count the number of zeros in an integer array. | ||||||||||||||
|
|
||||||||||||||
| Input format (stdin): | ||||||||||||||
| - First line: an integer n = number of elements | ||||||||||||||
| - Second line: n integers separated by spaces (or multiple lines allowed) | ||||||||||||||
|
|
||||||||||||||
| Output: | ||||||||||||||
| - Prints the count of elements equal to 0. | ||||||||||||||
|
|
||||||||||||||
| If run with no/invalid input, a small self-test runs instead. | ||||||||||||||
| """ | ||||||||||||||
|
|
||||||||||||||
| from typing import Iterable | ||||||||||||||
| import sys | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def count_zeros(iterable: Iterable[int]) -> int: | ||||||||||||||
| """Return the number of zeros in the given iterable of integers. | ||||||||||||||
|
|
||||||||||||||
| Args: | ||||||||||||||
| iterable: An iterable of integers. | ||||||||||||||
|
|
||||||||||||||
| Returns: | ||||||||||||||
| The count of values equal to 0. | ||||||||||||||
| """ | ||||||||||||||
| c = 0 | ||||||||||||||
| for x in iterable: | ||||||||||||||
| if x == 0: | ||||||||||||||
| c += 1 | ||||||||||||||
| return c | ||||||||||||||
|
Comment on lines
+29
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code-quality): We've found these issues:
Suggested change
|
||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def parse_input(stream) -> list: | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Consider specifying a more precise return type than 'list'. Annotating the return type as 'list[int]' will make the function's output clearer and assist static analysis tools in detecting type issues.
Suggested change
|
||||||||||||||
| """Parse input from a stream and return a list of integers. | ||||||||||||||
|
|
||||||||||||||
| Expected: first token may be n (ignored if present), remaining tokens are treated as numbers. | ||||||||||||||
| """ | ||||||||||||||
| data = stream.read().strip().split() | ||||||||||||||
| if not data: | ||||||||||||||
| return [] | ||||||||||||||
| # If first token looks like the count and matches remaining length, skip it | ||||||||||||||
| try: | ||||||||||||||
| nums = [int(tok) for tok in data] | ||||||||||||||
| except ValueError: | ||||||||||||||
| raise | ||||||||||||||
x0lg0n marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
|
||||||||||||||
| if len(nums) >= 2 and nums[0] == len(nums) - 1: | ||||||||||||||
| return nums[1:] | ||||||||||||||
| # Otherwise return all tokens | ||||||||||||||
| return nums | ||||||||||||||
|
Comment on lines
+50
to
+53
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code-quality): We've found these issues:
Suggested change
|
||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def main() -> int: | ||||||||||||||
| try: | ||||||||||||||
| arr = parse_input(sys.stdin) | ||||||||||||||
| except Exception: | ||||||||||||||
| print("Error: failed to parse input. Expected integers.") | ||||||||||||||
| return 1 | ||||||||||||||
|
|
||||||||||||||
| if not arr: | ||||||||||||||
| # self-test / demo | ||||||||||||||
| demo = [0, 1, 0, 2, 0, 3] | ||||||||||||||
| print("No input detected — running demo array:", demo) | ||||||||||||||
| print("Zero count:", count_zeros(demo)) | ||||||||||||||
| return 0 | ||||||||||||||
|
|
||||||||||||||
| print(count_zeros(arr)) | ||||||||||||||
| return 0 | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| if __name__ == '__main__': | ||||||||||||||
| raise SystemExit(main()) | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider simplifying the code by merging input parsing and zero counting into the main function using Python built-ins.
This:
parse_input+ exception handling (themap(int,…)will raise if invalid).for-loop incount_zerosin favor of.count(0).