Porting an algorithm in C to Python

Erik Max Francis max at alcyone.com
Tue Aug 27 02:09:57 CEST 2002


Raphael Ribeiro wrote:

> I couldn't port this , can someone tell me how this code would be if
> it was implemented in python? Any help would be appreciated..
> 
> int main()
> {
>   int teste=1, n;
>   while (scanf("%d", &n) == 1 && n != -1)
>     printf("Teste %d\n%d\n\n", teste++, ((1 << n)+1)*((1 << n)+1));
>   return 0;
> }

Something like (untested):

	test = 0
	while 1:
	    line = sys.stdin.readline()
	    if not line:
	        break
	    line = line[:-1]
	    n = int(line) # one number per line
	    if n == -1:
	        break
	    print "Test %d" % test
	    test += 1
	    print ((1 << n) + 1)**2
	    print
	

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ There is nothing so subject to the inconstancy of fortune as war.
\__/ Miguel de Cervantes
    Church / http://www.alcyone.com/pyos/church/
 A lambda calculus explorer in Python.



More information about the Python-list mailing list