# Pb with headers in table()

Here is the forgotten function which list all the preferences on a list of candidates

def All_pref(cand=["A","B","C","D"],code=1) :
ncand=len(cand)
Scand=sorted(Set(cand))
all_pref=Arrangements(Scand,ncand).list()
all_pref1=[str(Word(x)) for x in all_pref]
if code==1 :
return ncand
if code==2 :
return all_pref1


I have a problem with the following function :

# Borda(cand=["A","B","C","D"],ne=[18,16,14,12,11,20,19,14,16,12,2,1,0,0,20,16,13,15,11,10,9,8,7,5],cond=0)
# cond = 0 redonne la répartition des préférences
# cond = 1 donne le classement des candidats sur chaque type de préférences
# cond = 2 donne les scores sur chaque candidat
# cond = 2 donne l'accumulation des scores sur chaque candidat
# cond = 3 déclare le vainqueur au sens de Borda.
def Borda(cand=["A","B","C","D"],ne=[18,16,14,12,11,20,19,14,16,12,2,1,0,0,20,16,13,15,11,10,9,8,7,5],cond=0) :
Ap=All_pref(cand,2)
cond1=['$\\tiny${}{}{}{}$'.format(*w) for w in Ap] cond2 =[""]+ [r'${}$'.format(*w) for w in cand] cond3 =[r'${}\$'.format(*w) for w in cand]
rank=[[x.find(l) for x in Ap] for l in cand]
score_de_Borda=[[abs(x.find(l)-len(cand)) for x in Ap] for l in cand]
total_score_de_Borda=[add([score_de_Borda[j][i]*ne[i] for i in range(24)]) for j in range(len(cand))]
max_total_score_de_Borda=max(total_score_de_Borda)
ind_max_total_score_de_Borda=total_score_de_Borda.index(max_total_score_de_Borda)
vainqueur_de_Borda=cand[ind_max_total_score_de_Borda]
if cond==0 :
return t1
if cond==1 :
return t2
if cond==2 :
return t3
if cond==3 :
if cond==4 :
return vainqueur_de_Borda


and here is a call to the different variations.

show(LatexExpr(r"\text{Le tableau des rangs est :}"))
show(Borda(["A","B","C","D"],[18,16,14,12,11,20,19,14,16,12,2,1,0,0,20,16,13,15,11,10,9,8,7,5],1))
show(LatexExpr(r"\text{Le tableau des scores est :}"))
show(Borda(["A","B","C","D"],[18,16,14,12,11,20,19,14,16,12,2,1,0,0,20,16,13,15,11,10,9,8,7,5],2))
show(LatexExpr(r"\text{Les scores totaux de chaque candidat sont :}"))
Borda(["A","B","C","D"],[18,16,14,12,11,20,19,14,16,12,2,1,0,0,20,16,13,15,11,10,9,8,7,5],3)
show(LatexExpr(r"\text{Le vainqueur d'un décompte à la Borda est : }"),
Borda(["A","B","C","D"],[18,16,14,12,11,20,19,14,16,12,2,1,0,0,20,16,13,15,11,10,9,8,7,5],4),LatexExpr(r"."))


All is fine but if inside the function i remove the# and allow the header(s), I have an error which I do not understand since some time it works and some time not. (I am working with sagemath 9.2 under windows).

edit retag close merge delete

Some code is missiing:

NameError: name 'All_pref' is not defined


What 'All_pref' ?

( 2021-01-07 12:35:28 +0200 )edit

Sorry, my notebook become hudge because I am trying to compute election result from all systems.

( 2021-01-07 15:10:08 +0200 )edit

In any case, when you have some issue, you should provide a minimal example showing the problem.

( 2021-01-07 16:04:51 +0200 )edit

It works for me, i can not reproduce you issue when i remove the two #

( 2021-01-07 16:07:36 +0200 )edit

I will try with an other computer. But the main thing is that it works for you.

( 2021-01-07 17:15:08 +0200 )edit

Sort by » oldest newest most voted

I think you can greatly simplify your code:

• use f-strings instead of the format method;
• use HTML instead of LaTeX to format headers, since elements in headers are not math expressions;
• place conditionals at proper places, not at the end.

This works for me in a Jupyter notebook:

def All_pref(cand=["A","B","C","D"], code=1) :
ncand = len(cand)
Scand = sorted(Set(cand))
all_pref = Arrangements(Scand,ncand).list()
all_pref1 = [str(Word(x)) for x in all_pref]
if code==1 :
return ncand
if code==2 :
return all_pref1

def Borda(cand, ne, cond=0) :
Ap=All_pref(cand,2)
cond1 = [w for w in Ap]
cond3 = [f"<em>{w}</em>" for w in cand]
cond2 = [""] + cond3
if cond==0 :
rank = [[x.find(l) for x in Ap] for l in cand]
score_de_Borda = [[abs(x.find(l)-len(cand)) for x in Ap] for l in cand]
if cond==1 :
for i in range(24)]) for j in range(len(cand))]
if cond==3 :
if cond==2 :
max_total_score_de_Borda = max(total_score_de_Borda)
ind_max_total_score_de_Borda = total_score_de_Borda.index(max_total_score_de_Borda)
if cond==4 : # vainqueur_de_Borda
return cand[ind_max_total_score_de_Borda]

cand = ["A","B","C","D"]
ne = [18,16,14,12,11,20,19,14,16,12,2,1,0,0,20,16,13,15,11,10,9,8,7,5]
show(html("Le tableau des rangs est :"))
show(Borda(cand,ne,1))
show(html("Le tableau des scores est :"))
show(Borda(cand,ne,2))
show(html(f"Les scores totaux de chaque candidat sont : {Borda(cand,ne,3)}"))
show(html(f"Le vainqueur d'un décompte à la Borda est : {Borda(cand,ne,4)}."))

more

I am trying to also post a link to a SageMath Cell with the above code, but, for unknown reasons, I get an error each time a write the long permalink. Anyway, this is the short temporary link: https://sagecell.sagemath.org/?q=apzuww

( 2021-01-07 20:12:02 +0200 )edit

Hello, @Juanjo! In case you are trying to post the permalink as a comment, unfortunately, the link tends to be longer than the allowed number of characters for a comment. The odd thing is that sometimes there is no warning when that happens: Ask SageMath simply refuses to accept the publication.

In any case, is it my impression or more characters are allowed now?

( 2021-01-08 01:20:18 +0200 )edit

I first tried to add the permalink at the end of the answer, but I got an "internal server error" which prevented it to be published. Comments seem to be limited to 800 characters, too few for a permalink if the code is a bit complex.

( 2021-01-08 01:42:50 +0200 )edit

Thanks I was not aware of f-strings. And what about permalink. If I have an html page that can be usefull tosagemath users is there a way to publish it publicly ?

( 2021-01-08 15:16:42 +0200 )edit

Thanks a lot but I am surprise by the behavior of this little modification of 'cond 3' which gives the image added in the question.

( 2021-01-08 16:57:27 +0200 )edit