Рекурсия Python в треугольнике паскаля [дубликат]

mysqli_query() возвращает ресурс объекта в вашу переменную $result, а не строку.

Вам нужно закодировать его, а затем получить доступ к записям. Вы просто не можете напрямую использовать его как свою переменную $result.

Код ...

while ($row = $result->fetch_assoc()) {
    echo $row['classtype']."<br>";
}
11
задан louie mcconnell 7 June 2014 в 05:24
поделиться

9 ответов

Я знаю, что вы хотите реализовать себя, но лучший способ объяснить мне - пройти через реализацию. Вот как я это сделаю, и эта реализация основывается на моем довольно полном знании того, как работают функции Python, поэтому вы, вероятно, не захотите использовать этот код самостоятельно, но он может заставить вас указать в правильном направлении.

def pascals_triangle(n_rows):
    results = [] # a container to collect the rows
    for _ in range(n_rows): 
        row = [1] # a starter 1 in the row
        if results: # then we're in the second row or beyond
            last_row = results[-1] # reference the previous row
            # this is the complicated part, it relies on the fact that zip
            # stops at the shortest iterable, so for the second row, we have
            # nothing in this list comprension, but the third row sums 1 and 1
            # and the fourth row sums in pairs. It's a sliding window.
            row.extend([sum(pair) for pair in zip(last_row, last_row[1:])])
            # finally append the final 1 to the outside
            row.append(1)
        results.append(row) # add the row to the results.
    return results

использование:

>>> for i in pascals_triangle(6):
...     print(i)
... 
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
9
ответ дан Aaron Hall 24 August 2018 в 06:28
поделиться

Я обманываю из популярного решения fibonacci sequence . Для меня реализация треугольника Паскаля будет иметь одинаковую концепцию фибоначчи. В fibonacci мы используем одно число за раз и добавляем его к предыдущему. В треугольнике паскаля используйте строку за раз и добавьте ее к предыдущей.

Вот полный пример кода:

>>> def pascal(n):
...     r1, r2 = [1], [1, 1]
...     degree = 1
...     while degree <= n:
...         print(r1)
...         r1, r2 = r2, [1] + [sum(pair) for pair in zip(r2, r2[1:]) ] + [1]
...         degree += 1

Тест

>>> pascal(3)
[1]
[1, 1]
[1, 2, 1]
>>> pascal(4)
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
>>> pascal(6)
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]

Примечание : чтобы получить результат как генератор, измените print(r1) на yield r1.

0
ответ дан Community 24 August 2018 в 06:28
поделиться

Вот элегантное и эффективное рекурсивное решение. Я использую очень удобную библиотеку toolz .

from toolz import memoize, sliding_window

@memoize
def pascals_triangle(n):
    """Returns the n'th row of Pascal's triangle."""
    if n == 0:
        return [1]
    prev_row = pascals_triangle(n-1)
    return [1, *map(sum, sliding_window(2, prev_row)), 1]

pascals_triangle(300) занимает около 15 мс на macbook pro (2,9 ГГц Intel Core i5). Обратите внимание, что вы не можете идти намного выше, не увеличивая предел глубины рекурсии по умолчанию.

1
ответ дан fredcallaway 24 August 2018 в 06:28
поделиться

Вот моя попытка:

def generate_pascal_triangle(rows):
    if rows == 1: return [[1]]

    triangle = [[1], [1, 1]] # pre-populate with the first two rows

    row = [1, 1] # Starts with the second row and calculate the next

    for i in range(2, rows):
        row = [1] + [sum(column) for column in zip(row[1:], row)] + [1]
        triangle.append(row)

    return triangle

for row in generate_pascal_triangle(6):
    print row

Обсуждение

  • Первые две строки треугольника жестко закодированы
  • zip() в основном пары двух соседних чисел вместе
  • Нам еще нужно добавить 1 к началу и еще один к концу, потому что вызов zip() порождает только среднюю часть следующей строки
2
ответ дан Hai Vu 24 August 2018 в 06:28
поделиться
def pascal(n):
    if n==0:
        return [1]
    else:
        N = pascal(n-1)
        return [1] + [N[i] + N[i+1] for i in range(n-1)] + [1]


def pascal_triangle(n):
    for i in range(n):
        print pascal(i)
0
ответ дан Luis Miguel 24 August 2018 в 06:28
поделиться

Без использования zip, но с использованием генератора:

def gen(n,r=[]):
    for x in range(n):
        l = len(r)
        r = [1 if i == 0 or i == l else r[i-1]+r[i] for i in range(l+1)]
        yield r

example:

print(list(gen(15)))

output:

[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1], [1, 6, 15, 20, 15, 6, 1], [1, 7, 21, 35, 35, 21, 7, 1], [1, 8, 28, 56, 70, 56, 28, 8, 1], [1, 9, 36, 84, 126, 126, 84, 36, 9, 1], [1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1], [1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1], [1, 12, 66, 220, 495, 792, 924, 792, 495, 220, 66, 12, 1], [1, 13, 78, 286, 715, 1287, 1716, 1716, 1287, 715, 286, 78, 13, 1], [1, 14, 91, 364, 1001, 2002, 3003, 3432, 3003, 2002, 1001, 364, 91, 14, 1]]

DISPLAY AS TRIANGLE

Чтобы нарисовать его в красивом треугольнике (работает только для n & lt; 7, кроме того, что он получает distroted. ref draw_beautiful для n> 7)

для n & lt; lt ; 7

def draw(n):
    for p in gen(n):
        print(' '.join(map(str,p)).center(n*2)+'\n')

, например:

draw(10)

Выход

:

      1       

     1 1      

    1 2 1     

   1 3 3 1    

  1 4 6 4 1   

1 5 10 10 5 1   

для любого размера

, так как нам нужно знать максимальную ширину, мы не можем использовать генератор

def draw_beautiful(n):
    ps = list(gen(n))
    max = len(' '.join(map(str,ps[-1])))
    for p in ps:
        print(' '.join(map(str,p)).center(max)+'\n')

пример (2): работает для любого числа:

draw_beautiful(100)

2
ответ дан MortalViews 24 August 2018 в 06:28
поделиться
# call the function ! Indent properly , everything should be inside the function
def triangle():

          matrix=[[0 for i in range(0,20)]for e in range(0,10)]         # This method assigns 0's to all Rows and Columns , the range is mentioned
          div=20/2           # it give us the most middle columns 
          matrix[0][div]=1        # assigning 1 to the middle of first row 
          for i in range(1,len(matrix)-1): # it goes column by column
               for j in range(1,20-1):  #  this loop goes row by row
                   matrix[i][j]=matrix[i-1][j-1]+matrix[i-1][j+1]               # this is the formula , first element of the matrix gets , addition of i index (which is 0 at first ) with third value on the the related row
    # replacing 0s with spaces :) 
          for i in range(0,len(matrix)):
              for j in range(0,20):
                   if matrix[i][j]==0:       #  Replacing 0's with spaces
                        matrix[i][j]=" "

          for i in range(0,len(matrix)-1):           # using spaces , the triangle will printed beautifully 
                for j in range(0,20):
                    print 1*" ",matrix[i][j],1*" ", # giving some spaces in two sides of the printing numbers
triangle() # calling the function

напечатает что-то вроде этого

                       1
                1              1
           1           2            1
      1         3            3            1
  1        4        6            4               1
0
ответ дан Niamatullah Bakhshi 24 August 2018 в 06:28
поделиться
# combining the insights from Aaron Hall and Hai Vu,
# we get:

def pastri(n):
    rows = [[1]]
    for _ in range(1, n+1):
        rows.append([1] +
                    [sum(pair) for pair in zip(rows[-1], rows[-1][1:])] +
                    [1])
    return rows

# thanks! learnt that "shape shifting" data,
# can yield/generate elegant solutions.
0
ответ дан ssgam 24 August 2018 в 06:28
поделиться

Начинающий студент Python. Вот моя попытка, очень буквальный подход, использование двух циклов For:

pascal = [[1]]
num = int(input("Number of iterations: "))
print(pascal[0]) # the very first row
for i in range(1,num+1):
    pascal.append([1]) # start off with 1
    for j in range(len(pascal[i-1])-1):
    # the number of times we need to run this loop is (# of elements in the row above)-1
        pascal[i].append(pascal[i-1][j]+pascal[i-1][j+1])
        # add two adjacent numbers of the row above together
    pascal[i].append(1) # and cap it with 1
    print(pascal[i])
0
ответ дан user3932000 24 August 2018 в 06:28
поделиться
Другие вопросы по тегам:

Похожие вопросы: