Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions Python/countZero.py
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:
Copy link
Contributor

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.

Suggested change
def count_zeros(iterable: Iterable[int]) -> int:
You can collapse `parse_input` and `count_zeros` into just a few lines in `main`, leveraging Python built-ins and removing the custom loop / error branches:
```python
def main() -> int:
tokens = sys.stdin.read().split()
if not tokens:
demo = [0, 1, 0, 2, 0, 3]
print("No input detected — running demo array:", demo)
print("Zero count:", demo.count(0))
return 0
# parse ints, drop leading “count” if it matches remaining length
nums = list(map(int, tokens))
if len(nums) > 1 and nums[0] == len(nums) - 1:
nums = nums[1:]
print(nums.count(0))
return 0

This:

  • Eliminates the separate parse_input + exception handling (the map(int,…) will raise if invalid).
  • Removes the manual for-loop in count_zeros in favor of .count(0).
  • Keeps the demo fallback intact.

"""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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): We've found these issues:

Suggested change
c = 0
for x in iterable:
if x == 0:
c += 1
return c
return sum(1 for x in iterable if x == 0)



def parse_input(stream) -> list:
Copy link
Contributor

Choose a reason for hiding this comment

The 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
def parse_input(stream) -> list:
def parse_input(stream) -> list[int]:

"""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

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): We've found these issues:

Suggested change
if len(nums) >= 2 and nums[0] == len(nums) - 1:
return nums[1:]
# Otherwise return all tokens
return nums
return nums[1:] if len(nums) >= 2 and nums[0] == len(nums) - 1 else nums



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())