I'm trying to make a method that scans a list (my list comes from an external file that uses readlines () to assemble a list of strings) and compares with a string passed by parameter. The return for my test run needs to be a len (). I have already tried in several ways and the only way I got was this one, however, I can only know the number of characters in the list, one at a time and also I can not calculate a large character. I'm literally stuck.
def buscar(self, string):
arq = self.arquivo
for linha in arq.readlines():
novaLinha = []
novaLinha.append(linha.rstrip())
contTotal = 0
for string in novaLinha:
for p in string:
cont = 0
for w in string:
if w.startswith(p):
cont += 1
else:
cont = 0
contTotal += cont
print( p + ' aparece ' + str(cont) + ' vezes nas palavra ' + string)
This is the test I need to pass.
def test_01_buscar(self):
arquivo = open('texto1.txt', 'r')
sf = SearchFile(arquivo)
self.assertEqual(len(sf.buscar('a')), 2)
self.assertEqual(len(sf.buscar('b')), 1)
self.assertEqual(len(sf.buscar('c')), 1)
self.assertEqual(len(sf.buscar('z')), 0)
self.assertEqual(len(sf.buscar('pinha')), 1)