diff --git a/02-Array Sequences/Array Sequences Interview Questions/01-Anagram-Check/.ipynb_checkpoints/01-Anagram Check -checkpoint.ipynb b/02-Array Sequences/Array Sequences Interview Questions/01-Anagram-Check/.ipynb_checkpoints/01-Anagram Check -checkpoint.ipynb new file mode 100644 index 0000000..c44cd3a --- /dev/null +++ b/02-Array Sequences/Array Sequences Interview Questions/01-Anagram-Check/.ipynb_checkpoints/01-Anagram Check -checkpoint.ipynb @@ -0,0 +1,132 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Anagram Check\n", + "\n", + "## Problem\n", + "\n", + "Given two strings, check to see if they are anagrams. An anagram is when the two strings can be written using the exact same letters (so you can just rearrange the letters to get a different phrase or word). \n", + "\n", + "For example:\n", + "\n", + " \"public relations\" is an anagram of \"crap built on lies.\"\n", + " \n", + " \"clint eastwood\" is an anagram of \"old west action\"\n", + " \n", + "**Note: Ignore spaces and capitalization. So \"d go\" is an anagram of \"God\" and \"dog\" and \"o d g\".**\n", + "\n", + "## Solution\n", + "\n", + "Fill out your solution below:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "anagram('dog','god')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "anagram('clint eastwood','old west action')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "anagram('aa','bb')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Test Your Solution\n", + "Run the cell below to test your solution" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\"\n", + "RUN THIS CELL TO TEST YOUR SOLUTION\n", + "\"\"\"\n", + "from nose.tools import assert_equal\n", + "\n", + "class AnagramTest(object):\n", + " \n", + " def test(self,sol):\n", + " assert_equal(sol('go go go','gggooo'),True)\n", + " assert_equal(sol('abc','cba'),True)\n", + " assert_equal(sol('hi man','hi man'),True)\n", + " assert_equal(sol('aabbcc','aabbc'),False)\n", + " assert_equal(sol('123','1 2'),False)\n", + " print(\"ALL TEST CASES PASSED\")\n", + "\n", + "# Run Tests\n", + "t = AnagramTest()\n", + "t.test(anagram)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "t.test(anagram2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Good Job!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.12" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/02-Array Sequences/Array Sequences Interview Questions/01-Anagram-Check/01-Anagram Check .ipynb b/02-Array Sequences/Array Sequences Interview Questions/01-Anagram-Check/01-Anagram Check .ipynb index e27ff92..8bd69a5 100644 --- a/02-Array Sequences/Array Sequences Interview Questions/01-Anagram-Check/01-Anagram Check .ipynb +++ b/02-Array Sequences/Array Sequences Interview Questions/01-Anagram-Check/01-Anagram Check .ipynb @@ -25,38 +25,95 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def anagram(s1,s2):\n", " \n", - " pass" + " s1 = s1.replace(' ','').lower()\n", + " s2 = s2.replace(' ','').lower()\n", + " \n", + " \n", + " # Edge Case Check\n", + " if len(s1) != len(s2):\n", + " return False\n", + " \n", + " count = {}\n", + " for letter in s1:\n", + " if letter in count:\n", + " count[letter] += 1\n", + " else:\n", + " count[letter] = 1\n", + " \n", + " for letter in s2:\n", + " if letter in count:\n", + " count[letter] -= 1\n", + " else:\n", + " count[letter] = 1\n", + " \n", + " for k in count:\n", + " if count[k] != 0:\n", + " return False\n", + " return True" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "anagram('dog','god')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "anagram('clint eastwood','old west action')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "anagram('aa','bb')" ] @@ -114,7 +171,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -128,7 +185,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.10" + "version": "3.9.12" } }, "nbformat": 4, diff --git a/02-Array Sequences/Array Sequences Interview Questions/02-Array-Pair-Sum/.ipynb_checkpoints/02-Array Pair Sum -checkpoint.ipynb b/02-Array Sequences/Array Sequences Interview Questions/02-Array-Pair-Sum/.ipynb_checkpoints/02-Array Pair Sum -checkpoint.ipynb new file mode 100644 index 0000000..3cdf007 --- /dev/null +++ b/02-Array Sequences/Array Sequences Interview Questions/02-Array-Pair-Sum/.ipynb_checkpoints/02-Array Pair Sum -checkpoint.ipynb @@ -0,0 +1,131 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Array Pair Sum\n", + "\n", + "## Problem\n", + "\n", + "Given an integer array, output all the ** *unique* ** pairs that sum up to a specific value **k**.\n", + "\n", + "So the input:\n", + " \n", + " pair_sum([1,3,2,2],4)\n", + "\n", + "would return **2** pairs:\n", + "\n", + " (1,3)\n", + " (2,2)\n", + "\n", + "**NOTE: FOR TESTING PURPOSES CHANGE YOUR FUNCTION SO IT OUTPUTS THE NUMBER OF PAIRS**\n", + "\n", + "## Solution\n", + "\n", + "Fill out your solution below:" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def pair_sum(arr,k):\n", + " \n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pair_sum([1,3,2,2],4)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Test Your Solution" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ALL TEST CASES PASSED\n" + ] + } + ], + "source": [ + "\"\"\"\n", + "RUN THIS CELL TO TEST YOUR SOLUTION\n", + "\"\"\"\n", + "from nose.tools import assert_equal\n", + "\n", + "class TestPair(object):\n", + " \n", + " def test(self,sol):\n", + " assert_equal(sol([1,9,2,8,3,7,4,6,5,5,13,14,11,13,-1],10),6)\n", + " assert_equal(sol([1,2,3,1],3),1)\n", + " assert_equal(sol([1,3,2,2],4),2)\n", + " print('ALL TEST CASES PASSED')\n", + " \n", + "#Run tests\n", + "t = TestPair()\n", + "t.test(pair_sum)\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Good Job!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.12" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/02-Array Sequences/Array Sequences Interview Questions/02-Array-Pair-Sum/02-Array Pair Sum .ipynb b/02-Array Sequences/Array Sequences Interview Questions/02-Array-Pair-Sum/02-Array Pair Sum .ipynb index 69bb05f..ac84d2f 100644 --- a/02-Array Sequences/Array Sequences Interview Questions/02-Array-Pair-Sum/02-Array Pair Sum .ipynb +++ b/02-Array Sequences/Array Sequences Interview Questions/02-Array-Pair-Sum/02-Array Pair Sum .ipynb @@ -28,20 +28,31 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, + "execution_count": 5, + "metadata": {}, "outputs": [], "source": [ - "def pair_sum(arr,k):\n", + "def pair_sum(arr, k):\n", + " if len(arr)<2:\n", + " return\n", " \n", - " pass" + " #Sets for tracking\n", + " seen = set()\n", + " output = set()\n", + " \n", + " for num in arr:\n", + " target = k - num\n", + " \n", + " if target not in seen:\n", + " seen.add(num)\n", + " else:\n", + " output.add( ((min(num,target)), max(num,target)))\n", + " return len(output)" ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -50,7 +61,7 @@ "2" ] }, - "execution_count": 14, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -109,7 +120,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -123,7 +134,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.10" + "version": "3.9.12" } }, "nbformat": 4,