Saturday, 18 January 2020

Python string rotation - a post on optimising loop and runtime


Need for optimising code with respect to iterations or loops can be demonstrated with a below example :-

Taking an example below :-


string="abcd"
leftshift_count = 40000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000


case_1 = leftshift_count % len(string)
case_2 = leftshift_count

#BLOCK 1
print ("Case1")
for i in xrange(case_1):
  temp = string[0]
  string = string[1:len(string)]+temp

print("Shifted string {str}".format(str=string))



#BLOCK 2
print ("Case2")
for i in xrange(case_2):
  temp = string[0]
  string = string[1:len(string)]+temp

print("Shifted string {str}".format(str=string))



Here  Block 1 and Block 2 performs the same operation , but when executing ,

Case1
Shifted string abcd
Case2
Traceback (most recent call last):
  File "leftshift.py", line 20, in <module>
    for i in xrange(case_2):

OverflowError: Python int too large to convert to C long


Case 2 throws an error .

from the above program , which left shifts a string , abcd if left shifted 4 time gives abcd itself , so any multiples of the number of shifts rotates the string to itself so we just need to simplify that to see how many times we do the shift. 


So what i wanted to bring out of this is , every bigger problem has a solution in itself , there is a path for optimisation. Try to divide the problem into smaller parts and solve the smaller problem , the solution that you bring out of that will be optimal. 


Thursday, 2 January 2020

Layman's view of success



Strange heading , but thought to scribble few thoughts. How do we define success?

1) By Comparison?


Most of the people measure success by comparison , an example- An IT engineer who is settled as a middle class , compares himself with a H1B settled engineer ? Ah , true , from my case yes , i always used to do that . strange . ha hha . Even i used to compare my self with Sundar Pichai , epic isn't it?  


2) Uncertainty 

Uncertainty is part of life as long as time travel is impossible , uncertainty creates possibilities , an infinite set of possibilities to work on options . But it should not pull you down . An example , Its  bad to run on knowing that my neighbour has a puppy , it would grow up and will attack you sometime in future . again strange isn't it ?

3) Doubt

You wont have a doubt if you are successful , Just like after crossing a river , you see to believe that you crossed , you are alive , there is no point of doubt there .
When in doubt do something . Success is success . there is no either part for it , so don't doubt on outcomes , do it . Just like how i started writing this blog .


I know i have scribbled something . I would like to bring up yet another factor

"Happiness"- The factor that measures success - just not your happiness but others as well .
A child who built a paper boat looks successful when he is happy at his craftwork. 

He can be happy 
- if the boat just works .
- It looks like a boat .
- Others see to believe that its a boat .

That means a success can be measured if he is happy with his act . So if you are happy you are successful .  The sole purpose of life is to lead a happy life , bring happiness in others .