# Getting to know strings better in Python!

Hey welcome back!
Solutions to previous post questions:
Exercise: 1. define a function which does the following
print alternate letters
print the leftout letters
example : if string = ABCDEF # output : ACE,BDF # Hints :string[0::2]

In [108]:
def alternate(string):
print string[0::2] # print alternate letters starting from 0
print string[1::2] #print alternate letters starting from 1

In [109]:
alternate("ABCD123")#call the above "function"

AC13
BD2


In [110]:
alternate("I am a good girl")

Ia  odgr
mago il



Ex2: Writing in reverse

In [112]:
"Hello World"[::-1] #last index -1 reverses the order of the sentence! basically it reads,
#start from 0 to end by going negative steps

Out[112]:
'dlroW olleH'


So we have seen upto now how we can define strings and access its letters in them. Now things are going to get interesting. We are now going to use “functions” or commands that already the computer knows to manipulate a string and make it do things. But first we need to know what the computer understands right? It might be a bit boring, and I am not going to give it to you all at once, but through tits and bits and you can find help from google as always! So lets define a string and begin playing with it a bit. Lets see how many ways we can define a string in python

In [1]:
string = "I am a String"
character='A'
multiple_string = """I am multiple line
String !
I am line 2!
Hey I am line3
"""

In [2]:
string

Out[2]:
'I am a String'

In [3]:
character

Out[3]:
'A'

In [4]:
multiple_string

Out[4]:
'I am multiple line\n                    String !\n                    I am line 2!\n                    Hey I am line3\n                  '


O wait, what are those \n? Well computer understands some more letters which humans have no “sound” for. Example, “\n” stands for an “newline”(n for new) , it is basically equivalent of pressing enter! Similarly, “\t” is a tabular space! So in the above example a multiple line string is defined in triple quotes and it automatically takes in “newline” where ever you press “enter”

Ok now lets define some operation on strings. You can “add” two strings just as numerical things by “+” sign.(beware “-” does not work!) you can even multiply a sentence by a number, and it adds the same copy of itself , lets see some example below

In [8]:
string+string

Out[8]:
'I am a StringI am a String'

In [43]:
print "tabs\t"*4
# (the hash is for comment, nothing after
#this is seen as a command its for you )see the \t after 'tabs'
print "tabs"*4# see the difference. *4 adds
#the sentence 4 times!

tabs	tabs	tabs	tabs
tabstabstabstabs



Well now strings are immutable, i.e. you cannot change a string that you have defined.So any new changes you make must be saved under a new name to start with, you can also “reassign” the “changed” string to the original! Examples

In [47]:
str1  = "String1"
str1[:-1]# all but the last letter(called slicing)

Out[47]:
'String'

In [48]:
str1 #str1 remains the same

Out[48]:
'String1'

In [52]:
str = str1[:-1]#save it to a new variable

In [55]:
str

Out[55]:
'String'

In [56]:
str1

Out[56]:
'String1'

In [57]:
str1 = str1[:-1] # or reassign to same variable name ,
#when you dont need the previous sentence anymore in future

In [59]:
str1# str1 is changed to "String"

Out[59]:
'String'


Well now we will digress a little and do some numbers and then come back and use “strings” with a lot more power!
We have seen how we can access each letters of the sentence by the positions. How now about we manually do this scan through each letters and do something with it. Lets first do a simple example, how about counting the number of ‘e’ in a sentence. (BTW, ‘e’ is the most frequently used letter in English)

In [60]:
string  = "Count the number of eee's in me."

In [62]:
length = len(string) # number of "letters", including space and punctuations in the sentence!save it to variable length

In [63]:
length

Out[63]:
32

In [69]:
count_e = 0 # this is my counter, if i find 'e' then i will add 1 to this
for i in xrange(length): # this is called a "loop",  i is like your index finger running across the length of the sentence

if(string[i]=='e'): # check 'if' the letter at ith index is e. "==" checks if it is "equal". single "=" assigns value
count_e = count_e+1 #if yes add 1 to count_e variable

#Look carefully how I gave a "tab" in front of if. This is necessary and very important in python loops, to tell what are the
# set of instructions one will follow iteratively. Similarly for "if" we need to add in another "tab" to make it clear what are
# the statements one need to follow if the statement is true!

In [70]:
count_e

Out[70]:
6


Now we will try different functions that are there in python, to do various jobs.BTW caps are important. meaning variable name
A = 2 and a = 2 are different!

In [73]:
string

Out[73]:
"Count the number of eee's in me."

In [79]:
string.count('e') # do the same thing (count no of occurrences)
#   as above but this time it is inbuilt

Out[79]:
6

In [86]:
string.find('e')# find the "first" occurance of the letter in the string

Out[86]:
8

In [88]:
string.lower()# convert everthing to lower case

Out[88]:
"count the number of eee's in me."

In [92]:
string.upper()# convert everthing to upper

Out[92]:
"COUNT THE NUMBER OF EEE'S IN ME."

In [91]:
string.replace('e','_')# replace all occurrences with some other values

Out[91]:
"Count th_ numb_r of ___'s in m_."

In [100]:
"   abcd  ".strip()#removes "spaces" from lead or end

Out[100]:
'abcd'

In [103]:
string.split(' ')#split sentence into words by "space" = " "

Out[103]:
['Count', 'the', 'number', 'of', "eee's", 'in', 'me.']


Now let us count the total number of vowels in a sentence

In [117]:
sentence = "the quick brown fox jumped over the lazy dog" #define a sentence
length = len(sentence) # length of sentence(no of characters)
count_vowels = 0 # counter for vowels

for i in xrange(length): # same old indexing, get yours hands out!

letter  = sentence[i] #current letter in the sentence
if(letter =='a' or letter=='e' or letter=='i' or letter=='o' or letter=='u'):
#check if the letter is "a" or "e" so on....
count_vowels = count_vowels+1

print "Number of vowels:\t",count_vowels
print "Number of consonants:\t",length - count_vowels

Number of vowels:	12
Number of consonants:	32



Exercise:
A palindrome is a sentence, which when reversed reads the same as its original, other than punctuations and spaces. e.g.
“Was it a car or a cat I saw”
“Rise to vote Sir”

Write a function to test if a sentence is a palindrome.
If it is a palindrome print “it is a palindrome”
else print “it is not a palindrome”

tips:
def isPalindrome(string):
#remove spaces (hint : strip)
#find reverse String
#check for equality of strings
print “it is a palindrome”

#call the function
isPalindrome(“Rise to vote sir”)

In []:

# Installing python and your first program

Time has come to teach your computer to speak “Python”

Firstly, the choice of Operating system:

So what do you use?

Windows, Linux?

I would personally prefer Linux, as it is open source and that is what I primarily use for my work. Although it really does not matter once you teach your computer the language, it can understand what you say as well on windows or linux.

But hold on! I will be committing a big mistake if I don’t refer to the huge support group and documentation available online. I will not be extensive over these set of tutorials, but there is a huge crowd of people who is ready to help you always at stackoverflow.com and the Python Docs

Next, I would prefer one to use the userfrienidly IPython, but is it it better to go to the shop once rather than going 1000 times, here comes the Enthought distribution , select the operating system and the 32/64 bit system( which you can find from system properties) and download the package ,and install it! (Just do next next next  :p for windows). For linux its a bit tricky, you will download a “***.sh” file. save it somewhere, say downloads. Open terminal (I am assuming you have Ubuntu) with Ctrl+Alt+T( remember CAT!) , hopefully it will open in Desktop, change directory by command

chmod +x “***.sh”

sudo ./***.sh

This should be starting the installation process, and would ask for where you want to install just type “Y” or do as it suggests 🙂

#————————————————————————————————————————————————#

Once you are done installing , its time to start up the software,

In windows open “command” (start -> run->type “cmd”), a black box should pop up.

In Linux do the Ctrl+Alt+T thing again, and the lil box should show up.

Ok so there are few things you need to “know” to make things happen, its a repetitive thing, these are the few steps:

ok now type in:

ipython notebook –pylab [for a white good looking interface,opens up in browser] or

ipython –pylab [for nerdy looking ones]

Once it fires up there is an option (New Notebook)

click on that, and here you have the first terminal,

 Here is my first code, well as is the rule lets begin by "HelloWorld", its a computer speaking to human through human interface. Shift+Enter is the basic go to next line(Evaluate this line). So everything in this notebook is evaluated on the go, its like an advanced calculator. In [2]: print "Hello World" Hello World Yay , my computer responded back with "Hello World". Lets test if it can sum my grocery bill In [3]: print 212+190+40+15+64 521 Oh good, you see how easy a calculator it is! I can just sum eveything at one go! Ok now remeber I told you to break up simple tasks, well lets do the hello world with "functions" now,( this is a small digresson, if you find it complicated, skip it, you will get to know its advantages in future) In [4]: def HW(string): print string In [5]: HW("Hello World") Hello World This will require some explation so, every small task must have some "name", here "HW" is such a name, "def" stands for defination! so in natural language it is equivalent to saying "define a task HW, which takes in a sentence and prints it out!" And then call it! Ok lets now begin with some basics of text. So firstly note that "text" and numbers are different! And computers mostly understand numbers. Firstly for everything there is a name! even for a sentence! In [6]: sen = "AbCd123" In [7]: print sen # call sen by its name and see what is there # is a comment AbCd123 now a string is a collection of letters, so if say you wanted to know the 3rd letter of the sentence you would do something like: In [8]: sen[2] # notice for 3rd character we write 2, this is because the numbering in this language starts from 0 Out[8]: 'C' This is the concept of an "list" , which is an collection of objects, here that is letters. In general an list is defined with brackets, like In [9]: LList = ["A","b","C","d","1","2","3"] In [12]: print LList[2]," ",LList[0] C A one can also "slice" the array, that is tell the computer to return all letters after a particular letter, the syntax for that is simple like In [14]: sen[2:] #return everything else from 2nd position Out[14]: 'Cd123' In [16]: sen[:2]#return everything else till 2nd position Out[16]: 'Ab' In [18]: sen[::2]#return everything with a gap of 2, i.e. 1st letter then 3rd letter then 5th and so on Out[18]: 'AC13' In general, In [20]: sen[2:-1:2]#start from pos 2 and till the last, and jump by 2 steps in between Out[20]: 'C1' Exercise: 1. define a function which does the following def stringP(string): # print alternate letters # print the leftout letters # example : if string = ABCDEF # output : ACE,BDF # Hints :string[0::2] call the function with your favourite coute Exercise 2: define a code to write the string in reverse, that is for input of Sen, we get '321dCbA' In []: 

# Learning to speak to computers!

Good to be back! Forgive me of my sins for I was occupied with interests of my own. 🙂

So apparently my girlfriend is now studying Linguistics and programming can become handy for large dataset analysis and automating some stuffs, so she (who never programmed anything ever) asked me to teach “programming”. I thought for a while and thought it would be fun to teach someone programming from scratch, think about it, it is a “language” too, albeit a “written” one, which is quite similar to the set of instructions that we are accustomed to process.

Someone writes to you,

Find the vowels in the sentence:   “Programming is simple conversation with the machine!”

The word “Find” is a trigger your brain processes to “narrow” down your “attention” to your prior knowledge of vowels, namely “AEIOU”. You can do the same thing to a computer, using its language! Now as humans have developed different language, you can talk to computer using various “languages” which it can understand.

BTW computers mother toungue is just True(1) and Flase(0).

I have been a great fan of the language “Python”, because of its simplicity and user friendliness, so I will stick to that. The main aim of these set of blog tutorials will be 2 fold. Recently I have been interested in NLP, which is Natural Language Processing, and its applications, these blog posts will apart from doing the basics of programming, move on to some basic language processing tools, which will be my own study journal. I might think to introduce some maths later on in this course for understanding some statistical properties of language.

So are you ready to talk to computer and explore the fascinating world?

Lets begin with the analogies, so that it isnt too “unfamiliar”. Your friend who speaks only German says to you :

Schreib “back”

Now I cant understand what Schreib means! because I dont know the language! Similarly a computer would not know any language, but you can teach him by installing a “software” ! I will write about Installing python in your computer in the next post, but till then hang on. So the basic thing to know speak to computer is to know its language. Specifically forget the adjectives there are only Names(nouns) and Commands(verbs) :p

BTW did you know ?

Guido van Rossum, the creator of the Python language, named the language after the BBC show “Monty Python’s Flying Circus”. He doesn’t particularly like snakes that kill animals for food by winding their long bodies around them and crushing them.

Now it is important that you learn a language by practicing it! So it applies same here, the next set of tutorials will be on my view about how one should see programming, well since i have said that, I believe that we are fast evolving into a world where humans and technology will be inseparable, and programming will be an intrinsic part of everyone’s life, there will be pet cyborgs and humanoids to make your life easier, so you better learn how to control them.

Ok, enough digression, so lets get back to our second example, Schreib in german means write, to do the same thing to a computer you can just say

print “HelloWorld”

and as you might have imagined, it does exactly as it is told 🙂 We will get to these details later. To have a formal understanding on how programming works, you must be really clear on what you want to do, and how exactly you want to do it!

This is the first and last thing one must worry about. Like if I said, “you Good  are ” (well its a odd fact that you could still make out the meaning, but believe me “computers” take things quite “Literally”), this doesnt make sense because  what exactly I want to ask is “Are you Good?” So structure is important in program too. Again in the first example, how would you count all vowels in a sentence?

1. recall what vowels are! {A,E,I,O,U} , wow !

2. Read the sentence “Programming is simple conversation with the machine!” letter by letter and note down vowels from it!

3.Naievely, you can also take a copy and write 1+1+1….. as soon as you encounter a vowel in the sentence.

4. To be sane enough, start reading from left to right!

Simple isnt it? You see how I broke a “Task” to smaller steps of what needs to be done. This is an important concept.

Now first time my teacher would teach me how to find vowels like this, but the next time she says me to find vowels, its a “Task” we should already know. This is the concept of a function. It is always a good idea to learn to define new short “verbs” for actions which you do often, say you work in a brick Kline, and the owner asks you “to collect mud, shape it and then bake it”  , would it be easier for him to “invent” a single word  for the whole process say “comisab” or something! and everyone just understands what the owner is saying.

So I will end this topic by summarizing 2 things what i think is important for writing a program:

1. Manually first do what you want to instruct to your computer in pen and paper. Its like a translation, first your need to know your mother language!

Did you Know? Programme comes from word Pro meaning before, and graphe meaning write, which probably sums up,what you need to do before writing your first code!Think about it before writing!

2.Try to break the process to as simple as possible and group them as small “tasks”.

Next Tutorial:

Installing python in Linux machine/Windows. Editors

# Timed Encryption using Google and other news sites

I developed this crypt addiction long back, and was strengthened by “The Vinci Code”.We all know that we can encrypt some message with a “key”, which is shared by two parties(say alice and bob).Now most of these encryption are static in time.i.e. Say I encode “Arsenous” as “Bstfopvt” with padding 1.Of course this is a very simple example exposed to all kinds of attacks for reversal without keys.But lets say we can in principle encode a attack resistant code using a key.But this encoded text as well as the key is static in time, that is the encoded text can be decoded anytimme in future.

But what if the encoded message were to self destruct? what if we loose access to the key after sometime.This thought came to me sometime back.For this we need a time variable which is universal in nature.But it is diffucult to find something soo static in internet that is shared..well other than Google :p.(Death of Google?) ..

The procedure is then simple as outlined in the diagram.The encryption is done basically with a modified key based on some search result on Google or any news channel (24 hours change).Once the contents of the webpage is changed the same “key” would translate to a different modified key thus destroying decipherability of encrypted text.

The procedure can also be used to shared timed keys for MD5 irreversible encrypt.

This leaves the encrypted code vulnerable to attacks like freq analysis, etc .But what if the encrypted message is continuously changed such that along with the containts of the UTV(universal time variable) .As soon as UTV changes from its original state at which the msg was encrypted , the msg is encrypted using T(key)=f(key,D) where ‘D'(relative change in UTV).

This is mainly for fun 😛 but might have inteeresting applications when slightly modified.Like destruction of message once it s tried to decode incorrectly.Entropy of such encodings etc..

Im looking for someone to code this concept using php for a web based portal ..so if you are interested plz mail me or write in the comment..:)

# Hall Effect- a different outlook for seeing Hall coefficient

Hall effect is an extraordinary phenomena which involves generation of a subsidiary Electric field $V_H$ when a electric field and magnetic field is applied to a conductor. You can get a basic introduction from wiki. If you are not familiar with it

But familiar Hall effect introduces it through a bound rectangular boundary, which limits the current destity in y direction to zero in steady state. In brief the Hall coefficient is defined as $R_H=\frac{E_y}{J_x B}=\frac{1}{ne}$

where ”$J_x$” is the current density of the carrier electrons, and $E_y$ is the induced electric field,” n” is the carrier charge density and e is the carrier charge.

But consider the Hall effect in a 2D ring.The electric field applied in the ends if inner and outer ring and the magnetic field in the transverse direction.Then solving the Lorentz force equation along with Drude theory model (eqn 1)we get a 2 coupled equation,which can be solved directly

$\frac{dp}{dt}=-e(E+v\times B)-\frac{p}{\tau}$—–(1)

which gives from drude theory (see Ascroft Mermin)

$\sigma_0 E_x=\omega_c \tau J_y+J_x ----(2) \\ \sigma_0 E_y=-\omega_c \tau J_x+J_y----(3)$

Solving the two equations by multiplying the equations successively by $p_x,p_y$ and adding/sutracting we get

$\sigma_0 (E\times J)_z=\omega_c \tau J.J$—-(4)

and

$\sigma_0 (E. J)_{xy}= J.J$—-(5)

where $\sigma_0 (E\times J)_z=E_xJ_y-E_yJ_x$—-(6)

$\sigma_0 (E. J)_{xy}=J_xE_x+J_yE_y$—-(7)

So we can define $tan (\theta_H)=\omega_c\tau$

which in common terms is the hall angle

where $\omega_c=\frac{eH}{m}$

Therefore the relation can be written in a more generally

$\frac{tan (\theta_H)}{H}=\frac{\sigma}{ne} \\using \\ \sigma=\frac{ne^2\tau}{m}$

The hall coefficient would then be defined by

$R_H=\frac{tan (\theta_H)}{\sigma H}=\frac{1}{ne}$

The angle between the net electric field and the net current is given by the angle $tan (\theta)$

This to me is a more general statement for Hall coefficient which reduces to the normal hall coefficient if we consider appropriate boundaries and noticing tan(theta) must be dimensionless and considering $E_x< we get

$R_H=\frac{E_y}{J_x H}=\frac{1}{ne}$

which is the usual hall-coeficient.

Therefore the definition of Hall coefficient should be the tangent of Hall angle between the net Electric field and current in the conductor per unit applied magnetic field per unit conductivity.

Also this would help to resolve some issues regarding energy conservation in hall effect.

Suggestions are welcome.

Aritra Kundu

PS:here is an interesting article regarding hall effect in circular geometry http://www.jstor.org/stable/983911?seq=4

# High magnetic Field white dwarfs exceeding Chandrashekhar Mass Limit

In winter last year, I studied the effects of high magnetic field on Astrophysical objects ,specially related to the mass of a white dwarf.The origin of high magnetic field is stated briefly by http://www.iiap.res.in/meet/chandra/Chandra_files/Tout.pdf. Where it is briefly discussed about the origin of such high magnetic field white dwarfs and observational statistics.
In the article about super chadrasheker supernova from WD,they state that”Chandrasekhar’s 1931 model of stellar collapse was elegant and powerful; it won him the Nobel Prize,” says Nugent. “But it was a simple one-dimensional model. Just by adding rotation one can exceed the Chandrasekhar mass, as he himself recognized.”

Some readups on the topic/news are:
http://www.lbl.gov/Science-Articles/Archive/Phys-weird-supernova.html
http://iopscience.iop.org/0004-637X/744/1/69

In my analysis, I used quantized hall effect-like phenomena for the relativistic electron degeneracy pressure, and calculated the mass limit analytically.As a function of the residual magnetic field, the mass of white dwarf seemed to exceed the chandrashekhar mass limit.
here is my analysis: http://arxiv.org/abs/1204.1463v1
here is the computational (more detailed) analysis of the problem by my guides PhD student later.

http://arxiv.org/pdf/1204.1262.pdf

# TIPS for preparing input file for Molecular dymnamics simulations using DL_POLY

DL_POLY is a classical Molecular dynamics software by http://simx.epfl.ch/simx/en/dl_poly.html

,which is tweaked to work best with large macro molecules.It best simulates Organic molecules and thus is of use in pharmaceutical purpose of drug design etc

The preparation of the input file i.e the CONFIG file is one of the most troublesome episodes that I had.

Firstly, use aten to prepare the basic setup box.You can read throught their manual ,but the disorder button didnt work much for me from the GUI.I had to write script, i.e. use it from commandline to make the disorder work.

Check the output file,such that the output is in sequence,i.e. it follows some definite pattern.

Ensure that you dont have molecules towards the edges of the box, if you are usin periodic boundary condition, as these will culminate in errors during simulation.

If the resulting CONFIG is not in sequence, adjust some molecules and regenerate file again.Check using a program that you have ordering/pattern
as you need to specify the pattern in your FORCE file.

once this is done identify the pattern and write the FORCE field file, with the parameters of potential appropriately chosen from different models(CHARAMM, AMBER,etc etc.)

# Diracs Puzzle

I will post here with some musings I came across another blog:
http://www.minustwofish.com/Here is a question that was made to Dirac, the great genius !Here is it

Three fishermen come back from the sea. Each collapses in their respective tent. Fisherman #1 wakes up and decides to get his share of the catch. He counts the fish, realizes it is not a number divisible by three, throws away one fish to the sea correcting the situation, and takes a third of the remaining fish into his tent. Fisherman #2 wakes up later and also decides he is going to get his share of the catch. Fisherman #2 wants a third of the fish he sees. It is not a multiple of three, but he throws away one fish and takes a third of the fish and goes to sleep. Fisherman #3 wakes up after, and does the same: he throws away one fish, takes a third of the fish, and goes into his tent.
What is the smallest number of fish for which this would happen?

Ah its simple isnt it?

Guess what was Dirac’s answer -2! no wonder he came up with the idea of filled negative electrons in Dirac sea