From e17bde608fb4ecf5cdbd8bafe14d34e10124308d Mon Sep 17 00:00:00 2001 From: Abhishek Thummanapelli Date: Thu, 24 Jul 2025 00:08:54 +0200 Subject: [PATCH] Day 3 Lab part 2 done --- your-code/main.ipynb | 189 ++++++++++++++++++++++++++++++++----------- 1 file changed, 141 insertions(+), 48 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 9f0e67b..1245a6c 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -35,7 +35,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -48,9 +48,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "13637" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "len(prophet)" ] @@ -66,11 +77,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "prophet = prophet[568:]" ] }, { @@ -82,11 +93,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['PROPHET\\n\\n|Almustafa,', 'the{7}', 'chosen', 'and', 'the\\nbeloved,', 'who', 'was', 'a', 'dawn', 'unto']\n" + ] + } + ], "source": [ - "# your code here" + "print(prophet[0:10])" ] }, { @@ -100,7 +119,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, "outputs": [], "source": [ @@ -114,16 +133,25 @@ " Output: 'the'\n", " '''\n", " \n", - " # your code here" + " organised_prophet = []\n", + " # your code here\n", + " for i in x.split(): #Splits the text x into a list of words using whitespace\n", + " if \"{\" not in i: #If the word does not contain a { character ( i.e., a normal word like \"the\" or \"prophet\"):\n", + " organised_prophet.append(i) #Just append it as-is to list organise_prophet\n", + " else:\n", + " index = i.find('{') #It finds the index(POSITION) of thc character '{' in x\n", + " organised_prophet.append(i[:index]) #This adds only the part before '{'\n", + " return \" \".join(organised_prophet)\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "prophet_reference = list(map(reference, prophet))" ] }, { @@ -135,11 +163,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['PROPHET |Almustafa,', 'the', 'chosen', 'and', 'the beloved,', 'who', 'was', 'a', 'dawn', 'unto', 'his', 'own day,', 'had', 'waited', 'twelve', 'years', 'in', 'the', 'city of', 'Orphalese']\n" + ] + } + ], + "source": [ + "prophet_reference = list(map(reference, prophet))\n", + "\n", + "print(prophet_reference[:20])" ] }, { @@ -151,11 +189,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "def line_break(x):\n", + " return x.split('\\n')\n", " '''\n", " Input: A string\n", " Output: A list of strings split on the line break (\\n) character\n", @@ -165,7 +204,7 @@ " Output: ['the', 'beloved']\n", " '''\n", " \n", - " # your code here" + " " ] }, { @@ -181,9 +220,19 @@ "metadata": { "scrolled": true }, - "outputs": [], - "source": [ - "# your code here" + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "prophet_line = map(line_break, prophet_reference)\n", + "\n", + "\n" ] }, { @@ -195,22 +244,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, - "outputs": [], - "source": [ - "prophet_flat = [i for sub in prophet_line for i in sub]\n", - "prophet_flat" + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['PROPHET |Almustafa,', 'the', 'chosen', 'and', 'the beloved,', 'who', 'was', 'a', 'dawn', 'unto', 'his', 'own day,', 'had', 'waited', 'twelve', 'years', 'in', 'the', 'city of', 'Orphalese']\n" + ] + } + ], + "source": [ + "# To flatten the prophet_reference (list of lists) into a single list\n", + "prophet_flat = [word for sublist in prophet_line for word in sublist]\n", + "\n", + "\n", + "print(prophet_flat[:20])\n", + "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "PROPHET |Almustafa,the\n" + ] + } + ], + "source": [] }, { "cell_type": "markdown", @@ -223,7 +290,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 58, "metadata": {}, "outputs": [], "source": [ @@ -244,7 +311,7 @@ " \n", " word_list = ['and', 'the', 'a', 'an']\n", " \n", - " # your code here" + " " ] }, { @@ -256,13 +323,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "metadata": { "scrolled": true }, - "outputs": [], - "source": [ - "# your code here" + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['PROPHET |Almustafa,', 'chosen', 'the beloved,', 'who', 'was', 'dawn', 'unto', 'his', 'own day,', 'had', 'waited', 'twelve', 'years', 'in', 'city of', 'Orphalese']\n" + ] + } + ], + "source": [ + "word_list = ['and', 'the', 'a', 'an']\n", + "\n", + "def word_filter(x):\n", + " return x not in word_list \n", + "\n", + "prophet_filter = list(filter(word_filter, prophet_flat[:20]))\n", + "print(prophet_filter)" ] }, { @@ -276,15 +357,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 68, "metadata": {}, "outputs": [], "source": [ "def word_filter_case(x):\n", + " return x not in word_list\n", " \n", " word_list = ['and', 'the', 'a', 'an']\n", " \n", - " # your code here" + " " ] }, { @@ -300,7 +382,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, "metadata": {}, "outputs": [], "source": [ @@ -314,7 +396,8 @@ " Output: 'John Smith'\n", " '''\n", " \n", - " # your code here" + " return a + ' ' + b\n", + "\n" ] }, { @@ -324,9 +407,7 @@ "scrolled": true }, "outputs": [], - "source": [ - "# your code here" - ] + "source": [] }, { "cell_type": "markdown", @@ -337,17 +418,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 77, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "PROPHET |Almustafa, chosen the beloved, who was dawn unto his own day, had waited twelve years in city of Orphalese\n" + ] + } + ], "source": [ - "# your code here" + "from functools import reduce\n", + "\n", + "prophet_string = reduce(concat_space, prophet_filter)\n", + "\n", + "print(prophet_string)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -361,7 +454,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,