From 5614e2d464d666fb2818b8f94ab98a9751490ad5 Mon Sep 17 00:00:00 2001 From: Jeanlebaptist Date: Wed, 23 Jul 2025 19:06:05 +0200 Subject: [PATCH] lab map, filter, reduce --- your-code/main.ipynb | 1239 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 1189 insertions(+), 50 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 9f0e67b..0a0ea7e 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ @@ -35,7 +35,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, "outputs": [], "source": [ @@ -48,9 +48,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "13637" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "len(prophet)" ] @@ -66,11 +77,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "# Keep only words from index 568 to the end\n", + "prophet = prophet[568:]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "First 10 words after trimming: ['PROPHET\\n\\n|Almustafa,', 'the{7}', 'chosen', 'and', 'the\\nbeloved,', 'who', 'was', 'a', 'dawn', 'unto']\n", + "Total words after trimming: 13069\n" + ] + } + ], + "source": [ + "print(\"First 10 words after trimming:\", prophet[:10])\n", + "print(\"Total words after trimming:\", len(prophet))\n" ] }, { @@ -82,11 +114,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "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" + "# your code here\n", + "\n", + "# Show the first 10 words to examine attached references or formatting artifacts\n", + "print(prophet[:10])\n" ] }, { @@ -100,30 +143,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, "outputs": [], "source": [ + "# your code here\n", + "\n", + "import re\n", + "\n", "def reference(x):\n", " '''\n", " Input: A string\n", " Output: The string with references removed\n", - " \n", + "\n", " Example:\n", " Input: 'the{7}'\n", " Output: 'the'\n", " '''\n", - " \n", - " # your code here" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + " # Remove patterns like {7}, [1], etc.\n", + " return re.sub(r'[\\{\\[].*?[\\}\\]]', '', x)\n" ] }, { @@ -135,11 +173,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "\n", + "# Apply the reference() function to each word in the list\n", + "prophet_reference = list(map(reference, prophet))\n" ] }, { @@ -151,7 +192,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, "outputs": [], "source": [ @@ -165,7 +206,18 @@ " Output: ['the', 'beloved']\n", " '''\n", " \n", - " # your code here" + " # your code here\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", + " return x.split('\\n')\n" ] }, { @@ -177,13 +229,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[['PROPHET', '', '|Almustafa,'], ['the'], ['chosen'], ['and'], ['the', 'beloved,']]\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "prophet_line = list(map(line_break, prophet_reference))\n", + "\n", + "print(prophet_line[:5])\n", + "\n", + "\n", + "\n", + "\n" ] }, { @@ -195,9 +263,1020 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['PROPHET',\n", + " '',\n", + " '|Almustafa,',\n", + " 'the',\n", + " 'chosen',\n", + " 'and',\n", + " 'the',\n", + " 'beloved,',\n", + " 'who',\n", + " 'was',\n", + " 'a',\n", + " 'dawn',\n", + " 'unto',\n", + " 'his',\n", + " 'own',\n", + " 'day,',\n", + " 'had',\n", + " 'waited',\n", + " 'twelve',\n", + " 'years',\n", + " 'in',\n", + " 'the',\n", + " 'city',\n", + " 'of',\n", + " 'Orphalese',\n", + " 'for',\n", + " 'his',\n", + " 'ship',\n", + " 'that',\n", + " 'was',\n", + " 'to',\n", + " 'return',\n", + " 'and',\n", + " 'bear',\n", + " 'him',\n", + " 'back',\n", + " 'to',\n", + " 'the',\n", + " 'isle',\n", + " 'of',\n", + " 'his',\n", + " 'birth.',\n", + " '',\n", + " 'And',\n", + " 'in',\n", + " 'the',\n", + " 'twelfth',\n", + " 'year,',\n", + " 'on',\n", + " 'the',\n", + " 'seventh',\n", + " 'day',\n", + " 'of',\n", + " 'Ielool,',\n", + " 'the',\n", + " 'month',\n", + " 'of',\n", + " 'reaping,',\n", + " 'he',\n", + " 'climbed',\n", + " 'the',\n", + " 'hill',\n", + " 'without',\n", + " 'the',\n", + " 'city',\n", + " 'walls',\n", + " 'and',\n", + " 'looked',\n", + " 'seaward;',\n", + " 'and',\n", + " 'he',\n", + " 'beheld',\n", + " 'his',\n", + " 'ship',\n", + " 'coming',\n", + " 'with',\n", + " 'the',\n", + " 'mist.',\n", + " '',\n", + " 'Then',\n", + " 'the',\n", + " 'gates',\n", + " 'of',\n", + " 'his',\n", + " 'heart',\n", + " 'were',\n", + " 'flung',\n", + " 'open,',\n", + " 'and',\n", + " 'his',\n", + " 'joy',\n", + " 'flew',\n", + " 'far',\n", + " 'over',\n", + " 'the',\n", + " 'sea.',\n", + " 'And',\n", + " 'he',\n", + " 'closed',\n", + " 'his',\n", + " 'eyes',\n", + " 'and',\n", + " 'prayed',\n", + " 'in',\n", + " 'the',\n", + " 'silences',\n", + " 'of',\n", + " 'his',\n", + " 'soul.',\n", + " '',\n", + " '*****',\n", + " '',\n", + " 'But',\n", + " 'as',\n", + " 'he',\n", + " 'descended',\n", + " 'the',\n", + " 'hill,',\n", + " 'a',\n", + " 'sadness',\n", + " 'came',\n", + " 'upon',\n", + " 'him,',\n", + " 'and',\n", + " 'he',\n", + " 'thought',\n", + " 'in',\n", + " 'his',\n", + " 'heart:',\n", + " '',\n", + " 'How',\n", + " 'shall',\n", + " 'I',\n", + " 'go',\n", + " 'in',\n", + " 'peace',\n", + " 'and',\n", + " 'without',\n", + " 'sorrow?',\n", + " 'Nay,',\n", + " 'not',\n", + " 'without',\n", + " 'a',\n", + " 'wound',\n", + " 'in',\n", + " 'the',\n", + " 'spirit',\n", + " 'shall',\n", + " 'I',\n", + " 'leave',\n", + " 'this',\n", + " 'city.',\n", + " 'Long',\n", + " 'were',\n", + " 'the',\n", + " 'days',\n", + " 'of',\n", + " 'pain',\n", + " 'I',\n", + " 'have',\n", + " 'spent',\n", + " 'within',\n", + " 'its',\n", + " 'walls,',\n", + " 'and',\n", + " 'long',\n", + " 'were',\n", + " 'the',\n", + " 'nights',\n", + " 'of',\n", + " 'aloneness;',\n", + " 'and',\n", + " 'who',\n", + " 'can',\n", + " 'depart',\n", + " 'from',\n", + " 'his',\n", + " 'pain',\n", + " 'and',\n", + " 'his',\n", + " 'aloneness',\n", + " 'without',\n", + " 'regret?',\n", + " '',\n", + " 'Too',\n", + " 'many',\n", + " 'fragments',\n", + " 'of',\n", + " 'the',\n", + " 'spirit',\n", + " 'have',\n", + " 'I',\n", + " 'scattered',\n", + " 'in',\n", + " 'these',\n", + " 'streets,',\n", + " 'and',\n", + " 'too',\n", + " 'many',\n", + " 'are',\n", + " 'the',\n", + " 'children',\n", + " 'of',\n", + " 'my',\n", + " 'longing',\n", + " 'that',\n", + " 'walk',\n", + " 'naked',\n", + " 'among',\n", + " 'these',\n", + " 'hills,',\n", + " 'and',\n", + " 'I',\n", + " 'cannot',\n", + " 'withdraw',\n", + " 'from',\n", + " 'them',\n", + " 'without',\n", + " 'a',\n", + " 'burden',\n", + " 'and',\n", + " 'an',\n", + " 'ache.',\n", + " '',\n", + " 'It',\n", + " 'is',\n", + " 'not',\n", + " 'a',\n", + " 'garment',\n", + " 'I',\n", + " 'cast',\n", + " 'off',\n", + " 'this',\n", + " 'day,',\n", + " 'but',\n", + " 'a',\n", + " 'skin',\n", + " 'that',\n", + " 'I',\n", + " 'tear',\n", + " 'with',\n", + " 'my',\n", + " 'own',\n", + " 'hands.',\n", + " '',\n", + " 'Nor',\n", + " 'is',\n", + " 'it',\n", + " 'a',\n", + " 'thought',\n", + " 'I',\n", + " 'leave',\n", + " 'behind',\n", + " 'me,',\n", + " 'but',\n", + " 'a',\n", + " 'heart',\n", + " 'made',\n", + " 'sweet',\n", + " 'with',\n", + " 'hunger',\n", + " 'and',\n", + " 'with',\n", + " 'thirst.',\n", + " '',\n", + " '*****',\n", + " '',\n", + " 'Yet',\n", + " 'I',\n", + " 'cannot',\n", + " 'tarry',\n", + " 'longer.',\n", + " '',\n", + " 'The',\n", + " 'sea',\n", + " 'that',\n", + " 'calls',\n", + " 'all',\n", + " 'things',\n", + " 'unto',\n", + " 'her',\n", + " 'calls',\n", + " 'me,',\n", + " 'and',\n", + " 'I',\n", + " 'must',\n", + " 'embark.',\n", + " '',\n", + " 'For',\n", + " 'to',\n", + " 'stay,',\n", + " 'though',\n", + " 'the',\n", + " 'hours',\n", + " 'burn',\n", + " 'in',\n", + " 'the',\n", + " 'night,',\n", + " 'is',\n", + " 'to',\n", + " 'freeze',\n", + " 'and',\n", + " 'crystallize',\n", + " 'and',\n", + " 'be',\n", + " 'bound',\n", + " 'in',\n", + " 'a',\n", + " 'mould.',\n", + " '',\n", + " 'Fain',\n", + " 'would',\n", + " 'I',\n", + " 'take',\n", + " 'with',\n", + " 'me',\n", + " 'all',\n", + " 'that',\n", + " 'is',\n", + " 'here.',\n", + " 'But',\n", + " 'how',\n", + " 'shall',\n", + " 'I?',\n", + " '',\n", + " 'A',\n", + " 'voice',\n", + " 'cannot',\n", + " 'carry',\n", + " 'the',\n", + " 'tongue',\n", + " 'and',\n", + " 'the',\n", + " 'lips',\n", + " 'that',\n", + " 'gave',\n", + " 'it',\n", + " 'wings.',\n", + " 'Alone',\n", + " 'must',\n", + " 'it',\n", + " 'seek',\n", + " 'the',\n", + " 'ether.',\n", + " '',\n", + " 'And',\n", + " 'alone',\n", + " 'and',\n", + " 'without',\n", + " 'his',\n", + " 'nest',\n", + " 'shall',\n", + " 'the',\n", + " 'eagle',\n", + " 'fly',\n", + " 'across',\n", + " 'the',\n", + " 'sun.',\n", + " '',\n", + " '*****',\n", + " '',\n", + " 'Now',\n", + " 'when',\n", + " 'he',\n", + " 'reached',\n", + " 'the',\n", + " 'foot',\n", + " 'of',\n", + " 'the',\n", + " 'hill,',\n", + " 'he',\n", + " 'turned',\n", + " 'again',\n", + " 'towards',\n", + " 'the',\n", + " 'sea,',\n", + " 'and',\n", + " 'he',\n", + " 'saw',\n", + " 'his',\n", + " 'ship',\n", + " 'approaching',\n", + " 'the',\n", + " 'harbour,',\n", + " 'and',\n", + " 'upon',\n", + " 'her',\n", + " 'prow',\n", + " 'the',\n", + " 'mariners,',\n", + " 'the',\n", + " 'men',\n", + " 'of',\n", + " 'his',\n", + " 'own',\n", + " 'land.',\n", + " '',\n", + " 'And',\n", + " 'his',\n", + " 'soul',\n", + " 'cried',\n", + " 'out',\n", + " 'to',\n", + " 'them,',\n", + " 'and',\n", + " 'he',\n", + " 'said:',\n", + " '',\n", + " 'Sons',\n", + " 'of',\n", + " 'my',\n", + " 'ancient',\n", + " 'mother,',\n", + " 'you',\n", + " 'riders',\n", + " 'of',\n", + " 'the',\n", + " 'tides,',\n", + " '',\n", + " 'How',\n", + " 'often',\n", + " 'have',\n", + " 'you',\n", + " 'sailed',\n", + " 'in',\n", + " 'my',\n", + " 'dreams.',\n", + " 'And',\n", + " 'now',\n", + " 'you',\n", + " 'come',\n", + " 'in',\n", + " 'my',\n", + " 'awakening,',\n", + " 'which',\n", + " 'is',\n", + " 'my',\n", + " 'deeper',\n", + " 'dream.',\n", + " '',\n", + " 'Ready',\n", + " 'am',\n", + " 'I',\n", + " 'to',\n", + " 'go,',\n", + " 'and',\n", + " 'my',\n", + " 'eagerness',\n", + " 'with',\n", + " 'sails',\n", + " 'full',\n", + " 'set',\n", + " 'awaits',\n", + " 'the',\n", + " 'wind.',\n", + " '',\n", + " 'Only',\n", + " 'another',\n", + " 'breath',\n", + " 'will',\n", + " 'I',\n", + " 'breathe',\n", + " 'in',\n", + " 'this',\n", + " 'still',\n", + " 'air,',\n", + " 'only',\n", + " 'another',\n", + " 'loving',\n", + " 'look',\n", + " 'cast',\n", + " 'backward,',\n", + " '',\n", + " 'And',\n", + " 'then',\n", + " 'I',\n", + " 'shall',\n", + " 'stand',\n", + " 'among',\n", + " 'you,',\n", + " 'a',\n", + " 'seafarer',\n", + " 'among',\n", + " 'seafarers.',\n", + " 'And',\n", + " 'you,',\n", + " 'vast',\n", + " 'sea,',\n", + " 'sleepless',\n", + " 'mother,',\n", + " '',\n", + " 'Who',\n", + " 'alone',\n", + " 'are',\n", + " 'peace',\n", + " 'and',\n", + " 'freedom',\n", + " 'to',\n", + " 'the',\n", + " 'river',\n", + " 'and',\n", + " 'the',\n", + " 'stream,',\n", + " '',\n", + " 'Only',\n", + " 'another',\n", + " 'winding',\n", + " 'will',\n", + " 'this',\n", + " 'stream',\n", + " 'make,',\n", + " 'only',\n", + " 'another',\n", + " 'murmur',\n", + " 'in',\n", + " 'this',\n", + " 'glade,',\n", + " '',\n", + " 'And',\n", + " 'then',\n", + " 'shall',\n", + " 'I',\n", + " 'come',\n", + " 'to',\n", + " 'you,',\n", + " 'a',\n", + " 'boundless',\n", + " 'drop',\n", + " 'to',\n", + " 'a',\n", + " 'boundless',\n", + " 'ocean.',\n", + " '',\n", + " '*****',\n", + " '',\n", + " 'And',\n", + " 'as',\n", + " 'he',\n", + " 'walked',\n", + " 'he',\n", + " 'saw',\n", + " 'from',\n", + " 'afar',\n", + " 'men',\n", + " 'and',\n", + " 'women',\n", + " 'leaving',\n", + " 'their',\n", + " 'fields',\n", + " 'and',\n", + " 'their',\n", + " 'vineyards',\n", + " 'and',\n", + " 'hastening',\n", + " 'towards',\n", + " 'the',\n", + " 'city',\n", + " 'gates.',\n", + " '',\n", + " 'And',\n", + " 'he',\n", + " 'heard',\n", + " 'their',\n", + " 'voices',\n", + " 'calling',\n", + " 'his',\n", + " 'name,',\n", + " 'and',\n", + " 'shouting',\n", + " 'from',\n", + " 'field',\n", + " 'to',\n", + " 'field',\n", + " 'telling',\n", + " 'one',\n", + " 'another',\n", + " 'of',\n", + " 'the',\n", + " 'coming',\n", + " 'of',\n", + " 'his',\n", + " 'ship.',\n", + " '',\n", + " 'And',\n", + " 'he',\n", + " 'said',\n", + " 'to',\n", + " 'himself:',\n", + " '',\n", + " 'Shall',\n", + " 'the',\n", + " 'day',\n", + " 'of',\n", + " 'parting',\n", + " 'be',\n", + " 'the',\n", + " 'day',\n", + " 'of',\n", + " 'gathering?',\n", + " '',\n", + " 'And',\n", + " 'shall',\n", + " 'it',\n", + " 'be',\n", + " 'said',\n", + " 'that',\n", + " 'my',\n", + " 'eve',\n", + " 'was',\n", + " 'in',\n", + " 'truth',\n", + " 'my',\n", + " 'dawn?',\n", + " '',\n", + " 'And',\n", + " 'what',\n", + " 'shall',\n", + " 'I',\n", + " 'give',\n", + " 'unto',\n", + " 'him',\n", + " 'who',\n", + " 'has',\n", + " 'left',\n", + " 'his',\n", + " 'plough',\n", + " 'in',\n", + " 'midfurrow,',\n", + " 'or',\n", + " 'to',\n", + " 'him',\n", + " 'who',\n", + " 'has',\n", + " 'stopped',\n", + " 'the',\n", + " 'wheel',\n", + " 'of',\n", + " 'his',\n", + " 'winepress?',\n", + " 'Shall',\n", + " 'my',\n", + " 'heart',\n", + " 'become',\n", + " 'a',\n", + " 'tree',\n", + " 'heavy-laden',\n", + " 'with',\n", + " 'fruit',\n", + " 'that',\n", + " 'I',\n", + " 'may',\n", + " 'gather',\n", + " 'and',\n", + " 'give',\n", + " 'unto',\n", + " 'them?',\n", + " '',\n", + " 'And',\n", + " 'shall',\n", + " 'my',\n", + " 'desires',\n", + " 'flow',\n", + " 'like',\n", + " 'a',\n", + " 'fountain',\n", + " 'that',\n", + " 'I',\n", + " 'may',\n", + " 'fill',\n", + " 'their',\n", + " 'cups?',\n", + " '',\n", + " 'Am',\n", + " 'I',\n", + " 'a',\n", + " 'harp',\n", + " 'that',\n", + " 'the',\n", + " 'hand',\n", + " 'of',\n", + " 'the',\n", + " 'mighty',\n", + " 'may',\n", + " 'touch',\n", + " 'me,',\n", + " 'or',\n", + " 'a',\n", + " 'flute',\n", + " 'that',\n", + " 'his',\n", + " 'breath',\n", + " 'may',\n", + " 'pass',\n", + " 'through',\n", + " 'me?',\n", + " '',\n", + " 'A',\n", + " 'seeker',\n", + " 'of',\n", + " 'silences',\n", + " 'am',\n", + " 'I,',\n", + " 'and',\n", + " 'what',\n", + " 'treasure',\n", + " 'have',\n", + " 'I',\n", + " 'found',\n", + " 'in',\n", + " 'silences',\n", + " 'that',\n", + " 'I',\n", + " 'may',\n", + " 'dispense',\n", + " 'with',\n", + " 'confidence?',\n", + " '',\n", + " 'If',\n", + " 'this',\n", + " 'is',\n", + " 'my',\n", + " 'day',\n", + " 'of',\n", + " 'harvest,',\n", + " 'in',\n", + " 'what',\n", + " 'fields',\n", + " 'have',\n", + " 'I',\n", + " 'sowed',\n", + " 'the',\n", + " 'seed,',\n", + " 'and',\n", + " 'in',\n", + " 'what',\n", + " 'unremembered',\n", + " 'seasons?',\n", + " '',\n", + " 'If',\n", + " 'this',\n", + " 'indeed',\n", + " 'be',\n", + " 'the',\n", + " 'hour',\n", + " 'in',\n", + " 'which',\n", + " 'I',\n", + " 'lift',\n", + " 'up',\n", + " 'my',\n", + " 'lantern,',\n", + " 'it',\n", + " 'is',\n", + " 'not',\n", + " 'my',\n", + " 'flame',\n", + " 'that',\n", + " 'shall',\n", + " 'burn',\n", + " 'therein.',\n", + " '',\n", + " 'Empty',\n", + " 'and',\n", + " 'dark',\n", + " 'shall',\n", + " 'I',\n", + " 'raise',\n", + " 'my',\n", + " 'lantern,',\n", + " '',\n", + " 'And',\n", + " 'the',\n", + " 'guardian',\n", + " 'of',\n", + " 'the',\n", + " 'night',\n", + " 'shall',\n", + " 'fill',\n", + " 'it',\n", + " 'with',\n", + " 'oil',\n", + " 'and',\n", + " 'he',\n", + " 'shall',\n", + " 'light',\n", + " 'it',\n", + " 'also.',\n", + " '',\n", + " '*****',\n", + " '',\n", + " 'These',\n", + " 'things',\n", + " 'he',\n", + " 'said',\n", + " 'in',\n", + " 'words.',\n", + " 'But',\n", + " 'much',\n", + " 'in',\n", + " 'his',\n", + " 'heart',\n", + " 'remained',\n", + " 'unsaid.',\n", + " 'For',\n", + " 'he',\n", + " 'himself',\n", + " 'could',\n", + " 'not',\n", + " 'speak',\n", + " 'his',\n", + " 'deeper',\n", + " 'secret.',\n", + " '',\n", + " '*****',\n", + " '',\n", + " '[Illustration:',\n", + " '0020]',\n", + " '',\n", + " 'And',\n", + " 'when',\n", + " 'he',\n", + " 'entered',\n", + " 'into',\n", + " 'the',\n", + " 'city',\n", + " 'all',\n", + " 'the',\n", + " 'people',\n", + " 'came',\n", + " 'to',\n", + " 'meet',\n", + " 'him,',\n", + " 'and',\n", + " 'they',\n", + " 'were',\n", + " 'crying',\n", + " 'out',\n", + " 'to',\n", + " 'him',\n", + " 'as',\n", + " 'with',\n", + " 'one',\n", + " 'voice.',\n", + " '',\n", + " 'And',\n", + " 'the',\n", + " 'elders',\n", + " 'of',\n", + " 'the',\n", + " 'city',\n", + " 'stood',\n", + " 'forth',\n", + " 'and',\n", + " 'said:',\n", + " '',\n", + " 'Go',\n", + " 'not',\n", + " 'yet',\n", + " 'away',\n", + " 'from',\n", + " 'us.',\n", + " '',\n", + " 'A',\n", + " 'noontide',\n", + " 'have',\n", + " 'you',\n", + " 'been',\n", + " 'in',\n", + " 'our',\n", + " 'twilight,',\n", + " 'and',\n", + " 'your',\n", + " 'youth',\n", + " 'has',\n", + " 'given',\n", + " 'us',\n", + " 'dreams',\n", + " 'to',\n", + " 'dream.',\n", + " '',\n", + " 'No',\n", + " 'stranger',\n", + " 'are',\n", + " 'you',\n", + " 'among',\n", + " 'us,',\n", + " 'nor',\n", + " 'a',\n", + " 'guest,',\n", + " 'but',\n", + " 'our',\n", + " 'son',\n", + " 'and',\n", + " 'our',\n", + " 'dearly',\n", + " 'beloved.',\n", + " '',\n", + " 'Suffer',\n", + " 'not',\n", + " 'yet',\n", + " 'our',\n", + " 'eyes',\n", + " 'to',\n", + " 'hunger',\n", + " 'for',\n", + " 'your',\n", + " 'face.',\n", + " '',\n", + " '*****',\n", + " '',\n", + " 'And',\n", + " 'the',\n", + " 'priests',\n", + " 'and',\n", + " 'the',\n", + " 'priestesses',\n", + " 'said',\n", + " 'unto',\n", + " 'him:',\n", + " '',\n", + " 'Let',\n", + " 'not',\n", + " 'the',\n", + " 'waves',\n", + " 'of',\n", + " 'the',\n", + " 'sea',\n", + " 'separate',\n", + " 'us',\n", + " 'now,',\n", + " 'and',\n", + " 'the',\n", + " 'years',\n", + " 'you',\n", + " 'have',\n", + " 'spent',\n", + " 'in',\n", + " 'our',\n", + " 'midst',\n", + " 'become',\n", + " 'a',\n", + " 'memory.',\n", + " '',\n", + " 'You',\n", + " 'have',\n", + " 'walked',\n", + " 'among',\n", + " 'us',\n", + " 'a',\n", + " 'spirit,',\n", + " 'and',\n", + " 'your',\n", + " 'shadow',\n", + " 'has',\n", + " 'been',\n", + " 'a',\n", + " 'light',\n", + " 'upon',\n", + " 'our',\n", + " 'faces.',\n", + " '',\n", + " 'Much',\n", + " 'have',\n", + " 'we',\n", + " 'loved',\n", + " 'you.',\n", + " 'But',\n", + " 'speechless',\n", + " 'was',\n", + " 'our',\n", + " 'love,',\n", + " 'and',\n", + " 'with',\n", + " 'veils',\n", + " 'has',\n", + " 'it',\n", + " 'been',\n", + " 'veiled.',\n", + " '',\n", + " 'Yet',\n", + " 'now',\n", + " 'it',\n", + " 'cries',\n", + " 'aloud',\n", + " 'unto',\n", + " 'you,',\n", + " 'and',\n", + " 'would',\n", + " 'stand',\n", + " 'revealed',\n", + " 'before',\n", + " 'you.',\n", + " '',\n", + " 'And',\n", + " 'ever',\n", + " 'has',\n", + " 'it',\n", + " 'been',\n", + " 'that',\n", + " 'love',\n", + " ...]" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "prophet_flat = [i for sub in prophet_line for i in sub]\n", "prophet_flat" @@ -205,11 +1284,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 58, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "\n", + "prophet_flat = [i for sub in prophet_line for i in sub]\n" ] }, { @@ -223,9 +1304,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], "source": [ "def word_filter(x):\n", " '''\n", @@ -244,7 +1334,12 @@ " \n", " word_list = ['and', 'the', 'a', 'an']\n", " \n", - " # your code here" + " # your code here\n", + "\n", + " return x not in word_list\n", + "\n", + "print(word_filter(\"and\")) # False\n", + "print(word_filter(\"light\")) # True\n" ] }, { @@ -256,13 +1351,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "metadata": { "scrolled": true }, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "\n", + "# This removes empty and very short words (like 'a', 'i')\n", + "prophet_filtered = list(filter(lambda x: len(x) > 1, prophet_flat))\n", + "\n", + "\n", + "prophet_filter = list(filter(word_filter, prophet_filtered))\n" ] }, { @@ -276,15 +1377,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 64, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], "source": [ "def word_filter_case(x):\n", " \n", " word_list = ['and', 'the', 'a', 'an']\n", " \n", - " # your code here" + " # your code here\n", + " return x.lower() not in word_list\n", + "\n", + "print(word_filter_case(\"The\")) # False\n", + "print(word_filter_case(\"truth\")) # True\n" ] }, { @@ -300,7 +1414,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "metadata": {}, "outputs": [], "source": [ @@ -314,18 +1428,25 @@ " Output: 'John Smith'\n", " '''\n", " \n", - " # your code here" + " # your code here\n", + "\n", + " return a + ' ' + b" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 66, "metadata": { "scrolled": true }, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "\n", + "from functools import reduce\n", + "\n", + "# Assuming prophet_filter is your cleaned list of words\n", + "prophet_text = reduce(concat_space, prophet_filter)\n" ] }, { @@ -337,17 +1458,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 68, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "PROPHET |Almustafa, chosen beloved, who was dawn unto his own day, had waited twelve years in city of Orphalese for his ship that was to return bear him back to isle of his birth. And in twelfth year, on seventh day of Ielool, month of reaping, he climbed hill without city walls looked seaward; he b\n", + "\n", + "Total characters: 76740\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "from functools import reduce\n", + "\n", + "prophet_string = reduce(concat_space, prophet_filter)\n", + "\n", + "print(prophet_string[:300]) # Show the first 300 characters\n", + "print(\"\\nTotal characters:\", len(prophet_string))\n", + "\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -361,7 +1500,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,