# Calculate score in a pyramid score system

I am trying to calculate gamescores for a bunch over users and I haven't really got it yet. It is a pyramid game where you can invite people, and the people you invite is placed beneth you in the relations tree.

So if i invite X and X invites Y i get kickback from both of them. Let's say 10%^steps...

So from X i get 10% of his score and 1% from Y, and X get 10% from Y.

So to calculate this i was thinking that each "player" had a function that calculated his total score. This function had to be recursive and "know" how far in the tree it was so that it would kick back the right values.

```
def get_score(player):
if children:
score = player.points
for child in children:
score += child.points*math.pow(.1, get_ancestors(child))
score += get_score(child)
return score
else:
return player.points
```

But this doesnt work proper, it gives what i believe is the right values in some levels but not in others. So think my function is broken. Anybody got an idea on how to solve this?

Asked by:

**Edgar838**| Posted: 30-11-2021

# Answer 1

I doubt these two lines

```
score += child.points*math.pow(.1, get_ancestors(child))
score += get_score(child)
```

this is a simple recursive structure so i think something like below will suffice

```
score += get_score(child)*.1
```

and recursive beauty will take care of itself

you also do not need 'if children:' check so does it help

```
def get_score(player):
score = player.points
for child in children:
score += get_score(child)*.1
return score
```

Answered by: **Briony994**| Posted: 01-01-2022

# Answer 2

This can have very different implementations, depending on the way the score must be calculating :

Do you need to propagate the result of each gain, in real time ? In that case , you start from the bottom of the pyramid and give the feedback until the top.

Can you afford the result to be calculated at the end of the game for everybody ? In that cas you can just set a method on each player and only call the one at the top.

**E.G for the second option**

You used a functional approach. While this is valid, I am more into OO so I'll go this way :

```
class Player(object) :
def __init__(self) :
self.score = 0;
self.children = []
def updateScore(self) :
self.score = self.score + sum(((children.score * 10 / 100) for children in self.children))
class Pyramid(object) :
def __init__(self) :
self.top_child = Player()
def updateScore(self, player = None) :
if player == None :
player = self.top_child
for child in player.children :
self.updateScore(child)
child.updateScore()
```

You may want to use itertools to make it less CPU and memory intensive.

Answered by:**Carlos361**| Posted: 01-01-2022

# Similar questions

python - What's the best way to calculate a 3D (or n-D) centroid?

As part of a project at work I have to calculate the centroid of a set of points in 3D space. Right now I'm doing it in a way that seems simple but naive -- by taking the average of each set of points, as in:
centroid = average(x), average(y), average(z)
where x, y and z are arrays of floating-point numbers. I seem to recall that there is a way to get...

How Python calculate number?

This question already has answers here:

How to calculate a mod b in Python?

Is there a modulo function in the Python math library?
Isn't 15 % 4, 3? But 15 mod 4 is 1, right?

To calculate the sum of numbers in a list by Python

My data
466.67
465.56
464.44
463.33
462.22
461.11
460.00
458.89
...
I run in Python
sum(/tmp/1,0)
I get an error.
How can you calculate the sum of the values by Python?

python - How to calculate a date back from another date with a given number of work days

I need to calculate date (year, month, day) which is (for example) 18 working days back from another date. It would be enough to eliminate just weekends.
Example: I've got a date 2009-08-21 and a number of 18 workdays as a parameter, and correct answer should be 2009-07-27.
thanks for any help

python - How to calculate the scrape URL for a torrent

I've read the Bit-torrent specification and done a number of searches, trying to find out how I can get the seeds/peers/downloaded data from a torrent tracker (using Python). I can calculate the info hash from a Torrent no problem, which matches up with the info hash given by various working torrent applications.
However, when I try to get the information from the tracker I either timeout (the tracker is working) o...

datetime - How to use Python to calculate time

I want to write python script that acts as a time calculator.
For example:
Suppose the time is now 13:05:00
I want to add 1 hour, 23 minutes, and 10 seconds to it.
and I want to print the answer out.
How do I do this in Python?
What if date is also involved?

c# - Calculate percent at runtime

I have this problem where I have to "audit" a percent of my transtactions.
If percent is 100 I have to audit them all, if is 0 I have to skip them all and if 50% I have to review the half etc.
The problem ( or the opportunity ) is that I have to perform the check at runtime.
What I tried was:
audit = 100/percent
So if percent is 50
audit = 100 /...

python - Calculate time between time-1 to time-2?

enter time-1 // eg 01:12
enter time-2 // eg 18:59
calculate: time-1 to time-2 / 12
// i.e time between 01:12 to 18:59 divided by 12
How can it be done in Python. I'm a beginner so I really have no clue where to start.
Edited to add: I don't want a timer. Both time-1 and time-2 are entered by the user manually.
Thanks in advance for your help.

python - How to calculate positions of holes in a game board?

I'm making a game with Python->PyGame->Albow and ran into a problem with board generation. However I'll try to explain the problem in a language agnostic way. I believe it's not related to python.
I've split the game board generation into several parts.
Part one generates the board holes.
Holes are contained in a list/array. Each hole object has a mapping of angles relating to other...

# Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python