This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
OlderNewer