having downloaded this code and tested it myself, I see that you will need some casting somewhere here... I would cast the weights to int, and remove the casts around `len()` here and around `factor` below in the loop condition