¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita0
Return True if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period(.). So "xxyz" is True but "x.xyz" is False.
One of the many correct answers is: def xyz_there(str): for i in range(len(str)): if str[i] != '.' and str[i+1:i+4] == 'xyz': return True if str[0:3] == "xyz": return True return False Can anyone explain what the line of code means on line 3? Thanks in advance. Source of qn: codingbat.com
7 Respuestas
+ 4
str[i] != '.' checks if the character at index i is not a dot.
str[i+1:i+4] == 'xyz' checks if the substring from indexes i+1 to i+3 is "xyz".
+ 3
@Kelvin: Yes, != is the operator to check if the two operands are not equal.
+ 2
Zen is right. the third line you are asking is that very logic you need to implement to get the result.
0
So is it safe to say that "!=" is exactly "not =="?
etc. str[i] == '.' means that both of these operands are equivalent
0
Ok it's all clear now. Thank you very much =)
0
and you dont need to iterate i up to len(str) but up to len(str)-3
0
hi