To determine if a product is genuine or fake based on the given rules, follow these steps:
Rules:
- No Serial Number: If the product lacks a serial number (i.e.,
serial_numberisNoneor an empty string), it is fake. - Serial Number Starts with "XYZ": If the serial number starts with "XYZ", the product is genuine.
- Other Serial Numbers: For serial numbers not starting with "XYZ":
- If the manufacturing date is on or after January 1, 2020, the product is genuine.
- If the manufacturing date is before January 1, 2020, or not provided, the product is fake.
Solution Code:
def test_product(serial_number, manufacturing_date=None):
# Check if serial number is missing or empty
if not serial_number:
return "fake"
# Check if serial number starts with "XYZ"
if serial_number.startswith("XYZ"):
return "genuine"
# For non-XYZ serial numbers, check manufacturing date
if manufacturing_date is None:
return "fake"
# Compare manufacturing date with "2020-01-01"
if manufacturing_date >= "2020-01-01":
return "genuine"
else:
return "fake"
Explanation:
-
No Serial Number Check:
if not serial_numberchecks ifserial_numberisNoneor an empty string. If true, return"fake".
-
"XYZ" Prefix Check:
serial_number.startswith("XYZ")checks if the serial number starts with "XYZ". If true, return"genuine".
-
Manufacturing Date Check:
- If the serial number doesn’t start with "XYZ", verify the
manufacturing_date:- If
manufacturing_dateisNone, return"fake". - Compare
manufacturing_date(as a string in"YYYY-MM-DD"format) with"2020-01-01":- If
manufacturing_date >= "2020-01-01", return"genuine". - Otherwise, return
"fake".
- If
- If
- If the serial number doesn’t start with "XYZ", verify the
Example Usage:
# Fake: No serial number
print(test_product(None)) # Output: fake
# Fake: Serial doesn't start with "XYZ" and date is before 2020
print(test_product("ABC123", "2019-12-31")) # Output: fake
# Genuine: Serial doesn't start with "XYZ" but date is on/after 2020
print(test_product("ABC123", "2020-01-01")) # Output: genuine
# Fake: Serial doesn't start with "XYZ" and no date provided
print(test_product("ABC123")) # Output: fake
This implementation efficiently checks all rules to determine product authenticity.
Request an On-site Audit / Inquiry