This file contains 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
def write2file(filename,content,mode): | |
try: | |
with open(filename,mode) as f: | |
f.write(content) | |
return("Write Successful") | |
except IOError: | |
return("IOError-- "+IOError) | |
def readfromfile(filename): |
This file contains 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
def goo_longestsubstring(str,dictionary): | |
longest=0 | |
longeststr="" | |
for k,v in dictionary.items(): | |
if v in str: | |
if longest<len(v): | |
longest=len(v) | |
longeststr=v | |
return longeststr | |
dictionary={0:"i",1:"charles",2:"am",3:"gomes",4:"substitute",5:"subcharlessubstitute"} |
This file contains 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
#Generator function yields iterator rather than returning value, no function calls or calls on stack thus saving memory | |
def f(n): | |
for x in range(n): | |
yield x**3 | |
for i in f(5): | |
print(i) | |
This file contains 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
def binarySearch(arr,searchterm): | |
pivotpos=int((len(arr)-1)/2) | |
if pivotpos==0: | |
return False | |
if arr[pivotpos]==searchterm: | |
return arr[pivotpos] | |
elif arr[pivotpos]>searchterm: | |
return binarySearch(arr[:pivotpos],searchterm) #search left when searchterm is less than pivot | |
else: | |
return binarySearch(arr[pivotpos:],searchterm)#search right when searchterm is greater than pivot |
This file contains 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
def printsprial(m,n): | |
arr=[] | |
count=0 | |
for i in range(m): | |
arr.append([]) | |
for j in range(n): | |
count+=1 | |
arr[i].append(count) | |
print(arr) |
This file contains 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
def SortByExampleArray(exampleArr,testArr): | |
CountDict=dict((el,0) for el in testArr) #initialize dictionary | |
Result=[] #initialize empty Result array | |
for x in testArr: | |
CountDict[x]+=1 #Store count of each number with it's key | |
for x in exampleArr: | |
if x in testArr: | |
for i in range(CountDict[x]): | |
Result.append(x) | |
return Result |
NewerOlder