Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Computer Programming in Python questions?
I saw a similar question, and I am in the same boat. I am an actuarial science major, but I am forced to take the foreign language known as computer science. I actually find it pretty interesting, and I feel like I get it until I get these curve balls.
So, I have 2 questions about how to write 2 programs:
Question 1:
Define the function pascnext(L)
Function pascnext(L) returns a list made from L:
1. L is a list of integers
2. L is supposed to be a row from Pascal's Triangle
3. The result should be a list of integers.
4. The result should be the "next" row in Pascal's Triangle
Hints:
* Look up "Pascal's Triangle" on the web to see the
pattern from one row to the next.
* If L equals [], or L equals [1], then these are
special cases (use an if statement to test for these
cases an return the proper answer); see unit tests
below for expected responses.
>>> pascnext([])
[1]
>>> pascnext([1])
[1, 1]
>>> pascnext([1,1])
[1, 2, 1]
>>> pascnext([1,6,15,20,15,6,1])
[1, 7, 21, 35, 35, 21, 7, 1]
Question 2:
Define a function pascrow(n)
Function pascrow(n) returns a list:
1. n is the "row number" for Pascal's Triangle
2. Row 0 is the top of the triangle, which
is represented as [1].
Requirement:
* Even though there is a mathematical formula
to generate row k of Pascal's triangle, you
MUST use pascnext to compute the answer for
this problem.
Hint:
* in your function, put the line
from pascnext import pascnext
This will allow you to use pascnext(f)
for some f in your definition of pascrow.
>>> pascrow(0)
[1]
>>> pascrow(1)
[1, 1]
>>> pascrow(5)
[1, 5, 10, 10, 5, 1]
>>> pascrow(12)
[1, 12, 66, 220, 495, 792, 924, 792, 495, 220, 66, 12, 1]
1 Answer
- 1 decade agoFavorite Answer
Well, I shouldn't do the assignment for you, but I'll give you some guidance:
1) create a new list, which I'll call "ret". This is what you will return.
Iterate over the members of L. Have the body of the loop add the current member of L to the previous member of L, and append the result to ret. It is a special case if you are on the first member of L, in which case you must simply append "1" to L. Also, after the iteration is over, you'll need one more "1" appended to L.
return ret
2) The key to this is recursion. Something like this:
def pascrow(n):
return pascnext(pascrow(n-1)) if n > 0 else [1]
Source(s): myself