Function name: triangular Parameters: integer Returns: a list Write a function c
ID: 3825045 • Letter: F
Question
Function name: triangular Parameters: integer Returns: a list Write a function called triangular that accepts one integer parameter that returns a list with the same number of terms as the value of the parameter. If the parameter is zero or a negative number, you should return an empty list A triangular number is the sum of a positive integer and all the integers before it. The series is as follows: 1, 3, 6, 10, 15, 21, >>> triangular(3) [1, 3, 6] >>> triangular (5) [1, 3, 6, 10, 15] >>> triangular (-2) []Explanation / Answer
def traingularUtil(n, l):
if n == 1:
l.append(1)
return l
num = (n*(n+1))/2
l.append(num)
return traingularUtil(n-1, l)
def triangular(n):
result = []
if n <= 0:
return result
result = traingularUtil(n, result)
result.reverse()
return result
print(triangular(3))
print(triangular(5))
print(triangular(-2))
#pastebin link: https://pastebin.com/5Za2xbx8
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.