Posts

Showing posts from November, 2010

fast way to get input from stdin - Python

I am looking for faster way for taking input from stdin in Python. So far I found three methods. input() - which is suitable for integers, raw_input() - reads the input as string and another one is sys.stdin.readline(). So far I have found sys.stdin.readline() is the fastest. Do you have any idea to read input (from console) in Python which is faster?

Python program to find the next palindrome

Yesterday I wrote a Python program to find the next palindrome to solve this problem from SPOJ . My first accepted solution took four seconds to run. Today I improved the code and now it takes 1.13 seconds. In the rank-list I found people solving this problem with Python program that finishes execution in less than half seconds. Wondering whether I am missing any trick or not using Python properly. :-S The problem is simple. A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros. As the number can be as large as 1000000 digits, stupid solutions won't finish it's execution in time (thus will get 'Time Limit Exceeded'). I am not going to explain my logic in detail, as I don't want to depri

Simple Infix to Postfix in Python

Today I wrote a Python program to transform an expression to reverse polish notation in order to solve the problem Transform the Expression and it worked fine. Here is my code: def rpn(s): var_list = [] symb_list = [] for c in s: if c >= 'a' and c <= 'z': var_list.append(c) elif c in ['^', '*', '/', '+', '-']: symb_list.append(c) elif c == ')': x = var_list.pop() y = var_list.pop() z = symb_list.pop() var_list.append(y + x + z) #print var_list, symb_list return var_list[0] t = int(raw_input()) while t: t -= 1 s = raw_input() print rpn(s) The algorithm is very simple which you should be able to follow if you study my code. You can uncomment the line #print var_list, symb_list to see a simulation. Note that the Python program I wrote is not the perfect infix

Prime Number Generator in Python using Sieve Method

I have implemented a prime number generator in Python using Sieve of Eratosthenes . Here is my code: import math high = 10000 root = int(math.sqrt(high) + 1.0) ara = [x % 2 for x in xrange(0, high)] ara[1] = 0 ara[2] = 1 x = 3 while x <= root: if ara[x]: z = x * x while z < high: ara[z] = 0 z += (x + x) x += 2 prime = [x for x in xrange(2, len(ara)) if ara[x] == 1] print prime I am looking for ideas to make my code faster. I tested my Python program using the code for solving a problem named Prime Generator in SPOJ . I could solve the problem correctly that took 3 seconds time and 3.8 MB memory. I am posting another code I found here . This one is similar implementation to mine but more Pythonic and much faster: nroot = int(math.sqrt(n)) sieve = [True] * (n+1) sieve[0] = False sieve[1] = False for i in xrange(2, nroot+1): if sieve[i]: m = n/i - i sieve[i*i: n+1:i] = [

Teach Yourself Python using Google's Python class

Recently I found an interesting video and later found this ! It's a collection of well thought-out tutorials and videos with exercises (right now not very useful for me though :p). It's done under Google Edu project. I have checked couple of videos and looked at the exercises. I think it's a very good resource for someone who wants to learn Python. So, if you are a python beginner you will find Google's Python Class useful, but if you already know these basic Python stuffs, what you can do is to share so that another Python newbie can get help. :)