According to dictionary.com, recursion is defined as the process of defining a function or calculating a number by the repeated application of an algorithm. In computer programming, recursion is most used as a problem solving technique when an algorithm is being repeated.
Using python's built-in functions aid to create a recursive function.
The general form of recursion is:
if (condition to detect a base case):
(do something without recursion)
else:
(do something that involves recursive call(s))
The first if case is when or how to stop the the function (the base case), and the else case specifies how to combine recursive subcalls.
simple example) Factorial is the result of multiplying a sequence of descending natural numbers.
5! = 5 * 4 * 3 * 2 * 1
Another way to look at this is:
5! = 5 * 4!
5! = 5 * 4 * 3!
and so on.
This is recursive!
The following example uses recursion to evaluate factorials.
def factorial(number):
if number <= 1: # base case
return 1
else: # in general
return number * factorial(number - 1)
Looking at the call:
>>> factorial(5)
Tracing:
We notice that 5 (number) is greater than 1. So, we move to the else condition.
So:
return 5 * factorial(5 - 1) -> 5 * factorial(4)
We notice that 4 (number) is still greater than 1. Se we move to the else condition.
So:
-> 5 * 4 * factorial(3)
and so on, until we get:
-> 5 * 4 * 3 * 2 * factorial(1)
Now, we see that 1 (number) is equal to 1, so we go to the first condition (the base case) and see that we return 1.
Thus:
-> 5 * 4 * 3 * 2 * 1
-> 120
Thursday, 26 February 2015
Thursday, 29 January 2015
Impression of First Few Weeks in CSC148
The
first few weeks of CSC148 was a little intimidating. Entering with very basic
skills of Python from CSC108, CSC148 jumps right into where one finishes with
CSC108. They start using built-in functions I've never heard/ seen of, and
introduces short-hand coding I've never seen before. Other than that, it's been
interesting to learn new built-in functions, short hand techniques, and
(specifically) see built-in function turtle in the works! (I'm sure most people
would agree that what the Prof has been doing with turtle is pretty cool.)
This
week, we're learning recursive programming techniques. This was the first time
I saw any sort of recursive programming being used. It's a strange concept; to
define a function, and call that function in your code. It's like using a word when
you're not too sure what it means; how do you call a function that's not even
been fully defined yet? I don't fully understand it yet, but I do see that it
works. Tutorial today helped me understand recursion, hope it doesn't get too
complicated later on in the course!
Thursday, 22 January 2015
Why Geeks Should Know How To Write
Why geeks should know how to write. The word "write" is very vague. I'm
sure all people know how to write in whatever language is to their preference,
however, I am sure we are talking about writing code. Living in the 21st
century, the world practically runs on technology. Technology is used daily -
more than that - every hour, minute in our lives. Almost every daily use is
programmed by technology using some sort of code. Surely, as our technology
develops, the number of coding jobs are sure to increase more with time.
Learning how to code, either to learn the fundamentals of how things are
programmed, or for an occupation, it is the first step to your future. Whether
occupation you choose, there is a good 99% chance that technology is involved.
Aspiring to work for Google? Programming cars? Designing a web page? The first
step is to learn the fundamentals of computer programming. Aspiring to be a
secretary? A cashier? No matter what you do, technology is involved and
involves you to learn how to use a certain code or program. Most successful platforms
are developed by programmers. It is the key to success.
This
week, we are expanding our knowledge of classes in Python and to be more
efficient in writing code. I know but just a pebble in the ocean about
programming, but as said before, it is the first step to my future. For the first
assignment, the class was assigned to create a class of games. The layout is a
little confusing, but I've learned that it is extremely helpful to map out what
classes to create, what functions to define, and where to place everything on
paper before jumping into writing code. I think my partners and I are on the
right track. I highly encourage working in partners/group. It is a great way to
make friends and throw ideas out there, as well as hear other people's ideas
and views on how to tackle situations.
Subscribe to:
Posts (Atom)