1

I am new to python. I am trying to convert one of my c programs to corresponding python program, however I am not able to use global variable in python. My code in both c and python are :


#include <stdio.h>
int globalcount;

void noofways(int firstnumchosen,int sum,int numofnum)
{
if(sum<0)
    return;

if(sum==0 && numofnum!=0)
    return;

if(sum==0 && numofnum==0){
    globalcount++;
    return;
}

if(numofnum<=0)
    return;

if(firstnumchosen>sum)
    return;

noofways(firstnumchosen+1,sum,numofnum);
noofways(firstnumchosen+1,sum-firstnumchosen,numofnum-1);
}

int main()
{
noofways(1,8,3);
printf("Required number: %d",globalcount);
return 0;
}

def noofways(firstnumchosen, sum, numofnum):
    global count
    count=0
    if sum<0:
        return 
    if sum==0 and not(numofnum==0):
        return
    if sum==0 and numofnum==0:
       count+=1
       return
    if numofnum<=0:
       return
    if firstnumchosen>sum:
       return
    noofways(firstnumchosen+1,sum,numofnum)
    noofways(firstnumchosen+1,sum-firstnumchosen,numofnum-1)

res=noofways(1,8,3);
print count

I think I know how to declare a global variable in python, but I am having problem in figuring out how to use that variable with recursion.

7
  • 1
    In what way is the code not working? It looks to me like it is printing 0, which is what I would expect it to do. Furthermore, why are you using a global variable with recursion? Why not just return the variable you're interested in? Commented Oct 19, 2012 at 5:02
  • 1
    move count = 0 outside the function Commented Oct 19, 2012 at 5:02
  • 3
    I would note that Python and C are so different in terms of abstraction, trying to reimplement code from one language to another is a bad idea. The best thing is to ask yourself what the original was trying to do, then write it from scratch - that way you'll end up with Python that works in a Pythonic way, which will be far easier to read and maintain. Commented Oct 19, 2012 at 5:03
  • It should return 2. I need to increment the count once in a while (when i get the sum). Commented Oct 19, 2012 at 5:04
  • @J.F.Sebastian : Then the interpreter says "global name 'count' is not defined" Commented Oct 19, 2012 at 5:05

1 Answer 1

4

Each recursive call will set the count back to 0

def noofways(firstnumchosen, sum, numofnum):
    global count
    # don't set count to 0 here
    if sum<0:
        return 
    if sum==0 and not(numofnum==0):
        return
    if sum==0 and numofnum==0:
       count+=1
       return
    if numofnum<=0:
       return
    if firstnumchosen>sum:
       return
    noofways(firstnumchosen+1,sum,numofnum)
    noofways(firstnumchosen+1,sum-firstnumchosen,numofnum-1)

# set count to 0 here
count = 0
res=noofways(1,8,3);
print count
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.