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

  Рет қаралды 24,010

TheCodingCreator

TheCodingCreator

Күн бұрын

Пікірлер: 85
@SomeStuff9
@SomeStuff9 Жыл бұрын
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.
@NickRoeder
@NickRoeder Жыл бұрын
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.
@deljimaetalk7766
@deljimaetalk7766 Жыл бұрын
Mine is 59 lines 🤣 Got 8 /10. Brilliant is an understatement
@Pr0feBboy
@Pr0feBboy Жыл бұрын
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
@6hbaufhehdj
@6hbaufhehdj Жыл бұрын
Dont be discouraged. Be happy you are learning, this was a hard problem for me as I am a computer science student, but it comes down to learning the string methods.
@Pr0feBboy
@Pr0feBboy Жыл бұрын
@@6hbaufhehdj Thanks. Im still going but where I'll get 5 months from now no idea
@miquelr2353
@miquelr2353 9 ай бұрын
Exactly the same. Im trying now for about hour and a half, I wrote that last loop to check like 8 or 9 different ways. Each time it decided a different input to mess up This is very hard
@noisi_tc3068
@noisi_tc3068 8 ай бұрын
It is a difficult and steep learning curve and this problem is definitief not easy
@abdullahadel1141
@abdullahadel1141 6 ай бұрын
Thanks Omar for the amazing explanation, It really helped me a lot
@TheCodingCreator
@TheCodingCreator 6 ай бұрын
Glad it helped!
@studywithshin2205
@studywithshin2205 4 ай бұрын
if we set int(char) != 0 wouldn't 'CS50' return False? Why does it return True?? Can someone please explain ?
@MohammedArab-f6i
@MohammedArab-f6i 4 ай бұрын
cus only the first number in the phrase will enter the if statments. If the first number is 0 then return false vai the else statment.
@getstart98
@getstart98 Жыл бұрын
wow, amazing!! my solutions passed the test but one was about 40 lines long and the other was almost 50😶
@minibus1351
@minibus1351 Жыл бұрын
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()
@tchavofrund1908
@tchavofrund1908 11 ай бұрын
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
@ravishankar-ix7qj
@ravishankar-ix7qj 10 ай бұрын
We used the last "Return True " if the string contains only letters. if there are no number it will not enter if condition....
@aigerimabseit816
@aigerimabseit816 2 жыл бұрын
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
@TheCodingCreator
@TheCodingCreator 2 жыл бұрын
great work. but also try putting your code inside the is_valid() function, and leave the main function for calling the function and printing a message to the user.
@aristhera
@aristhera 2 жыл бұрын
doesn't work. Let s = 'aaa888' That's valid, right? Now let it be 'aaa088' That's invalid because of the leading 0 but s[-2:][0] matches only 88 (and then 8) and is therefor True
@aristhera
@aristhera 2 жыл бұрын
so you will definitely need a for-loop like in the video, check for isdigit(), create an index (see vid) and then s[index:][0] instead of [-2:] and check if the string before index is alphabetical with s[index - 1].isalpha()
@jessicaly8893
@jessicaly8893 2 жыл бұрын
can you explain why there is a return True on line 18?
@TheCodingCreator
@TheCodingCreator 2 жыл бұрын
because the for loop checks if a string containing digits is valid or not and that's it, so we also have to consider a string with no digits, and that's also a valid plate and the function should return true.
@mohammadamintorabi8415
@mohammadamintorabi8415 2 жыл бұрын
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
@BaraBarabere.
@BaraBarabere. Жыл бұрын
not working
@MohammedArab-f6i
@MohammedArab-f6i 4 ай бұрын
yea but then you would have to change every instance of char to s[i], which honestly is more annoying then just assigning the index to a var 🦐
@KurtGibson-p4q
@KurtGibson-p4q 6 ай бұрын
Can someone explain to me the syntax of line 14 i dont get why the : in [index:] ?
@seifyounes5306
@seifyounes5306 5 ай бұрын
+ 1
@studywithshin2205
@studywithshin2205 4 ай бұрын
please watch tutorials on index function. here in this code index is a variable assigned to index = s.index(char) if char is a digit and [index:] means starting from that index in the iterated string of variable char, it will be returned True. if there is an alphabet after that index, it will return False since numbers cannot be in the middle.
@btobmelody3217
@btobmelody3217 2 жыл бұрын
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?
@KawsarAhmed-xd4jg
@KawsarAhmed-xd4jg Жыл бұрын
Hey! So a bug may arise because the if statement at first handles most of the logic except it does not encounter the '0' or zero that comes just after the leading alphabets. In that case, it may return True which isn't the correct output. For this reason, it's necessary to implement the for loop inside the if statement to check whether the zero is positioned just after the alphabets or not. Hope you will find it helpful.
@chikamatthew959
@chikamatthew959 2 жыл бұрын
I don’t understand the s[0:2].isalpha() function in the program
@HollowSunny69
@HollowSunny69 2 жыл бұрын
Check the hints in the problem set. It explains it well.
@TheCodingCreator
@TheCodingCreator 2 жыл бұрын
it checks if the first two charachters in our string is alphabetic letters, because as the problem stated “All vanity plates must start with at least two letters.”
@jermovankatwijk2290
@jermovankatwijk2290 2 жыл бұрын
Makes sure that the first spots are always metters
@MrKamoliddin
@MrKamoliddin Жыл бұрын
the code should fail with inputs with , such as CS50P2.
@MrKamoliddin
@MrKamoliddin Жыл бұрын
but this works: def is_valid(s): if 2
@miquelr2353
@miquelr2353 9 ай бұрын
I like how i did check50 and instantly got 4 red ansers with this code
@mariakeramida1032
@mariakeramida1032 Жыл бұрын
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)
@hongcao309
@hongcao309 2 жыл бұрын
appreciate this video
@jdubiez
@jdubiez 2 жыл бұрын
Why do you is .isdigit instead of .isnumeric ? Or does it not matter?
@hernansaa
@hernansaa Жыл бұрын
both work well in this case.
@lucascamarasa2081
@lucascamarasa2081 2 жыл бұрын
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
@abdullashafi580
@abdullashafi580 2 жыл бұрын
yup this line was confusing! but there is a built in function in python called index() "The Python index() method helps you find the index position of an element or an item in a string of characters or a list of items. It spits out the lowest possible index of the specified element in the list. In case the specified item does not exist in the list, a ValueError is returned " __google
@floki0407
@floki0407 Жыл бұрын
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 better......
@nted
@nted 2 жыл бұрын
Loops are annoying to me because I break them very easily
@TheCodingCreator
@TheCodingCreator 2 жыл бұрын
sometimes it be annoying, but you have to get used to it because it is very useful and most of the time you will need loops in your program.
@9ionet
@9ionet Жыл бұрын
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
@julialett
@julialett 9 ай бұрын
hii can you please explain to me the "stack = []" part of the code pls?
@seifyounes5306
@seifyounes5306 5 ай бұрын
i still can't get it or understand it at all
@seifyounes5306
@seifyounes5306 5 ай бұрын
i feel like im the only one who can't do it
@cruseder2
@cruseder2 Жыл бұрын
def main(): plate = input("Plate: ") if is_valid(plate): print("Valid") else: print("Invalid") def is_valid(s): list = [] length = len(s) - 1 if len(s) < 2 or len(s) > 6: return False for i in range(length): if s[i].isnumeric() and s[i+1].isalpha(): return False for i in s: if i.isnumeric(): list.append(i) if i[0] == 0: return False if s.isalnum() and s[0:2].isalpha(): return True if __name__ == "__main__": main()
@tonybuss8242
@tonybuss8242 2 жыл бұрын
Your code needs an improvement, the way you typed it above will not work.... after your if 6 >= len(s) >= 2 and s[0:2].isalpha() and s.isalnum(): you need to add if s.isalpha(): return True if you do not, a standar all alfa AAAAAA will not work. // TAB
@TheCodingCreator
@TheCodingCreator 2 жыл бұрын
i tried and it is working, why do you think it won't work?
@jermovankatwijk2290
@jermovankatwijk2290 2 жыл бұрын
Jea indeed, without this addition NVROUS would be Invalid
@NickRoeder
@NickRoeder Жыл бұрын
AAAAAA.isalnum is True so it will return valid. It does indeed return valid too.
@janardhan2jordan
@janardhan2jordan 2 жыл бұрын
thanks a lot bud
@simeonrf7047
@simeonrf7047 Жыл бұрын
def main(): plate = input("Plate: ") if is_valid(plate): print("Valid") else: print("Invalid") def is_valid(input_string): while True: length = len(input_string) if ( input_string[0:2].isalpha() and 2
@ramireddyvarun26
@ramireddyvarun26 Жыл бұрын
feel so foolish! I tried enumerate to get the index etc but never explored index directly from string methods! lesson learnt ... read the bloody documentation! :) Thanks though.
@amirprx3
@amirprx3 10 ай бұрын
❤❤
@curiousLeafy
@curiousLeafy Жыл бұрын
``` # import re def main(): plate = input("Plate: ") if is_valid(plate): print("Valid") else: print("Invalid") def is_valid(s): if(len(s) < 2): return False s = s.lower() length = len(s) is_min_two_letters = s[0: 2].isalpha() valid_length = length >=2 and length
@adarshgurung9432
@adarshgurung9432 2 жыл бұрын
Please atleast try of explain what you are doing.
@TheCodingCreator
@TheCodingCreator 2 жыл бұрын
which part do u need explanation for?
@adarshgurung9432
@adarshgurung9432 2 жыл бұрын
@@TheCodingCreator I dont understand the use of "char" , "index" and line 14 .
@TheCodingCreator
@TheCodingCreator 2 жыл бұрын
@@adarshgurung9432 my goal was to look for the first digit that appears in a string, so that is why I made the for loop. after knowing that the character is a digit, I want to check if that character doesn't equal 0, and each subsequent character is also a digit (line 14). char, and index are just variable names, you can call them whatever u like. for further understanding, let's assume that our string is "CS50" and we are on line 11. first loop, char will store "C", then we check if char is a digit on line 12, since it is not a digit, our loop ends. second loop, char will store "S", then again we check if char is a digit, since it is not, our loop ends. third loop, char will store "5", then we check if char is a digit, since it is a digit, the variable index will store the index of this character which will be 2 (index count start from 0), then on line 14, s[index:] slices our string starting at index till the last character, in our case: s[2:], which returns "50", then we check if that string contains only digits (that is true). in line 14 we also check if char != 0, in our case 5 != 0, which return true. i hope that makes sense.
@adarshgurung9432
@adarshgurung9432 2 жыл бұрын
@@TheCodingCreator Thanks, I understand most of it except, Which line of code ensures that if first numeric value is not zero. I guess it is int(char) != 0? But I don't understand how.
@adarshgurung9432
@adarshgurung9432 2 жыл бұрын
I mean in line 14 ( int(char) != 0), won't this line disturb the "0" in "CS50" as input, even if 0 is at the end of the word CS50?
@ayounis5158
@ayounis5158 Жыл бұрын
what do you think of my solution: def is_valid(inp): if 2
@koenokatachiii9218
@koenokatachiii9218 Жыл бұрын
trash
@ayounis5158
@ayounis5158 Жыл бұрын
@@koenokatachiii9218 fatherless behaviour
@koenokatachiii9218
@koenokatachiii9218 Жыл бұрын
​@@ayounis5158 youre editing anime girls, get a life
@ayounis5158
@ayounis5158 Жыл бұрын
@@koenokatachiii9218 get a family, oh you can't, srry :(
@koenokatachiii9218
@koenokatachiii9218 Жыл бұрын
@@ayounis5158 what an insult!!!!!!!!!!!!!
@TheFailMistress
@TheFailMistress 2 жыл бұрын
i know an egyptian when i hear one 😅
@TheCodingCreator
@TheCodingCreator 2 жыл бұрын
was it that obvious 😂
@christopherreina4901
@christopherreina4901 Жыл бұрын
without using index: def main(): plate = input("Plate: ") if is_valid(plate): print("Valid") else: print("Invalid") def is_valid(s): if s[0:2].isalpha() and 2
@oltionr
@oltionr 2 жыл бұрын
That's not fair to distribute the solutions. I hope CS50 will not consider this type of solution. Anyone should try to learn it on their own.
@TheCodingCreator
@TheCodingCreator 2 жыл бұрын
that is not the solution, that is just a solution. the point of these videos is not to make you copy and paste the solution and submit it, the point is to explain the language and learn how to solve problems and make programs with it. but yeah everyone should try it on their own, I should probably mention that in my videos.
@jorgeav527-xyz
@jorgeav527-xyz 2 жыл бұрын
if re.search("0.", frase): return False
PROBLEM SET 2: VANITY PLATES | SOLUTION (CS50 PYTHON)
18:28
Dors Coding School
Рет қаралды 58 М.
БАБУШКА ШАРИТ #shorts
0:16
Паша Осадчий
Рет қаралды 4,1 МЛН
Counter-Strike 2 - Новый кс. Cтарый я
13:10
Marmok
Рет қаралды 2,8 МЛН
Outdated  -Problem Set 3 (CS50's Introduction to Programming with Python)
20:15
PROBLEM SET 3: FUEL GAUGE | SOLUTION (CS50 PYTHON)
19:14
Dors Coding School
Рет қаралды 28 М.
Harvard CS50’s Artificial Intelligence with Python - Full University Course
11:51:22
Vanity Plates | CS50P PSet Solution
12:55
The IT Shed
Рет қаралды 7 М.
PROBLEM SET 0: TIP CALCULATOR | SOLUTION (CS50 PYTHON)
9:35
Dors Coding School
Рет қаралды 31 М.
Python laid waste to my C++!
17:18
Sheafification of G
Рет қаралды 202 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 342 М.
How to STUDY so FAST it feels like CHEATING
8:03
The Angry Explainer
Рет қаралды 2,7 МЛН
Vanity Plates Solution CS50P - Problem Set 2
12:57
Simple Easy Python
Рет қаралды 430