Skip to content

Commit f237636

Browse files
author
unknown
committed
lect 18 and 19 added
1 parent dec8ef7 commit f237636

File tree

2 files changed

+291
-0
lines changed

2 files changed

+291
-0
lines changed

Lect18.ipynb

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"data": {
10+
"text/plain": [
11+
"4"
12+
]
13+
},
14+
"execution_count": 1,
15+
"metadata": {},
16+
"output_type": "execute_result"
17+
}
18+
],
19+
"source": [
20+
"# A = LIST\n",
21+
"# KEY = TARGET VALUE\n",
22+
"# SORTED ARRAY\n",
23+
"# TIME COMP = O(LOGN) \n",
24+
"# SPACE COMP = O(LOGN) (RECURSIVE) \n",
25+
"# SPACE COMP = O(1) (ITERATIVE)\n",
26+
"def binSearch(A,left,right,key):\n",
27+
" while left <= right:\n",
28+
" mid = (left+right) // 2\n",
29+
" if A[mid] == key:\n",
30+
" return mid\n",
31+
" elif A[mid] < key:\n",
32+
" left = mid+1\n",
33+
" else:\n",
34+
" # key < A[mid]\n",
35+
" right = mid -1\n",
36+
" return -1\n",
37+
"A = [25,35,80,96,200,500,850,999]\n",
38+
"KEY = 200\n",
39+
"binSearch(A,0,len(A)-1,KEY)\n",
40+
" "
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 2,
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"data": {
50+
"text/plain": [
51+
"4"
52+
]
53+
},
54+
"execution_count": 2,
55+
"metadata": {},
56+
"output_type": "execute_result"
57+
}
58+
],
59+
"source": [
60+
"# Bisect acts as a binary search\n",
61+
"import bisect\n",
62+
"A = [25,35,80,96,200,500,850,999]\n",
63+
"KEY = 200\n",
64+
"bisect.bisect_left(A,KEY,0,len(A))"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 3,
70+
"metadata": {},
71+
"outputs": [
72+
{
73+
"data": {
74+
"text/plain": [
75+
"[10, 7]"
76+
]
77+
},
78+
"execution_count": 3,
79+
"metadata": {},
80+
"output_type": "execute_result"
81+
}
82+
],
83+
"source": [
84+
"# A = List\n",
85+
"# K = Target\n",
86+
"# Unsorted List\n",
87+
"def findTargetPair(A,K):\n",
88+
" st = set()\n",
89+
" for i in range(len(A)):\n",
90+
" complement = K - A[i]\n",
91+
" if complement in st:\n",
92+
" return [complement,A[i]]\n",
93+
" else:\n",
94+
" st.add(A[i])\n",
95+
" \n",
96+
"A = [5,100,50,10,30,5,7,85,90,100]\n",
97+
"K = 17\n",
98+
"findTargetPair(A,K)"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": null,
104+
"metadata": {},
105+
"outputs": [],
106+
"source": []
107+
}
108+
],
109+
"metadata": {
110+
"kernelspec": {
111+
"display_name": "Python 3",
112+
"language": "python",
113+
"name": "python3"
114+
},
115+
"language_info": {
116+
"codemirror_mode": {
117+
"name": "ipython",
118+
"version": 3
119+
},
120+
"file_extension": ".py",
121+
"mimetype": "text/x-python",
122+
"name": "python",
123+
"nbconvert_exporter": "python",
124+
"pygments_lexer": "ipython3",
125+
"version": "3.8.3"
126+
}
127+
},
128+
"nbformat": 4,
129+
"nbformat_minor": 4
130+
}

Lect19.ipynb

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 4,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"rotator\n",
13+
"rotator\n",
14+
"True\n",
15+
"level\n",
16+
"level\n",
17+
"True\n"
18+
]
19+
}
20+
],
21+
"source": [
22+
"# T.C = O(N)\n",
23+
"def isPalindrome(s):\n",
24+
" print(s)\n",
25+
" print(s[::-1])\n",
26+
" return s == s[::-1]\n",
27+
"s1 = \"rotator\"\n",
28+
"s2 = \"level\"\n",
29+
"print(isPalindrome(s1))\n",
30+
"print(isPalindrome(s2))"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": 5,
36+
"metadata": {},
37+
"outputs": [
38+
{
39+
"name": "stdout",
40+
"output_type": "stream",
41+
"text": [
42+
"['E', 'I', 'L', 'N', 'S', 'T']\n",
43+
"['E', 'I', 'L', 'N', 'S', 'T']\n",
44+
"True\n"
45+
]
46+
}
47+
],
48+
"source": [
49+
"# T.C = O(NLOGN)\n",
50+
"def isAnagram(s1,s2):\n",
51+
" print(sorted(s1))\n",
52+
" print(sorted(s2))\n",
53+
" return sorted(s1) == sorted(s2)\n",
54+
"s1 = \"SILENT\"\n",
55+
"s2 = \"LISTEN\"\n",
56+
"print(isAnagram(s1,s2))"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 6,
62+
"metadata": {},
63+
"outputs": [
64+
{
65+
"name": "stdout",
66+
"output_type": "stream",
67+
"text": [
68+
"Counter({'S': 1, 'I': 1, 'L': 1, 'E': 1, 'N': 1, 'T': 1})\n",
69+
"Counter({'L': 1, 'I': 1, 'S': 1, 'T': 1, 'E': 1, 'N': 1})\n",
70+
"True\n"
71+
]
72+
}
73+
],
74+
"source": [
75+
"# T.C = O(N)\n",
76+
"from collections import Counter\n",
77+
"def isAnagram(s1,s2):\n",
78+
" print(Counter(s1))\n",
79+
" print(Counter(s2))\n",
80+
" return Counter(s1) == Counter(s2)\n",
81+
"s1 = \"SILENT\"\n",
82+
"s2 = \"LISTEN\"\n",
83+
"print(isAnagram(s1,s2))"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": 7,
89+
"metadata": {},
90+
"outputs": [
91+
{
92+
"name": "stdout",
93+
"output_type": "stream",
94+
"text": [
95+
"['programming', 'welcome', 'Coding', 'Hello', 'world', 'Happy', 'to']\n"
96+
]
97+
}
98+
],
99+
"source": [
100+
"# SORT THE WORDS OF THE STRING BY LENGTH IN DESCENDING ORDER\n",
101+
"# T.C = O(NLOGN)\n",
102+
"s = \"Hello world welcome to programming Happy Coding\"\n",
103+
"A = s.split()\n",
104+
"A.sort(key=len,reverse=True)\n",
105+
"print(A)"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": 11,
111+
"metadata": {},
112+
"outputs": [
113+
{
114+
"name": "stdout",
115+
"output_type": "stream",
116+
"text": [
117+
"[('a', 1), ('d', 1), ('i', 1), ('k', 1), ('l', 1), ('p', 1), ('w', 1), ('e', 2), ('m', 2), ('n', 2), ('o', 2), ('r', 2), ('g', 3)]\n"
118+
]
119+
}
120+
],
121+
"source": [
122+
"# Sort by Frequency\n",
123+
"# T.C = O(NLOGN)\n",
124+
"s = \"programmingknowledge\"\n",
125+
"from collections import Counter\n",
126+
"CT = Counter(s)\n",
127+
"A = list(CT.items())\n",
128+
"A.sort(key=lambda x:[x[1],x[0]])\n",
129+
"print(A)"
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": null,
135+
"metadata": {},
136+
"outputs": [],
137+
"source": []
138+
}
139+
],
140+
"metadata": {
141+
"kernelspec": {
142+
"display_name": "Python 3",
143+
"language": "python",
144+
"name": "python3"
145+
},
146+
"language_info": {
147+
"codemirror_mode": {
148+
"name": "ipython",
149+
"version": 3
150+
},
151+
"file_extension": ".py",
152+
"mimetype": "text/x-python",
153+
"name": "python",
154+
"nbconvert_exporter": "python",
155+
"pygments_lexer": "ipython3",
156+
"version": "3.8.3"
157+
}
158+
},
159+
"nbformat": 4,
160+
"nbformat_minor": 4
161+
}

0 commit comments

Comments
 (0)