SageMath code is written in Python (with a few extensions by the preparser). Your code does not resemble Python. Indentation matters, no end is required, forloops are written as for b in s:, functions are defined as def bruteforce():, write print instead of puts.