Porting an algorithm in C to Python

Sean 'Shaleh' Perry shalehperry at attbi.com
Mon Aug 26 20:07:28 EDT 2002


On Monday 26 August 2002 04:49 pm, 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;
> }

teste = 1
while 1:
    input = raw_input("") # or perhaps some form of sys.stdin.read()
    if not input: break

    n = int(input)
    if n != -1:
        break
    
    print "Teste %d\n%d\n\n" % (teste, ((1<<n)+1)*((1 <<n)+1)))
    teste += 1 # or teste = teste + 1 under python 1.5.2
# end while

All of this is untested but the ideas here should point you in the right 
direction.
    




More information about the Python-list mailing list