This blog will be my notepad of things i learn , new programming language, tools , compilers etc.This blog is more of my penesive to see how much my understanding of things have evolved.
If you are one of the guys who are used to programming in c/c++ , one commonly ++ operator in loops.Recently i have been taking a look at python because its one of the most common scripting language around with loads of interesting library/bindings.
So i started writing the usual code for reading a file and counting the number of lines as below
And running the program i got the following output
count 0
I was suprised to see the output as there is nothing that can go wrong.OH!! God how naive of me.On turing to my most trused source, Google ofcourse i found the reason as follows [1]
Simple increment and decrement aren’t needed as much as in other languages. You don’t write things like for(int i = 0; i < 10; ++i) in Python very often; instead you do things like for i in range(0, 10).
This make sense because we hard make loops as above in python.
Now that we undestand the reason for such a decision,lets explore a little more.
Why did the interpretor did not throw any error ?
The reason is simple because the + operator is evaluated as unary operator.From the doc of the python
The unary + (plus) operator yields its numeric argument unchanged.
So in the above case the python interprator evalutes to two unary + evaluation and hence the value of the count remains unchanged.
If we try to convert the above code into post increment operator (i.e. count++) the interpretor throws an error as below.
The above error is thrown as unary operator(+) does not have any operand.
Recently i started to learn python. For a long time, i wanted to learn a scripting language. i thought i would learn python by solving spoj problems.spoj is a site that has a set of programming problems and one can write code in any language to solve it.On solving the problem ,the solution is submitted to a spoj server and is evaluated for its correctness by running it across a set of test cases.Its good site to get used to solving programming problems.
I started writing some of the basic problems to get an idea of working with python.One thing i stumble often is dynamic typed feature of python, it could lead to subtle errors while programming. I not blaming the language but its something that people from static typed language find it a bit hard at first.In static typed language, the variables are checked at compile time their types and hence one can avoid common errors of type changes easily.
The problem i am faced was because of dynamic typing and it startled me for a second. It was fun and i wanted to kick myself when i found it.Here it goes
I was trying to solve the problem of finding the number of trailing zeros for factorial.This was a solution, to one of the spoj problem.In order to get the input, which will contain all the numbers to find the solution i ended up writing a code as below
1
2
3
4
5
6
7
8
9
10
11
12
13
#finding the number of zeros in factorial
# the trick is to find the number of 5 and its factors
AllInputs=[]#get the number of inputs
numberOfInputs=input()# get all the inputs
foriinrange(numberOfInputs):AllInputs=input()fornumberinAllInputs:# logic to handle the inputs
Now on executing the program on input i ended up getting the following error
Traceback (most recent call last):
File "test4.py", line 13, in <module>
for number in AllInputs :
TypeError: 'int' object is not iterable
In the above code line 13 is accessing all the input values. I was wondering that i have already declared AllInputs as in list in line 1. This got me confusing and on closer look at code the culprit was the below line
In the assignment section i have mistakenly converted the array into a integer and python being a dynamically typed language did not warn me of the change in type :)
The fix was of course a simple one
The above problem tought me more about static typed language and dynamic typed language more than i could have ever understood or learnt from book.
Recently i was testing jemalloc for detecting memory corruptions of the program.This was tested by writing programs that intentionally crash and looking for jemalloc to abort and raise errors.
On my ubuntu machine, i found that program was not generating core. I was confused. So the first thing to check is to ulimit.
pradheep@PradheepVM:~/jemalloc$ ulimit
unlimited
The default output says unlimited. So looked at the man page of bash(1) this defaults to -f which is “The maximum size of files written by the shell and its children”.
So looking at ulimit -a
pradheep@PradheepVM:/$ ulimit-a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
Now we need to set the core value to unlimited.This can be done using
ulimit -c unlimited
The output should be
pradheep@PradheepVM:/$ ulimit-a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7402
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
This is set only on the shell you have run, to set the value permentenly on all the new shells that are spawned you should edit $HOME/.bashrc to add ulimit -c unlimited
In all your new shell invocation you will be provided with unlimited core size dump.