Skip to content

added hash set and valid sudoku solutions #3

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 1 commit into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
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
57 changes: 57 additions & 0 deletions 03. Data Structures/Hash Tables/Hash_Set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# hashset | leetcode 705 | https://leetcode.com/problems/design-hashset/
class HashSet:

# constructor
def __init__ (self, hash_set = None):
'''initialize a hash set'''
self.hash_set: dict = {} if hash_set == None else hash_set
self.size: int = len(self.hash_set.keys())

# initialize iterator for __iter__
def __iter__ (self):
self.n = 0
return self

# return next element for __next__
def __next__ (self):
if self.n < self.size:
result = list(self.hash_set.keys())[self.n]
self.n = self.n + 1
return result
else:
raise StopIteration

def add (self, key) -> None:
'''add element to hash set'''
self.hash_set[key] = True
self.size = self.size + 1

def contains (self, key) -> bool:
'''does hash set contain element'''
return True if self.hash_set.get(key) else False

def remove (self, key) -> None:
'''remove element from hash set'''
if self.contains(key):
self.hash_set.pop(key)
self.size = self.size - 1

# initialize a new hashset
hashSet = HashSet()

# add values to a hash set
hashSet.add('first')
hashSet.add('second')
hashSet.add('third')
hashSet.add('fourth')

# remove from a hash set
hashSet.remove('fourth')

# check if value exists in a hash set
print(hashSet.contains('first'))
print(hashSet.contains('fourth'))

# iterate through a hash set
for element in hashSet:
print(element)
30 changes: 30 additions & 0 deletions 03. Data Structures/Hash Tables/Valid_Sudoku.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# valid sudoku | leetcode 36 | https://leetcode.com/problems/valid-sudoku/
# Determine if a n^2 x n^2 Psuedo-Sudoku board is valid.
# Only the filled cells need to be validated.

import collections

class Solution:
def isValidSudoku(self, board: list[list[str]]) -> bool:
rows = collections.defaultdict(set)
columns = collections.defaultdict(set)
squares = collections.defaultdict(set)

for i in range(len(board)):
for j in range(len(board[i])):

if board[i][j] == '.':
continue

isInRow = board[i][j] in rows[i]
isInColumn = board[i][j] in columns[j]
isInSquare = board[i][j] in squares[(i//3, j//3)]
if (isInRow or isInColumn or isInSquare):
return False

rows[i].add(board[i][j])
columns[j].add(board[i][j])
squares[(i//3, j//3)].add(board[i][j])

return True

20 changes: 12 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# Data Structures and Algorithms

![](https://img.shields.io/github/issues/shushrutsharma/Data-Structures-and-Algorithms-Python?color=red&style=for-the-badge)
![](https://img.shields.io/github/issues-pr-closed/shushrutsharma/Data-Structures-and-Algorithms-Python?style=for-the-badge)
![](https://img.shields.io/github/stars/shushrutsharma/Data-Structures-and-Algorithms-Python?style=for-the-badge)
![](https://img.shields.io/badge/Built%20With-Python-blueviolet?style=for-the-badge)
![Issues](https://img.shields.io/github/issues/shushrutsharma/Data-Structures-and-Algorithms-Python?color=red&style=for-the-badge)
![PRs](https://img.shields.io/github/issues-pr-closed/shushrutsharma/Data-Structures-and-Algorithms-Python?style=for-the-badge)
![Stars](https://img.shields.io/github/stars/shushrutsharma/Data-Structures-and-Algorithms-Python?style=for-the-badge)
![Built With](https://img.shields.io/badge/Built%20With-Python-blueviolet?style=for-the-badge)

All the essential resources and template code needed to understand and practice data structures and algorithms in python with few small projects to demonstrate their practical application.

## Index

### 1. 📚 [Resources](/01.%20Resources/)

1. [Books](/01.%20Resources/Books/)
- [Data Structures - Reema Thareja](/01.%20Resources/Books/Data%20Structures%20-%20Reema%20Thareja.pdf)
- [competitiveCoding](https://github.com/mihirs16/Data-Structures-and-Algorithms-Python/blob/master/01.%20Resources/Books/competitiveCoding.pdf)
Expand All @@ -20,12 +21,14 @@ All the essential resources and template code needed to understand and practice
6. [Master the Interview](/01.%20Resources/Master_the_Interview.pdf)

### 2. ⭕ [Big-O](/02.%20Big-O/)

1. [O(1)](/02.%20Big-O/O(1).py)
2. [O(m+n)](/02.%20Big-O/O(m%20+%20n).py)
3. [O(n)](/02.%20Big-O/O(m%20x%20n).py)
4. [O(n^2)](/02.%20Big-O/O(n^2).py)

### 3. 🏢 [Data Structures](/03.%20Data%20Structures/)

1. [Arrays](/03.%20Data%20Structures/Arrays/)
2. [Graphs](/03.%20Data%20Structures/Graphs)
3. [Hash Tables (Dictionary)](/03.%20Data%20Structures/Hash%20Tables)
Expand All @@ -35,20 +38,22 @@ All the essential resources and template code needed to understand and practice
7. [Trees](/03.%20Data%20Structures/Trees)

### 4. 🛠 [Algorithms](/04.%20Algorithms/)

1. [Divide and Conquer](/04.%20Algorithms/Divide%20and%20Conquer/)
2. [Dynamic Programming](/04.%20Algorithms/Dynamic%20Programming/)
3. [Recursion](/04.%20Algorithms/Recursion/)
4. [Sorting](/04.%20Algorithms/Sorting/)
5. [Traversals](/04.%20Algorithms/Traversals)

### 5. 📂 [File Handling and OOPS](/05.%20File%20Handling%20and%20OOPS/)

1. [File + Classes Demo](/05.%20File%20Handling%20and%20OOPS/file%2Bclasses.py)

# Additional:
## Additional

### 6. ❗ [Error Handling](/06.%20Error%20Handling/)

### 7. ➗ [Functional Programming](/07.%20Functional%20Programming/)
### 7. ➗ [Functional Programming](/07.%20Functional%20Programming/)

### 8. 🎉 [Decorators](/08.%20Decorators/)

Expand All @@ -60,9 +65,8 @@ All the essential resources and template code needed to understand and practice

### 12. 🧪 [Unit Testing](/12.%20Unit%20Testing/)

------------------------------------------

### 13. 👷 ‍[Projects](/13.%20Mini-Projects/)

1. [Job Scheduler](/13.%20Mini-Projects/Job%20Scheduler)
2. [Email Project](/13.%20Mini-Projects/email_project)
3. [Hash Project](/13.%20Mini-Projects/hash_project)
Expand Down