{ "cells": [ { "cell_type": "markdown", "id": "476288d6", "metadata": {}, "source": [ "# Task 2: Mask Code\n", "\n", "You are given a list of codes in many random format.\n", "\n", "Write a script to mask first four characters (a-z,0-9) and keep the rest as it is.\n", "Example 1\n", "```\n", "Input: @list = ('ab-cde-123', '123.abc.420', '3abc-0010.xy')\n", "Output: ('xx-xxe-123', 'xxx.xbc.420', 'xxxx-0010.xy')\n", "```\n", "Example 2\n", "```\n", "Input: @list = ('1234567.a', 'a-1234-bc', 'a.b.c.d.e.f')\n", "Output: ('xxxx567.a', 'x-xxx4-bc', 'x.x.x.x.e.f')\n", "````" ] }, { "cell_type": "code", "execution_count": 1, "id": "2226efbf", "metadata": {}, "outputs": [], "source": [ "import re" ] }, { "cell_type": "code", "execution_count": 2, "id": "ba2106d7", "metadata": {}, "outputs": [], "source": [ "ex1 = ('ab-cde-123', '123.abc.420', '3abc-0010.xy')\n", "ex2 = ('1234567.a', 'a-1234-bc', 'a.b.c.d.e.f')" ] }, { "cell_type": "markdown", "id": "65f5f1bb", "metadata": {}, "source": [ "First step is to mask a single code using pythons re.sub(). re.sub has a named argument ```count=4``` which will substitute the required number of matches. \\w regex matches alphanumeric characters." ] }, { "cell_type": "code", "execution_count": 3, "id": "56d192cb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'xx-xxe-123'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "re.sub(r'[\\w]', 'x', ex1[0], count=4)" ] }, { "cell_type": "markdown", "id": "8834b502", "metadata": {}, "source": [ "Next wrap this in a function which handles a list of codes to mask. Using map() with a lambda function as the first argument is a common idiom in python." ] }, { "cell_type": "code", "execution_count": 4, "id": "54223d5f", "metadata": {}, "outputs": [], "source": [ "def mask_list_of_codes(loc):\n", " result = map(lambda code: re.sub(r'[\\w]', 'x', code, count=4), loc)\n", " return list(result)" ] }, { "cell_type": "markdown", "id": "a9daffb3", "metadata": {}, "source": [ "Finally test the function with the examples above:" ] }, { "cell_type": "code", "execution_count": 5, "id": "3aea4dac", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['xx-xxe-123', 'xxx.xbc.420', 'xxxx-0010.xy']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mask_list_of_codes(ex1)" ] }, { "cell_type": "code", "execution_count": 6, "id": "30d532a7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['xxxx567.a', 'x-xxx4-bc', 'x.x.x.x.e.f']" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mask_list_of_codes(ex2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }