|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "1421b4aa-68b9-4d3c-9760-e7728aad28c8", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "### \n", |
| 9 | + "🧠 Problem: Character Frequency Counter\n", |
| 10 | + "You are given a paragraph of text stored as a string. Your task is to:\n", |
| 11 | + "\n", |
| 12 | + "Ignore case sensitivity (treat A and a as same).\n", |
| 13 | + "\n", |
| 14 | + "Count how many times each character (a–z) appears in the text (ignore spaces, punctuation, numbers).\n", |
| 15 | + "\n", |
| 16 | + "Print the characters and their counts in alphabetical order." |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": 60, |
| 22 | + "id": "7df9a0c5-3cae-4b83-b8c3-11f16d4bc2ad", |
| 23 | + "metadata": {}, |
| 24 | + "outputs": [ |
| 25 | + { |
| 26 | + "name": "stdout", |
| 27 | + "output_type": "stream", |
| 28 | + "text": [ |
| 29 | + "A:5\n", |
| 30 | + "C:2\n", |
| 31 | + "D:2\n", |
| 32 | + "E:9\n", |
| 33 | + "F:4\n", |
| 34 | + "G:1\n", |
| 35 | + "I:7\n", |
| 36 | + "K:1\n", |
| 37 | + "L:3\n", |
| 38 | + "M:1\n", |
| 39 | + "N:6\n", |
| 40 | + "O:3\n", |
| 41 | + "P:2\n", |
| 42 | + "R:2\n", |
| 43 | + "S:3\n", |
| 44 | + "T:2\n", |
| 45 | + "U:2\n", |
| 46 | + "Y:1\n" |
| 47 | + ] |
| 48 | + } |
| 49 | + ], |
| 50 | + "source": [ |
| 51 | + "text = \"Anime is life. Naruto and Luffy are legends! One Piece is peak fiction.\"\n", |
| 52 | + "# text = 'kiraa'\n", |
| 53 | + "\n", |
| 54 | + "import re\n", |
| 55 | + "\n", |
| 56 | + "pattern = re.findall(r'[a-z]' , text.lower())\n", |
| 57 | + "emp_dict = {}\n", |
| 58 | + "\n", |
| 59 | + "for char in pattern:\n", |
| 60 | + " if char not in emp_dict:\n", |
| 61 | + " emp_dict[char] = 1\n", |
| 62 | + " else:\n", |
| 63 | + " emp_dict[char] += 1\n", |
| 64 | + "sorted_dict = dict(sorted(emp_dict.items()))\n", |
| 65 | + "for chars, val in sorted_dict.items():\n", |
| 66 | + " print(f\"{chars.upper()}:{val}\")\n" |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": null, |
| 72 | + "id": "6edf4299-c35a-4d45-ab03-2c38331a4202", |
| 73 | + "metadata": {}, |
| 74 | + "outputs": [], |
| 75 | + "source": [] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": null, |
| 80 | + "id": "7cb9bb5d-f2c3-4057-8928-8acd56ea1c3d", |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [], |
| 83 | + "source": [] |
| 84 | + } |
| 85 | + ], |
| 86 | + "metadata": { |
| 87 | + "kernelspec": { |
| 88 | + "display_name": "Python 3 (ipykernel)", |
| 89 | + "language": "python", |
| 90 | + "name": "python3" |
| 91 | + }, |
| 92 | + "language_info": { |
| 93 | + "codemirror_mode": { |
| 94 | + "name": "ipython", |
| 95 | + "version": 3 |
| 96 | + }, |
| 97 | + "file_extension": ".py", |
| 98 | + "mimetype": "text/x-python", |
| 99 | + "name": "python", |
| 100 | + "nbconvert_exporter": "python", |
| 101 | + "pygments_lexer": "ipython3", |
| 102 | + "version": "3.12.7" |
| 103 | + } |
| 104 | + }, |
| 105 | + "nbformat": 4, |
| 106 | + "nbformat_minor": 5 |
| 107 | +} |
0 commit comments