コード例 #1
0
class Controller:
    def __init__(self):
        self.__repo = Repository()

    def add_dna(self, dna_string):
        if DNAValidator.validate_dna_string(dna_string):
            self.__repo.append(dna_string)
            return True

        return False

    def filter_dna_by_subseq(self, dna_substring):
        # filter the original list
        filtered_list = []
        for lst in self.__repo.get_data():
            if dna_substring in lst:
                filtered_list.append(lst)

        # return the sorted list by string length, reversed
        filtered_list.sort(key=lambda lst: len(lst), reverse=True)
        return filtered_list

    def filter_punct3(self):
        filtered_list = []
        longest_seq_dict = {}
        for lst in self.__repo.get_data():
            cnt = 1
            cnt_max = 1
            for i in range(len(lst) - 1):
                if lst[i] == lst[i + 1]:
                    cnt += 1

                    if cnt > cnt_max:
                        cnt_max = cnt

            #print("FOR " + str(lst) + " GOT CNT MAX " + str(cnt_max))
            longest_seq_dict.update({cnt_max: lst})

        keys = longest_seq_dict.keys()
        keys = sorted(keys, reverse=True)

        for key in keys:
            filtered_list.append(longest_seq_dict[key])

        return filtered_list

    def clear_repo(self):
        self.__repo.clear()
コード例 #2
0
class TestDNA(unittest.TestCase):
    def run(self):
        self.__ctrl = Controller()

        self.assertFalse(self.__ctrl.add_dna('GATAX'))
        self.assertFalse(self.__ctrl.add_dna(''))

        # 101 de caractere - invalid
        self.assertFalse(
            self.__ctrl.add_dna(
                'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
            ))

        # 100 caractere - valid
        self.assertTrue(
            self.__ctrl.add_dna(
                'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
            ))

        self.assertTrue(self.__ctrl.add_dna('GATCAGGCTAGCTAGTCA'))
        self.assertTrue(self.__ctrl.add_dna('GGGGGGGGGGGGGGGGAT'))

        self.__ctrl.clear_repo()
        self.__ctrl.add_dna('GATA')
        self.__ctrl.add_dna('GTAAA')
        self.__ctrl.add_dna('GTTTTTTT')

        self.assertEqual(self.__ctrl.filter_dna_by_subseq('GA'), ['GATA'])
        self.assertEqual(self.__ctrl.filter_punct3(),
                         ['GTTTTTTT', 'GTAAA', 'GATA'])

        self.__repo = Repository()
        self.assertEqual(self.__repo.get_data(), [])
        self.__repo.append('GATA')
        self.assertEqual(self.__repo.length(), 1)
        self.assertEqual(self.__repo.get_data(), ['GATA'])
        self.__repo.clear()
        self.assertEqual(self.__repo.get_data(), [])