""" Authors: Consulted: Date: Purpose: Lab 08 tracing practice """ def hasCGBlock(sequence): """ Given a sequence of RNA bases, returns True if the sequence contains a run of 5 or more 'C' or 'G' bases in a row. The run may contain any combination of 'C' and 'G' bases, but must have at least 5 in a row to count. """ # Counter for runs of 'C' and/or 'G' run = 0 # Consider each base in the sequence for base in sequence: print('base', base, base in 'CG') if base in 'CG': # if it's a 'C' or 'G', count it run += 1 # If our run is long enough, we can be done immediately if run == 5: print('found it') return True else: # not a 'C' or 'G': reset our run counter to 0 run = 0 print('run', run) print('no blocks') return False def hasCGBlock1(sequence): """ An incorrect version of hasCGBlock above. """ # Counter for runs of 'C' and/or 'G' run = 0 # Consider each base in the sequence for base in sequence: if base in 'CG': # if it's a 'C' or 'G', count it run += 1 # If our run is long enough, we can be done immediately if run == 5: return True else: # not a 'C' or 'G': reset our run counter to 0 run = 0 return False def hasCGBlock2(sequence): """ A second incorrect version of hasCGBlock above. """ # Counter for runs of 'C' and/or 'G' run = 0 # Consider each base in the sequence for base in sequence: if base in 'CG': # if it's a 'C' or 'G', count it run += 1 # If our run is long enough, we can be done immediately if run == 5: return True else: # not a 'C' or 'G': reset our run counter to 0 return True return False def hasCGBlock3(sequence): """ A third incorrect version of hasCGBlock above. The bug in this version is quite subtle. """ # Counter for runs of 'C' and/or 'G' run = 1 # Consider each base in the sequence for base in sequence: if base in 'CG': # if it's a 'C' or 'G', count it run += 1 # If our run is long enough, we can be done immediately if run == 5: return True else: # not a 'C' or 'G': reset our run counter to 0 run = 0 return False from optimism import * def test(): """ This function is designed to be used to set up and run tests. If you put them here, they won't interfere with anything else you might want to do in the rest of the file, until you call this function. """ # Put your test cases and expectations here for arg, exp in [ ('CGAGGGCCUG', True), # hasCGBlock1 fails this case ('CG', False), # hasCGBlock2 fails this case ('CGGG', False), # hasCGBlock3 fails this case # The cases above are enough as counterexamples of each # provided function, but here are some more reasonable cases ('CCCCC', True), ('CCCCCA', True), ('AAAAA', False), ('AUCAUC', False), ('CGCGAGCGC', False), ('AAACCCGGUUU', True), ]: testCase(hasCGBlock(arg)) expectResult(exp) testCase(hasCGBlock1(arg)) expectResult(exp) testCase(hasCGBlock2(arg)) expectResult(exp) testCase(hasCGBlock3(arg)) expectResult(exp)