-
Notifications
You must be signed in to change notification settings - Fork 45
Add unique number finder script with input parsing #126
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,66 @@ | ||||||||||
|
|
||||||||||
| """ | ||||||||||
| uniqueNo.py | ||||||||||
|
|
||||||||||
| Find the unique number in an array where every other element appears exactly twice. | ||||||||||
|
|
||||||||||
| Input (stdin): | ||||||||||
| - First line: optional integer n (number of elements) | ||||||||||
| - Remaining tokens: integers (either n integers, or a list of integers) | ||||||||||
|
|
||||||||||
| Output: | ||||||||||
| - Prints the unique integer (the one that appears once) to stdout. | ||||||||||
|
|
||||||||||
| If no input is provided, a small demo runs. | ||||||||||
| """ | ||||||||||
|
|
||||||||||
| import sys | ||||||||||
| from typing import Iterable | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def unique_number(arr: Iterable[int]) -> int: | ||||||||||
| """Return the unique number when every other number appears exactly twice. | ||||||||||
|
|
||||||||||
| This uses XOR properties: x ^ x = 0 and x ^ 0 = x, so XORing all numbers | ||||||||||
| leaves the unique value. | ||||||||||
| """ | ||||||||||
| res = 0 | ||||||||||
| for x in arr: | ||||||||||
| res ^= x | ||||||||||
| return res | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def parse_input(stream) -> list: | ||||||||||
| data = stream.read().strip().split() | ||||||||||
| if not data: | ||||||||||
| return [] | ||||||||||
| try: | ||||||||||
| nums = [int(tok) for tok in data] | ||||||||||
| except ValueError: | ||||||||||
| raise | ||||||||||
|
|
||||||||||
| # If first token is count and matches remaining length, skip it | ||||||||||
| if len(nums) >= 2 and nums[0] == len(nums) - 1: | ||||||||||
| return nums[1:] | ||||||||||
| return nums | ||||||||||
|
Comment on lines
+43
to
+45
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: | ||||||||||
| demo = [2, 3, 5, 4, 5, 3, 4] | ||||||||||
| print("No input detected — running demo array:", demo) | ||||||||||
| print("Unique number:", unique_number(demo)) | ||||||||||
| return 0 | ||||||||||
|
|
||||||||||
| print(unique_number(arr)) | ||||||||||
| return 0 | ||||||||||
|
|
||||||||||
|
|
||||||||||
| if __name__ == '__main__': | ||||||||||
| raise SystemExit(main()) | ||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Critical bug: ambiguous count detection can remove the unique number.
The heuristic
nums[0] == len(nums) - 1cannot reliably distinguish between a count prefix and an actual array element. This causes incorrect results when the unique number happens to be the first element AND equalslen(nums) - 1.Example failure case:
5 1 2 3 1 2(no count, unique number is 5)Recommended fix: Remove the ambiguity by requiring a consistent input format. Choose one of:
Update the docstring accordingly.
🤖 Prompt for AI Agents