Start with Python
This commit is contained in:
18
1- python/11-factorial/README.md
Normal file
18
1- python/11-factorial/README.md
Normal file
@ -0,0 +1,18 @@
|
||||
Factorial!
|
||||
==========
|
||||
|
||||
Get the factorial of a given number. Using sys.argv, pass in a number and return it's factorial.
|
||||
|
||||
To calculate the factorial of 5, for example, the equation is ```5 * 4 * 3 * 2 * 1 = 120```
|
||||
|
||||
#### Iterative
|
||||
|
||||
Write the equation iteratively, using a for or while loop. How to use loops in Python is well documented [here](https://docs.python.org/3.4/tutorial/controlflow.html)
|
||||
|
||||
#### Bonus
|
||||
|
||||
Write this function [recursively](http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3/Recursion). If you run into difficulty, think about how your stack will develop as your function recurses and remember to define your base case.
|
||||
|
||||
Write assert statements to test your code in both cases.
|
||||
|
||||
[Wikipedia on Factorials](http://en.wikipedia.org/wiki/Factorial)
|
13
1- python/11-factorial/factorial_iterative.py
Normal file
13
1- python/11-factorial/factorial_iterative.py
Normal file
@ -0,0 +1,13 @@
|
||||
import sys
|
||||
|
||||
def factorial(num):
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#write assert statements below
|
||||
|
||||
assert(factorial(5) == 120), "5! should return 120"
|
||||
assert(factorial(20) == 2432902008176640000), "Well that escalated quickly"
|
13
1- python/11-factorial/factorial_recursive.py
Normal file
13
1- python/11-factorial/factorial_recursive.py
Normal file
@ -0,0 +1,13 @@
|
||||
import sys
|
||||
|
||||
def factorial(num):
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#write assert statements below
|
||||
|
||||
assert(factorial(5) == 120), "5! should return 120"
|
||||
assert(factorial(20) == 2432902008176640000), "Well that escalated quickly"
|
Reference in New Issue
Block a user