Loading...
More Python Posts
def sum_of_powers(end, power = 2, start = 1):return sum([(i) ** power for i in range(start, end + 1)])sum_of_powers(10) # 385sum_of_powers(10, 3) # 3025sum_of_powers(10, 3, 5) # 2925
""" Binary Search Algorithm----------------------------------------"""#iterative implementation of binary search in Pythondef binary_search(a_list, item):"""Performs iterative binary search to find the position of an integer in a given, sorted, list.a_list -- sorted list of integersitem -- integer you are searching for the position of"""first = 0last = len(a_list) - 1while first <= last:i = (first + last) / 2if a_list[i] == item:return ' found at position '.format(item=item, i=i)elif a_list[i] > item:last = i - 1elif a_list[i] < item:first = i + 1else:return ' not found in the list'.format(item=item)#recursive implementation of binary search in Pythondef binary_search_recursive(a_list, item):"""Performs recursive binary search of an integer in a given, sorted, list.a_list -- sorted list of integersitem -- integer you are searching for the position of"""first = 0last = len(a_list) - 1if len(a_list) == 0:return ' was not found in the list'.format(item=item)else:i = (first + last) // 2if item == a_list[i]:return ' found'.format(item=item)else:if a_list[i] < item:return binary_search_recursive(a_list[i+1:], item)else:return binary_search_recursive(a_list[:i], item)
import sys# sample TuplesTuple1 = ("A", 1, "B", 2, "C", 3)Tuple2 = ("Geek1", "Raju", "Geek2", "Nikhil", "Geek3", "Deepanshu")Tuple3 = ((1, "Lion"), ( 2, "Tiger"), (3, "Fox"), (4, "Wolf"))# print the sizes of sample Tuplesprint("Size of Tuple1: " + str(sys.getsizeof(Tuple1)) + "bytes")print("Size of Tuple2: " + str(sys.getsizeof(Tuple2)) + "bytes")print("Size of Tuple3: " + str(sys.getsizeof(Tuple3)) + "bytes")
def clamp_number(num, a, b):return max(min(num, max(a, b)), min(a, b))clamp_number(2, 3, 5) # 3clamp_number(1, -1, -5) # -1
import subprocess #for the praat callsimport os #for ffmpeg and the pause call at the end#Even if we wanted all videos being rendered asynchronously, we couldn't see progress or errorsimport glob #for the ambiguous filesimport tempfileaudioFileDirectory = 'Audio Files'timeList = {}fileList = glob.glob(audioFileDirectory + '\\*.wav')pipeList = {}for fileName in fileList:arglist = ['Praat.exe', '--run', 'crosscorrelateMatch.praat', 'zeussound.wav', fileName, "0" , "300"]print(' '.join(arglist))pipe = subprocess.Popen(arglist, stdout=subprocess.PIPE)pipeList[fileName[len(audioFileDirectory)+1:-4]] = pipe #+1 because of back slash, -4 because .wav#for fileName, pipe in pipeList.items():# text = pipe.communicate()[0].decode('utf-8')# timeList[fileName] = float(text[::2])for fileName, pipe in pipeList.items():if float(pipe.communicate()[0].decode('utf-8')[::2]) > .0003: #.000166 is not a match, and .00073 is a perfect match. .00053 is a tested matcharglist = ['Praat.exe', '--run', 'crosscorrelate.praat', 'zeussound.wav', audioFileDirectory + '\\' + fileName + '.wav', "0" , "300"]print(' '.join(arglist))text = subprocess.Popen(arglist, stdout=subprocess.PIPE).communicate()[0].decode('utf-8')timeList[fileName] = float(text[::2])clipLength = 10for fileName, time in timeList.items():arglist = ['ffmpeg', '-i', '"'+fileName+'.mp4"', '-ss', str(time-clipLength), '-t', str(clipLength*2), '-acodec', 'copy' , '-vcodec', 'copy', '"ZEUS'+ fileName + '.mp4"']print(' '.join(arglist))os.system(' '.join(arglist))tempFile = tempfile.NamedTemporaryFile(delete=False)for fileName in glob.glob('ZEUS*.mp4'):tempFile.write(("file '" + os.path.realpath(fileName) + "'\n").encode());tempFile.seek(0)print(tempFile.read())tempFile.close()arglist = ['ffmpeg', '-safe', '0', '-f', 'concat', '-i', '"'+tempFile.name+'"', '-c', 'copy', 'ZeusMontage.mp4']print(' '.join(arglist))os.system(' '.join(arglist))os.unlink(tempFile.name) #Delete the temp file#print(timeList)os.system('PAUSE')
import randomclass Node:def __init__(self, c):self.left = Noneself.right = Noneself.color = cdef SetColor(self,c) :self.color = cdef PrintNode(self) :print(self.color)def insert(s, root, i, n):if i < n:temp = Node(s[i])root = temproot.left = insert(s, root.left,2 * i + 1, n)root.right = insert(s, root.right,2 * i + 2, n)return rootdef MakeTree(s) :list = insert(s,None,0,len(s))return listdef MakeSet() :s = []count = random.randint(7,12)for _ in range(count) :color = random.randint(0,1) == 0 and "Red" or "White"s.append(color)return sdef ChangeColor(root) :if (root != None) :if (root.color == "White") :root.SetColor("Red")ChangeColor(root.left)ChangeColor(root.right)def PrintList(root) :if root.left != None :PrintList(root.left)else :root.PrintNode()if root.right != None :PrintList(root.right)else :root.PrintNode()t1 = MakeTree(MakeSet())print("Original Colors For Tree 1:\n")PrintList(t1)ChangeColor(t1)print("New Colors For Tree 1:\n")PrintList(t1)t2 = MakeTree(MakeSet())print("Original Colors For Tree 2:\n")PrintList(t2)ChangeColor(t2)print("New Colors For Tree 2:\n")PrintList(t2)t3 = MakeTree(MakeSet())print("Original Colors For Tree 3:\n")PrintList(t3)ChangeColor(t3)print("New Colors For Tree 3:\n")PrintList(t3)