Reversing a string – Recursive

When it comes to reversing a string in python, we do have s[::-1], boring!

In the following recursive code, at every call, the first string will go to the end and stored in stack. So, when there is only one character left as an input string, the code starts rewind and retrieve the character one by one. The net effect is it gets the character in reverse. It becomes obvious when we see the printed output below:

Multi-line coding

days is OK because expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslashes. The months is also OK because backslashes are used to join physical lines, even though they are not required in this case. The print statement will not print the data because 2 logical lines are used without a backslash to join them into a logical line.

In the output from fibo(10), the first line indicates that 21893 calls were monitored. Of those calls, 3 were primitive, meaning that the call was not induced via recursion. The next line: Ordered by: standard name, indicates that the text string in the far right column was used to sort the output. The column headings include:

ncalls
for the number of calls

tottime
for the total time spent in the given function (and excluding time made in calls to sub-functions)

percall
is the quotient of tottime divided by ncalls

cumtime
is the cumulative time spent in this and all subfunctions (from invocation till exit). This figure is accurate even for recursive functions.

percall
is the quotient of cumtime divided by primitive calls

filename:lineno(function)
provides the respective data of each function

When there are two numbers in the first column (for example 21891/1), it means that the function recursed. The second value is the number of primitive calls and the former is the total number of calls. Note that when the function does not recurse, these two values are the same, and only the single figure is printed.

Calling a base class method from a child class that overrides it

The super() can be used when the root class inherits from the object class, and here is the code of calling a base class method from a child class that overrides it:

How do we find the current module name?

A module can find out its own module name by looking at the predefined global variable __name__. If this has the value ‘__main__’, the program is running as a script.

Many modules that are usually used by importing them also provide a command-line interface or a self-test, and only execute this code after checking __name__:

Why did changing list ‘newL’ also change list ‘L’?

We might be wondering why appending an element to newL changed L too.

There are two factors that produce this result:

Variables are simply names that refer to objects. Doing newL = L doesn’t create a copy of the list – it creates a new variable newL that refers to the same object L refers to. This means that there is only one object (the list), and both L and newL refer to it.

Colon separated sequence

Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 3, between 1 and 100 (both included). The numbers obtained should be printed in a colon(:)-separated sequence on a single line as shown below: