mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Compare commits
6 Commits
58534113d4
...
3985835aa5
Author | SHA1 | Date | |
---|---|---|---|
|
3985835aa5 | ||
|
f3f32ae3ca | ||
|
e3bd7721c8 | ||
|
e3f3d668be | ||
|
20070c23e5 | ||
|
15658bfe12 |
|
@ -16,7 +16,7 @@ repos:
|
||||||
- id: auto-walrus
|
- id: auto-walrus
|
||||||
|
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
rev: v0.7.2
|
rev: v0.7.4
|
||||||
hooks:
|
hooks:
|
||||||
- id: ruff
|
- id: ruff
|
||||||
- id: ruff-format
|
- id: ruff-format
|
||||||
|
@ -42,7 +42,7 @@ repos:
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
|
|
||||||
- repo: https://github.com/abravalheri/validate-pyproject
|
- repo: https://github.com/abravalheri/validate-pyproject
|
||||||
rev: v0.22
|
rev: v0.23
|
||||||
hooks:
|
hooks:
|
||||||
- id: validate-pyproject
|
- id: validate-pyproject
|
||||||
|
|
||||||
|
|
30
bit_manipulation/single_number.py
Normal file
30
bit_manipulation/single_number.py
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
def single_number(nums: list[int]) -> int:
|
||||||
|
"""
|
||||||
|
Find the single number in an array where every other number appears twice.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
nums: List of integers where every integer appears twice except for one.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The single integer that appears once.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> single_number([4, 1, 2, 1, 2])
|
||||||
|
4
|
||||||
|
>>> single_number([2, 2, 1])
|
||||||
|
1
|
||||||
|
>>> single_number([1])
|
||||||
|
1
|
||||||
|
"""
|
||||||
|
result = 0
|
||||||
|
for num in nums:
|
||||||
|
result ^= num # Applying XOR operation
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# Test cases
|
||||||
|
print(single_number([4, 1, 2, 1, 2])) # Output: 4
|
||||||
|
print(single_number([2, 2, 1])) # Output: 1
|
||||||
|
print(single_number([1])) # Output: 1
|
||||||
|
print(single_number([5, 3, 5, 7, 3])) # Output: 7
|
|
@ -172,7 +172,7 @@ def solved(values):
|
||||||
|
|
||||||
def from_file(filename, sep="\n"):
|
def from_file(filename, sep="\n"):
|
||||||
"Parse a file into a list of strings, separated by sep."
|
"Parse a file into a list of strings, separated by sep."
|
||||||
return open(filename).read().strip().split(sep) # noqa: SIM115
|
return open(filename).read().strip().split(sep)
|
||||||
|
|
||||||
|
|
||||||
def random_puzzle(assignments=17):
|
def random_puzzle(assignments=17):
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/env python3
|
#!python
|
||||||
import os
|
import os
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user