[SOLVED] CS115 Homework 1- Writing your own factorial function

35.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You will receive the following solution file(s) instantly after successful payment:

zip file icon HW-1-csxnql.zip (886.6 KB)
Assignment Instructions Updated Recently? Submit Below and we will provide new Solution!
Submit New Instructions
🔒 Securely Powered by:
Secure Checkout
5/5 - (4 votes)

>>> f r o m m a t h i m p o r t factorial
>>> factorial( 5)
>>> 120
As shown above, we can use the factorial function from the math module. Here, you’ll write your own factorial function. First, we start with a simple function that returns the pro duct of its two inpu ts:
def mult(x, y):
“””Returns the product of x and y””” return x * y
Nothing too surprising here. Now, take a look at this:
>>> reduce(mult, [2, 3])
6
>>> reduce(mult, [2, 3, 4])
24
>>> reduce(mult, [1, 2, 3, 4])
24
Notice that reduce takes two inputs: A function and a list and it applies that function to “compress” the list into a single value. In this case, it multiplied all of the values together.
Now, write a function factorial(n) that takes a positive integer n and returns n!.
This is “mean”…
>>> len([1, 3, 5])
3
>>> len(range(1,10))
9
Here is the mean function in action:
>>> mean([1, 2, 3])
2
>>> mean([1, 1, 1])
1

1 file · 886.6 KB

  • HW-1-csxnql.zip 886.6 KB