From 73eea07c82c57583ee099b0ac3dce2f8e4636d0f Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Mon, 3 Oct 2022 18:33:59 +0100 Subject: add solution week 185 task 1 in python --- challenge-185/steven-wilson/python/ch-01.ipynb | 147 +++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 challenge-185/steven-wilson/python/ch-01.ipynb diff --git a/challenge-185/steven-wilson/python/ch-01.ipynb b/challenge-185/steven-wilson/python/ch-01.ipynb new file mode 100644 index 0000000000..7cbfa28f3b --- /dev/null +++ b/challenge-185/steven-wilson/python/ch-01.ipynb @@ -0,0 +1,147 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "0898b4b4", + "metadata": {}, + "source": [ + "# Task 1: MAC Address\n", + "\n", + "You are given MAC address in the form i.e. hhhh.hhhh.hhhh.\n", + "\n", + "Write a script to convert the address in the form hh:hh:hh:hh:hh:hh.\n", + "\n", + "Example 1\n", + "```\n", + "Input: 1ac2.34f0.b1c2\n", + "Output: 1a:c2:34:f0:b1:c2\n", + "```\n", + "Example 2\n", + "```\n", + "Input: abc1.20f1.345a\n", + "Output: ab:c1:20:f1:34:5a\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "6cafcb43", + "metadata": {}, + "outputs": [], + "source": [ + "import re" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "86a7b3f6", + "metadata": {}, + "outputs": [], + "source": [ + "ex1 = \"1ac2.34f0.b1c2\"\n", + "ex2 = \"abc1.20f1.345a\"" + ] + }, + { + "cell_type": "markdown", + "id": "8ac1ec54", + "metadata": {}, + "source": [ + "Work out a regular expressions to break up the mac address and capture groups. It's pretty long, I'm sure there is a more consise way to express it." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "a8094e85", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'1a:c2:34:f0:b1:c2'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result = re.search(r'([\\w]{2})([\\w]{2})\\.([\\w]{2})([\\w]{2})\\.([\\w]{2})([\\w]{2})', ex1)\n", + "\":\".join(result.groups())" + ] + }, + { + "cell_type": "markdown", + "id": "ff372d91", + "metadata": {}, + "source": [ + "Put this into function for reuse and test the examples above:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "cfa8172d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1a:c2:34:f0:b1:c2\n" + ] + } + ], + "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", + " return \":\".join(result.groups())\n", + "\n", + "print(convert_mac(ex1))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "dcbe6934", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ab:c1:20:f1:34:5a\n" + ] + } + ], + "source": [ + "print(convert_mac(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