aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Wilson <steven1170@zoho.eu>2022-10-04 17:51:07 +0100
committerSteven Wilson <steven1170@zoho.eu>2022-10-04 17:51:07 +0100
commitcc5074e61c6027803d0578e8236acc5c2e50e6be (patch)
tree2aa5109b2d51923012ef0b0bc2bc327bd0853a84
parente982029e0a461a37acfdd6e68a72cad976d444d6 (diff)
downloadperlweeklychallenge-club-cc5074e61c6027803d0578e8236acc5c2e50e6be.tar.gz
perlweeklychallenge-club-cc5074e61c6027803d0578e8236acc5c2e50e6be.tar.bz2
perlweeklychallenge-club-cc5074e61c6027803d0578e8236acc5c2e50e6be.zip
add solution week 185 task 2 in python
-rw-r--r--challenge-185/steven-wilson/python/ch-02.ipynb167
1 files changed, 167 insertions, 0 deletions
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
+}