Some notes on Javascripts

April 11, 2014

JS array can store list of different types of data. Thus they act like the lists in Python. Also, their position is fixed, thus they are ordered, just like lists in Python but unlike dictionaries in Python. They have 0-based indexing.

Use <arr>.push() method to push elements to the array.

String(<other type var>) for type-casting.

isNaN(<var>) behaves in a strange way with the quoted numbers (e.g. “23”). It automatically type-casts them to a number and then returns false. However, if it is to behave like equivalent functions in other languages, it should return true for isNaN(“23”), since “23” is a string.

JS allows creation of jagged array. i.e. two-dimensional arrays with variable number of columns i.e. variable number of elements in each row.

for/in loop behaves like a combination of for loop from JS (in syntax) and comprehensions from Python (in behavior).

[Doubtfull!]No semicolon at the end of the constructor is required. Constructors are a special kind of methods.

We can have functions performing different functions in the constructor.

<classname>.prototype.<new Method> = function() {statements;}; is the format to change the prototype of a class and add a method named <new Method> to the class, thus making it accessible to all of the objects.

<childClass>.prototype = new <parent-class-constructor>; is the inheritance in JS.

In JS all properties of an object are public by default. To create a private property(variable) or method, define the variable, method inside the constructor using var keyword instead of using this.<property/method_name>. Write get and set methods inside the constructor to access these variables/methods.

 

IS IT POSSIBLE TO ADD THE GET AND SET METHODS LATER BY PROTOTYPING??????????????????????????????????????????

 

 

Functions can return functions as well (like Pyhton). This is how you access the private methods of a class: by accessing them through a public method.

hasOwnProperty() comes from Object.prototype.

Advertisements

Checking types in Python isinstance() and others

April 8, 2014

basestring

isinstance()

ducktyping

ABC

iter vs. scalar items

http://stackoverflow.com/questions/1549801/differences-between-isinstance-and-type-in-python

The Evil Genius

February 25, 2014

Is Simulation Hypothesis (SH) real?

Gödel's Lost Letter and P=NP

Unknown

Ren Descartes is famous for countless things in mathematics—Cartesian products, Cartesian coordinates, Descartes’ rule of signs, the folium of Descartes. He is also famous for his work in philosophy and the notion of an evil genius. The evil genius presents a full illusion of a reality, and “fools” Descartes into believing there is a reality, while actually there is none.

Today Ken and I want to talk about the evil genius, and its relationship to the simulation hypothesis.

The hypothesis (SH) can be traced back far into history; even Descartes was not the first to wonder about reality and the possibility that it is an illusion. Recently the SH has surfaced as a “serious” thought. So Ken and I decided that we would comment on SH: did we really decide this on our own, or are we just part of the simulation? Oh well, who knows.

The Simulation Hypothesis

View original post 1,708 more words

On ipython

February 19, 2014

Some notes that I scribbled over ipython:

Use %magic functions. %paste for pasting code that has been indented in a foreign environment. Actually the “%” here can be skipped. Some other examples are: run <script_name.py>, pwd, cd <dir_name> and some other shell commands as well. Shell commands can be run by using ! as prefix.

%timeit for easy profiling.

%run to execute the scripts in the current namespace.

<func_name>, <module_name> followed by ? or ?? return documentation and even source code. Out[n] provides the results for the previous expressions.

After a program or script has crashed, %debug takes you back to the line where it failed in the debug mode. %pdb will also do the same but before running the program or script.

%pdef, %pdoc, %edit, pdb.run() profile.run(), !echo “Hello {<python_var>}”, CTRL+p CTRL+n, CTRL+r and finally %lsmagic.

Ending is a bit abrupt as I had to salvage this post. So I only left the keywords in.

Regular expressions in Python

January 4, 2014

Roman characters:
I = 1, V = 5, X = 10, L= 50, C= 100, D = 500, M = 1000

Caveat: There is no way to programmatically determine that two regular expressions are same. Only way to find out is: write a lot of test cases.

Regex pattern to check for roman numerals on thousands, hundreds, tens and ones places:

pattern  = ‘^M?M?M?(CD|CM|D?C?C?C?)(XL|XC|L?X?X?X?)(IV|IX|V?I?I?I?)$’

or pattern  = ‘^M{0,3}(CD|CM|D?C{0,3})(XL|XC|L?X{0,3})(IV|IX|V?I{0,3})$’

POINT: Do not put spaces in these braces.

Writing verbose regular expressions:

One’s above this are called compact regular expressions. These one’s are, as the name suggests: verbose.

Biggest difference (at least for me) between these two kind of expressions is that white-spaces are ignored in the verbose expressions while they are not in compact regular expressions. If you want to match these in a regular expression, you need to escape them by putting a backslash(\) in front of them.

Lets get started:

WENT TO NOTEBOOK!

May be I’ll update here later.