aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-10-02 19:56:18 +0100
committerGitHub <noreply@github.com>2022-10-02 19:56:18 +0100
commiteca4423a53891951064f41611d7db28bb9211c20 (patch)
treeef55eb04c8510025630a85b7cbe577d59b84130e
parent39e5e429c1c17c3953f14f2424cb764d5ca49b4a (diff)
parent7ce5676b505561ef575c2953b5343f8dba10a6f9 (diff)
downloadperlweeklychallenge-club-eca4423a53891951064f41611d7db28bb9211c20.tar.gz
perlweeklychallenge-club-eca4423a53891951064f41611d7db28bb9211c20.tar.bz2
perlweeklychallenge-club-eca4423a53891951064f41611d7db28bb9211c20.zip
Merge pull request #6823 from oWnOIzRi/week184
add solution week 184 task 2 in python
-rw-r--r--challenge-184/steven-wilson/python/ch-02.ipynb186
1 files changed, 186 insertions, 0 deletions
diff --git a/challenge-184/steven-wilson/python/ch-02.ipynb b/challenge-184/steven-wilson/python/ch-02.ipynb
new file mode 100644
index 0000000000..73d3f5095d
--- /dev/null
+++ b/challenge-184/steven-wilson/python/ch-02.ipynb
@@ -0,0 +1,186 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "9127c568",
+ "metadata": {},
+ "source": [
+ "# Week 184 The Weekly Challenge\n",
+ "# Task 2: Split Array\n",
+ "\n",
+ "You are given list of strings containing 0-9 and a-z separated by space only.\n",
+ "\n",
+ "Write a script to split the data into two arrays, one for integers and one for alphabets only.\n",
+ "Example 1\n",
+ "```\n",
+ "Input: @list = ( 'a 1 2 b 0', '3 c 4 d')\n",
+ "Output: [[1,2,0], [3,4]] and [['a','b'], ['c','d']]\n",
+ "```\n",
+ "Example 2\n",
+ "```\n",
+ "Input: @list = ( '1 2', 'p q r', 's 3', '4 5 t')\n",
+ "Output: [[1,2], [3], [4,5]] and [['p','q','r'], ['s'], ['t']]\n",
+ "```"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "id": "34ae5529",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "input1 = ('a 1 2 b 0', '3 c 4 d')\n",
+ "input2 = ('1 2', 'p q r', 's 3', '4 5 t')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "95af3e0d",
+ "metadata": {},
+ "source": [
+ "Start by working out how to take one string and split the data into two arrays."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "315587cc",
+ "metadata": {},
+ "source": [
+ "Split the string on whitespace:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "id": "35dfad2b",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['a', '1', '2', 'b', '0']"
+ ]
+ },
+ "execution_count": 2,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "input1[0].split(\" \")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "7e670f24",
+ "metadata": {},
+ "source": [
+ "Digits need to be cast to int type.\n",
+ "Function returns a tuple containing 2 arrays, first of integers then of letters. Empty arrays may be returned."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "id": "b5021170",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "([1, 2, 0], ['a', 'b'])"
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "def separate_alpha_num ( alpha_num_string ):\n",
+ " integers, letters = [], []\n",
+ " for x in alpha_num_string.split(\" \"):\n",
+ " letters.append(x) if x.isalpha() else integers.append(int(x)) \n",
+ " return integers, letters\n",
+ "\n",
+ "separate_alpha_num(input1[0])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "9ad2ac4e",
+ "metadata": {},
+ "source": [
+ "Next create a function to handle the list of strings. It will return 2 arrays of arrays, first of integers then letters."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "id": "aac5d69f",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[[1, 2, 0], [3, 4]] and [['a', 'b'], ['c', 'd']]\n"
+ ]
+ }
+ ],
+ "source": [
+ "def separate_alpha_num_from_strings(los):\n",
+ " lo_integers, lo_letters = [], []\n",
+ " for s in los:\n",
+ " integers, letters = separate_alpha_num(s)\n",
+ " if integers: lo_integers.append(integers) # Only add arrays if not empty\n",
+ " if letters: lo_letters.append(letters)\n",
+ " return lo_integers, lo_letters\n",
+ "\n",
+ "integers, letters = separate_alpha_num_from_strings(input1)\n",
+ "print(integers, \" and \", letters)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "id": "97771b02",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[[1, 2], [3], [4, 5]] and [['p', 'q', 'r'], ['s'], ['t']]\n"
+ ]
+ }
+ ],
+ "source": [
+ "integers, letters = separate_alpha_num_from_strings(input2)\n",
+ "print(integers, \" and \", letters)"
+ ]
+ }
+ ],
+ "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
+}