2014年3月14日 星期五

Three weeks to go!

Well, is close to the end of the school year, and have been learnt a lot about computer science. Class. stack, tree, recursion, list sorting methods and some other things.

I guess Class structure and recursion is what we focused on in this course. Here is a simple example,
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)
The two key elements of a recursive algorithm are: 

  • 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

  1. 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.
  2. 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.
  3. 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

沒有留言:

張貼留言