Vanity Plates -Problem Set 2 (CS50's Introduction to Programming with Python)

preview_player
Показать описание
hello, everyone and welcome to another video, in this video, I will explain and solve problem set 2, Vanity Plates.

if you haven't already submitted this problem, You have to try this on your own.
This video is to just give you a way to solve this problem, try solving it using a different approach, to get the most benefits out of this experience.

Don't forget to hit the like button and subscribe to the channel, and if you have any questions or want an explanation for a specific problem leave a comment down below.

Рекомендации по теме
Комментарии
Автор

this was very helpful. i could not think thru the logic to [index: ] to make sure everything after the first digit was also a a digit. I was slicing and dicing and failing all over the place. very nice.

SomeStuff
Автор

Freaking brilliant. I had 45 lines of code that passed check50 but not technically the requirements of the pset. I had to see how it was properly done. This is great. Thank you.

NickRoeder
Автор

I spent a whole day trying to find a way to check if there are middle numbers and finally decided to check a tutorial that was actually wrong.... Is it just me or this for a beginner with 1 month of experience in python is really difficult ? It's kind of discouraging to fail all the time

PrfeBboy
Автор

This worked for me

def main():
plate = input("Plate: ")
if is_valid (plate):
print("Valid")
else:
print("Invalid")

def is_valid(s):
if 6 >= len(s) and s[0:2].isalpha() and s.isalnum():
for char in s:
if char.isdigit():
index = s.index(char)
if s[index:].isdigit() and int(char) != 0:
return True
else:
return False
return True

if __name__ == "__main__":
main()

minibus
Автор

wow, amazing!! my solutions passed the test but one was about 40 lines long and the other was almost 50😶

getstart
Автор

Hi thanks a lot for the explaination it is very concise !

However, I don't understand the role of the very last 'return True'.
From what I understand, if all the 'if' statments are True, it goes all the way down to the last one and if the last one is True then it works. If not, there is the 'else' that returns False and then it is invalid.


So why after that 'else' do we still need the 'return True' ? Wouldn't it mean that even if the 'if' above return False, at the end, it overwrites it and it is still True ?

Thanks

tchavofrund
Автор

if we set int(char) != 0
wouldn't 'CS50' return False? Why does it return True??
Can someone please explain ?

studywithshin
Автор

can i do the for loop outside the if statement in line 10? like after that conditional, return true else false. then do the for loop?

imvhp
Автор

Thanks Omar for the amazing explanation, It really helped me a lot

abdullahadel
Автор

def main():
plate = input("Plate: ")
if not plate.isalnum() or (len(plate) < 2 or len(plate) > 6):
print("Invalid")
return False

if is_valid(plate):
print("Valid")
else:
print("Invalid")


def is_valid(s):
if s.isalpha() or s[:2].isalpha() and s[-2:].isnumeric() and s[-2:][0] != "0":
return True
else:
return False
main()

try this way too

aigerimabseit
Автор

Thanks. small improvement though, instead of "

for char in s:
if s.isdigit():
index = s.index(char)


you can say:

for i in range(len(s)):
if s[i:].isdigit() and s[i]!= '0':

"i" is your index

mohammadamintorabi
Автор

can you explain why there is a return True on line 18?

jessicaly
Автор

the code should fail with inputs with <letter><number><letter><number>, such as CS50P2.

MrKamoliddin
Автор

I don’t understand the s[0:2].isalpha() function in the program

chikamatthew
Автор

I like how i did check50 and instantly got 4 red ansers with this code

miquelr
Автор

For those who don't quite get it, hopefully below code is less cryptic.

def main():
plate = input("Plate: ")
if is_valid(plate):
print("Valid")
else:
print("Invalid")

def is_valid(s):
if not 2 <= len(s) <= 6 or not s[0:2].isalpha() or not s[2:].isalnum():
return False

stack = []
for i in s[2:]:
if i.isdigit():
stack.append(i)
if stack[0] == '0':
return False

elif stack and i.isalpha():
return False

return True

if __name__ == "__main__":
main()

ionet
Автор

Do you mind explaining line 13 and line 14 of your final code again? I don't quite get the syntax, it looks cryptic to me

lucascamarasa
Автор

Hi all.
I m trying to solve the problem but the elif statement is not working.
I checked everything but the CS50P has a result Valid.
Can you please help me with my mistake?
Here is my code:
def main():
    plate = input("Plate: ")
    if is_valid(plate):
        print("Valid")
    else:
        print("Invalid")

def is_valid(s):
    if len(s) >= 2 and len(s) <= 6 and s[2] != "0" and s[0].isalpha() and s[1].isalpha() and s.isalnum():
        return True
   elif s[2].isnumeric() and s[3].isdigit():
        return False

mariakeramida
Автор

Loops are annoying to me because I break them very easily

nted
Автор

I did same for first three.. But instead i used nested loop for each and ended up in problem.. For next 2 conditions... But writing them in only one line and nesting only tei conditions make it

floki
visit shbcf.ru