Need help understanding this code about recursion in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Need help understanding this code about recursion in python

Hi! I'm doing an online course aside the coursera ones about python. In a practice quiz i got this problem: The count_users function recursively counts the amount of users that belong to a group in the company system, by going through each of the members of a group and if one of them is a group, 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 print(count_users("sales")) # Should be 3 print(count_users("engineering")) # Should be 8 print(count_users("everyone")) # Should be 18 And i had to fix it so the results were 3, 8 and 18 respectively given those parameters I just went and moved the count += 1 as an else: statement because i felt it was counting all users even if they were not on a group and it looked like this and reurned all answers as requested: 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 print(count_users("sales")) # Returned 3 as requested print(count_users("engineering")) # Returned 8 as requested print(count_users("everyone")) # Returned 18 as requested My problem is that i don't understand a lot from that script 1) It only runs on the practice quiz interpreter, i tend to edit and run my exercises in vscode for review purposes but it doesn't run because get_members is not defined. 2) I don't understand where that get_members(group) came from 3) How the program knows that "sales", "engineering" and "everyone" has those values? Can someone give me a step by step explanation? Thanks :)

15th May 2022, 4:56 PM
Joan Sanchez
Joan Sanchez - avatar
2 Answers
+ 5
Hi, you have to derive it from the context and the naming will guide you. Finally you had to find out, that you shouldn't add up if the member is a group. obviously u did it.
15th May 2022, 5:21 PM
Oma Falk
Oma Falk - avatar
+ 3
This is not a complete program. Maybe the rest of the program was used during the course and the task here is to add a function. It doesn't really matter. You just have to get the function to work as intended. The rest (the other functions, the data, etc.) is taken care of in the background. What the function does is loop over some form of container (like a dictionary) that contains both users and groups of users to count the number of users. If an item is a user it is counted. If it is a group the same function is used to loop over this group to count its members (this is where recursion comes into play). Here is a complete program that does the same using a dictionary as container. https://code.sololearn.com/cfAb6OfaE1zf/?ref=app
15th May 2022, 11:03 PM
Simon Sauter
Simon Sauter - avatar