Question

Use python to write a function that will generate the ith row of pascal’s triangle (using...

Use python to write a function that will generate the ith row of pascal’s triangle (using recursion)

The method signature should be pascal(row). It should return a list of elements that would be in that row of Pascal’s Triangle. For example the 0th row is [1] and the 2nd row is [1,2,1]

Homework Answers

Answer #1

Solution:

Recursive function to generate ith row of pascal's triangle:

#Function to return nth pascal row
def pascal(row):

    pascalrow=[]
    #First element in row is always 1
    pascalrow.append(1);

    #If the row is 0
    if(row==0):
        return pascalrow

    #Generating previous row
    previousrow=pascal(row-1)


    for i in range(1,len(previousrow)):
        #Generating urrent row by using previous row
        current=previousrow[i-1]+previousrow[i];
        pascalrow.append(current)

    #last element is always 1
    pascalrow.append(1)

    #returning the nth pascalrow
    return pascalrow


Output by calling function in python command shell:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(USING ML) (Using ML) Write a function dupList of type 'a list -> 'a list whose...
(USING ML) (Using ML) Write a function dupList of type 'a list -> 'a list whose output list is the same as the input list but with each element of the input list repeated twice in a row. For example, if the input is [1, 2, 3], the output list should produce [1, 1, 2, 2, 3, 3]. If the input list [], the output list should be []. Do not use explicit recursion but use one of the fold...
IN SML Write a function dupList of type 'a list -> 'a list whose output list...
IN SML Write a function dupList of type 'a list -> 'a list whose output list is the same as the input list but with each element of the input list repeated twice in a row. For example, if the input is [1, 2, 3], the output list should produce [1, 1, 2, 2, 3, 3]. If the input list [], the output list should be []. Do not use explicit recursion but use one of the fold functions. Do...
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum...
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum number in the list. Do NOT use Python’s built-in function max. Example: result = myMax([-999000, -1000000, -2000000]); print(result) #output is -999000 Example: result = myMax([1000000, 1000001, 1000002]); print(result) #output is 1000002
Python: Working with CSV file --> How would I write a function that will return a...
Python: Working with CSV file --> How would I write a function that will return a list of lists, with the frequency and the zip code. Organize the list in decreasing order of frequency (Print the number of suppliers and the zip code for the 10 most common zip codes in the file. ) An example of three items from the 720 zip codes results in: [ ... [9, '65616'], [8, '94573'], [8, '63103'] ...]. This tells us that Branson,...
Python Question: Write a function which returns the sum of squares of the integers 1 to...
Python Question: Write a function which returns the sum of squares of the integers 1 to n. For example, the sum of the squares from 1 to 4 is 1 + 4 + 9 + 16, or 30. You may choose what should be returned if n == 0 You may not use the built-in Python sum function. The Solution Must be recursive >>> sum_of_squares(1) 1 >>> sum_of_squares(4) 30 >>> sum_of_squares(8) # 64 + 49 + 36 + ... +...
using dr.racket programing language If we write a function that tests whether a list contains only...
using dr.racket programing language If we write a function that tests whether a list contains only strings, odd numbers, or even numbers, you will notice that the code that iterates through the list stays the same, with the only change being the predicate function that checks for the desired list element. If we were to write a new function for each of the tests listed above, it would be more error-prone and an example of bad abstraction. We could write...
Write a Python function which receives 3 lists as its input parameters and combines the lists...
Write a Python function which receives 3 lists as its input parameters and combines the lists and remove repeated numbers from the combined list and return the combined list. For instance, if the input is [1,2,3,4,2,3] and [3,4,6,7] and [-1,0,23,4] the result is [1,2,3,4,6,7,-1,0,23] - Note, the order the lists are combined together does not matter. Use main function.
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
Write a Python program using that takes a number as input and then prints if the...
Write a Python program using that takes a number as input and then prints if the number is even or odd. The program should use a function isEven that takes a number as a parameter and returns a logical value true if the number is even, false otherwise.
[Python] Write a function named "total_population" that takes a string then a list as parameters where...
[Python] Write a function named "total_population" that takes a string then a list as parameters where the string represents the name of a CSV file containing city data in the format "CountryCode,CityName,Region,Population,Latitude,Longitude" and the second parameter is a list where each element is itself a list containing 3 strings as elements representing the CountryCode, CityName, and Region in this order. Return the total population of all cities in the list. Note that the city must match the country, name, and...