예제 #1
0
 def test_solver(self):
     self.assertEqual(Solution().two_sum([4, 7, 1, -3, 2], 5), True)
     self.assertEqual(Solution().two_sum([4, 7, 1, -3, 2], 10), False)
     self.assertEqual(Solution().two_sum([2, 2, 2, 2, 2], 4), True)
     self.assertEqual(Solution().two_sum([2], 2), False)
     self.assertEqual(Solution().two_sum([2, 4, 0], 4), True)
     self.assertEqual(Solution().two_sum([2, 4, 6, -4], 0), True)
     self.assertEqual(Solution().two_sum([], 0), False)
예제 #2
0
def index():
    single_solve = request.json['single']
    board = request.json['board']

    solution = Solution(board)
    valid = solution.verify()
    if valid:
        solution_found = solution.solve()
        if solution_found:
            if single_solve:
                row_id = request.json['row_id']
                col_id = request.json['col_id']
                return jsonify({'solution': solution.board[row_id][col_id]})
            else:
                return jsonify({'solution': solution.board})
        else:
            return jsonify({'error': 'No Solutions Found'})
    else:
        return jsonify({'error': 'Invalid Puzzle'})
예제 #3
0
    def test_invert_recursively(self):
        root = Node('a')
        root.left = Node('b')
        root.right = Node('c')
        root.left.left = Node('d')
        root.left.right = Node('e')
        root.right.left = Node('f')

        self.assertEqual(root.string(), "abdecf")
        Solution().invert_recursively(root)
        self.assertEqual(root.string(), "acfbed")
예제 #4
0
    def test_solver(self):
        klass = Solution()

        self.assertEqual(
            klass.getRange([1, 3, 3, 5, 7, 8, 9, 9, 15], target=9), [6, 7])
        self.assertEqual(
            klass.getRange([1, 3, 3, 5, 7, 8, 9, 9, 15], target=7), [4, 4])
        self.assertEqual(klass.getRange([100, 150, 150, 153], target=150),
                         [1, 2])
        self.assertEqual(
            klass.getRange([100, 100, 100, 150, 150, 200], target=100), [0, 2])
        self.assertEqual(klass.getRange([100, 150, 200, 200, 200], target=200),
                         [2, 4])
        self.assertEqual(klass.getRange([1, 2, 3, 4, 5, 6, 10], target=9),
                         [-1, -1])
        self.assertEqual(klass.getRange(range(1000000), target=500),
                         [500, 500])
예제 #5
0
 def test_solver(self):
     self.assertEqual(Solution().addTwoNumbers([2,4,3  ], [5,6,4  ]), [7,0,8  ])
     self.assertEqual(Solution().addTwoNumbers([2,4,3,1], [5,6,4  ]), [7,0,8,1])
     self.assertEqual(Solution().addTwoNumbers([2,4,3  ], [5,6,4,1]), [7,0,8,1])
     self.assertEqual(Solution().addTwoNumbers([1,2,3  ], [       ]), [1,2,3  ])
     self.assertEqual(Solution().addTwoNumbers([       ], [1,2,3  ]), [1,2,3  ])
     self.assertEqual(Solution().addTwoNumbers([       ], [       ]), [       ])
예제 #6
0
def create_initial_solution(instance, path):

	oligos = instance.oligos
	result_length = instance.result_length
	oligos_len = len(oligos[0].nuc)

	overlaps_pairs = create_overlaps_hash(oligos)

	start = time()
	starting_oligo = choose_starting_oligo(oligos, overlaps_pairs)
	sequence, overlaps = make_starting_solution(starting_oligo, result_length, oligos_len, overlaps_pairs)
	stop = time()

	elapsed_time = round(stop-start,2)

	solution = Solution()
	solution.sequence = sequence
	solution.overlaps = overlaps


	log_to_file (instance, solution, overlaps, sequence, oligos_len, path, elapsed_time, "INITIAL")

	return solution
예제 #7
0
 def test_instance_a(self):
     solver = ModelTwoNumericalSolver()
     par = Parameter(MODEL_2,
                     tau=0.15,
                     a=0.0013471502590673575,
                     s=0.05,
                     cr=0.01,
                     cn=0.1,
                     delta=0.85)
     dec = solver._optimize_case_one_c(par)
     profit_man, profit_ret = solver.calc_profits(par, dec)
     sol = Solution(dec, profit_man, profit_ret, '1c')
     print(dec.qr - (par.tau / dec.rho) * dec.qn)
     print(solver._is_valid(par, sol))
     print(profit_man)
예제 #8
0
 def test_move_zeros(self):
     self.assertEqual(Solution().move_zeros([1, 0, 3, 5, 0, 12]),
                      [1, 3, 5, 12, 0, 0])
     self.assertEqual(Solution().move_zeros([3, 0, 1, 12, 0, 5]),
                      [3, 1, 12, 5, 0, 0])
     self.assertEqual(Solution().move_zeros([1, 20, 3, 5, 8, 12]),
                      [1, 20, 3, 5, 8, 12])
     self.assertEqual(Solution().move_zeros([-1, 0, 2, -3, 0, 0]),
                      [-1, 2, -3, 0, 0, 0])
     self.assertEqual(Solution().move_zeros([0, 0, 0, 0]), [0, 0, 0, 0])
     self.assertEqual(Solution().move_zeros([]), [])
예제 #9
0
    def solve(self):
        if self.solution is None:
            self.interpret()
            self.solution = Solution(self)

        return self.solution
예제 #10
0
 def test_solver_V1(self):
     self.assertEqual(Solution().sortNumsV2([3, 3, 2, 1, 3, 2, 1]),
                      [1, 1, 2, 2, 3, 3, 3])
예제 #11
0
from solver import Solution

sol = Solution()

def test_palindrome():
    assert sol.isPalindrome(121) == True

def test_not_palindrome_negative():
    assert sol.isPalindrome(-121) == False

def test_not_palindrome():
    assert sol.isPalindrome(21) == False
예제 #12
0
 def test_solver(self):
     self.assertEqual(Solution().first_missing_positive([3, 4, -1, 1]), 2)
     self.assertEqual(Solution().first_missing_positive([1, 2, 0]), 3)
     self.assertEqual(Solution().first_missing_positive([2, 1, 3]), 4)
     self.assertEqual(Solution().first_missing_positive([]), 1)
예제 #13
0
    def transform(solution):
        new_solution = Solution(solution)
        used_oligos = filter(lambda x: x.used, instance.oligos)
        unused_oligos = filter(lambda x: not x.used, instance.oligos)

        def choose_used():
            probability = 0.8 * ((float(len(used_oligos)) / len(used_oligos + unused_oligos)) ** 2)
            dice = random.random()
            return (dice < probability)

        if len(used_oligos) == 0: choose_from = unused_oligos
        elif len(unused_oligos) == 0: choose_from = used_oligos
        else: choose_from = used_oligos if choose_used() else unused_oligos
        chosen_oligo = random.choice(choose_from)

        new_oligo_pos = -1
        # print len(solution.overlaps)
        if len(solution.overlaps) > 0:
            tabu_filtered_overlaps = filter(lambda x: x not in tabu_overlaps, solution.overlaps)
            min_overlap = min(tabu_filtered_overlaps, key=lambda(o1,o2,len,pos): len)
            # Add overlap to tabu list if exceeded limit
            global revert_attempts, max_revert_attempts
            if revert_attempts >= max_revert_attempts:
                revert_attempts = 0
                tabu_overlaps.append(min_overlap)
            left_trailing = -1 * solution.overlaps[0][3]
            right_trailing = -1 * (len(instance.solution.sequence) - solution.overlaps[-1][3] - 2 * oligo_length + solution.overlaps[-1][2])
            o1, o2, overlap_len, o1_pos = min_overlap

            # print overlap_len, left_trailing, right_trailing

            while not (new_oligo_pos in range(0, len(solution.sequence) - oligo_length + 1)):
                if (overlap_len <= min(left_trailing, right_trailing)) or (left_trailing == 0 and right_trailing == 0):
                    # print "Inserting in the middle"
                    # calculate offset defined as distance from the overlap's beginning
                    offset = random.randint(0, oligo_length + o1.overlap(o2)) - oligo_length
                    new_oligo_pos = o1_pos + oligo_length - overlap_len + offset

                elif (overlap_len > left_trailing) and (overlap_len > right_trailing):
                    # print "Inserting in random trailing space"
                    # left_index = random.randint(0, -1 * left_trailing)
                    # right_index = random.randint(len(solution.sequence) - oligo_length - (-1 * right_trailing) + 1, len(solution.sequence) - oligo_length + 1)
                    left_index = 0
                    right_index = len(solution.sequence) - oligo_length
                    new_oligo_pos = random.choice([left_index, right_index])

                elif overlap_len > left_trailing:
                    # print "Inserting in left trailing space"
                    # new_oligo_pos = random.randint(0, -1 * left_trailing)
                    new_oligo_pos = 0

                elif overlap_len > right_trailing:
                    # print "Inserting in right trailing space"
                    # new_oligo_pos = random.randint(len(solution.sequence) - oligo_length - (-1 * right_trailing) + 1, len(solution.sequence) - oligo_length + 1)
                    new_oligo_pos = len(solution.sequence) - oligo_length

                else:
                    new_oligo_pos = random.randint(0, len(solution.sequence) - oligo_length)
        else:
            new_oligo_pos = random.randint(0, len(solution.sequence) - oligo_length)
        # print "Inserting %s at %d" % (chosen_oligo, new_oligo_pos)

        new_oligo_end = new_oligo_pos + (oligo_length - 1)

        # this is some info returned for reverting changes
        old_overlaps = copy(solution.overlaps)
        old_oligo_usage = { oligo: oligo.used_times for oligo in instance.oligos }

        # substitute the newly chosen oligo into the sequence
        new_solution.sequence = solution.sequence[:new_oligo_pos] + chosen_oligo.nuc + solution.sequence[new_oligo_end + 1:]
        # print new_solution.sequence

        # calculate new usage of oligos
        new_overlaps = []
        previous = None
        for oligo in instance.oligos: oligo.used_times = 0
        for i in range(len(new_solution.sequence) - oligo_length + 1):
            nuc = new_solution.sequence[i:i+oligo_length]
            if instance.oligos_dict.has_key(nuc):
                oligo = instance.oligos_dict[nuc]
                oligo.used_times += 1
                if previous == None: # next iteration if it's the first found oligo
                    previous = (oligo, i)
                    continue
                else:
                    prev_oligo, prev_pos = previous
                    overlap_len = oligo_length - i + prev_pos
                    new_overlaps.append((prev_oligo, oligo, overlap_len, prev_pos))
                    previous = (oligo, i)

        x = len(filter(lambda o: o.used, instance.oligos))
        # print x, "used out of", len(instance.oligos)
        if x > len(instance.oligos):
            raw_input()

        new_solution.overlaps = new_overlaps

        return new_solution, old_overlaps, old_oligo_usage

        pass
예제 #14
0
 def test_solver(self):
     self.assertEqual(
         Solution().lengthOfLongestSubstring("abrkaabcdefghijjxxx"), 10)
     self.assertEqual(Solution().lengthOfLongestSubstring("abcdefghij"), 10)
     self.assertEqual(Solution().lengthOfLongestSubstring("aaaaaaaaaa"), 1)
     self.assertEqual(Solution().lengthOfLongestSubstring(""), 0)
예제 #15
0
 def test_solver(self):
     self.assertEqual(Solution().longestPalindrome("banana"), "anana")
     self.assertEqual(Solution().longestPalindrome("million"), "illi")
     self.assertEqual(Solution().longestPalindrome("arara"), "arara")
     self.assertEqual(Solution().longestPalindrome("abcxyz"), "a")
     self.assertEqual(Solution().longestPalindrome(""), "")