Skip to content

Add challenge Array_strings/non_recurring_char #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions arrays_strings/non_repeated_char/non_repeated_char_challenge.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
185 changes: 185 additions & 0 deletions arrays_strings/non_repeated_char/non_repeated_char_solution.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 18 additions & 0 deletions arrays_strings/non_repeated_char/test_non_repeated_char.py
Original file line number Diff line number Diff line change
@@ -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()