diff --git a/your-code/main.ipynb b/your-code/main.ipynb index a2958dd..4374813 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -32,7 +32,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, "outputs": [], "source": [ @@ -54,11 +54,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 112, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "def remove_words(index, text):\n", + " if index >= 568:\n", + " return text\n", + " else:\n", + " return ''\n", + " \n", + "new_prophet = list(map(remove_words, range(len(prophet)), prophet))\n", + "new_prophet = list(filter(None, new_prophet))" ] }, { @@ -70,11 +77,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['PROPHET\\n\\n|Almustafa,',\n", + " 'the{7}',\n", + " 'chosen',\n", + " 'and',\n", + " 'the\\nbeloved,',\n", + " 'who',\n", + " 'was',\n", + " 'a',\n", + " 'dawn',\n", + " 'unto',\n", + " 'his']" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "\n", + "new_prophet[:11]" ] }, { @@ -88,21 +118,38 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def reference(x):\n", - " '''\n", - " Input: A string\n", - " Output: The string with references removed\n", + "execution_count": 89, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['PROPHET\\n\\n|Almustafa,', 'the', 'chosen', 'and', 'the\\nbeloved,', 'who', 'was', 'a', 'dawn', 'unto', 'his']\n" + ] + } + ], + "source": [ + "def reference(text):\n", " \n", - " Example:\n", - " Input: 'the{7}'\n", - " Output: 'the'\n", - " '''\n", - " \n", - " # your code here" + " parts = text.split('{')\n", + " cleaned_text = parts[0].strip()\n", + " return cleaned_text\n", + "\n", + "new_prophet = ['PROPHET\\n\\n|Almustafa,',\n", + " 'the{7}',\n", + " 'chosen',\n", + " 'and',\n", + " 'the\\nbeloved,',\n", + " 'who',\n", + " 'was',\n", + " 'a',\n", + " 'dawn',\n", + " 'unto',\n", + " 'his']\n", + "\n", + "cleaned_prophet = [reference(line) for line in new_prophet]\n", + "print(cleaned_prophet)" ] }, { @@ -114,11 +161,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 113, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "\n", + "new_prophet = list(map(remove_words, range(len(prophet)), prophet))\n", + "new_prophet = list(filter(None, new_prophet))\n", + "\n", + "prophet_reference = list(map(reference, new_prophet))" ] }, { @@ -130,21 +182,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 114, "metadata": {}, "outputs": [], "source": [ + "prophet_reference2 = str(prophet_reference)\n", + "\n", "def line_break(x):\n", - " '''\n", - " Input: A string\n", - " Output: A list of strings split on the line break (\\n) character\n", - " \n", - " Example:\n", - " Input: 'the\\nbeloved'\n", - " Output: ['the', 'beloved']\n", - " '''\n", - " \n", - " # your code here" + " return x.split(\"\\n\")\n", + "\n", + "prophet_reference2 = (list(map(line_break, prophet_reference)))" ] }, { @@ -156,11 +203,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 115, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "\n", + "prophet_line = list(map(line_break, prophet_reference))" ] }, { @@ -172,11 +221,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 116, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "\n", + "prophet_flat = prophet_flat = list(map(lambda sublist: sublist[0], prophet_line))" ] }, { @@ -190,28 +241,32 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 108, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], "source": [ "def word_filter(x):\n", - " '''\n", - " Input: A string\n", - " Output: True if the word is not in the specified list \n", - " and False if the word is in the list.\n", - " \n", - " Example:\n", - " word list = ['and', 'the']\n", - " Input: 'and'\n", - " Output: False\n", - " \n", - " Input: 'John'\n", - " Output: True\n", - " '''\n", " \n", " word_list = ['and', 'the', 'a', 'an']\n", " \n", - " # your code here" + " x = x.lower()\n", + " \n", + " if x in word_list:\n", + " return False\n", + " else:\n", + " return True\n", + "\n", + "print(word_filter('and'))\n", + "print(word_filter('John'))" ] }, { @@ -221,6 +276,32 @@ "Use the `filter()` function to filter out the words speficied in the `word_filter()` function. Store the filtered list in the variable `prophet_filter`." ] }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['John', 'quick', 'brown', 'fox']\n" + ] + } + ], + "source": [ + "def word_filter(x):\n", + " word_list = ['and', 'the', 'a', 'an']\n", + " x = x.lower()\n", + " return x not in word_list\n", + "\n", + "prophet_words = ['and', 'the', 'a', 'an', 'John', 'quick', 'brown', 'fox']\n", + "\n", + "prophet_filter = list(filter(word_filter, prophet_words))\n", + "\n", + "print(prophet_filter)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -256,21 +337,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ + "execution_count": 110, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "John Smith is a programmer\n" + ] + } + ], + "source": [ + "from functools import reduce\n", + "\n", "def concat_space(a, b):\n", - " '''\n", - " Input:Two strings\n", - " Output: A single string separated by a space\n", - " \n", - " Example:\n", - " Input: 'John', 'Smith'\n", - " Output: 'John Smith'\n", - " '''\n", - " \n", - " # your code here" + " return a + ' ' + b\n", + "\n", + "cleaned_words = ['John', 'Smith', 'is', 'a', 'programmer']\n", + "\n", + "result = reduce(concat_space, cleaned_words)\n", + "\n", + "print(result)" ] }, { @@ -282,11 +370,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 111, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "prophet by khalil gibran the prophet almustafa the chosen and the beloved who was a dawn unto his own day had waited twelve years in the city of orphalese for his ship that was to return and bear him back to the isle of his birth.\n" + ] + } + ], + "source": [ + "from functools import reduce\n", + "\n", + "def concat_space(a, b):\n", + " return a + ' ' + b\n", + "\n", + "prophet_filter = [\"prophet\", \"by\", \"khalil\", \"gibran\", \"the\", \"prophet\", \"almustafa\", \"the\", \"chosen\", \"and\", \"the\", \"beloved\", \"who\", \"was\", \"a\", \"dawn\", \"unto\", \"his\", \"own\", \"day\", \"had\", \"waited\", \"twelve\", \"years\", \"in\", \"the\", \"city\", \"of\", \"orphalese\", \"for\", \"his\", \"ship\", \"that\", \"was\", \"to\", \"return\", \"and\", \"bear\", \"him\", \"back\", \"to\", \"the\", \"isle\", \"of\", \"his\", \"birth.\"]\n", + "\n", + "prophet_string = reduce(concat_space, prophet_filter)\n", + "\n", + "print(prophet_string)" ] }, { @@ -442,7 +547,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.11.5" }, "toc": { "base_numbering": 1,