Skip to content
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

Complete PreCourse-2 #1620

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
18 changes: 18 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "windows-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "gcc",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "windows-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": true,
"cwd": "e:/Learning/S30/PreCourse-2",
"program": "e:/Learning/S30/PreCourse-2/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
59 changes: 59 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
}
16 changes: 14 additions & 2 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#Time Complexity is O(log n) and Space Complexity is O(1)

# Python code to implement iterative Binary
# Search.

Expand All @@ -6,6 +8,16 @@
def binarySearch(arr, l, r, x):

#write your code here
while l<=r:
mid = l+(r-1)//2

if arr[mid] == x:
return mid
elif arr[mid]>x:
r=mid-1
else:
l = mid + 1
return -1



Expand All @@ -17,6 +29,6 @@ def binarySearch(arr, l, r, x):
result = binarySearch(arr, 0, len(arr)-1, x)

if result != -1:
print "Element is present at index % d" % result
print ("Element is present at index % d" % result )
else:
print "Element is not present in array"
print ("Element is not present in array")
19 changes: 19 additions & 0 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
#Time complexity is O(nlogn) and Space complexity is O(logn)

# Python program for implementation of Quicksort Sort

# give you explanation for the approach
def partition(arr,low,high):


#write your code here
pivot = arr[high]

i = low-1

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

return i + 1


# Function to do Quick sort
def quickSort(arr,low,high):

#write your code here
if low < high:

pivotIndex = partition(arr, low, high)

quickSort(arr, low, pivotIndex-1)
quickSort(arr, pivotIndex + 1, high)

# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
Expand Down
19 changes: 17 additions & 2 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
# Time complexity is O(n) and space complexity is O(1)
# Node class
class Node:

# Function to initialise the node object
def __init__(self, data):
def __init__(self, data):
self.data = data
self.next = None

class LinkedList:

def __init__(self):
self.head = None


def push(self, new_data):

newNode = Node(new_data)

newNode.next = self.head
self.head = newNode

# Function to get the middle of
# the linked list
def printMiddle(self):
slow = self.head
fast = self.head
while fast is not None and fast.next is not None:
slow = slow.next
fast = fast.next.next

if slow is not None:
print("Middle Element: ", slow.data)

# Driver code
list1 = LinkedList()
Expand Down
47 changes: 47 additions & 0 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,59 @@
#Time complexity is O(nlogn) and space complexity is O(n).

# Python program for implementation of MergeSort
def merge(arr, left, mid, right):
n1 = mid - left + 1
n2 = right - mid

leftArr = arr[left:mid + 1]
rightArr = arr[mid + 1:right + 1]

i = j = 0
k = left

while i < n1 and j < n2:
if leftArr[i] < rightArr[j]:
arr[k]=leftArr[i]
i+=1
else:
arr[k] = rightArr[j]
j+=1
k += 1
while i < n1:
arr[k] = leftArr[i]
i+=1
k+=1
while j < n2:
arr[k] = rightArr[j]
j+=1
k+=1



def mergeSort(arr):

#write your code here

if len(arr) > 1:
mid = len(arr) // 2

left = arr[:mid]
right = arr[mid:]


mergeSort(left)
mergeSort(right)

merge(arr, 0, mid - 1, len(arr) - 1)

# Code to print the list
def printList(arr):

#write your code here

for i in arr:
print(i, end=" ")
print()

# driver code to test the above code
if __name__ == '__main__':
Expand Down
22 changes: 22 additions & 0 deletions Exercise_5.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
#Time Complexity O(n^2) and space complexity O(n)
# Python program for implementation of Quicksort



# This function is same in both iterative and recursive
def partition(arr, l, h):
#write your code here
pivot = arr[h]
i = l-1

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


def quickSortIterative(arr, l, h):
#write your code here
stack = []

stack.append((1, h))

while stack:
l,h = stack.pop()

if l<h:
pivot = partition(arr, l, h)
stack.append((l, pivot - 1))
stack.append((pivot + 1, h))
Loading