Skip to content

Commit 69d6ed5

Browse files
committed
Updated
1 parent 3d3e79f commit 69d6ed5

File tree

8 files changed

+1196
-0
lines changed

8 files changed

+1196
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

Python_Basics/.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 339 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,339 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"<h1 align=\"center\"> Anagrams using Python </h1>"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"## What is an Anagram?"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once."
22+
]
23+
},
24+
{
25+
"cell_type": "markdown",
26+
"metadata": {},
27+
"source": [
28+
"Task: Write a program that takes in a word list and outputs a list of all the words that are anagrams of another word in the list."
29+
]
30+
},
31+
{
32+
"cell_type": "markdown",
33+
"metadata": {},
34+
"source": [
35+
"While there are many different ways to solve this problem, this notebook gives a couple different approaches to solve this problem. \n",
36+
"For each of the approaches below, we first define a word list"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": 1,
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"word_list = ['percussion',\n",
46+
" 'supersonic',\n",
47+
" 'car',\n",
48+
" 'tree',\n",
49+
" 'boy',\n",
50+
" 'girl',\n",
51+
" 'arc']"
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"metadata": {},
57+
"source": [
58+
"## Approach 1: For Loop"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 2,
64+
"metadata": {},
65+
"outputs": [],
66+
"source": [
67+
"temp = word_list[0]"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": 3,
73+
"metadata": {},
74+
"outputs": [
75+
{
76+
"data": {
77+
"text/plain": [
78+
"['c', 'e', 'i', 'n', 'o', 'p', 'r', 's', 's', 'u']"
79+
]
80+
},
81+
"execution_count": 3,
82+
"metadata": {},
83+
"output_type": "execute_result"
84+
}
85+
],
86+
"source": [
87+
"sorted(temp)"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": 4,
93+
"metadata": {},
94+
"outputs": [
95+
{
96+
"data": {
97+
"text/plain": [
98+
"['percussion', 'supersonic', 'car', 'tree', 'boy', 'girl', 'arc']"
99+
]
100+
},
101+
"execution_count": 4,
102+
"metadata": {},
103+
"output_type": "execute_result"
104+
}
105+
],
106+
"source": [
107+
"word_list"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": 5,
113+
"metadata": {},
114+
"outputs": [],
115+
"source": [
116+
"# initialize a list\n",
117+
"anagram_list = []\n",
118+
"\n",
119+
"for word_1 in word_list: \n",
120+
" for word_2 in word_list: \n",
121+
" if word_1 != word_2 and (sorted(word_1)==sorted(word_2)):\n",
122+
" anagram_list.append(word_1)"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": 6,
128+
"metadata": {},
129+
"outputs": [
130+
{
131+
"name": "stdout",
132+
"output_type": "stream",
133+
"text": [
134+
"['percussion', 'supersonic', 'car', 'arc']\n"
135+
]
136+
}
137+
],
138+
"source": [
139+
"print(anagram_list)"
140+
]
141+
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": 7,
145+
"metadata": {},
146+
"outputs": [
147+
{
148+
"name": "stdout",
149+
"output_type": "stream",
150+
"text": [
151+
"['c', 'e', 'i', 'n', 'o', 'p', 'r', 's', 's', 'u']\n"
152+
]
153+
}
154+
],
155+
"source": [
156+
"print(sorted('percussion'))"
157+
]
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": 8,
162+
"metadata": {},
163+
"outputs": [
164+
{
165+
"name": "stdout",
166+
"output_type": "stream",
167+
"text": [
168+
"['c', 'e', 'i', 'n', 'o', 'p', 'r', 's', 's', 'u']\n"
169+
]
170+
}
171+
],
172+
"source": [
173+
"print(sorted('supersonic'))"
174+
]
175+
},
176+
{
177+
"cell_type": "markdown",
178+
"metadata": {},
179+
"source": [
180+
"## Approach 2: Dictionaries"
181+
]
182+
},
183+
{
184+
"cell_type": "markdown",
185+
"metadata": {},
186+
"source": [
187+
"Sorting of lists in Python is O(nlogn) versus O(n) with a dictionary. If you have difficulty understanding the dictionary get method, I encourage you to see one of the following tutorials: [Python Dictionary and Dictionary Methods](https://hackernoon.com/python-basics-10-dictionaries-and-dictionary-methods-4e9efa70f5b9) or [Python Word Count](https://codeburst.io/python-basics-11-word-count-filter-out-punctuation-dictionary-manipulation-and-sorting-lists-3f6c55420855). "
188+
]
189+
},
190+
{
191+
"cell_type": "code",
192+
"execution_count": 9,
193+
"metadata": {},
194+
"outputs": [],
195+
"source": [
196+
"def freq(word):\n",
197+
" freq_dict = {}\n",
198+
" for char in word:\n",
199+
" freq_dict[char] = freq_dict.get(char, 0) + 1\n",
200+
" return freq_dict "
201+
]
202+
},
203+
{
204+
"cell_type": "code",
205+
"execution_count": 10,
206+
"metadata": {},
207+
"outputs": [
208+
{
209+
"name": "stdout",
210+
"output_type": "stream",
211+
"text": [
212+
"['percussion', 'supersonic', 'car', 'arc']\n"
213+
]
214+
}
215+
],
216+
"source": [
217+
"# initialize a list\n",
218+
"anagram_list = []\n",
219+
"for word_1 in word_list: \n",
220+
" for word_2 in word_list: \n",
221+
" if word_1 != word_2 and (freq(word_1) == freq(word_2)):\n",
222+
" anagram_list.append(word_1)\n",
223+
"print(anagram_list)"
224+
]
225+
},
226+
{
227+
"cell_type": "code",
228+
"execution_count": 11,
229+
"metadata": {},
230+
"outputs": [
231+
{
232+
"name": "stdout",
233+
"output_type": "stream",
234+
"text": [
235+
"{'p': 1, 'e': 1, 'r': 1, 'c': 1, 'u': 1, 's': 2, 'i': 1, 'o': 1, 'n': 1}\n"
236+
]
237+
}
238+
],
239+
"source": [
240+
"print(freq('percussion'))"
241+
]
242+
},
243+
{
244+
"cell_type": "code",
245+
"execution_count": 12,
246+
"metadata": {},
247+
"outputs": [
248+
{
249+
"name": "stdout",
250+
"output_type": "stream",
251+
"text": [
252+
"{'s': 2, 'u': 1, 'p': 1, 'e': 1, 'r': 1, 'o': 1, 'n': 1, 'i': 1, 'c': 1}\n"
253+
]
254+
}
255+
],
256+
"source": [
257+
"print(freq('supersonic'))"
258+
]
259+
},
260+
{
261+
"cell_type": "code",
262+
"execution_count": 13,
263+
"metadata": {},
264+
"outputs": [
265+
{
266+
"data": {
267+
"text/plain": [
268+
"[('c', 1),\n",
269+
" ('e', 1),\n",
270+
" ('i', 1),\n",
271+
" ('n', 1),\n",
272+
" ('o', 1),\n",
273+
" ('p', 1),\n",
274+
" ('r', 1),\n",
275+
" ('s', 2),\n",
276+
" ('u', 1)]"
277+
]
278+
},
279+
"execution_count": 13,
280+
"metadata": {},
281+
"output_type": "execute_result"
282+
}
283+
],
284+
"source": [
285+
"sorted(list(freq('percussion').items()))"
286+
]
287+
},
288+
{
289+
"cell_type": "code",
290+
"execution_count": 14,
291+
"metadata": {},
292+
"outputs": [
293+
{
294+
"data": {
295+
"text/plain": [
296+
"[('c', 1),\n",
297+
" ('e', 1),\n",
298+
" ('i', 1),\n",
299+
" ('n', 1),\n",
300+
" ('o', 1),\n",
301+
" ('p', 1),\n",
302+
" ('r', 1),\n",
303+
" ('s', 2),\n",
304+
" ('u', 1)]"
305+
]
306+
},
307+
"execution_count": 14,
308+
"metadata": {},
309+
"output_type": "execute_result"
310+
}
311+
],
312+
"source": [
313+
"sorted(list(freq('supersonic').items()))"
314+
]
315+
}
316+
],
317+
"metadata": {
318+
"anaconda-cloud": {},
319+
"kernelspec": {
320+
"display_name": "Python 3",
321+
"language": "python",
322+
"name": "python3"
323+
},
324+
"language_info": {
325+
"codemirror_mode": {
326+
"name": "ipython",
327+
"version": 3
328+
},
329+
"file_extension": ".py",
330+
"mimetype": "text/x-python",
331+
"name": "python",
332+
"nbconvert_exporter": "python",
333+
"pygments_lexer": "ipython3",
334+
"version": "3.6.8"
335+
}
336+
},
337+
"nbformat": 4,
338+
"nbformat_minor": 2
339+
}

0 commit comments

Comments
 (0)