Can you review my code?

Hi Roy

Could you please review this and give me idea how to make this approach to work.
(I will proceed to work along the line suggested by the hint, but was curious.)

def count_multi_char_x(word, x):
counter = 0
for i in range(0, len(word), len(x)):
if x[0:len(x)-1] == word[0:len(x)-1]:
counter += 1
return counter

Given that x is a character, meaning a str with length, 1. that argument is moot. The default stride of a range is, 1.

There is a lot of mechanics going on here. Is it really necessary to involve slices?

Given that we are only polling the string, is it necessary to generate an index sequence? We can, after all put the membership operator, in to good use with a conditional expression.

>>> def fn(word, x):
	counter = 0
	for char in word:
		if char == x: counter += 1
	return counter

>>> fn('mississippi', 'i')

Thank you very much. This makes it very clear.

1 Like