Adnan wroteFeel free to point out my bad habits. Thanks !
The biggest issue I have with your code is that i, li and lii are terrible variable names. Unfortunately naming variables is particularly difficult when working on math problems. So try to include a description of what you're trying to do. Currently my favorite way of dealing with this problem is
doctest.
As mentioned, your code duplicates existing internal functions available in the standard library. You should use them whenever possible. In your case check out
range (or
xrange if you're using python 2) and the
factorial function of the math module.
However, just as you mention, you're using lists when you shouldn't. A list is expensive and should be avoided when possible. Can you think of a way to rewrite your code without creating these lists?
What's a lambda?
Adnan wroteI've came across people using lambda but I still don't understand how to use it, I'll read some docs / tutorials to learn it.
A lambda is a fancy name for a fairly simple concept called
anonymous functions. Here's how Python deals with it.
First you have to understand that in Python, a function is an object just like int, str, list, tuple, dict or any other kind of object you came across. So just like any object, it can be assigned to a variable. Look at this:
# Let's define a simple function
def say_hello(name):
return "Hello " + name
assert say_hello("Adnan") == "Hello Adnan"
# Now let's create a new variable pointing to this function
greet = say_hello
assert greet("Adnan") == "Hello Adnan"
lambda is just an operator that creates a new function without associating it to a variable name (hence the term "anonymous"). Here's how it can be used:
lambda <comma-separated list of arguments>: <value to be returned>
For instance, here's another way to define the
say_hello function:
say_hello = lambda name: "Hello " + name
assert say_hello("Adnan") == "Hello Adnan"
Why is this useful and how to use it is a complicated topic; some programmers think that lambdas are themost powerful programming technique, while others believe they're evil and should never be used. On top of this, for better or worse, Python has added some restrictions by design to the lambdas that make them limited. If you're interested in the subject, open a new topic where we can discuss the use of lambdas specifically.
If you want to read more, I wrote a
small article a while back that details a good use for lambdas.
What's yield?
Adnan wrote (and lots of "yield" here and there, not sure what that is yet, but the word is catchy).
The short answer is that
yield is a keyword that allows the creation of generators. So before going further you should note this:
Python generators are an advanced topic. Even programmers with several years of experience might struggle with the concept upon discovering it. So it's normal if you find it a bit difficult to understand at first.
I'm not going to explain how they work, but I'm going to point you to relevant documentation: The most relevant one (just like anything in Python) is the PEP. In our case it's
PEP 255: Simple Generators. However I find this PEP in particular a little difficult to read. So I found that
this SO question and its answers do a good job at explaining it.
Again, if you have questions, open a new topic and we'll discuss it there :)