How to use algorithms - 10 common examples

To help you get started, we’ve selected a few algorithms examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github keon / algorithms / algorithms / linkedlist / add_two_numbers.py View on Github external
def convert_to_list(number: int) -> Node:
    """
        converts a positive integer into a (reversed) linked list.
        for example: give 112
        result 2 -> 1 -> 1
    """
    if number >= 0:
        head = Node(0)
        current = head
        remainder = number % 10
        quotient = number // 10

        while quotient != 0:
            current.next = Node(remainder)
            current = current.next
            remainder = quotient % 10
            quotient //= 10
        current.next = Node(remainder)
        return head.next
    else:
        print("number must be positive!")
github keon / algorithms / algorithms / linkedlist / add_two_numbers.py View on Github external
def add_two_numbers(left: Node, right: Node) -> Node:
    head = Node(0)
    current = head
    sum = 0
    while left or right:
        print("adding: ", left.val, right.val)
        sum //= 10
        if left:
            sum += left.val
            left = left.next
        if right:
            sum += right.val
            right = right.next
        current.next = Node(sum % 10)
        current = current.next
    if sum // 10 == 1:
        current.next = Node(1)
    return head.next
github keon / algorithms / algorithms / linkedlist / add_two_numbers.py View on Github external
def add_two_numbers(left: Node, right: Node) -> Node:
    head = Node(0)
    current = head
    sum = 0
    while left or right:
        print("adding: ", left.val, right.val)
        sum //= 10
        if left:
            sum += left.val
            left = left.next
        if right:
            sum += right.val
            right = right.next
        current.next = Node(sum % 10)
        current = current.next
    if sum // 10 == 1:
        current.next = Node(1)
    return head.next
github JanCVanB / netflix / tests / test_rbm.py View on Github external
def test_rbm_set_train_points_sets_same_train_points_passed_to_it():
    train_points = make_simple_train_points()
    model = algorithms.rbm.RBM()
    model.set_train_points(train_points)
    numpy.testing.assert_array_equal(model.train_points, train_points)
github JanCVanB / netflix / tests / test_svd.py View on Github external
def test_svd_update_all_features_updates_each_feature_once_in_any_order():
    model = svd.SVD()
    model.update_feature = MockThatTracksCallsWithoutRunning()
    model.update_all_features()
    assert model.update_feature.call_count == model.num_features
    expected_calls = [call(feature) for feature in range(model.num_features)]
    model.update_feature.assert_has_calls(expected_calls, any_order=True)
github JanCVanB / netflix / tests / test_svd.py View on Github external
def test_svd_update_user_and_movie_modifies_matrices_as_expected():
    from utils.data_io import get_user_movie_time_rating
    model = svd.SVD()
    initialize_model_with_simple_train_points_but_do_not_train(model)
    for train_point in model.train_points:
        user, movie, _, rating = get_user_movie_time_rating(train_point)
        for feature in range(model.num_features):
            error = model.calculate_prediction_error(user, movie, rating)
            expected_users = np.copy(model.users)
            expected_movies = np.copy(model.movies)
            expected_user_change = (model.learn_rate * error *
                                    model.movies[movie, feature])
            expected_movie_change = (model.learn_rate * error *
                                     model.users[user, feature])
            expected_users[user, feature] += expected_user_change
            expected_movies[movie, feature] += expected_movie_change
            model.update_user_and_movie(user, movie, feature, error)
            actual_users = model.users
            actual_movies = model.movies
github JanCVanB / netflix / tests / test_svd.py View on Github external
def test_svd_init_sets_default_learn_rate():
    default_learn_rate = 0.001
    model = svd.SVD()
    assert model.learn_rate == default_learn_rate
github JanCVanB / netflix / tests / test_svd.py View on Github external
def test_svd_train_sets_train_points_and_stats():
    model = svd.SVD()
    simple_train_points = make_simple_train_points()
    simple_stats = make_simple_stats()
    model.initialize_users_and_movies = MockThatAvoidsErrors()
    model.update_all_features = MockThatAvoidsLongRunTime()
    model.set_train_points = MockThatTracksCallsWithoutRunning()
    model.set_stats = MockThatTracksCallsWithoutRunning()
    model.train(simple_train_points, stats=simple_stats)
    assert model.set_train_points.call_count == 1
    assert model.set_stats.call_count == 1
github JanCVanB / netflix / tests / test_svd.py View on Github external
def test_svd_train_more_sets_train_points_when_train_points_passed():
    model = svd.SVD()
    simple_train_points = make_simple_train_points()
    model.initialize_users_and_movies = MockThatAvoidsErrors()
    model.update_all_features = MockThatAvoidsLongRunTime()
    model.set_train_points = MockThatTracksCallsWithoutRunning()
    model.train_more(simple_train_points)
    assert model.set_train_points.call_count == 1
github JanCVanB / netflix / tests / test_svd.py View on Github external
def test_svd_init_sets_default_feature_initial_value_for_default_number():
    from utils.constants import SVD_FEATURE_VALUE_INITIAL
    default_num_features = 3
    expected_feature_initial = SVD_FEATURE_VALUE_INITIAL
    model = svd.SVD()
    actual_feature_initial = model.feature_initial
    assert float(actual_feature_initial) == expected_feature_initial