From e982029e0a461a37acfdd6e68a72cad976d444d6 Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Tue, 4 Oct 2022 16:40:49 +0100 Subject: change regex --- challenge-185/steven-wilson/python/ch-01.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-185/steven-wilson/python/ch-01.ipynb b/challenge-185/steven-wilson/python/ch-01.ipynb index 7cbfa28f3b..ea1d969399 100644 --- a/challenge-185/steven-wilson/python/ch-01.ipynb +++ b/challenge-185/steven-wilson/python/ch-01.ipynb @@ -98,7 +98,7 @@ ], "source": [ "def convert_mac(mac):\n", - " result = re.search(r'([a-z0-9]{2})([a-z0-9]{2})\\.([a-z0-9]{2})([a-z0-9]{2})\\.([a-z0-9]{2})([a-z0-9]{2})', mac)\n", + " result = re.search(r'([\\w]{2})([\\w]{2})\\.([\\w]{2})([\\w]{2})\\.([\\w]{2})([\\w]{2})', mac)\n", " return \":\".join(result.groups())\n", "\n", "print(convert_mac(ex1))" -- cgit From cc5074e61c6027803d0578e8236acc5c2e50e6be Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Tue, 4 Oct 2022 17:51:07 +0100 Subject: add solution week 185 task 2 in python --- challenge-185/steven-wilson/python/ch-02.ipynb | 167 +++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 challenge-185/steven-wilson/python/ch-02.ipynb diff --git a/challenge-185/steven-wilson/python/ch-02.ipynb b/challenge-185/steven-wilson/python/ch-02.ipynb new file mode 100644 index 0000000000..0841c67f9f --- /dev/null +++ b/challenge-185/steven-wilson/python/ch-02.ipynb @@ -0,0 +1,167 @@ +{ + "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 +} -- cgit