aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-184/steven-wilson/python/ch-01.ipynb245
1 files changed, 245 insertions, 0 deletions
diff --git a/challenge-184/steven-wilson/python/ch-01.ipynb b/challenge-184/steven-wilson/python/ch-01.ipynb
new file mode 100644
index 0000000000..b6a7bb6a74
--- /dev/null
+++ b/challenge-184/steven-wilson/python/ch-01.ipynb
@@ -0,0 +1,245 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "9127c568",
+ "metadata": {},
+ "source": [
+ "# Week 184 The Weekly Challenge\n",
+ "# Task 1: Sequence Number\n",
+ "\n",
+ "You are given list of strings in the format aa9999 i.e. first 2 characters can be anything 'a-z' followed by 4 digits '0-9'.\n",
+ "\n",
+ "Write a script to replace the first two characters with sequence starting with '00', '01', '02' etc.\n",
+ "\n",
+ "Example 1\n",
+ "\n",
+ "```\n",
+ "Input: @list = ( 'ab1234', 'cd5678', 'ef1342')\n",
+ "Output: ('001234', '015678', '021342')\n",
+ "```\n",
+ "\n",
+ "Example 2\n",
+ "\n",
+ "```\n",
+ "Input: @list = ( 'pq1122', 'rs3334')\n",
+ "Output: ('001122', '013334')\n",
+ "```"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "id": "d9ddd9d0",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import re"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "id": "0346d2ba",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "input1 = ['ab1234', 'cd5678', 'ef1342']\n",
+ "output1 = ['001234', '015678', '021342']\n",
+ "input2 = ['pq1122', 'rs3334']\n",
+ "output2 = ['001122', '013334']"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "336541b3",
+ "metadata": {},
+ "source": [
+ "Function to substitute the first 2 letters in a string with a given prefix:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "id": "0b2cb0f3",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'001122'"
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "def change_prefix_string (string, prefix_string):\n",
+ " result = re.sub(r'^[a-z]{2}', prefix_string, string)\n",
+ " return result\n",
+ "\n",
+ "change_prefix_string(\"pq1122\", \"00\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "b791cddb",
+ "metadata": {},
+ "source": [
+ "Working out how to create 0 padded prefix between 00 and 99. Loops back to 00 after 99 if more than 100:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "id": "c7ee98bf",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'01'"
+ ]
+ },
+ "execution_count": 4,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "num = 1\n",
+ "prefix = (num) % 100\n",
+ "prefix_as_string = \"%02d\" % (prefix,)\n",
+ "prefix_as_string"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "8825dd5b",
+ "metadata": {},
+ "source": [
+ "Function to generate a list of prefixes which are zero padded:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "id": "8e168ad7",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['00', '01', '02', '03', '04']"
+ ]
+ },
+ "execution_count": 5,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "def generate_list_of_prefixes (number_of_prefixes):\n",
+ " list_of_prefixes = [\"%02d\" % (num % 100) for num in range(number_of_prefixes)]\n",
+ " return list_of_prefixes\n",
+ "\n",
+ "generate_list_of_prefixes(5)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "564c5b35",
+ "metadata": {},
+ "source": [
+ "Bringing everything together:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "id": "688ef188",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def change_prefix_list_of_strings (los):\n",
+ " length_of_list = len(los)\n",
+ " list_of_prefixes = generate_list_of_prefixes(length_of_list)\n",
+ " result_list = [change_prefix_string(los[i], list_of_prefixes[i]) for i in range(length_of_list)]\n",
+ " return result_list"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "5e899500",
+ "metadata": {},
+ "source": [
+ "Testing the function with example lists above:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "id": "f2e4b48b",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['001234', '015678', '021342']"
+ ]
+ },
+ "execution_count": 7,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "change_prefix_list_of_strings(input1)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "id": "b9fe98c2",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['001122', '013334']"
+ ]
+ },
+ "execution_count": 8,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "change_prefix_list_of_strings(input2)"
+ ]
+ }
+ ],
+ "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
+}