Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d661bfe

Browse files
Two-Pointers
1 parent a2dcb5b commit d661bfe

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed

‎Two-pointers/twopointer.ipynb

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"True\n"
13+
]
14+
}
15+
],
16+
"source": [
17+
"#if string is palindrome\n",
18+
"\n",
19+
"def ispalindrome(s):\n",
20+
" #start\n",
21+
" i=0\n",
22+
" #end\n",
23+
" j=len(s)-1\n",
24+
" while i<j:\n",
25+
" if s[i]!=s[j]:\n",
26+
" return False\n",
27+
" \n",
28+
" i=i+1\n",
29+
" j=j-1\n",
30+
" return True\n",
31+
"\n",
32+
"print(ispalindrome(\"civic\")) #True"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 2,
38+
"metadata": {},
39+
"outputs": [
40+
{
41+
"name": "stdout",
42+
"output_type": "stream",
43+
"text": [
44+
"True\n"
45+
]
46+
}
47+
],
48+
"source": [
49+
"#sum of element of sorted array is equal to target\n",
50+
"\n",
51+
"def twosum(arr,target):\n",
52+
" #start \n",
53+
" i=0\n",
54+
" #end\n",
55+
" j=len(arr)-1\n",
56+
" while i<j:\n",
57+
" sum = arr[i]+arr[j]\n",
58+
" if target == sum:\n",
59+
" return True\n",
60+
" elif sum<target:\n",
61+
" i=i+1\n",
62+
" else:\n",
63+
" j=j-1\n",
64+
" return False\n",
65+
"\n",
66+
"print(twosum([4,6,9,10,12,18,22],19))"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": 3,
72+
"metadata": {},
73+
"outputs": [
74+
{
75+
"name": "stdout",
76+
"output_type": "stream",
77+
"text": [
78+
"7\n"
79+
]
80+
}
81+
],
82+
"source": [
83+
"#Two Pointer Technique In sorted Array and Binary Search\n",
84+
"\n",
85+
"def sum(arr,key):\n",
86+
" l=0\n",
87+
" r=len(arr)-1\n",
88+
" while l<=r:\n",
89+
" m=(l+r)//2\n",
90+
" if arr[m]==key:\n",
91+
" return m\n",
92+
" elif arr[m]<key:\n",
93+
" l=m+1\n",
94+
" else:\n",
95+
" #arr[m]>key\n",
96+
" r=m-1\n",
97+
" return -1\n",
98+
"\n",
99+
"print(sum([6,8,9,10,12,42,51,65,82],65)) "
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": 4,
105+
"metadata": {},
106+
"outputs": [
107+
{
108+
"data": {
109+
"text/plain": [
110+
"7"
111+
]
112+
},
113+
"execution_count": 4,
114+
"metadata": {},
115+
"output_type": "execute_result"
116+
}
117+
],
118+
"source": [
119+
"#inbuilt function- bisect acts as a binary search\n",
120+
"import bisect\n",
121+
"arr = [6,8,9,10,12,42,51,65,82]\n",
122+
"key = 65\n",
123+
"bisect.bisect_left(arr,key)"
124+
]
125+
},
126+
{
127+
"cell_type": "code",
128+
"execution_count": 5,
129+
"metadata": {},
130+
"outputs": [
131+
{
132+
"name": "stdout",
133+
"output_type": "stream",
134+
"text": [
135+
"[8, 2]\n"
136+
]
137+
}
138+
],
139+
"source": [
140+
"#Two Pointer Technique In unsorted Array\n",
141+
"\n",
142+
"def findtarget(arr,target):\n",
143+
" st =set() #unordered data structure can take hashmap here \n",
144+
"\n",
145+
" for i in range(len(arr)):\n",
146+
" complement = target - arr[i]\n",
147+
" if complement in st:\n",
148+
" return [complement,arr[i]]\n",
149+
" else:\n",
150+
" st.add(arr[i])\n",
151+
"print(findtarget([8,2,9,3,6,4,13,41,15],10))"
152+
]
153+
}
154+
],
155+
"metadata": {
156+
"kernelspec": {
157+
"display_name": "Python 3.9.7 64-bit",
158+
"language": "python",
159+
"name": "python3"
160+
},
161+
"language_info": {
162+
"codemirror_mode": {
163+
"name": "ipython",
164+
"version": 3
165+
},
166+
"file_extension": ".py",
167+
"mimetype": "text/x-python",
168+
"name": "python",
169+
"nbconvert_exporter": "python",
170+
"pygments_lexer": "ipython3",
171+
"version": "3.9.7"
172+
},
173+
"orig_nbformat": 4,
174+
"vscode": {
175+
"interpreter": {
176+
"hash": "0d591c6e422414675974e227c13f5382000c440fedd3c5006ef2be5d887f0ba7"
177+
}
178+
}
179+
},
180+
"nbformat": 4,
181+
"nbformat_minor": 2
182+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /