diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-10-04 19:52:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-04 19:52:53 +0100 |
| commit | 77916daf06a9a0e89bacc43edf34e10099301b02 (patch) | |
| tree | 232126c9b3d239546603a591e004a3d5d296f528 | |
| parent | 5701d1ff7a61d5992290615037c09ec36b41d859 (diff) | |
| parent | cc5074e61c6027803d0578e8236acc5c2e50e6be (diff) | |
| download | perlweeklychallenge-club-77916daf06a9a0e89bacc43edf34e10099301b02.tar.gz perlweeklychallenge-club-77916daf06a9a0e89bacc43edf34e10099301b02.tar.bz2 perlweeklychallenge-club-77916daf06a9a0e89bacc43edf34e10099301b02.zip | |
Merge pull request #6849 from oWnOIzRi/week185
add solution week 185 task 2 in python
| -rw-r--r-- | challenge-185/steven-wilson/python/ch-01.ipynb | 2 | ||||
| -rw-r--r-- | challenge-185/steven-wilson/python/ch-02.ipynb | 167 |
2 files changed, 168 insertions, 1 deletions
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))" 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 +} |
