40 likes | 146 Vues
This document explores various coding techniques across different algorithms, focusing on debugging and optimizing code for Average Path Length (APTs) and DNA sequence analysis. We delve into practical implementations such as `findNames`, which retrieves names based on birth years, and `cgratio`, which calculates the CG ratio in DNA strands. Through the example of determining the maximum index of CG ratios in a given window size, we illustrate the real-world application of these techniques, ultimately bridging the gap between theory and practical coding.
E N D
Debugging APTs: Going green • SandwichBar APT: from ideas to code to green def whichOrder(available,orders): for idx,sand in enumerate(orders): • CirclesCountry APT: from ideas to code to green def leastBorders(x,y,r,x1,y1,x2,y2): count = 0 for i,r in enumerate(r):
Question from previous Compsci 6 names=["Jessica Chang: Feb 10, 1991", "Leo Rofe: Aug 3, 1973", "Chris Brown: May 14, 1991", "Wayne Dark Light: Dec 25, 1985", "Zhiyi Zhang: Nov 24, 1995"] • Spec for format given, and the call findNames(names,1991) returns… • ["Jessica Chang", "Chris Brown"] def findNames(list,year):
Revisiting cgratio APT • 'cost' of finding likely sources of protein in DNA def cgratio(strand): cg = 0 for nuc in strand: if nuc == 'c' or nuc == 'g': cg += 1 return cg def maxIndex(strand,windowSize): index,max = 0,0 for i in range(0,len(strand)-windowSize+1): cg = cgratio(strand[i:i+windowSize]) if cg > max: max,index = cg,i return index
Revisiting cgratio APT • 'cost' of finding likely sources of protein in DNA def runningMax(strand,windowSize): gc,counters = 0,[] for nuc in strand: counters.append(gc) if nuc == 'c' or nuc == 'g': gc += 1 counters.append(gc) index,max = 0,0 for i in range(windowSize,len(strand)+1): diff = counters[i] - counters[i-windowSize] if diff > max: max,index = diff,i return index-windowSize