#🔒 optimize my code pls

15 messages · Page 1 of 1 (latest)

flat barn
#

im trying to create a function that return the n'th line of the pascal triangle with the argument n

bleak snowBOT
#

@flat barn

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

flat barn
#
def dong_pascal(n: int) -> list[int]:
    if n <= 0:
        return [1]
    prev_line: list[int] = dong_pascal(n - 1)
    new_line: list[int] = [1] + [(prev_line[m - 1] + prev_line[m]) for m in range(1, len(prev_line))] + [1]
    return new_line
#

here is my code

#

its limit is n = 998

turbid stone
#

use binomial coefficients

from math import factorial

def n_choose_k(n, k):
    return factorial(n)//(factorial(k)*factorial(n - k))

def pascals_triangle(n):
    return [n_choose_k(n, k) for k in range(n + 1)]
flat barn
#

im not even studied wat the the hell is binomial so i cant do that way

#

i only can do the way that i posted

#

but is my code optimized??

turbid stone
#

If this is for an assignment you should really just submit what you have made yourself. Submitting other people's work is a breach of academic honesty.

#

To answer your question, what you have is a perfectly fine brute force way to solve the problem. "Optimized" is subjective especially when it hinges on what you're expected to know from class.

flat barn
#

ok thx alot

#

!close

bleak snowBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.