{ "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'([\\w]{2})([\\w]{2})\\.([\\w]{2})([\\w]{2})\\.([\\w]{2})([\\w]{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 }