Skip to content

Commit

Permalink
Data structures
Browse files Browse the repository at this point in the history
  • Loading branch information
224apps committed Jul 4, 2020
1 parent 0e8c865 commit 09d13cd
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 1 deletion.
20 changes: 20 additions & 0 deletions 1-100/32.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,24 @@ def longestValidParentheses(self, s):
else:
result = max(result, index-currIndex+1)
return result


# class Solution(object):
# def longestValidParentheses(self, s):
# """
# :type s: str
# :rtype: int
# """
# q = []
# start, ans = 0, 0
# for i in range(len(s)):
# if s[i] == '(':
# q.append(i)
# continue
# if not q:
# start = i + 1
# else:
# q.pop()
# ans = max(ans, i - q[-1] if q else i - start + 1)
# return ans

32 changes: 31 additions & 1 deletion 1-100/37.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,34 @@
The given board contain only digits 1-9 and the character '.'.
You may assume that the given Sudoku puzzle will have a single unique solution.
The given board size is always 9x9.
'''
'''
class Solution(object):
def solveSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: None Do not return anything, modify board in-place instead.
"""
def isValid(x,y):
tmp=board[x][y]; board[x][y]='D'
for i in range(9):
if board[i][y]==tmp: return False
for i in range(9):
if board[x][i]==tmp: return False
for i in range(3):
for j in range(3):
if board[(x/3)*3+i][(y/3)*3+j]==tmp: return False
board[x][y]=tmp
return True
def dfs(board):
for i in range(9):
for j in range(9):
if board[i][j]=='.':
for k in '123456789':
board[i][j]=k
if isValid(i,j) and dfs(board):
return True
board[i][j]='.'
return False
return True
dfs(board)

22 changes: 22 additions & 0 deletions Data-Structures/Arrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import array
arr = array.array('i', [1,2,3])

print("The new array created is: ")
for i in range(0,3):
print(arr[i])

print("\r")

arr.append(4)

print("The appended array is: ")
for i in range(0,4):
print(arr[i])


arr.insert(2,5)


print("The inserted array is: ")
for i in range(0,5):
print(arr[i])
39 changes: 39 additions & 0 deletions Data-Structures/DLinkedList.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Node:
def __init__(self, next=None, prev=None, data= None):
self.next = next
self.prev = prev
self.data = data

class DoublyLinkedList:
def __init__(self):
self.head = None

def insert_at_begin(self, data):
node = Node(data)
node.next = self.head
if self.head:
self.head.prev = node
self.head = node

def insert_after(self, prev_node, data):
if not prev:
print("This node is not present in the list")
return
node = Node(data)

node.next = prev_node.next
prev_node.next = node
node.prev = prev_node

if node.next:
node.next.prev = node

def insert_at_end(self, data):

node = Node(data)
last = self.head





98 changes: 98 additions & 0 deletions Data-Structures/Linked_List.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
class Node:
def __init__(self, data):
self.data = data
self.next = None

class LinkedList:
def __init__(self):
self.head = None

# insert a new node at the beginning
def insert_at_begin(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node

# Inserts a new node after the given prev_node

def insertAfter(self, prev, data):
new_node = Node(data)

if not prev:
print("prev must be in the node")
return
new_node = Node(data)
curr= self.head
while curr:
if curr != prev:
curr = curr.next
new_node.next = prev.next
prev.next = new_node

def insertAtEnd(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
curr = self.head
while curr.next:
curr = curr.next
self.head.next = new_node

# Given a reference to the head of a list
# and a position, delete the node at a given position
def deleteNode(self, position):
if not self.head:
return
curr = self.head
if position == 0:
self.head = curr.next
curr = None
return
for index in range(position -1):
curr = curr.next
if curr is None:
break
if curr is None:
return
next = curr.next.next
curr.next = None
curr.next = next


# This function prints contents of linked list
# starting from head
def printList(self):
temp = self.head
res = []
while temp:
res.append(temp.data)
temp = temp.next
return res












# Start with the empty list


llist = LinkedList()
llist.insert_at_begin(6)
llist.insert_at_begin(6)
llist.insert_at_begin(1)
llist.insertAtEnd(4)
llist.insertAtEnd(9)
llist.insertAfter(llist.head.next, 8)
llist.printList()
print("Created Linked List: ")
llist.printList()
llist.deleteNode(4)
print ("\nLinked List after Deletion at position 4: ")
llist.printList()
27 changes: 27 additions & 0 deletions Data-Structures/Quick_Sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution:
def partition(self, arr, low, high):
i = (low -1)
pivot = arr[high]

for j in range(low, high):
if arr[j] < pivot:
i = i+1
arr[i],arr[j] = arr[j], arr[i]
arr[i+1], arr[high] = arr[high], arr[i+1]
return (i+1)

def quick_sort(self, arr, low, high):
if low < high:
pi = self.partition(arr, low, high)
self.quick_sort(arr, low, pi -1)
self.quick_sort(arr, pi + 1, high)




arr = [10, 7, 8, 9, 1, 5]
n = len(arr)
Solution().quick_sort(arr,0,n-1)
print ("Sorted array is:")
for i in range(n):
print ("%d \t" %arr[i] + " ")

0 comments on commit 09d13cd

Please sign in to comment.