Example #1
0
def test_encode_bytearray():
    helpers.test({
        'type': parser.BINARY_EVENT,
        'data': helpers.gen_bytearray(),
        'id': 23,
        'nsp': '/cool'
    })
Example #2
0
def main(arguments):
    """
    Main function to run the stock market tester
    """
    if arguments.download_data:
        download_all_data()
        print("Downloaded Data!")
        # clean_data()
        # print("Cleaned Data")
        return
    if arguments.download_stock is not None:
        print(arguments.download_stock)
        install_data(arguments.download_stock)
        return
    
    DAYS_OUT = 5
    covid_data = get_covid_data()
    model = Historic()
    train_data, test_data = get_all_stocks(covid_data)
    losses = []
    for i in range(0, model.num_epochs):
        train_loss = train(model, train_data, DAYS_OUT)
        print("EPOCH {} training loss: {}".format(i, train_loss))

        test_loss = test(model, test_data, DAYS_OUT)
        print("EPOCH {} Test loss: {}".format(i, test_loss))

        losses.append(test_loss)
    visualize_loss(losses)
    print("Last 10 Epochs Average MAPE: {}".format(sum(losses[-10:]) / 10))

    sp_data = pd.read_csv("../data/^GSPC.csv")
    sp_data = join(sp_data, covid_data)
    sp_data = normalize(sp_data)
    base_data = sp_data.iloc[:-DAYS_OUT]
    base_data = tf.convert_to_tensor(base_data)
    base_data = tf.expand_dims(base_data, 0)
    labels = sp_data.iloc[DAYS_OUT:]
    labels = labels["Adjusted Close"]
    labels = labels.values.tolist()
    # print(labels)
    predictions = predict(model, base_data)
    # print(len(predictions))
    visualize_predictions(predictions, labels)
Example #3
0
    count = 0

    for n in range(upper_bound):
        if is_lychrel(n):
            print n
            count += 1

    return count

def is_lychrel(n):
    for i in range(1, 50):

        #if n % 10 == 0:
        #    return False

        n += reverse_number(n)

        if is_palindrome(n):
            return False

    return True


test(reverse_number(4321), 1234)
test(is_lychrel(47), False)
test(is_lychrel(349), False)
test(is_lychrel(196), True)
test(is_lychrel(10677), True)

print find_lychrel_numbers(10000)
Example #4
0
#create criterions
CE = Criterion.CrossEntropy()
MSE = Criterion.MSE()
CE_nn = nn.CrossEntropyLoss()
MSE_nn = nn.MSELoss()

#actual comparison
helpers.test(model_relu,
             model_relu_nn,
             opti,
             opti_nn,
             CE,
             CE_nn,
             "relu CE",
             "relu nn CE",
             repetitions=rep,
             message="Relu vs Relu nn with crossentropy",
             plots=plots,
             show_plots=show_plots,
             chrono=True,
             training2=train_ref,
             title_plots="comparison_CE_",
             save_result_csv=save_csv,
             filename="../results/csv/CE_comparison.csv")

helpers.test(model_relu,
             model_relu_nn,
             opti_mom,
             opti_mom_nn,
             CE,
             CE_nn,
Example #5
0
from math import factorial
from helpers import test

def ways(n, r):
    diff = n - r
    cancel = max(diff, r)

    numerator = reduce(lambda a, b: a*b, [1] + range(cancel + 1, n + 1))
    denominator = factorial(min(diff, r))

    return numerator / denominator

def ways_greater_than(value, n_upper_bound):
    ways_greater = 0

    for n in range(1, n_upper_bound + 1):
        for r in range(1, n + 1):
            if ways(n, r) > value:
                ways_greater += 1

    return ways_greater

test(ways(23, 10), 1144066)
test(ways(10, 10), 1)

print ways_greater_than(1000000, 100)
Example #6
0

def sum_pandigital():
    # Don't double count any products
    products = set([])

    # 4-digit and 1-digit
    for m1 in range(10**3, 10**4):
        for m2 in range(10**0, 10**1):
            p = m1 * m2
            if is_expression_pandigital(m1, m2, p):
                products.add(p)
                print "%s * %s = %s" % (m1, m2, p)

    # 3-digit and 2-digit
    for m1 in range(10**2, 10**3):
        for m2 in range(10**1, 10**2):
            p = m1 * m2
            if is_expression_pandigital(m1, m2, p):
                products.add(p)
                print "%s * %s = %s" % (m1, m2, p)

    return sum(products)


test(is_expression_pandigital(39, 186, 7254), True)
test(is_expression_pandigital(33, 1216, 7254), False)
test(is_expression_pandigital(39, 186, 725), False)
test(is_expression_pandigital(1209, 7, 8463), False)
print sum_pandigital()
Example #7
0
from typing import Callable

from helpers import test


def sum_digits(number: int) -> int:
    sum = 0
    for index, character in enumerate(reversed(list(str(number)))):
        sum += int(character)
    return sum


if __name__ == '__main__':
    test(sum_digits, 10, 1234)
Example #8
0
from helpers import test, is_palindrome


def is_dual_palindrome(n):
    return is_palindrome(n) and is_palindrome(bin(n).lstrip('0b'))


def find_dual_palindromes(n):
    return [i for i in range(n) if is_dual_palindrome(i)]


test(is_dual_palindrome(585), True)
test(is_dual_palindrome(9), True)
test(is_dual_palindrome(22), False)
test(is_dual_palindrome(16), False)

print sum(find_dual_palindromes(10**6))
Example #9
0
def find_consecutive_prime_sum(upper_bound):
    primes = primes_up_to(upper_bound)
    sorted_primes = sorted(list(primes))

    longest_sequence = []
    for i in range(len(sorted_primes)):
        sequence = []

        k = 0
        while (True):
            if i + k >= len(sorted_primes):
                break

            sequence.append(sorted_primes[i + k])

            if sum(sequence) > upper_bound:
                break

            if sum(sequence) in primes:
                if len(sequence) > len(longest_sequence):
                    longest_sequence = copy(sequence)

            k += 1
    return longest_sequence


test(sum(find_consecutive_prime_sum(100)), 41)
test(sum(find_consecutive_prime_sum(1000)), 953)

print sum(find_consecutive_prime_sum(1000000))
Example #10
0
from helpers import test


def pairwise_digits(num1: int, num2: int) -> str:
    num1_s = str(num1)
    num2_s = str(num2)
    iterations = min(len(num1_s), len(num2_s))
    result = ''
    for i in range(iterations):
        result += '1' if num1_s[i] == num2_s[i] else '0'
    result += '0' * (abs(len(num1_s) - len(num2_s)))
    return result


if __name__ == '__main__':
    test(pairwise_digits, '0011', 1234, 2134)
    test(pairwise_digits, '10010', 1213, 10435)
    test(pairwise_digits, '1110', 1213, 121)
Example #11
0
 def get(self):
     helpers.test()
     self.render("signup.html")
Example #12
0
def test_tie(winner, loser):
    winner_hand = Hand([Card.parse_card(card) for card in winner])
    loser_hand = Hand([Card.parse_card(card) for card in loser])

    test(winner_hand == loser_hand, True)
Example #13
0
def test_hand(cards, rank, rank_values):
    hand = Hand([Card.parse_card(card) for card in cards])
    test(hand.rank, rank)
    test(hand.rank_values, rank_values)
Example #14
0
            distinct.setdefault(card.value, 0)
            distinct[card.value] += 1

        return distinct

    def __lt__(self, other):
        return self.rank < other.rank \
                or (self.rank == other.rank and \
                    self.rank_values < other.rank_values)

    def __eq__(self, other):
        return self.rank == other.rank \
                and self.rank_values == other.rank_values


test(Card.parse_card('6D').suit, Card.DIAMOND)
test(Card.parse_card('6D').value, 6)
test(Card.parse_card('KD').value, 13)


def test_hand(cards, rank, rank_values):
    hand = Hand([Card.parse_card(card) for card in cards])
    test(hand.rank, rank)
    test(hand.rank_values, rank_values)


def test_win(winner, loser):
    winner_hand = Hand([Card.parse_card(card) for card in winner])
    loser_hand = Hand([Card.parse_card(card) for card in loser])

    test(winner_hand > loser_hand, True)
Example #15
0
def consecutive_digits(n):
    digits = []

    i = 1

    while (True):
        for d in str(i):
            digits.append(int(d))

        if len(digits) >= n:
            return digits

        i += 1


def evaluate_consecutive_expression():
    digits = consecutive_digits(1000000)

    expression = 1

    for p in range(7):
        expression *= digits[(10**p) - 1]

    return expression


test(consecutive_digits(100)[11], 1)
test(consecutive_digits(100)[18], 4)

print evaluate_consecutive_expression()
Example #16
0
from helpers import test, hexagonal_number, is_pentagonal_number, is_hexagonal_number


def find_pentagonal_and_hexagonal(lower_bound):
    n = lower_bound + 1

    while (True):
        hn = hexagonal_number(n)

        if is_pentagonal_number(hn):
            return hn

        n += 1


test(hexagonal_number(1), 1)
test(hexagonal_number(2), 6)
test(hexagonal_number(3), 15)

test(is_pentagonal_number(22), True)
test(is_pentagonal_number(24), False)
test(is_hexagonal_number(45), True)
test(is_hexagonal_number(46), False)

test(find_pentagonal_and_hexagonal(1), 40755)
print find_pentagonal_and_hexagonal(143)
Example #17
0
    n = str(n)

    for i in range(1, 8):
        sub_string = int(n[i:i + 3])

        if sub_string % PRIMES[i - 1] != 0:
            return False

    return True


def find_sub_string_divisibility():
    digits = [str(d) for d in range(10)]
    numbers = []

    for arrangement in permutations(digits):
        if arrangement[0] == 0:
            continue

        n = int(''.join(arrangement))

        if check_sub_string_divisibility(n):
            numbers.append(n)

    return numbers


test(check_sub_string_divisibility(1406357289), True)
test(check_sub_string_divisibility(1046357289), False)
print sum(find_sub_string_divisibility())
Example #18
0
from helpers import test


def same_digits(n, m):
    return sorted(str(n)) == sorted(str(m))


def find_same_digit_product(multiplications):
    x = 1

    while (True):
        if all(same_digits(x, x * m) for m in range(2, multiplications + 1)):
            return x

        x += 1


test(same_digits(125874, 251748), True)
test(same_digits(425874, 257748), False)
test(find_same_digit_product(2), 125874)

print find_same_digit_product(6)
Example #19
0
 def get(self):
     helpers.test()  
     self.render("signup.html")
Example #20
0
    while(True):
        digits = digits[1:] + digits[0]

        if digits == str(p):
            return True

        if int(digits) not in primes:
            return False

def count_circular_primes(n):
    circular_primes = []
    primes = primes_up_to(n)

    for prime in primes:
        if is_circular_prime(prime, primes):
            circular_primes.append(prime)

    return len(circular_primes)


primes = primes_up_to(1000)

test(is_circular_prime(2, primes), True)
test(is_circular_prime(197, primes), True)
test(is_circular_prime(43, primes), False)

test(count_circular_primes(100), 13)

print count_circular_primes(10**6)