update code on 9/4/2018

This commit is contained in:
Cris Liu 2018-09-05 00:01:03 -04:00
parent 561b3203cc
commit 20a61e1ca3
47 changed files with 312 additions and 214 deletions

View File

@ -93,17 +93,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def fizz_buzz(self, num):\n",
" # TODO: Implement me\n",
" pass"
" if num is None:\n",
" raise TypeError('num cannot be None')\n",
" if num < 1:\n",
" raise ValueError('num cannot be less than one')\n",
" results = []\n",
" for i in range(1, num + 1):\n",
" if i % 3 == 0 and i % 5 == 0:\n",
" results.append('FizzBuzz')\n",
" elif i % 3 == 0:\n",
" results.append('Fizz')\n",
" elif i % 5 == 0:\n",
" results.append('Buzz')\n",
" else:\n",
" results.append(str(i))\n",
" return results\n",
" "
]
},
{
@ -122,11 +134,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_fizz_buzz\n"
]
}
],
"source": [
"# %load test_fizz_buzz.py\n",
"from nose.tools import assert_equal, assert_raises\n",
@ -179,8 +197,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -194,9 +213,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -105,9 +105,7 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"class Solution(object):\n",
@ -140,9 +138,7 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
@ -196,9 +192,7 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
@ -214,8 +208,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -229,9 +224,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -73,9 +73,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"class ReverseString(object):\n",
@ -104,9 +102,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# %load test_reverse_string.py\n",
@ -152,8 +148,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -167,9 +164,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -75,17 +75,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class UniqueChars(object):\n",
"\n",
" def has_unique_chars(self, string):\n",
" # TODO: Implement me\n",
" pass"
" if string is None:\n",
" return False\n",
" charset = set()\n",
" \n",
" for char in string:\n",
" if char in charset:\n",
" return False\n",
" else:\n",
" charset.add(char)\n",
" return True"
]
},
{
@ -104,11 +111,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_unique_chars\n"
]
}
],
"source": [
"# %load test_unique_chars.py\n",
"from nose.tools import assert_equal\n",
@ -154,8 +167,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -169,9 +183,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -88,9 +88,7 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"class UniqueCharsSet(object):\n",
@ -135,9 +133,7 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"class UniqueChars(object):\n",
@ -183,9 +179,7 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"class UniqueCharsInPlace(object):\n",
@ -209,9 +203,7 @@
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
@ -258,9 +250,7 @@
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
@ -278,8 +268,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -293,9 +284,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -79,39 +79,46 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"class Item(object):\n",
"\n",
" def __init__(self, key, value):\n",
" # TODO: Implement me\n",
" pass\n",
" self.key = key\n",
" self.value = value\n",
"\n",
"\n",
"class HashTable(object):\n",
"\n",
" def __init__(self, size):\n",
" # TODO: Implement me\n",
" pass\n",
" self.size = size\n",
" self.table = [[] for _ in range(self.size)]\n",
"\n",
" def _hash_function(self, key):\n",
" # TODO: Implement me\n",
" pass\n",
" return key % self.size\n",
"\n",
" def set(self, key, value):\n",
" # TODO: Implement me\n",
" pass\n",
" self.hash_[key] = value\n",
" \n",
"\n",
" def get(self, key):\n",
" # TODO: Implement me\n",
" pass\n",
" if key in self.hash_:\n",
" return self.hash[key]\n",
" else:\n",
" raise KeyError\n",
"\n",
" def remove(self, key):\n",
" # TODO: Implement me\n",
" pass"
" if key in self.hash_:\n",
" return self.hash[key]\n",
" else:\n",
" raise KeyError"
]
},
{
@ -133,9 +140,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# %load test_hash_map.py\n",
@ -199,8 +204,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -214,9 +220,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -114,9 +114,7 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"class Item(object):\n",
@ -169,9 +167,7 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
@ -235,9 +231,7 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
@ -260,7 +254,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -274,9 +268,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -75,17 +75,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def add_digits(self, val):\n",
" # TODO: Implement me\n",
" pass"
" if (type(val)!=int) or (val is None):\n",
" raise TypeError\n",
" if val < 0:\n",
" raise ValueError\n",
" if val >= 10:\n",
" v = str(val)\n",
" v2 = [int(i) for i in v]\n",
" return self.add_digits(sum(v2))\n",
" else: \n",
" return val"
]
},
{
@ -104,11 +111,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {
"collapsed": false
"scrolled": true
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_add_digits\n"
]
}
],
"source": [
"# %load test_add_digits.py\n",
"from nose.tools import assert_equal, assert_raises\n",
@ -153,8 +168,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -168,9 +184,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -72,17 +72,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"class Math(object):\n",
"\n",
" def check_prime(self, num):\n",
" # TODO: Implement me\n",
" pass"
" if num is None:\n",
" raise TypeError\n",
" if type(num)!=int:\n",
" raise TypeError\n",
" if num < 2:\n",
" return False\n",
" for i in range(2,int(math.sqrt(num))):\n",
" if num%i==0:\n",
" return False\n",
" return True"
]
},
{
@ -101,11 +109,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_check_prime\n"
]
}
],
"source": [
"# %load test_check_prime.py\n",
"from nose.tools import assert_equal, assert_raises\n",
@ -140,11 +154,21 @@
"\n",
"Review the [Solution Notebook]() for a discussion on algorithms and code solutions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -158,9 +182,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -71,17 +71,34 @@
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"import math \n",
"\n",
"class PrimeGenerator(object):\n",
"\n",
" def generate_primes(self, max_num):\n",
" # TODO: Implement me\n",
" pass"
" if max_num is None:\n",
" raise TypeError('max_num cannot be None')\n",
" array = [True] * max_num\n",
" array[0] = False\n",
" array[1] = False\n",
" prime = 2\n",
" while prime <= math.sqrt(max_num):\n",
" self._cross_off(array,prime)\n",
" prime = self._next_prime(array, prime)\n",
" return array\n",
" def _cross_off(self,array,prime):\n",
" for i in range(prime*prime,len(array),prime):\n",
" array[i] = False\n",
" def _next_prime(self,array,prime):\n",
" next = prime + 1\n",
" while next < len(array) & (not array[next]):\n",
" next+=1\n",
" return next\n",
" "
]
},
{
@ -100,11 +117,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: generate_primes\n"
]
}
],
"source": [
"# %load test_generate_primes.py\n",
"from nose.tools import assert_equal, assert_raises\n",
@ -146,8 +169,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -161,9 +185,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -88,9 +88,7 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"import math\n",
@ -134,9 +132,7 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
@ -179,9 +175,7 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
@ -198,7 +192,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -212,9 +206,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -81,27 +81,46 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def __init__(self, upper_limit=100):\n",
" # TODO: Implement me\n",
" pass\n",
" self.max = None\n",
" self.min = None\n",
" # Mean\n",
" self.num_items = 0\n",
" self.running_sum = 0\n",
" self.mean = None\n",
" self.array = [0] * (upper_limit + 1)\n",
" self.mode_occurrences = 0\n",
" self.mode = None\n",
"\n",
" def insert(self, val):\n",
" # TODO: Implement me\n",
" pass"
" if val is None:\n",
" raise TypeError('val cannot be None')\n",
" if self.max is None or val > self.max:\n",
" self.max = val\n",
" if self.min is None or val < self.min:\n",
" self.min = val\n",
" self.num_items += 1\n",
" self.running_sum += val\n",
" self.mean = self.running_sum / self.num_items\n",
" self.array[val] += 1\n",
" if self.array[val] > self.mode_occurrences:\n",
" self.mode_occurrences = self.array[val]\n",
" self.mode = val"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Unit Test"
]
},
@ -114,11 +133,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_math_ops\n"
]
}
],
"source": [
"# %load test_math_ops.py\n",
"from nose.tools import assert_equal, assert_true, assert_raises\n",
@ -167,8 +192,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -182,9 +208,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -93,9 +93,7 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"from __future__ import division\n",
@ -143,9 +141,7 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
@ -195,9 +191,7 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
@ -214,7 +208,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -228,9 +222,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -76,17 +76,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def is_power_of_two(self, val):\n",
" # TODO: Implement me\n",
" pass"
" if val is None:\n",
" raise TypeError\n",
" if val == 0:\n",
" return False\n",
" elif val == 1:\n",
" return True\n",
" elif val%2 == 1:\n",
" return False\n",
" else:\n",
" return self.is_power_of_two(val/2)"
]
},
{
@ -105,11 +112,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_is_power_of_two\n"
]
}
],
"source": [
"# %load test_is_power_of_two.py\n",
"from nose.tools import assert_equal, assert_raises\n",
@ -148,8 +161,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -163,9 +177,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -98,9 +98,7 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"class Solution(object):\n",
@ -123,9 +121,7 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
@ -165,9 +161,7 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
@ -183,8 +177,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -198,9 +193,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -68,15 +68,15 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def sum_two(self, val):\n",
" def sum_two(self, a,b):\n",
" # TODO: Implement me\n",
" result = a ^ b\n",
" result \n",
" pass"
]
},
@ -97,9 +97,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# %load test_sum_two.py\n",
@ -137,8 +135,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -152,9 +151,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -112,9 +112,7 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"class Solution(object):\n",
@ -139,9 +137,7 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
@ -180,7 +176,6 @@
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
@ -198,8 +193,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -213,9 +209,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}