Porting an algorithm in C to Python

Gerhard Häring gerhard.haering at gmx.de
Tue Aug 27 02:05:13 CEST 2002


* Raphael Ribeiro <gusraan at terra.com.br> [2002-08-26 16:49 -0700]:
> 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;
> }

Ok, let's port undocumented C code where I have no idea what it really
does to undocumented Python code where I have no idea what it really
does. It doesn't handle EOF by design, as input() is unsafe.

teste = 1
while 1:
    n = int(raw_input())
    if n == -1:
        break
    print "Teste %i\n%i\n\n" % (teste, ((1 << n)+1)*((1 << n)+1))
    teste += 1

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9  3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))




More information about the Python-list mailing list