Engineering Topics and a Python Code Challenge - 401-advanced-javascript-davetrost/alchemy-fsjs-fall-2019 GitHub Wiki
Readings:
- Solving Problems
- Act like you make $1000/hr
- How to think like a programmer
- The 5 Whys and Hows
Notes:
Solving problems
This is a solid article about how important the planning phase of coding is. If you don't understand the problem well enough to explain it in plain English (or insert your language of choice here), then you're not ready to code it productively.
Act like you make $1000/hr
Meant to encourage more thoughtful work. Less busy work and more work that builds toward one's own sense of purpose.
How to think like a programmer
Pairs very well with the Solving Problems article. One thing that stuck with me is the idea of sub-problems. There is always a sub-problem and if you can identify it, you can probably identify more sub-problems. Solve enough sub-problems and you can solve the larger problem. Also, they aren't so much problems as a means to your own improvement.
My input
I don't want to write this solely to get a grade. In the spirit of the $1000/hour article, I am motivated to get something out of this. So, I'm going to write some code in Python to solve some problems (I've been wanting to learn Python for some time now). I'm going to check out Codebyte (a code challenge website) for the problem to be solved - as recommended by the author of "How to think like a programmer". And I'm going to use the techniques endorsed by the authors of articles #1 and #3 to plan my approach before I start coding.
The problem is: write a function that returns the factorial of a number. The factorial of 4 is 4 * 3 * 2 * 1. Any other number works the same - it's a product of itself and all the integers lower than it, down to 1.
Here is how I plan to approach this, in plain English (planning as per article #2)
The solution needs to generate a running product. The running product will be initialized to the number 1. Then, each successive number between 1 and the input number will be multiplied with the running product to update it. Once the input number is reached, the running product contains the final answer. A loop can provide the means to get the successive numbers.
Here are the sub-problems to be solved (planning as per article #4), and my sub-solutions
- How to write a for loop in Python
for x in range(0, 3):
# code here
- How to declare a variable with function-level scope
var1 = 1
- How to overwrite the value of an existing number by multiplying it with another integer
x*=5
Putting that all together gives me the solution to the code challenge
def FirstFactorial(num):
product = 1
for x in range(1, num + 1):
product *= x
return product
Learning:
I found a good website for Python documentation: TutorialsPoint. I discovered that the python for loop I used was exclusive of the second number. So, I needed to add 1 to num in the loop parameters to make sure that num was included in the final returned product.