Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON Problem using a recursive method Pascal's Triangle Pascal's triangle is a triangular arrangement of numbers where the top row contains the single number 1,

PYTHON Problem using a recursive method

Pascal's Triangle

Pascal's triangle is a triangular arrangement of numbers where the top row contains the single number 1, and the values in each following (centered) row are the sum of the value(s) in the row above. The following first five rows of Pascal's triangle should help demonstrate the idea:

 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 

By convention, the rows and columns of Pascal's triangle are numbered starting from 0 note that the 0th column of every row contains the value 1. To aid in the computation of edge cases (columns in rows that do not have two values above them), it is also convenient to imagine that there are columns in row 0 extending off in both directions that contain 0s. I.e., we might envision the first row of Pascal's triangle as follows:

... 0 0 0 1 0 0 0 ... 1 1 1 2 1 1 3 3 1 1 4 6 4 1 

Wolfram Mathworld has a good writeup on the properties and provenance of Pascal's Triangle.

Complete the following function, which returns the value to be found in a given row and column of Pascal's triangle.

In [ ]:

def pascal(row, column):
 # YOUR CODE HERE
 raise NotImplementedError()

In [ ]:

# generate the first 10 rows of Pascal's Triangle
for row in range(10):
 print('{: ^45}'.format(' '.join(str(pascal(row, col)) for col in range(row+1))))

In [ ]:

# (5 points)
?
from unittest import TestCase
?
tc = TestCase()
?
tc.assertEqual(pascal(0, 0), 1)
tc.assertEqual(pascal(1, 0), 1)
tc.assertEqual(pascal(2, 1), 2)
tc.assertEqual(pascal(5, 1), 5)
tc.assertEqual(pascal(5, 2), 10)
tc.assertEqual(pascal(10, 5), 252)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learn Mysql The Easy Way A Beginner Friendly Guide

Authors: Kiet Huynh

1st Edition

B0CNY7143T, 979-8869761545

More Books

Students also viewed these Databases questions

Question

Why could the Robert Bosch approach make sense to the company?

Answered: 1 week ago