From e6684fcf04508312500ec3c0d8c50c95f27cc2cf Mon Sep 17 00:00:00 2001 From: Tarinee Date: Mon, 9 Oct 2023 20:47:27 +0200 Subject: [PATCH 1/2] TarineeMeesters --- your-code/challenge-1.ipynb | 167 ++++++++++++++++++++++++++++-------- 1 file changed, 133 insertions(+), 34 deletions(-) diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index c574eba..d810bc6 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -33,12 +33,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Durante un tiempo no estuvo segura de si su marido era su marido.\n" + ] + } + ], "source": [ "str_list = ['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", - "# Your code here:\n" + "combine_str_list = \" \".join(str_list)+\".\"\n", + "print (combine_str_list)\n" ] }, { @@ -50,12 +59,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Grocery_list ['bananas', 'bread', 'brownie mix', 'broccoli'] .\n" + ] + } + ], "source": [ "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", - "# Your code here:\n" + "\n", + "Grocery_list = []\n", + "for item in food_list:\n", + " if item.lower()[0] == 'b':\n", + " Grocery_list.append(item.lower())\n", + " \n", + "print(\"Grocery_list\", (Grocery_list),\".\")\n", + "\n" ] }, { @@ -69,9 +93,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The area of the circle with radius: 4.5 is: 63.61725123519331\n" + ] + } + ], "source": [ "import math\n", "\n", @@ -80,17 +112,13 @@ "radius = 4.5\n", "\n", "def area(x, pi = math.pi):\n", - " # This function takes a radius and returns the area of a circle. We also pass a default value for pi.\n", - " # Input: Float (and default value for pi)\n", - " # Output: Float\n", - " \n", - " # Sample input: 5.0\n", - " # Sample Output: 78.53981633\n", - " \n", - " # Your code here:\n", " return pi * (x**2)\n", - " \n", - "# Your output string here:\n" + "\n", + "output_string = f\"{string1} {radius} {string2} {area(radius)}\"\n", + "\n", + "print(output_string)\n", + "\n", + "\n" ] }, { @@ -106,9 +134,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 'suffice.': {'some': 2, 'say': 3, 'the': 1, 'world': 1, 'will': 1, 'end': 1, 'in': 2, 'fire,': 1, 'ice.': 1, 'from': 1, 'what': 1, 'i’ve': 1, 'tasted': 1, 'of': 2, 'desire': 1, 'i': 3, 'hold': 1, 'with': 1, 'those': 1, 'who': 1, 'favor': 1, 'fire.': 1, 'but': 1, 'if': 1, 'it': 1, 'had': 1, 'to': 2, 'perish': 1, 'twice,': 1, 'think': 1, 'know': 1, 'enough': 1, 'hate': 1, 'that': 1, 'for': 1, 'destruction': 1, 'ice': 1, 'is': 1, 'also': 1, 'great': 1, 'and': 1, 'would': 1, 'suffice.': 1}\n" + ] + } + ], "source": [ "poem = \"\"\"Some say the world will end in fire,\n", "Some say in ice.\n", @@ -120,7 +156,18 @@ "Is also great\n", "And would suffice.\"\"\"\n", "\n", - "# Your code here:\n" + "\n", + "list = poem.split()\n", + "\n", + "count_words = {}\n", + "for words in list:\n", + " words = words.lower()\n", + " count_words[words] = count_words.get(words, 0) +1\n", + "print(f\" '{words}': {count_words}\")\n", + " \n", + " \n", + " \n", + " " ] }, { @@ -132,9 +179,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'i': 1, 'was': 1, 'angry': 1, 'with': 1, 'my': 1, 'friend': 1, 'told': 1, 'wrath': 1, 'did': 1, 'end': 1, 'foe': 1, 'not': 1, 'grow': 1, 'waterd': 1, 'fears': 1, 'night': 1, 'morning': 1, 'tears': 1, 'sunned': 1, 'smiles': 1, 'soft': 1, 'deceitful': 1, 'wiles': 1, 'grew': 1, 'both': 1, 'day': 1, 'till': 1, 'bore': 1, 'apple': 1, 'bright': 1, 'beheld': 1, 'shine': 1, 'he': 1, 'knew': 1, 'that': 1, 'mine': 1, 'into': 1, 'garden': 1, 'stole': 1, 'when': 1, 'had': 1, 'veild': 1, 'pole': 1, 'glad': 1, 'see': 1, 'outstretched': 1, 'beneath': 1, 'tree': 1}\n" + ] + } + ], "source": [ "blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n", "\n", @@ -158,7 +213,19 @@ "In the morning glad I see; \n", "My foe outstretched beneath the tree.\"\"\"\n", "\n", - "# Your code here:\n" + "\n", + "import re\n", + "lower_case_poem = poem.lower()\n", + "words = re.findall(r'\\b\\w+\\b', lower_case_poem)\n", + "check_with_blacklist = {}\n", + "\n", + "for item in words:\n", + " if item not in blacklist:\n", + " check_with_blacklist[item] = 1\n", + " \n", + "print(check_with_blacklist)\n", + " \n", + " \n" ] }, { @@ -172,16 +239,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "THE APPARITION OF THESE FACES IN THE CROWD;\n", + "PETALS ON A WET, BLACK BOUGH.\n" + ] + } + ], "source": [ "import re\n", "\n", "poem = \"\"\"The apparition of these faces in the crowd;\n", "Petals on a wet, black bough.\"\"\"\n", "\n", - "# Your code here:\n" + "upper_case_poem = poem.upper()\n", + "\n", + "print (upper_case_poem)\n" ] }, { @@ -193,13 +271,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n" + ] + } + ], "source": [ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", "\n", - "# Your code here:\n" + "\n", + "lookup = str(data)\n", + "filtered_list = []\n", + "for item in data:\n", + " if re.search(lookup, item) is not None:\n", + " filtered_list.append(item)\n", + "print(filtered_list)\n", + "\n", + " \n" ] }, { @@ -220,13 +314,18 @@ "outputs": [], "source": [ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", - "# Your code here:\n" + "# Your code here:\n", + "\n", + "\n", + "\n", + "\n", + "\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -240,7 +339,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.9.13" } }, "nbformat": 4, From b346b174b42f6cefc1d88b1180c017742c11857a Mon Sep 17 00:00:00 2001 From: Tarinee Date: Wed, 11 Oct 2023 17:24:45 +0200 Subject: [PATCH 2/2] TarineeMeesters --- your-code/challenge-1.ipynb | 11 ++- your-code/challenge-2.ipynb | 173 +++++++++++++++++++++++++++++------- 2 files changed, 152 insertions(+), 32 deletions(-) diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index d810bc6..1b7bea7 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -309,11 +309,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "\n", "# Your code here:\n", "\n", "\n", @@ -321,6 +321,13 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index 6873bd2..c9e88dc 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -67,16 +67,23 @@ "]\n", "```\n", "\n", + "\n", "Now let's define the `docs` array that contains the paths of `doc1.txt`, `doc2.txt`, and `doc3.txt`." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ - "docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']" + "# doc1.txt = 'Ironhack is cool'\n", + "\n", + "# doc2.txt = 'I love Ironhack.''\n", + "\n", + "# doc3.txt = 'I am a student at Ironhack.'\n", + "\n", + "docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']\n" ] }, { @@ -88,11 +95,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ - "# Write your code here\n" + "\n", + "\n", + "docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']\n", + "corpus = []\n", + "for files in docs:\n", + " with open(files,'r')as file:\n", + " content = file.read()\n", + " corpus.append(content)\n", + "\n", + "\n", + " " ] }, { @@ -104,10 +121,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']\n" + ] + } + ], + "source": [ + "print (corpus)" + ] }, { "cell_type": "markdown", @@ -132,11 +159,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhackiscool ', 'IloveIronhack ', 'IamastudentatIronhack ']\n" + ] + } + ], "source": [ - "# Write your code here" + "\n", + "corpus = ['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']\n", + "\n", + "def remove_punctuation(input_string):\n", + " cleaned_string = input_string.replace(\" \", \"\").replace(\".\", \" \")\n", + " return cleaned_string\n", + "\n", + "cleaned_corpus = [remove_punctuation(s) for s in corpus]\n", + "\n", + "print(cleaned_corpus)\n" ] }, { @@ -148,10 +192,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "bag_of_words = []" + ] }, { "cell_type": "markdown", @@ -166,11 +212,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack', 'is', 'cool.', 'I', 'love', 'Ironhack.', 'am', 'a', 'student', 'at', 'ironhack', 'i', 'ironhack.']\n" + ] + } + ], "source": [ - "# Write your code here\n" + "\n", + "\n", + "for sentences in corpus:\n", + " terms = sentences.split()\n", + " \n", + " for term in terms:\n", + " if term not in bag_of_words:\n", + " bag_of_words.append(term)\n", + "\n", + "print(bag_of_words)\n", + "\n", + "\n", + "\n" ] }, { @@ -186,10 +252,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'is', 'cool.', 'i', 'love', 'ironhack.', 'am', 'a', 'student', 'at']\n" + ] + } + ], + "source": [ + "corpus = ['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']\n", + "bag_of_words = ()\n", + "\n", + "for sentences in corpus:\n", + " terms = sentences.lower().split()\n", + " \n", + " for term in terms:\n", + " if term not in bag_of_words:\n", + " bag_of_words.append(term)\n", + "\n", + "print(bag_of_words)\n", + "\n", + "\n" + ] }, { "cell_type": "markdown", @@ -200,11 +288,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 75, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'ironhack': 1, 'is': 1, 'cool.': 1}, {'i': 1, 'love': 1, 'ironhack.': 1}, {'i': 1, 'am': 1, 'a': 1, 'student': 1, 'at': 1, 'ironhack.': 1}]\n" + ] + } + ], "source": [ - "# Write your code here\n" + "corpus = ['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']\n", + "bag_of_words = set()\n", + "\n", + "term_freq =[]\n", + "\n", + "for sentences in corpus:\n", + " terms = sentences.lower().split()\n", + " term_freq_sentence = {}\n", + " \n", + " for term in terms:\n", + " if term in bag_of_words:\n", + " bag_of_word.add(term)\n", + " \n", + " if term in term_freq_sentence:\n", + " term_freq_sentence[term] += 1\n", + " else:\n", + " term_freq_sentence[term] = 1\n", + " \n", + " term_freq.append(term_freq_sentence) \n", + "\n", + "print(term_freq)\n", + " \n", + " \n", + "\n", + " " ] }, { @@ -216,13 +336,6 @@ "```[[1, 1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0], [1, 0, 0, 1, 0, 1, 1, 1, 1]]```" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "metadata": {}, @@ -304,7 +417,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -318,7 +431,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.9.13" } }, "nbformat": 4,