{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Dictionary practice problems\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 1\n", "Given a paragraph, write a function which returns the word count of each element.\n", "\n", "- Input: \"Hello I am Nick Hello\"\n", "- Output: \"Hello\":2, \"I\":1, \"am\":1, Nick:\"1\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Commodo': 1, 'exercitation': 1, 'mollit': 3, 'culpa': 1, 'aute': 2, 'nostrud': 1, 'dolore.': 1, '\\\\': 1, 'Ullamco': 1, 'et': 1, 'sit': 2, 'eiusmod': 1, 'anim': 1, 'cillum': 2, 'ipsum': 2, 'amet': 1, 'ex': 1, 'occaecat': 1, 'irure.': 1, 'Occaecat': 1, 'eu': 1, 'aliqua': 1, 'enim': 1, 'sunt.': 1, 'Consequat': 1, 'magna': 1, 'elit': 1, 'irure': 1, 'et.': 1}\n" ] } ], "source": [ "def wordCount(text):\n", " counts = {}\n", " for word in text.split():\n", " if word not in counts:\n", " counts[word] = 0\n", " counts[word] += 1\n", " return counts\n", "\n", "paragraph = \"\"\"Commodo exercitation mollit mollit culpa aute nostrud dolore. \\ \n", " Ullamco et sit eiusmod anim cillum ipsum amet mollit ex occaecat aute irure. \\\n", " Occaecat eu aliqua cillum sit enim sunt. Consequat magna elit ipsum irure et.\"\"\"\n", "\n", "count = wordCount(paragraph)\n", "print(count) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "interpreter": { "hash": "40d3a090f54c6569ab1632332b64b2c03c39dcf918b08424e98f38b5ae0af88f" }, "kernelspec": { "display_name": "Python 3.8.3 ('base')", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.3" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }