text: Trinomial Triangle | Function to find the trinomial triangle value . ; Using property of trinomial triangle . ; If value already calculated , return that . ; base case ; base case ; recursive step and storing the value . ; Function to print Trinomial Triangle of height n . ; printing n rows . ; printing first half of triangle ; printing second half of triangle . ; Driven Program
code: def TrinomialValue ( dp , n , k ) : NEW_LINE INDENT if k < 0 : NEW_LINE INDENT k = - k NEW_LINE DEDENT if dp [ n ] [ k ] != 0 : NEW_LINE INDENT return dp [ n ] [ k ] NEW_LINE DEDENT if n == 0 and k == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT if k < - n or k > n : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( TrinomialValue ( dp , n - 1 , k - 1 ) + TrinomialValue ( dp , n - 1 , k ) + TrinomialValue ( dp , n - 1 , k + 1 ) ) NEW_LINE DEDENT def printTrinomial ( n ) : NEW_LINE INDENT dp = [ [ 0 ] * 10 ] * 10 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( - i , 1 ) : NEW_LINE INDENT print ( TrinomialValue ( dp , i , j ) , end = " ▁ " ) NEW_LINE DEDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT print ( TrinomialValue ( dp , i , j ) , end = " ▁ " ) NEW_LINE DEDENT print ( " " , end = ' ' ) NEW_LINE DEDENT DEDENT n = 4 NEW_LINE printTrinomial ( n ) NEW_LINE