Beispiel #1
0
def bst_sequences(root):
    """
    This is a Python implementation of Gayle's solution from CtCI.
    I struggled with this problem so I just want to move past it.
    """
    result = LinkedList()

    if root is None:
        result.insert(LinkedList())
        return result

    prefix = LinkedList()
    prefix.add(root.data)

    #Recurse on left and right subtrees
    left_sequences = bst_sequences(root.left)
    right_sequences = bst_sequences(root.right)

    #Weave together each list from the left and right sides.
    while not left_sequences.is_empty():
        left = left_sequences.get_first().data
        while not right_sequences.is_empty():
            right = right_sequences.get_first().data
            weaved = LinkedList()
            weave_lists(left, right, weaved, prefix)
            result.add_all(weaved)

    return result
Beispiel #2
0
def main():
    n = -1
    while n < 0:
        try:
            n = int(input('Введите n: '))
            if n < 0:
                raise ValueError
        except ValueError:
            print('Нужно вводить целое положительное число!')
    if n < 2:
        print('Недостаточно значений для расчёта.')
        return
    l = LL()
    result = 1

    for i in range(1, n + 1):
        f = None
        while f is None:
            try:
                f = float(input(f'Введите x{i}: '))
            except ValueError:
                print('Нужно вводить число!')
        l.add(f)
    ch = l.first.next
    ct = l.last
    while ch.next is not None:
        result *= ch.prev.value + ch.value + 2 * ct.value
        ch = ch.next
        ct = ct.prev

    print(f'Резульат: {result}')
Beispiel #3
0
def main():
    n = -1
    while n < 0:
        try:
            n = int(input('Введите n: '))
            if n < 0:
                raise ValueError
        except ValueError:
            print('Нужно вводить целое положительное число!')
    l = LL()
    result = 0

    for i in range(1, n + 1):
        f = None
        while f is None:
            try:
                f = float(input(f'Введите x{i}: '))
            except ValueError:
                print('Нужно вводить число!')
        l.add(f)
    ch = l.first
    while ch.next is not None:
        result += sqrt(10 + ch.value**2)
        ch = ch.next

    print(f'Резульат: {result}')
Beispiel #4
0
def main():
    n = -1
    while n < 0:
        try:
            n = int(input('Введите n: '))
            if n < 0:
                raise ValueError
        except ValueError:
            print('Нужно вводить целое положительное число!')

    l = LinkedList()
    fac_sum = 0
    cur_fac = 1
    for i in range(1, n + 1):
        cur_fac *= i
        fac_sum += 1 / cur_fac
        l.add(i * fac_sum)

    for i in l.list():
        print('%.2f' % i)