Skip to content

Instantly share code, notes, and snippets.

View pratik7368patil's full-sized avatar

Pratik Patil pratik7368patil

  • Maharashtra, India
View GitHub Profile
@pratik7368patil
pratik7368patil / array-1.py
Last active May 26, 2020 17:27
Data Structures - 1
arr = [3,4,5,7,8,9] # This is a List
print(arr) # output will be [3,4,5,7,8,9]
# for array in python you'll need array module or nympy package
# let's import that
from array import *
arr = array('i', [3,4,5,7,8,9]) # array() is a function defined in array module
# to print array
@pratik7368patil
pratik7368patil / array-2.py
Last active May 26, 2020 18:27
Data Structures - 1
arr = [3,4,5,7,8,9] # list in python
# throught this we will refer it as a array
# 1. append(value)
arr.append(10) # now the array will be [3,4,5,7,8,9,10]
# 2. insert(i, value) ------ i refer index
arr.insert(0,2) # now the array will be [2,3,4,5,7,8,9,10]
# 3. extend(arr) ------ arr refer another array
arr.extend([11,12]) # now the array will be [2,3,4,5,7,8,9,10,11,12]
# 4. remove(value)
@pratik7368patil
pratik7368patil / string-1.py
Created May 27, 2020 12:02
Data Structure - 1
# define string in python
s = "hack your code"
# now let's see some of the operations that can be perform on the string
# accessing characters in string is same as accessing elements in array
s[0] # == "h"
s[2] # == "c"
s[-1] # == "e" last character of string
# you can not assign or update a single character in string
s[3] = "u" # this is not valid this will give you error
@pratik7368patil
pratik7368patil / reverse_array-2.py
Created May 27, 2020 15:33
Reverse an array in place in Python
# our task is to reverse an array
# Their are lots of method to reverse an array in Python
# 1. Reverse an array using Slicing method.
arr = [10,20,30,40,50] # the original array
res = arr[::-1] # reversing given array using slicing
print(res)
# output wil be [50,40,30,20,10]
# 2. Reverse an array using reverse method.
@pratik7368patil
pratik7368patil / reverse_array-1.py
Last active May 27, 2020 16:01
Reverse an Array in Place in Python
# reverse an array in place
# We can do this using two pointer approach with O(n) time and O(1) space complexity.
# here we are going to define function
def reverse_in_place(arr):
i = 0 # i hold starting index which is 0
j = len(arr) - 1 # j hold last index which is 5 (0-based indexing)
while(j > i):
arr[i], arr[j] = arr[j], arr[i] # swapping an elements
i += 1
@pratik7368patil
pratik7368patil / remove_duplicate.py
Created May 27, 2020 17:23
Remove Duplicates from array in python
# remove duplicate from array in place
# below function returns modified array
# time complexity of this program is O(nlogn) + O(n) that is nothing but O(nlogn).
def remove_duplicate(arr):
arr.sort() # sort array that will take O(nlogn) time
l = len(arr)
if l == 0 or l == 1: # check for a base condition
return arr
x = 0 # extra pointer
@pratik7368patil
pratik7368patil / remove_duplicate-2.py
Last active May 28, 2020 02:32
Remove Duplicate from Array in Place in Python
# Method 1 ----- first travese the an array then keep record of first occurring element
arr = [10,20,10,20,30,40,30]
dummy = [] ------------ # O(n) space
for i in arr: --------- # O(n)
if i not in dummy: -- # O(n^2) this is a time complexity of this method
dummy.append(i)
print(dummy) # this will give you desired output
# the output will be [10,20,30,40]
# Methos 2 using set() function which is in-build in python
@pratik7368patil
pratik7368patil / find_duplicate-1.py
Created May 27, 2020 18:24
Find Duplicate Numbers from Array
# we can use dictionary to store key-value pair
# by using this we can slove this problem in O(n) time complexity
# to do that we need to import Counter from collections module
from collections import Counter
def find_duplicate(arr):
dc = Counter(arr) # accoring to python's docs this will take O(n)
for val, count in dc.items(): # this will take O(n)
@pratik7368patil
pratik7368patil / find_duplicate-common.py
Created May 27, 2020 18:33
Find Duplicate Numbers from an Array
# this problem can be solve using nested for loops
# this solution is not quite efficient
def find_duplicate(arr):
l = len(arr)
res = []
for i in range(l):
for j in range(i,l):
if arr[i] == arr[j] and not in res:
res.append(arr[i]) # you can also print values
@pratik7368patil
pratik7368patil / quicksort.py
Created May 28, 2020 03:07
Sort array in Place
# implimentation of quicksort algorithm
# it can be implimented both recursively and iteratively.
def quicksort(arr):
l = len(arr)
if l < 2:
return arr
pivot = 0 # pivot element