{ "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 }