Skip to content

Instantly share code, notes, and snippets.

@brunosan
Last active September 12, 2023 06:33
Show Gist options
  • Select an option

  • Save brunosan/96288a8612894fca718aacbcc501ee09 to your computer and use it in GitHub Desktop.

Select an option

Save brunosan/96288a8612894fca718aacbcc501ee09 to your computer and use it in GitHub Desktop.
Python D'Hont Formula
"""
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to [http://unlicense.org]
"""
def dhont(nSeats,votes,verbose=False):
"""
nSeats is the number of seats
votes is a dictionary with the key:value {'party':votes}
verbose is an option to print designation info
"""
t_votes=votes.copy()
seats={}
for key in votes: seats[key]=0
while sum(seats.values()) < nSeats:
max_v= max(t_votes.values())
next_seat=list(t_votes.keys())[list(t_votes.values()).index(max_v)]
if next_seat in seats:
seats[next_seat]+=1
else:
seats[next_seat]=1
if verbose:
print("{} Escaño: {}".format(sum(seats.values()),next_seat))
for key in t_votes:
print("\t{} [{}]: {:.1f}".format(key,seats[key],t_votes[key]))
print("\b")
t_votes[next_seat]=votes[next_seat]/(seats[next_seat]+1)
return seats
"""
Example:
nSeats = 8
votes = {'a':168,'b':104,'c':72,'d':64,'e':40}
seats=dhont(nSeats,votes,verbose=True)
1 Escaño: a
a [1]: 168.0
b [0]: 104.0
c [0]: 72.0
d [0]: 64.0
e [0]: 40.0

2 Escaño: b
a [1]: 84.0
b [1]: 104.0
c [0]: 72.0
d [0]: 64.0
e [0]: 40.0

3 Escaño: a
a [2]: 84.0
b [1]: 52.0
c [0]: 72.0
d [0]: 64.0
e [0]: 40.0

4 Escaño: c
a [2]: 56.0
b [1]: 52.0
c [1]: 72.0
d [0]: 64.0
e [0]: 40.0

5 Escaño: d
a [2]: 56.0
b [1]: 52.0
c [1]: 36.0
d [1]: 64.0
e [0]: 40.0

6 Escaño: a
a [3]: 56.0
b [1]: 52.0
c [1]: 36.0
d [1]: 32.0
e [0]: 40.0

7 Escaño: b
a [3]: 42.0
b [2]: 52.0
c [1]: 36.0
d [1]: 32.0
e [0]: 40.0

8 Escaño: a
a [4]: 42.0
b [2]: 34.7
c [1]: 36.0
d [1]: 32.0
e [0]: 40.0

print(steats)
{'a': 4, 'b': 2, 'c': 1, 'd': 1, 'e': 0}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment