Validate a discount code format

Python data engineering interview problem. Difficulty: beginner. Pattern: Strings. About 10 minutes. Free to practice.

Checkout accepts a discount code string. Fraud wants a strict format check before the code is sent to billing, not a fuzzy 'looks ok' guess.

A checkout form must reject bad discount codes before they reach billing. A valid code is 4 to 8 capital letters, a dash, then exactly 3 digits. Return True only if the whole code matches that format, otherwise False. Write valid_discount_code(code). 'SAVE-100' is valid (4 capital letters, a dash, 3 digits). 'NO' is not valid. The starter file already has the function name and its print lines. Keep them and fill in the body.

Requirements

  • The code must be 4 to 8 capital letters.
  • Then one dash.
  • Then exactly 3 digits.
  • Nothing may come before or after.

Constraints

  • code is a string.
  • The result is True or False.

Examples

Input: valid_discount_code('SAVE-100') Output: True This input follows the stated rules and produces this output.

Topics: lakebench, python, functions.

More Python interview questions · All interview problems · Learn data engineering

beginner

Validate a discount code format

Interview-style drill: Write a function that checks whether a discount code matches a required pattern.

Return whether a discount code matches the required format.