Codingbat - xyz_there (Python)

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

For people that are struggling to understand why he starts from 1 instead of 0 in the loop.
He says at 6:45 that python -1 index starts from last character. The issue is that codingbat didn't anticipate this solution and didn't input a test that would fail with this.
An example of a test that would fail would be a string "xyz."
It contains one "xyz" and no ".xyz" so it should give True for this task, but -1 checks if last character is not a dot, and since it is a dot it would give False.
So his solution is correct.

As for why he does -2 after len(str) that's because of "reading frame". Python is forgiving with out of bound index range, so if for example:
str = 'abcd' # That's a 4 length string
test = str[0:1000] # You're asking to assign position 0 to 1000 of string "str" to value test.
This would work in Python. If you do print(test) it would print "abcd" as Python is very forgiving with slicing and indexing (even though many of positions you asked for dont exist).

But python is not everything, you will likely learn multiple other languages in future and they have their own rules. It's best to practice python in such way that the way you code in python can be applied to Java, JS, C etc.

If you tried to do the same thing in Java it would give outofbounds error, if you tried to do this in JS it would return an empty string.
That's why he does -2 because we're looking for a string of length 3 (xyz).

If you take the above str = 'abcd' as example, in a loop i starts with 0. When u look for 3-length string then:
When i is 0 -> You look for [0, 1, 2] positions [0:3]
When i is 1 -> You look for [1, 2, 3] positions [1:4]
When i is 2 -> You look for [2, 3, 4] positions [2:5]
(...)
But when length is 4, positions that exist are 0 1 2 3. So you don't want to ask for position 4 or you get errors in other languages.
So len(str) is 4, len(str) - 2 = 2. When i turns 2 then "when is 2" doesn't execute anymore. So the last time you asked for positions was for [1, 2, 3] (str[1:4]).
The reason you can do str[1:4] even though 4 doesn't exist is because end value in string slicing is exclusive.

Hope this helps to anyone still learning in 2024!

Kamil-rfqn
Автор

I had solved this similar to the third method except that I couldn't figure out how to make it pass for the .xyzxyz test. Thanks for helping!

snootking
Автор

whoever came up with that 1st approach is amazing. I would've never thought about it.

MwSama
Автор

I have a question. For the 3rd solution: "for i in range(1, len(str) - 2, 1):" --> Why is it len(str) _minus two?_ And what is the 1 for at the end?
Thanks for all your solution videos! They really help me as a beginner.

filippians
Автор

Try approach 2 for the following example: "x.xyzyz"

JavaTutorRU
Автор

i don't get approach 2 if anyone can help me?

karimraef
Автор

why do you set i = 1 and not i = 0 in method 3?

maxhsu
Автор

Can someone please tell me why this doesn't work? :

if len(str) >= 3 and str[0:3] == "xyz":
return True

for i in range(1, len(str)-2):
return str[i:i+3] == "xyz" and str[i-1] != "."
return False

abdallahsiyabi
Автор

def xyz_there(str):
for i in range(0, len(str), 1):
if str[i] == "x" and str[i+1:i+3] == "yz" and str[i-1] != ".":
return True
return False

maybenew
welcome to shbcf.ru