Monday, May 27, 2019

[b]*N Syntax in Python

[b] * N creates a list of size N which contains only b's, where

  • 'b' is a reference to an object, and
  • [b] * N does not make copies of 'b' inside the generated list but simply copies the reference of 'b' to produce the list.

For example, X =[0] * N, produces a list of size N, with all N elements being the value zero.
Thus, X = [0] * 8, produces a list of size 8 and results in X = [0, 0, 0, 0, 0, 0, 0, 0]

The pictorial representation of X = [0] * 8 will be like below:

Be warned that all eight cells of the list reference the same object. This is okay for immutables like integers or strings but may produce unintended results for mutable objects like lists, because of the fact that lists are referential structures in python. If you try to assign a new value to list, say X[2] = 10, this does not technically change the value of the existing integer instance of 0. This computes a new integer, with value 10, and sets cell 2 to reference the newly computed value. Now, hypothetically let's assume an integer is a mutable object, then integer value would have instead changed and all the cells would be pointing to the old reference (holding new value) only. Pictorial representation change due to X[2] = 10 is below:


Thus, for creating a list of lists, the above approach might produce an unintended result as mentioned above because list is a mutable object. For example:
>>> t = [[]] * 5
>>> t [[], [], [], [], []]
>>> t[0].append(5)
>>> t
[[5], [5], [5], [5], [5]]  # Observe value at all indices got changed

So, if you want to create a list of lists properly, one of the approaches could be:
>>> t = []
>>> for i in range(5):
>>>     t.append([])
>>> t[0].append(5)
>>> t
[[5], [], [], [], []]


Solve this question [valid-tic-tac-toe-state] to understand the importance of initializing a list of lists in a proper way with Python.
Reference: StackOverflow

Friday, May 10, 2019

To Me, I Known?

Bullah asked, "Pir, my name is Bullah and I wish to know how I can realize God." Inayat Shah said "Bullah, what problem is there in finding God? It only needs to be uprooted from here and planted there." This was enough for Bulleh Shah, he got what he had wished for. His master had poured the essence of spirituality in these few words. He conveyed to Bulleh Shah that the secret of spiritual progress lay in detaching one's mind from the world outside and attaching it to God within. We live for moments such as this to die for. [The Spirit of Oriental Poetry]

 Kafi of Mir Bulleh Shah Qadiri Shatari (1680-1758)

Bulla! to me, I am not known
Neither am I the believer in mosque
Bulla! to me, I am not known
Neither am I Arabic, nor from Lahore
Bulla! to me, I am not known
Neither Moses, nor the Pharoh
Bulla! to me, I am not known

Nor am I a pagan disciple of false rites
Not the pure amongst the impure
Nor do I exist in the Vedas
Neither am I in opium, nor in liquor
Not in the drunkard's wasted intoxication
Neither am I in happiness, not in sorrow
Neither clean, nor a filthy mire
Neither am I of the water, nor of the land
Neither fire, nor from air

Neither am I from the Indian city of Nagaur
Neither a Hindu nor a Turk (Muslim) from Peshawar
Neither I found the secret of religion
Nor was I born of Adam and Eve
I am not the name I assume
From beginning to end, I tried to understand myself
None other, have I ever known
I am not just another wise one
Bulleh Shah, who is this man standing here

Neither awake, nor in a sleeping daze
Neither fire, nor from air
Nor do I live in Nadaun (City of innocents)
Neither am I in stillness, nor in movement
Bulleh Shah, who is this man standing here

"Bulla Ki Jaana Main Kaun", this soundscape for the soul reflects the anguish in discovering this mystery of self, the longest journey of any kind. A man wants to know his own real identity, his everlasting quest to discover his own place in the world agonies him. He keeps comparing himself to something that transcends humanity but denies them one by one for what he is not. He knows materialistic things are of no use if we don't have a proper introduction with ourselves and can only find his answer inward. In the end, he realizes, he knows None other than the God who was the First and will be the Last, which holds the truth of his own identity that he was, has been and will be, who lives within.