Skip to content
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

arrays #29

Open
wants to merge 2 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
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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')"
]
Expand Down Expand Up @@ -114,7 +171,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -128,7 +185,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.9.12"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Loading