Archive

Posts Tagged ‘Python’

Project euler problem 20


This problem is very simple one liner in python.

n! means n × (n − 1) × … × 3 × 2 × 1

For example, 10! = 10 × 9 × … × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

Find the sum of the digits in the number 100!

import math
print sum([int(x) for x in  str(math.factorial(100))])

Answer: 648

real 0m0.164s
user 0m0.031s
sys 0m0.108s

Regular Expressions in Python


Here is the way of matching an expression in Python program. Its absolutely very easy to do this. All that you need to do is importing regular expression re and use re.search(pattern,string).  The below example gives a brief idea of how it is done.

#Author:Vasanth Raja Chittampally
import re
pat=raw_input("Enter the pattern:")
f=open('b.txt', 'r')
l=f.read()
match=re.search(pat,l)
if match :
	print match.group()
else:
	print 'pattern was not found'

Selection sort in Python


Python has lists that come very handy in doing many complex works such as finding min, max, sort etc., Here I’ll show sorting a list using selection sort and the other function of one line sort() function.

#Author: Vasanth Raja Chittampally

numbers=[]
n=int(raw_input("Enter n:"))
for i in range(0,n):
    numbers.insert(i,int(raw_input("Enter numbers[%d]: " %i)))

for i in range(0,n):
    min =numbers[i]
    min_indx=i
    for j in range(i+1,n) :
        if min > numbers[j] :
            min =numbers
            min_indx=j
    temp=numbers[min_indx]
    numbers[min_indx]=numbers[i]
    numbers[i]=temp
print numbers

The above same thing can be done with just one line built in function of python sort(). like “numbers.sort()”

Loops in python illustrated


In python loops are very easy to use, if you know already while, for in C or Java those are almost similar in Python. The amount of code that we have to write in Python is much lesser when compared to many other languages, so its easier and saves a lot of time.

Ex1: In this example I’m using the “for” loop and “list”. You may be scared of what a list is. List is pretty similar to array in C or Java with many other built in function. Let us take a look at these things at work.

numbers=[]
i=1
while i<10:
    print "i above %d" %i
    numbers.append(i)
    print "number now is:", numbers
    i=i+1
    print "i below is %d" %i

Output

i above 1
number now is: [1]
i below is 2
i above 2
number now is: [1, 2]
i below is 3
i above 3
number now is: [1, 2, 3]
i below is 4
i above 4
number now is: [1, 2, 3, 4]
i below is 5
i above 5
number now is: [1, 2, 3, 4, 5]
i below is 6
i above 6
number now is: [1, 2, 3, 4, 5, 6]
i below is 7
i above 7
number now is: [1, 2, 3, 4, 5, 6, 7]
i below is 8
i above 8
number now is: [1, 2, 3, 4, 5, 6, 7, 8]
i below is 9
i above 9
number now is: [1, 2, 3, 4, 5, 6, 7, 8, 9]
i below is 10

The above same thing is achieved using for loop as

numbers=[]
for i in range(1,10):
    print "i above %d" %i
    numbers.append(i)
    print "number now is:", numbers
    i=i+1
    print "i below is %d" %i

range(1,10) assigns “i” to values “1 to 9”, by default the increment is 1 in the range function. If you want to increment the range value by 2. You have to give the syntax as range(1,10,2) which assings i to values 1,3,5,7,9

Finding GCD of given two numbers in Python


Here is a simple program to find the gcd of given two numbers. Here the procedure I’m using is simple logic that we have learned at our school level.

d1=int(raw_input("Enter a number:"))
d2=int(raw_input("Enter another number"))
rem=d1%d2
while rem!=0 :
    d1=d2
    d2=rem
    rem=d1%d2
print "gcd of given numbers is : %d" %(d2)

In the above program takes input from the command line using raw_input() (takes input as a string). And rest of the program is very simple. I run it in python shell it worked fine.

My First Python Program


This is the fibonacci number program in python

def fibo(n) :

if n==0 :
return 0
elif n==1 :
return 1
else :
return fibo(n-1)+fibo(n-2)
print fibo(3)
print fibo(9)
print fibo(13)

This is my first ever Python program. Using IDLE(interactive python GUI). Python is really cool language and its easy too!!

 

Different types of data types


There actually different types of datatypes as the programming languages specify them. Here I’m going to specify four types of them, Almost all predominant languages should have datatypes belonging to the following four kinds of datatypes

1) Statically typed language

2) Dynamically typed language

3) Strongly typed language

4) Weakly typed language

Statically typed language:

In this kind of languages variables should be assigned types at the compile time itself( and fixed at compile time).  All the variables must be used before using them.

ex: Java, C, C++ ect.,

Dynamically typed language

It is exactly opposite to that of statically typed language. The type of the variable is decided at the runtime rather than at compile time.

ex:VBscript, Python

Strongly typed language

These kind of typed language don’t allow you to change the type of a variable once the type is decided(at runtime or at compile time). Once a variable is declared as String in Java during runtime you cannot convert the type of the variable without explicit type conversion.

ex: Python, Java

Weakly typed language

This type of languages are exactly opposite to that of Strongly typed languages, you can change the type of the variable at anytime during the execution of the program.

ex: VBScript