forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 28-perfect-numbers
Showing
5 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# **Challenge: Hide a Credit Card Number** | ||
<!-- markdownlint-disable MD036 --> | ||
**Description** | ||
<!-- markdownlint-disable MD013 --> | ||
The "Hide a Credit Card Number" challenge involves writing a function to mask all but the last four digits of a credit card number. This is useful for displaying sensitive information in a secure manner. The masked digits should be replaced with the `*` character. | ||
|
||
Write a function that takes a credit card number as a string and returns the masked version of the number, with only the last four digits visible. | ||
|
||
**For example:** | ||
<!-- markdownlint-disable MD040 --> | ||
``` | ||
Input: '1234567812345678' | ||
Output: '************5678' | ||
``` | ||
|
||
**Requirements:** | ||
|
||
The input will always be a valid credit card number as a string. | ||
The function should preserve the last four digits. | ||
It should handle inputs of varying lengths (e.g., short card numbers). | ||
|
||
**Example** | ||
|
||
``` | ||
hide_credit_card('1234567812345678') | ||
# Output: '************5678' | ||
hide_credit_card('9876543210987654') | ||
# Output: '************7654' | ||
hide_credit_card('1234') | ||
# Output: '1234' (no masking needed if the number has only 4 or fewer digits) | ||
``` | ||
|
||
**Testing** | ||
|
||
Develop unit tests to validate the function's correctness. Include cases for: | ||
|
||
- Regular 16-digit credit card numbers. | ||
- Short numbers (e.g., fewer than 8 digits). | ||
- Edge case: Exactly 4 digits (no masking needed). | ||
|
||
**Helpful Links** | ||
|
||
- [String Slicing in Python](https://www.geeksforgeeks.org/string-slicing-in-python/) | ||
- [GeeksforGeeks: Python String Methods](https://www.geeksforgeeks.org/python-string-methods/) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module for masking sensitive credit card numbers. | ||
Module contents: | ||
- hide_credit_card: masks all but the last four digits of a credit card number. | ||
Created on 04/01/2025 | ||
Author: Hector Colmenares | ||
""" | ||
|
||
|
||
def hide_credit_card(card_number: str) -> str: | ||
"""Masks all but the last four digits of a credit card number. | ||
Parameters: | ||
card_number: str, a valid credit card number as a string | ||
Returns -> str with the masked credit card number, keeping only the last four digits visible | ||
Raises: | ||
ValueError: if the input is not a string | ||
ValueError: if the input is empty or contains non-digit characters | ||
>>> hide_credit_card('1234567812345678') | ||
'************5678' | ||
>>> hide_credit_card('9876543210987654') | ||
'************7654' | ||
>>> hide_credit_card('1234') | ||
'1234' | ||
""" | ||
# Ensure the input is a valid string of digits | ||
if not isinstance(card_number, str): | ||
raise ValueError("Input must be a string.") | ||
if not card_number.isdigit() or len(card_number) == 0: | ||
raise ValueError("Input must be a non-empty string of digits.") | ||
|
||
# If the length is less than or equal to 4, return the card number unchanged | ||
if len(card_number) <= 4: | ||
return card_number | ||
|
||
# Mask all but the last four digits | ||
return "*" * (len(card_number) - 4) + card_number[-4:] |
Empty file.
46 changes: 46 additions & 0 deletions
46
solutions/tests/challenge_16/test_hide_credit_card_number.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Unit tests for the hide_credit_card function. | ||
Class: | ||
TestHideCreditCard: Contains tests for various cases. | ||
Created on 04/01/2025 | ||
Author: Hector Colmenares | ||
""" | ||
|
||
import unittest | ||
|
||
from solutions.challenge_16.hide_credit_card_number import hide_credit_card | ||
|
||
|
||
class TestHideCreditCard(unittest.TestCase): | ||
"""Tests for the hide_credit_card function.""" | ||
|
||
def test_regular_number(self): | ||
"""Test with a regular 16-digit credit card number.""" | ||
self.assertEqual(hide_credit_card("1234567812345678"), "************5678") | ||
|
||
def test_short_number(self): | ||
"""Test with a short credit card number.""" | ||
self.assertEqual(hide_credit_card("12345"), "*2345") | ||
|
||
def test_exactly_four_digits(self): | ||
"""Test with a credit card number of exactly four digits.""" | ||
self.assertEqual(hide_credit_card("1234"), "1234") | ||
|
||
def test_non_digit_characters_raises_value_error(self): | ||
"""Test with a credit card number containing non-digit characters.""" | ||
with self.assertRaises(ValueError): | ||
hide_credit_card("1234abcd5678") | ||
|
||
def test_empty_string_raises_value_error(self): | ||
"""Test with an empty string.""" | ||
with self.assertRaises(ValueError): | ||
hide_credit_card("") | ||
|
||
def test_non_string_input_raises_value_error(self): | ||
"""Test with non-string input.""" | ||
with self.assertRaises(ValueError): | ||
hide_credit_card(12345678) |