From f62ce3707a5b9199830c812cb92d9a3ea353d11e Mon Sep 17 00:00:00 2001 From: Sanjay Muthu Date: Tue, 21 Jan 2025 15:43:37 +0530 Subject: [PATCH 1/3] Create non_repeated_char_challenge.ipynb --- .../non_repeated_char_challenge.ipynb | 160 ++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 arrays_strings/non_repeated_char/non_repeated_char_challenge.ipynb diff --git a/arrays_strings/non_repeated_char/non_repeated_char_challenge.ipynb b/arrays_strings/non_repeated_char/non_repeated_char_challenge.ipynb new file mode 100644 index 00000000..1a078a7c --- /dev/null +++ b/arrays_strings/non_repeated_char/non_repeated_char_challenge.ipynb @@ -0,0 +1,160 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge Notebook\n", + "\n", + "\n", + "## Problem: Implement an algorithm to find the first non-repeated character in a string\n", + "\n", + "* [Constraints](#Constraints)\n", + "* [Test Cases](#Test-Cases)\n", + "* [Algorithm](#Algorithm)\n", + "* [Code](#Code)\n", + "* [Unit Test](#Unit-Test)\n", + "* [Solution Notebook](#Solution-Notebook)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Constraints\n", + "\n", + "* Can we assume the string is ASCII\n", + " * Yes\n", + "\n", + "* Can we assume this is case sensitive\n", + " * Yes\n", + "\n", + "* Can we use additional data structures?\n", + " * Yes\n", + "\n", + "* Can we assume this fits in memory?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "* Input:- None\n", + "\n", + " Output:- \"\"\n", + "\n", + "\n", + "* Input:- \"a\"\n", + "\n", + " Output:- \"a\"\n", + "\n", + "\n", + "* Input:- \"absfasxv\"\n", + "\n", + " Output:- \"b\"\n", + "\n", + "\n", + "* Input:- \"\"\n", + "\n", + " Output:- \"\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "Refer to the [Solution Notebook](https://github.com). If you are stuck and need a hint, the solution notebook's algorithm discussion discussion might be a good place to start." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "class NonRepeatedChar(object):\n", + " def non_repeated_char(self, string):\n", + " # TODO: Implement me\n", + " pass" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test\n", + "\n", + "**The following unit test is expected to fail until you solve the challenge.**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# %Load test_non_repeated_char.py\n", + "import unittest\n", + "\n", + "class TestNonRepeatedChar(unittest.TestCase):\n", + " def test_non_repeated_char(self, func):\n", + " self.assertEqual(func(None), '')\n", + " self.assertEqual(func('a'), 'a')\n", + " self.assertEqual(func('absfasxv'), 'b')\n", + " self.assertEqual(func(''), '')\n", + " print('Success: test_non_repeated_char')\n", + "\n", + "def main():\n", + " test = TestNonRepeatedChar()\n", + " non_repeated_char = NonRepeatedChar()\n", + " test.test_non_repeated_char(non_repeated_char.non_repeated_char)\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Solution Notebook\n", + "\n", + "Review the [Solution Notebook](https://github.com) for a discussion on algorithms and code solutions." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.12.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From b2184df50505a133d3bc761fd729529d85f9132c Mon Sep 17 00:00:00 2001 From: Sanjay Muthu Date: Tue, 21 Jan 2025 15:44:29 +0530 Subject: [PATCH 2/3] Create non_repeated_char_solution.ipynb --- .../non_repeated_char_solution.ipynb | 185 ++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 arrays_strings/non_repeated_char/non_repeated_char_solution.ipynb diff --git a/arrays_strings/non_repeated_char/non_repeated_char_solution.ipynb b/arrays_strings/non_repeated_char/non_repeated_char_solution.ipynb new file mode 100644 index 00000000..32e501fb --- /dev/null +++ b/arrays_strings/non_repeated_char/non_repeated_char_solution.ipynb @@ -0,0 +1,185 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Solution Notebook\n", + "\n", + "\n", + "## Problem: Implement an algorithm to find the first non-repeated character in a string\n", + "\n", + "* [Constraints](#Constraints)\n", + "* [Test Cases](#Test-Cases)\n", + "* [Algorithm 1: Frequency Map](#Algorithm-1:-Frequency-Map)\n", + "* [Code: Frequency Map](#Code:-Frequency-Map)\n", + "* [Unit Test](#Unit-Test)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Constraints\n", + "\n", + "* Can we assume the string is ASCII\n", + " * Yes\n", + "\n", + "* Can we assume this is case sensitive\n", + " * Yes\n", + "\n", + "* Can we use additional data structures?\n", + " * Yes\n", + "\n", + "* Can we assume this fits in memory?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "* Input:- None\n", + "\n", + " Output:- \"\"\n", + "\n", + "\n", + "* Input:- \"a\"\n", + "\n", + " Output:- \"a\"\n", + "\n", + "\n", + "* Input:- \"absfasxv\"\n", + "\n", + " Output:- \"b\"\n", + "\n", + "\n", + "* Input:- \"\"\n", + "\n", + " Output:- \"\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Algorithm 1: Frequency Map\n", + "\n", + "A frequency map is a map which is used to denote the frequency of each element (character) in the array (string).\n", + "\n", + "We can use the frequency map to find out which characters occur only once in the string.\n", + "\n", + "Then we can check which of the characters occur sooner and output that as out answer.\n", + "\n", + "Complexity:\n", + "* Time: O(N log N)\n", + "* Space: O(N)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Code: Frequency Map" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "class NonRepeatedChar(object):\n", + " def non_repeated_char(self, string):\n", + " if string is None or string == '':\n", + " return ''\n", + " \n", + " # Make the frequency map\n", + " frequency_map = {'a' : 0, 'b' : 0, 'c' : 0, 'd' : 0, 'e' : 0, 'f' : 0, 'g' : 0, 'h' : 0, 'i' : 0, 'j' : 0, 'k' : 0, 'l' : 0, 'm' : 0,\n", + " 'n' : 0, 'o' : 0, 'p' : 0, 'q' : 0, 'r' : 0, 's' : 0, 't' : 0, 'u' : 0, 'v' : 0, 'w' : 0, 'x' : 0, 'y' : 0, 'z' : 0}\n", + "\n", + " for char in string:\n", + " frequency_map[char] += 1\n", + " \n", + " # Make a list of all characters with frequency of 1\n", + " chars = []\n", + " for char, frequency in frequency_map.items():\n", + " if frequency == 1:\n", + " chars.append(char)\n", + " \n", + " # Check which of the chars occur first and return it\n", + " for char in string:\n", + " if char in chars:\n", + " return char\n", + " \n", + " # If none of the characters are non repeated then just return an empty string\n", + " return ''" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Unit Test" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting test_non_repeated_char.py\n" + ] + } + ], + "source": [ + "#%%writefile test_non_repeated_char.py\n", + "import unittest\n", + "\n", + "class TestNonRepeatedChar(unittest.TestCase):\n", + " def test_non_repeated_char(self, func):\n", + " self.assertEqual(func(None), '')\n", + " self.assertEqual(func('a'), 'a')\n", + " self.assertEqual(func('absfasxv'), 'b')\n", + " self.assertEqual(func(''), '')\n", + " print('Success: test_non_repeated_char')\n", + "\n", + "def main():\n", + " test = TestNonRepeatedChar()\n", + " non_repeated_char = NonRepeatedChar()\n", + " test.test_non_repeated_char(non_repeated_char.non_repeated_char)\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.12.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 7a54ffccf3a55ee7044158e69a53c74ef3709bbf Mon Sep 17 00:00:00 2001 From: Sanjay Muthu Date: Tue, 21 Jan 2025 15:44:49 +0530 Subject: [PATCH 3/3] Create test_non_repeated_char.py --- .../test_non_repeated_char.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 arrays_strings/non_repeated_char/test_non_repeated_char.py diff --git a/arrays_strings/non_repeated_char/test_non_repeated_char.py b/arrays_strings/non_repeated_char/test_non_repeated_char.py new file mode 100644 index 00000000..011d763a --- /dev/null +++ b/arrays_strings/non_repeated_char/test_non_repeated_char.py @@ -0,0 +1,18 @@ +import unittest + +class TestNonRepeatedChar(unittest.TestCase): + def test_non_repeated_char(self, func): + self.assertEqual(func(None), '') + self.assertEqual(func('a'), 'a') + self.assertEqual(func('absfasxv'), 'b') + self.assertEqual(func(''), '') + print('Success: test_non_repeated_char') + +def main(): + test = TestNonRepeatedChar() + non_repeated_char = NonRepeatedChar() + test.test_non_repeated_char(non_repeated_char.non_repeated_char) + + +if __name__ == '__main__': + main()