Skip to content

Commit

Permalink
feat: add elements of programming
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwin-nair98 committed Mar 5, 2024
1 parent 5375ac8 commit eb51fec
Show file tree
Hide file tree
Showing 22 changed files with 51 additions and 0 deletions.
42 changes: 42 additions & 0 deletions elements/4_1_Parity_of_word/solution.py
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))
1 change: 1 addition & 0 deletions elements/4_7_x_pow_y/solution.py
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.
8 changes: 8 additions & 0 deletions leetcode/README.md
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`

0 comments on commit eb51fec

Please sign in to comment.