From 593824f4df51e259c91e989eba4a9ea21d62641c Mon Sep 17 00:00:00 2001 From: Sangeeth Sudheer Date: Thu, 7 Apr 2022 02:19:35 +0530 Subject: [PATCH] feat: add counter ipynb --- notebooks/collections/counter.ipynb | 287 ++++++++++++++++++++++++++++ 1 file changed, 287 insertions(+) create mode 100644 notebooks/collections/counter.ipynb diff --git a/notebooks/collections/counter.ipynb b/notebooks/collections/counter.ipynb new file mode 100644 index 0000000..b1060de --- /dev/null +++ b/notebooks/collections/counter.ipynb @@ -0,0 +1,287 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Counter\n", + "\n", + "Keeps track of number of times an item is added." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from collections import Counter" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Counter({'b': 3, 'a': 2, 'c': 1})\n", + "Counter({'c': 3, 'b': 2, 'a': 1})\n", + "Counter({'b': 3, 'a': 2, 'c': 1})\n" + ] + } + ], + "source": [ + "print(Counter(['a', 'a', 'b', 'b', 'b', 'c']))\n", + "\n", + "# Notice that in the following cases of dict and kwargs, a new Counter\n", + "# is created with the given number and no \"counting\" happens\n", + "print(Counter({ 'a': 1, 'b': 2, 'c': 3}))\n", + "print(Counter(a=2, b=3, c=1))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Empty counter can be created and later items added." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Counter({'a': 1})\n", + "Counter({'a': 2})\n", + "Counter({'b': 4, 'a': 2, 'c': 2})\n" + ] + } + ], + "source": [ + "empty = Counter()\n", + "\n", + "empty.update('a')\n", + "print(empty)\n", + "\n", + "empty.update('a')\n", + "print(empty)\n", + "\n", + "empty.update('bbbbcc')\n", + "print(empty)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Counts of an item can be retrieved by simply indexing." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of 'b': 4\n" + ] + } + ], + "source": [ + "print(\"Number of 'b':\", empty['b'])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "❗️ Counter returns count of `0` for non-existent keys." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of 'z': 0\n" + ] + } + ], + "source": [ + "print(\"Number of 'z':\", empty['z'])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### ⭐️ `most_common(n)`\n", + "\n", + "Returns `n` most common items as a sequence of pairs. If `n` is not provided, returns all in decreasing order." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Top 3 letters in the dictionary:\n", + "e : 235331\n", + "i : 201032\n", + "a : 199554\n" + ] + } + ], + "source": [ + "c = Counter()\n", + "\n", + "with open('/usr/share/dict/words', 'r') as f:\n", + " for line in f:\n", + " c.update(line.rstrip().lower())\n", + "\n", + "print(\"Top 3 letters in the dictionary:\")\n", + "for item, count in c.most_common(3):\n", + " print(f\"{item} : {count}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Set and arithmetic operations" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "c1 + c2 = Counter({'a': 5, 'b': 5, 'e': 5, 'c': 3, 'd': 1})\n", + "c1 - c2 = Counter({'e': 5, 'a': 3})\n", + "c1 | c2 = Counter({'e': 5, 'a': 4, 'b': 3, 'c': 2, 'd': 1})\n", + "c1 & c2 = Counter({'b': 2, 'a': 1, 'c': 1})\n" + ] + } + ], + "source": [ + "c1 = Counter(a=4, b=2, c=1, d=0, e=5)\n", + "c2 = Counter(a=1, b=3, c=2, d=1)\n", + "\n", + "print(\"c1 + c2 =\", c1 + c2)\n", + "print(\"c1 - c2 =\", c1 - c2) # NOTE: 0 counts and -ve counts are removed\n", + "\n", + "print(\"c1 | c2 =\", c1 | c2) # NOTE: Largest counts are preserved\n", + "print(\"c1 & c2 =\", c1 & c2) # NOTE: Smallest counts are preserved" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Some extra APIs" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a : 2\n", + "a : 2\n", + "b : 4\n", + "b : 4\n", + "b : 4\n", + "b : 4\n", + "c : 3\n", + "c : 3\n", + "c : 3\n", + "d : 1\n" + ] + } + ], + "source": [ + "c = Counter('aabbbbcccd')\n", + "\n", + "# Get an iterator to loop through\n", + "# NOTE: Does not return count == 0 items\n", + "for el in c.elements():\n", + " print(f\"{el} : {c[el]}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total items = 5\n" + ] + } + ], + "source": [ + "print(\"Total items =\", Counter('aaabb').total())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "interpreter": { + "hash": "9e0fd7424d8281b91a098a189d6ae37333be9d5a00ec75c9d3dec88c73652714" + }, + "kernelspec": { + "display_name": "Python 3.10.1 ('leetcode-yPz04C5d')", + "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.10.1" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +}