'recursively calling the function and counting the members. But it has a bug! Can you spot the problem and fix it?

def count_users(group):
    count = 0
    for member in get_members(group):
        count += 1
        if is_group(member):
            count += count_users(member)
    return count


Solution 1:[1]

I guess it should be like:

def count_users(group):
    count = 0
    for member in get_members(group):
        if is_group(member):
            count += count_users(member)
        else
            count += 1
    return count

if you don't consider groups as users.

It seems there is no other bug or whatsoever. If the problem remains, you should maybe publish the code for the other parts of the project as well.

Solution 2:[2]

def count_users(group):
  count = 0
  for member in get_members(group):
    count += 1
    if is_group(member):
      count += count_users(member)-1
  return count

if a member is a group add the number of members in that group - 1 (since u added 1 previously as if it was a menber, but it is not, its a group) here is the code:

Solution 3:[3]

def count_users(group):
  count = 0
  for member in get_members(group):
    if is_group(member):
      count += count_users(member)
    else:
      count += 1 #Moved from the outside of the "IF" block and put into the ELSE 
                 #part of the IF statement. This removes the double counting. 
  return count

print(count_users("sales")) # Should be 3
print(count_users("engineering")) # Should be 8
print(count_users("everyone")) # Should be 18

Solution 4:[4]

This is solved here: https://brainly.in/question/17342864

def count_users(group):
  count = 0
  for member in get_members(group):
    count += 1
    if is_group(member):
      count -=1
      count += count_users(member)
  return count

print(count_users("sales")) # Should be 3
print(count_users("engineering")) # Should be 8
print(count_users("everyone")) # Should be 18

I think this is poor example, as if you break it down it will render lots of name errors - Coursera defines the functions called in the example like is_group or get_members without providing a proper explaination.

Solution 5:[5]

def count_users(group):
  count = 0
  for member in get_members(group):
    count += 1 #it is also increasing when the member is a group
    if is_group(member):
      **count -= 1** #if the member is a group, decrease 1 which was increased previously
      count += count_users(member)
  return count

print(count_users("sales")) # Should be 3
print(count_users("engineering")) # Should be 8
print(count_users("everyone")) # Should be 18

Solution 6:[6]

def sum_positive_numbers(n):
  count=0
  if n<1:
    return n
  else:
    return n + sum_positive_numbers(n - 1); 

print(sum_positive_numbers(3)) # Should be 6
print(sum_positive_numbers(5)) # Should be 15

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 agayev169
Solution 2
Solution 3 Datawarrior
Solution 4 Waly
Solution 5
Solution 6 Stéphane Blond