diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 9f0e67b..a2e9594 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -35,7 +35,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -48,9 +48,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "13637" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "len(prophet)" ] @@ -66,11 +77,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "13637\n", + "13069\n" + ] + } + ], "source": [ - "# your code here" + "location = '../data/58585-0.txt'\n", + "with open(location, 'r', encoding=\"utf8\") as f:\n", + " prophet = f.read().split(' ')\n", + "\n", + " len(prophet)\n", + "\n", + "print(len(prophet))\n", + "prophet = prophet[568:]\n", + "print(len(prophet))\n" ] }, { @@ -82,11 +110,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "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 +136,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -114,16 +150,25 @@ " Output: 'the'\n", " '''\n", " \n", - " # your code here" + " # Split the string at '{' adn take first part (before '{')\n", + " return x.split('{')[0]" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the\n" + ] + } + ], "source": [ - "# your code here" + "print(reference('the{7}')) #Output: 'the' " ] }, { @@ -135,11 +180,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['PROPHET\\n\\n|Almustafa,', 'the', 'chosen', 'and', 'the\\nbeloved,', 'who', 'was', 'a', 'dawn', 'unto']\n" + ] + } + ], + "source": [ + "prophet_reference = list(map(reference, prophet)) \n", + "\n", + "print(prophet_reference[:10]) # Check first 10 words" ] }, { @@ -151,7 +206,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -165,7 +220,7 @@ " Output: ['the', 'beloved']\n", " '''\n", " \n", - " # your code here" + " return x.split('\\n')" ] }, { @@ -177,13 +232,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": { "scrolled": true }, - "outputs": [], - "source": [ - "# your code here" + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[['PROPHET', '', '|Almustafa,'], ['the'], ['chosen'], ['and'], ['the', 'beloved,'], ['who'], ['was'], ['a'], ['dawn'], ['unto']]\n" + ] + } + ], + "source": [ + "# Apply the line_break function to the prophet_reference list\n", + "prophet_reference = list(map(line_break, prophet_reference))\n", + "\n", + "# Check the first 10 elements \n", + "print(prophet_reference[:10]) # Check first 10 elements" ] }, { @@ -197,19 +264,21 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], - "source": [ - "prophet_flat = [i for sub in prophet_line for i in sub]\n", - "prophet_flat" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "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']\n" + ] + } + ], "source": [ - "# your code here" + "\n", + "prophet_line = list(map(line_break, prophet_reference))\n", + "# Flatten the list of lists into a single list\n", + "prophet_flat = [item for sublist in prophet_reference for item in sublist]\n", + "print(prophet_flat[:20]) # Check first 20 elements" ] }, { @@ -244,7 +313,8 @@ " \n", " word_list = ['and', 'the', 'a', 'an']\n", " \n", - " # your code here" + " # Check if x is in the list, return False if yes, True is no\n", + " return x.lower() not in word_list" ] }, { @@ -256,13 +326,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": { "scrolled": true }, "outputs": [], "source": [ - "# your code here" + "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", + " # Check if x is in the list, return False if yes, True is no\n", + " return x.lower() not in word_list" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n", + "False\n" + ] + } + ], + "source": [ + "print(word_filter('and')) # Output: False\n", + "print(word_filter('John')) # Output: True \n", + "print(word_filter('the')) # Output: False" ] }, { @@ -276,7 +385,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ @@ -284,7 +393,27 @@ " \n", " word_list = ['and', 'the', 'a', 'an']\n", " \n", - " # your code here" + " # Check if x is in the list, return False if yes, True if no\n", + " return x.lower() not in word_list " + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], + "source": [ + "print(word_filter('and')) # Output: False\n", + "print(word_filter('John')) # Output: True " ] }, { @@ -300,7 +429,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ @@ -313,19 +442,26 @@ " Input: 'John', 'Smith'\n", " Output: 'John Smith'\n", " '''\n", - " \n", - " # your code here" + " return a + ' ' + b" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "John Smith\n" + ] + } + ], "source": [ - "# your code here" + "print(concat_space('John', 'Smith'))" ] }, { @@ -337,17 +473,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "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 for his ship that was to return and bear him back to the isle of his birth. And in the twelfth year, on the seventh day of Ielool, the month of reaping, he climbed the \n" + ] + } + ], "source": [ - "# your code here" + "from functools import reduce\n", + "prophet_string = reduce(concat_space, prophet_flat) \n", + "print(prophet_string[:300]) # Check the concatenated string" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -361,7 +507,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,