-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5375ac8
commit eb51fec
Showing
22 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,42 @@ | ||
# The parity of a word is 1 if the number of 1 bits in the word is odd, else it's 0 | ||
# | ||
# Question: Find the parity of a word? | ||
|
||
|
||
# Solution 1 | ||
# Brute force to find all 1s | ||
def parity(x): | ||
result = 0 | ||
while x: | ||
result ^= x & 1 | ||
x >>= 1 | ||
return result | ||
|
||
|
||
# Solution 2 | ||
# Instead of checking for every bit, check for last set bit | ||
# Use x & x - 1 to remove the last set bit | ||
def parity2(x): | ||
result = 0 | ||
while x: | ||
result ^= 1 | ||
x &= x - 1 | ||
return result | ||
|
||
|
||
# Test | ||
def expect(a, b): | ||
result = a == b | ||
print("Test for " + str(a) + (" passed!" if result else " failed!")) | ||
|
||
|
||
print("Parity 1") | ||
a = 0b1010 | ||
expect(0, parity(a)) | ||
a = 0b1110 | ||
expect(1, parity(a)) | ||
print("Parity 2") | ||
a = 0b1010 | ||
expect(0, parity2(a)) | ||
a = 0b1110 | ||
expect(1, parity2(a)) |
This file contains 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 @@ | ||
# Question: Find x^y |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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,8 @@ | ||
# LeetCode Solutions | ||
|
||
This repository contains my solutions to various LeetCode problems. Each solution is implemented in Python, and the goal is to provide clear, concise, and efficient solutions. | ||
|
||
## Structure | ||
Each problem has its own directory named after the problem's number and title. | ||
`<problem_number>_<topic>/` | ||
` solution.py` |