Many of us would have encountered this situation where we need to run some script which takes hours together to complete.

And most of the time the shell goes inactive and it becomes really hard to track what has happened.

One simple solution for that is to run the script in background.\

For example:

./run_my_script.sh &

Oh.. But where are my logs.. Yes, its difficult for us to track what has happened, unless we redirect our output to some specific file .

Anyhow, we have a posix command in linux which can do this stuff. Its nohub.

Checkout the wikipedia link for it here

https://en.wikipedia.org/wiki/Nohup

all you need is to add nohup parameter before your script

nohup run_my_script.sh

Even if the shell goes down, doesnt matter.. We will have the process running in the background. And we can track the logs from nohub.out file . This file will be autocreated in the directory where we ran the script.

For installing python releated packages we need to use pip. Hence installing pip in our server is our first task.

the package name for pip is “python-pip”. Hence the straight forward approach to install pip would be

apt-get install python-pip

But, if the source list is not updated properly , we will end up with older version of pip (in my case i got 1.5.3 however, 8.1.1 is the latest one). Hence to avoid this issue , we need to follow the approach given in wikipedia

i.e  to download the package from internet via this url . Hence we use the curl command

curl https://bootstrap.pypa.io/get-pip.py

Once get-pip.py gets downloaded. run the python file to install pip

python get-pip.py

Even beyond this, if we need to install pip package without internet .

Download the source from https://pypi.python.org/pypi/pip

wget https://pypi.python.org/pypi?:action=show_md5&digest=6b86f11841e89c8241d689956ba99ed7

extract the folder

tar -xvzf pip-8.1.1.tar.gz

Move inside the folder and install it

cd pip-8.1.1; python setup.py install

Square Root Algorithm

Posted: March 17, 2016 in Uncategorized

There are N number of ways to compute square root of a number

we can use the existing library (math) in python to compute the square root

However, there is a constrain in number of decimal digits . Hence, we need an algorithm which can compute a square root and provide n decimal digits .

The following algorithm uses Digit by digit calculation from this wikipedia link

Square Root Computation Algorithms

I have created a python script which will compute square root of 2 with 100 decimal digits .

And i verified the digits from this link

First 100 Digits for SQRT of 2 – Verification

The algorithm is not optimized. However, it gives me the result quickly for first 100 digits

For multiple irrational number , populate the irr list (by default contains 2)

To control number of digits after decimal change decimal variable (by default set to 100)

<br data-mce-bogus="1">
import time
import math

starttime = time.time()

global flag, start, end, decimal
flag = True
start = 0
end = 1
decimal = 100


def main():

answer = 0
irr = [2]

# for each irrational number
for num in irr:

# Step - 1 : Converting number to string and appending 200 zeros
str_num = str(num)
for i in range(0, 210):
str_num = str_num + "0"

# Step - 2: Setting Flag : if lenght of the number is odd we need to take one digit or else two digits
if len(str_num) % 2 != 0:
flag = True
else:
flag = False

start = 0
end = 0
ap_new_dig = 0

# Step - 3: Logic to take one or 2 digits
if flag:
start = 0
end = 1

else:
start = 0
end = 2

temp_num = str_num[start:end]
temp_int_num = int(temp_num)

# Step - 4 : Calculate the initial quotiend and remainder

quo = int(math.sqrt(temp_int_num))
rem = temp_int_num - int(quo ** 2)

# Step - 5 : Here is the main logic to iterate the steps
counter = 0
while counter < decimal - 1 :

# Step - 6 : Bring down the digits
if flag:
start = start + 1
end = start + 2
flag = False
else:
start = start + 2
end = start + 2
new_dig = str_num[start:end]

# Step -7 : Add the digits with previous remainder
ap_new_dig = str(rem) + new_dig

# Step - 8 : Compute a number less than max ap_new_digit
double_num = quo * 2
max_val = 0
t = 0
for i in range(1, 10):
if int( str(double_num) + str(i) ) * i <= int(ap_new_dig):
t = int( str(double_num) + str(i) ) * i
max_val = i
else:
break

# Step - 9 : Find new quotient and remainder
quo = int ( str(quo) + str(max_val) )
rem = int(ap_new_dig) - t

counter = counter + 1

# Final Quotient will be the answer
print "Square Root of N is" , quo



main()

print "Time taken ", time.time() - starttime

Excecution:
 

>>>
Square Root of N is 1414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572
Time taken  0.0269999504089

 

 

I believe Bhagavad Gita is one of the best text where we can enjoy the essence of Story, Situation, Interpretation, Divinity and knowledge for life.

When we re-iterate the verses in our mind, every time a new interpretation for the verse pops up. The same happened today and hence i am writing this.

I am not writing something new. I heard these from different sources and all i am doing it to wrap it into a single text. Here you go.

Chapter -1 , First sloka –

http://www.bhagavad-gita.org/Gita/verse-01-01.html

The verse explains about the conversation between Dhritarashtra and Sanjaya where Dhritarashtra is asking about the current situation prevailing in the battle field.

  1. The first word describing about the Battle field – kurukshetra hints us the result. Dharma Kshetre – The land of righteousness. Author picked the right word in the situation, which clearly insights the victory of Pandavas over Kauravas. Author could have used any adjective about bravery , beauty or situation about the battle field , however, Righteousness was the right pick.
  2. Why does the author picks Dhritarashtra to be the first person to speak in bhagavad gita ? May be, those who are weak in mind and having evil thoughts will have very less things to be spoken. And so, the author picked him to be the first.
  3.  Evenhough Dhritarashtra is aware about the present scenario, his weak minded intuition triggers him to ask about the events happening in Kurukshetra. When two powerful teams are in battle, there will always be a win and lose. His low confidence about his team’s status triggers him to ask , “what they are doing in the battle field” – whole intention behind the statement was to know – “who is winning the war”.
  4.  He could have mentioned the team names to be Kauravas and Pandavas . But, he utters “My sons and Pandu’s sons ” which clearly establishes the difference he had in his mind for pandu and his team. Eventhough Pandu is his own brother
  5. And the whole verse not only explains about the battle field of Kurukshetra, it signifies about our own mind , where there is always a fight between good and an evil. We need to be aware that we may have less number of good qualities in our mind, we should never allow them to be taken over by evil .

 

     After all life is nothing but the war between a good and an evil.

 

 

 

 

 

 

A Tour On Python List

Posted: May 28, 2015 in Programming
Tags: , , ,

 For those who have used Arrays in C and C++, list in Python is going to be a lucky charm . As Head First Python states, List is nothing but an array with steroids. Python list makes the life very easier as its dynamic in nature and the in-build functions are available to perform all the actions on it.

First lets list down the basic stuffs that we always do on arrays .

1. Declaring
2. Creating
3. Initializing with predefined input
4. Adding More input to it
5. Searching
6. Indexing
7. Removing
8. Erasing
9. In Built Functions

In this tutorial, am going to use 2 lists. One named numbers and other named alphabets. Lets go and see one by one .

1. Declaring a list

As we know, we need not declare a variable with appropriate data type in python . both a = 5 and a = ‘5’ are valid without specifying the corresponding data type. And more importantly, its applicable to lists as well.

2. Creating a list

When i say creating , it has 2 modes. Creating an empty one to add items later. Or creating a predefined set of elements in the list. To create an empty list, all we need to do is to write an open and closed square brackets.

numbers = []

https://shrinathsuccess.files.wordpress.com/2015/05/list2.png?w=614

3. Initializing with Predefined Input:

Sometimes, we will be really sure that we are going to work with only some specific inputs. In those situations we can create a list with pre defined inputs in it. That can be done by enclosing the inputs inside the square bracket

numbers = [1,2,3,4]

https://shrinathsuccess.files.wordpress.com/2015/05/list3.png?w=614

4. Adding more input to a List:

Adding input to the list can be categorized as following

4.1 – Adding elements one by one to a list
4.2 – Adding elements in a block
4.3 – Multiplying the list
4.4 – Adding list inside a list

4.1 – Adding elements one by one to the list :

Python has predefined function named “insert” , which can be used to add elements to the list. However, the insert takes two arguments. first one is the position and second one is the value. The list with numbers 1,2,3,4 can also be created as below

numbers = []
numbers.insert(0,1)
numbers.insert(1,2)
numbers.insert(2,3)
numbers.insert(3,4)

https://shrinathsuccess.files.wordpress.com/2015/05/list4-1.png?w=614

4.2 – Adding elements in block

If suppose we have 2 lists. first one containing numbers 1 to 3 and other one containing numbers 4 to 6. And we need to merge two list. ie. adding elements in block, then we can do it as below:

num1 = [1,2,3]
num2 = [4,5,6]
num1 + num2

or simply

[1,2,3] + [4,5,6]

https://shrinathsuccess.files.wordpress.com/2015/05/list4-2.png?w=614

4.3 – Multiplying the list

Python creates a easy way of inserting repeated items. For example, if we need a list which has 3 numbers 1,2,3 ten times. we need not write a for loop . Just a simple multiplication operator on list will do it as below

numbers = [1,2,3]
numbers * 10

https://shrinathsuccess.files.wordpress.com/2015/05/list4-3.png?w=614

4.4. Nesting Lists

Yes, we can have list inside a list. And there is a specific way for indexing the list. In the list below , 4th item is a list which contains 3 items in turn.

numAlpha = [1,2,3 [ “a”, “b”, “c” ]]

https://shrinathsuccess.files.wordpress.com/2015/05/list4-4.png?w=614

5. Searching an element in the list

If we need to check an element exists in the list , we can use “in” keyword with list which will return a boolean value (True indicating the element exists and false indicating the element doesnt exist in the list)

numbers = [1,2,3,4,5,6]
4 in numbers
7 in numbers

https://shrinathsuccess.files.wordpress.com/2015/05/list5.png?w=614

6. Indexing list

We can get the element based on the index of that element in the list. Since, all the elements are inserted into the list with the index element, we can derive any element if we know the index. Index starts with 0 and moves on as we insert the elements.

List_name[start : end]

This is the common notation . start denotes the starting index that we need to start with, to start from beginning give 0 or leave it empty. End denotes the position to which list should print

numbers = [1,2,3,4,5,6]
numbers[1:4] - show elements from index 1 till 3
numbers[:2] - show elements from beginning till 1st index
numbers[2:] - show elements from index 2 to end of the list
numbers[:] - print all elements
numbers[-2] - tricky one .. show 2nd last element in the list

https://shrinathsuccess.files.wordpress.com/2015/05/list6.png?w=614

7. Removing elements from a list :

We can remove an element from the list if we know the index or element value. remove function can be used if we know the list value and del function can be used to remove using index.

numbers = [1,2,3,4,5,6]
numbers.remove(5)
numbers
del numbers[-4]
numbers

https://shrinathsuccess.files.wordpress.com/2015/05/list7.png?w=614

8. Erasing the list :

If we need to erase all the elements in the list , we can use clear function . List_name.clear() will remove all the elements from list.

numbers = [1,2,3,4,5,6]
numbers.clear()
numbers

https://shrinathsuccess.files.wordpress.com/2015/05/list8.png?w=614

9. Inbuilt Functions :

There are lot of inbuilt functions available in python list. One that we saw is len(list_name) which gives the length . Similarly we have

len – finds the total number of elements in the list
count – finds the number of occurrence for an element
sort – sorts the list
reverse – reverse the list
index – returns the index of first matching element

https://shrinathsuccess.files.wordpress.com/2015/05/list9.png?w=614

The firefox update can be downloaded as *.tar.gz from the official website.

We can directly move to the downloaded directory and untar it . It will give us a folder named firefox .Inside that , we will be having an executable named firefox. Once you trigger the executable , the new version of firefox will launch.

But, we have a problem here. The existing version still exists in home folder as .firefox . And if you take an attempt by clicking on the firefox icon, it will launch the older version of your firefox.

We need to find out the directory where the firefox is installed. In my case, it was in /usr/lib . Sometimes, it may get installed in /opt or /etc. we need to replace the /usr/lib/firefox directory with our downloaded latest firefox.

cd /usr/lib
sudo cp -r /home/user/download/firefox .

Enter the password to proceed with the action.

After the copy, when you click on the firefox icon , new version of the firefox will open.

One of the best rubik Timer with lot of inbuilt options. I took an attempt of using Prisma Puzzle Timer in Ubuntu 14.04.

We can download the jar from this website
Prisma Puzzle Timer

After downloading we can run the timer from terminal using the following command

Move to the downloaded jar file location

cd /home/user/Downloads

and run the jar file by

java -jar PrismaPuzzleTimer0.6.jar

which will invoke the timer .

But everytime , we need to type this command in the terminal to invoke it .

Instead we can create a laucher file which will invoke this timer. For that we need a simple software .

Alacarte.

We can download the software using the command

sudo apt-get install alacarte

Once downloaded invoke alacarte from the terminal by typing its name

alacarte

and click on new item button to create a new launcher.

In the command type java -jar PathToTheJarFile

in my case its java -jar /home/user/Software/PrismaPuzzleTimer.jar

Save and close . With the given Name (in my case its “RubikTimer”) it will be added to the main menu.

You can search for the work RubikTimer in the menu search tab and launch the software by clicking on the file .

Please refer to the images below .

Installing Alacarte

Setting Up The Launcher Icon

Searching for the Launcer

Prisma Puzzle Timer