I guess Class structure and recursion is what we focused on in this course. Here is a simple example,
The two key elements of a recursive algorithm are:def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1)
- The termination condition: n == 0
- The reduction step where the function calls itself with a smaller number each time: factorial(n - 1)
Source: stackoverflow
Recursion required more thinking when designing a program, but it is a "lazier way" of programming. Using recursion, you need to write less lines of code and the whole function will looks much simpler and neat.
There are three great virtues of a programmer
- Laziness: The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful and document what you wrote so you don't have to answer so many questions about it.
- Impatience: The anger you feel when the computer is being lazy. This makes you write programs that don't just react to your needs, but actually anticipate them. Or at least pretend to.
- Hubris: The quality that makes you write (and maintain) programs that other people won't want to say bad things about.
By: Larry Wall, the original author of the Perl programming language
沒有留言:
張貼留言