Week 2: If you know Python, I need your Help!

Saddab AnsariSaddab Ansari
2 min read

This is An update of my week 2 of CS50. Which is now completed and i am about to move to Week 3 soon! there were 5 problems in Problem set 2 and i got all of them right after some iterations and polishing, except question no. 4 : Plates. I got only 8/9 correct evaluations however that’s it. I got a perfect score in all the other problems (6/6 , 10/10 and even 12/12).

Vanity Plates

In Massachusetts, the place that is home to Harvard University and MIT. It is possible to request for a custom Vanity plate for vehicles. However there are some rules or requirements that must be followed while creating your own license plate alphanumeric pattern.

Rules:

  1. All vanity plates must start with at least two letters.

  2. Vanity plates may contain a maximum of 6 characters (letters or numbers) and a minimum of 2 characters.

  3. Numbers cannot be used in the middle of a plate; they must come at the end. For example, AAA222 would be an acceptable … vanity plate; AAA22A would not be acceptable.

  4. The first number used cannot be a ‘0’. therefore AA022 is Invalid.

  5. No periods, spaces, or punctuation marks are allowed

Examples that will be tested : (highlighted once are “Valid”

  1. CS50

  2. ECTO88

  3. NRVOUS

  4. CS05

  5. 50

  6. CS50P2 ———Answer is “Invalid” but “valid” in My case.

  7. PI3.14

  8. H

  9. OUTATIME

Yep, only the first 3 should return “Valid“ Others must return “Invalid”.

My Code :

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

    digits = ""
    for char in s:
        if char.isdigit():
            digits += char
        else:
            digits += ""

    punctuation = ""
    for char in s:
        if not char.isalnum():
            punctuation += char
        else :
            punctuation += ""

    if not s[0:2].isalpha():
        return False
    elif len(s) < 2 or len(s) > 6:
        return False
    elif len(punctuation) > 0:
        return False
    elif len(digits) > 0:
        if digits[0] == "0":
            return False
        else: 
            return True
    else:
        return True

main()

You can copy it to your code editors and try other methods to solve it, if you do get it right on all 9 Examples or maybe just have some advice for me then please contact!

LinkedIn - https://www.linkedin.com/in/saddab-ansari-809b46367
or Comment Here below this post as well…!

Thank You For Reading and Do Help me!

0
Subscribe to my newsletter

Read articles from Saddab Ansari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Saddab Ansari
Saddab Ansari

I’m an aspiring computer science engineer interested in data science, AI, and coding. I enjoy learning by doing — from online courses to small projects and self-study. Right now, I’m focused on improving my programming skills, exploring new tech, and preparing for future opportunities to study and hoping to work abroad. Open to connecting with people who share similar interests or have advice to share.