Skip to content

Instantly share code, notes, and snippets.

View charles2588's full-sized avatar

charles gomes charles2588

View GitHub Profile
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):
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"}
#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)
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
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)
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