Skip to content

Commit 9624c04

Browse files
committed
rename folders
1 parent 658ba2b commit 9624c04

File tree

95 files changed

+2861
-2861
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+2861
-2861
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
1-
"""
2-
Intuition:
3-
If you have to guess a magic number from 1-100, the best first attempt would be to guess '50'
4-
or in other words, the middle. If I tell you that the magic number is higher,
5-
you now don't need to consider all numbers 1-50,
6-
and if it is lower you wouldn't need to consider numbers 50-100!!
7-
8-
In Binary Search, we follow the same idea,
9-
1. Compar the target with the middle element.
10-
2. If the target is higher, then the target can only lie in the right (greater) subarray. We re-calculate mid and repeat step 1.
11-
3. If the target is lower, the target can only lie in the left (lower) half. We re-calculate mid and repeat step 1.
12-
13-
Binary search can only operate on a sorted array.
14-
Further reading: https://en.wikipedia.org/wiki/Binary_search_algorithm
15-
"""
16-
17-
18-
19-
20-
import math
21-
22-
def binary_search(lst, target):
23-
if not lst:
24-
return -1
25-
lo = 0
26-
hi = len(lst)-1
27-
28-
while lo <= hi:
29-
mid = math.floor(lo + (hi - lo) / 2) # Find mid. math.floor is used to round floats down.
30-
if lst[mid] < target: # Element in mid is lower than target.
31-
lo = mid + 1 # Our low (start) becomes the element after mid.
32-
elif lst[mid] > target: # Element in mid is higher than target.
33-
hi = mid - 1 # Our high (end) becomes the element before mid.
34-
elif lst[mid] == target:
35-
print(f"Found {target} at index {mid}.")
36-
return mid
37-
print(f"Target {target} not found.")
38-
return -1
39-
40-
41-
arr = [10, 20, 30, 50, 60, 80, 110, 130, 140, 170]
42-
binary_search(arr, 80)
43-
binary_search(arr, 10)
44-
binary_search(arr, 110)
45-
binary_search(arr, 20)
46-
binary_search(arr, 140)
47-
binary_search(arr, 2)
48-
binary_search(arr, 1)
1+
"""
2+
Intuition:
3+
If you have to guess a magic number from 1-100, the best first attempt would be to guess '50'
4+
or in other words, the middle. If I tell you that the magic number is higher,
5+
you now don't need to consider all numbers 1-50,
6+
and if it is lower you wouldn't need to consider numbers 50-100!!
7+
8+
In Binary Search, we follow the same idea,
9+
1. Compar the target with the middle element.
10+
2. If the target is higher, then the target can only lie in the right (greater) subarray. We re-calculate mid and repeat step 1.
11+
3. If the target is lower, the target can only lie in the left (lower) half. We re-calculate mid and repeat step 1.
12+
13+
Binary search can only operate on a sorted array.
14+
Further reading: https://en.wikipedia.org/wiki/Binary_search_algorithm
15+
"""
16+
17+
18+
19+
20+
import math
21+
22+
def binary_search(lst, target):
23+
if not lst:
24+
return -1
25+
lo = 0
26+
hi = len(lst)-1
27+
28+
while lo <= hi:
29+
mid = math.floor(lo + (hi - lo) / 2) # Find mid. math.floor is used to round floats down.
30+
if lst[mid] < target: # Element in mid is lower than target.
31+
lo = mid + 1 # Our low (start) becomes the element after mid.
32+
elif lst[mid] > target: # Element in mid is higher than target.
33+
hi = mid - 1 # Our high (end) becomes the element before mid.
34+
elif lst[mid] == target:
35+
print(f"Found {target} at index {mid}.")
36+
return mid
37+
print(f"Target {target} not found.")
38+
return -1
39+
40+
41+
arr = [10, 20, 30, 50, 60, 80, 110, 130, 140, 170]
42+
binary_search(arr, 80)
43+
binary_search(arr, 10)
44+
binary_search(arr, 110)
45+
binary_search(arr, 20)
46+
binary_search(arr, 140)
47+
binary_search(arr, 2)
48+
binary_search(arr, 1)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
// Program to count the number of bits that are set to 1 in an integer
2-
// The following program tests bits one at a time starting with the least-significant bit.
3-
// Since we perform O(1) computation per bit, the time complexity is O(n) where n is number of bits in the integer
4-
// Best case time complexity is O(1), if the input io 0
5-
#include<bits/stdc++.h>
6-
using namespace std;
7-
int find_set_bits(int n){
8-
int set_bits = 0;
9-
while(n){
10-
set_bits += (n & 1);
11-
n >>= 1;
12-
}
13-
return set_bits;
14-
}
15-
int main(){
16-
cout << find_set_bits(4) << endl;
17-
return 0;
18-
}
1+
// Program to count the number of bits that are set to 1 in an integer
2+
// The following program tests bits one at a time starting with the least-significant bit.
3+
// Since we perform O(1) computation per bit, the time complexity is O(n) where n is number of bits in the integer
4+
// Best case time complexity is O(1), if the input io 0
5+
#include<bits/stdc++.h>
6+
using namespace std;
7+
int find_set_bits(int n){
8+
int set_bits = 0;
9+
while(n){
10+
set_bits += (n & 1);
11+
n >>= 1;
12+
}
13+
return set_bits;
14+
}
15+
int main(){
16+
cout << find_set_bits(4) << endl;
17+
return 0;
18+
}
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
1-
/*
2-
Given two strings str1 and str2 and following three operations that can performed on str1.
3-
1) Insert
4-
2) Remove
5-
3) Replace
6-
Find minimum number of operations required to convert ‘str1’ into ‘str2’.
7-
For example if input strings are CAT AND CAR the edit distance is 1.
8-
9-
Input : s1 : saturday s2 : sunday
10-
Output : 3
11-
*/
12-
// Dynamic Programming Solution : TC O(n^2)
13-
// Porgram Author : Abhisek Kumar Gupta
14-
#include<bits/stdc++.h>
15-
using namespace std;
16-
int find_edit_distance(string s1, string s2, int l1, int l2){
17-
int dp[100][100] = {};
18-
for(int i = 0; i <= l1; i++){
19-
dp[i][0] = i;
20-
}
21-
for(int i = 0; i <= l2; i++){
22-
dp[0][i] = i;
23-
}
24-
for(int i = 1; i <= l1; i++){
25-
for(int j = 1; j <= l2; j++){
26-
if(s1[i] == s2[j])
27-
dp[i][j] = dp[i - 1][j - 1];
28-
else{
29-
int del = dp[i][j - 1];
30-
int replace = dp[i - 1][j - 1];
31-
int insert = dp[i - 1][j];
32-
dp[i][j] = min(del, min(replace, insert)) + 1;
33-
}
34-
}
35-
}
36-
for(int i = 0; i <= l1; i++){
37-
for(int j = 0; j <= l2; j++){
38-
cout << setw(5) <<dp[i][j] << " ";
39-
}
40-
cout << "\n";
41-
}
42-
return dp[l1][l2];
43-
}
44-
int main(){
45-
string s1 = "abhisek";
46-
string s2 = "tsunade";
47-
int l1 = s1.length() - 1;
48-
int l2 = s2.length() - 1;
49-
int result = find_edit_distance(s1, s2, l1, l2);
50-
cout << result;
51-
return 0;
52-
}
1+
/*
2+
Given two strings str1 and str2 and following three operations that can performed on str1.
3+
1) Insert
4+
2) Remove
5+
3) Replace
6+
Find minimum number of operations required to convert ‘str1’ into ‘str2’.
7+
For example if input strings are CAT AND CAR the edit distance is 1.
8+
9+
Input : s1 : saturday s2 : sunday
10+
Output : 3
11+
*/
12+
// Dynamic Programming Solution : TC O(n^2)
13+
// Porgram Author : Abhisek Kumar Gupta
14+
#include<bits/stdc++.h>
15+
using namespace std;
16+
int find_edit_distance(string s1, string s2, int l1, int l2){
17+
int dp[100][100] = {};
18+
for(int i = 0; i <= l1; i++){
19+
dp[i][0] = i;
20+
}
21+
for(int i = 0; i <= l2; i++){
22+
dp[0][i] = i;
23+
}
24+
for(int i = 1; i <= l1; i++){
25+
for(int j = 1; j <= l2; j++){
26+
if(s1[i] == s2[j])
27+
dp[i][j] = dp[i - 1][j - 1];
28+
else{
29+
int del = dp[i][j - 1];
30+
int replace = dp[i - 1][j - 1];
31+
int insert = dp[i - 1][j];
32+
dp[i][j] = min(del, min(replace, insert)) + 1;
33+
}
34+
}
35+
}
36+
for(int i = 0; i <= l1; i++){
37+
for(int j = 0; j <= l2; j++){
38+
cout << setw(5) <<dp[i][j] << " ";
39+
}
40+
cout << "\n";
41+
}
42+
return dp[l1][l2];
43+
}
44+
int main(){
45+
string s1 = "abhisek";
46+
string s2 = "tsunade";
47+
int l1 = s1.length() - 1;
48+
int l2 = s2.length() - 1;
49+
int result = find_edit_distance(s1, s2, l1, l2);
50+
cout << result;
51+
return 0;
52+
}
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
/*
2-
Given two strings str1 and str2 and following three operations that can performed on str1.
3-
1) Insert
4-
2) Remove
5-
3) Replace
6-
Find minimum number of operations required to convert ‘str1’ into ‘str2’.
7-
For example if input strings are CAT AND CAR the edit distance is 1.
8-
9-
Input : s1 : saturday s2 : sunday
10-
Output : 3
11-
*/
12-
// Memoized Solution : TC O(n^2)
13-
// Porgram Author : Abhisek Kumar Gupta
14-
#include<bits/stdc++.h>
15-
using namespace std;
16-
int calls = 0;
17-
int memoize[1009][1009];
18-
int find_edit_distance(string s1, string s2, int l1, int l2){
19-
calls++;
20-
if(l1 == 0)
21-
return l2;
22-
if(l2 == 0)
23-
return l1;
24-
if(memoize[l1][l2] != -1) return memoize[l1][l2];
25-
if(s1[l1] == s2[l2]){
26-
return find_edit_distance(s1, s2, l1 - 1, l2 - 1);
27-
}
28-
int del = find_edit_distance(s1, s2, l1, l2 - 1);
29-
int replace = find_edit_distance(s1, s2, l1 - 1, l2 - 1);
30-
int insert = find_edit_distance(s1, s2, l1 - 1, l2);
31-
memoize[l1][l2] = min (del, min(replace, insert)) + 1;
32-
return min (del, min(replace, insert)) + 1;
33-
}
34-
int main(){
35-
memset(memoize, -1, sizeof(memoize));
36-
string s1 = "abhisek";
37-
string s2 = "tsunade";
38-
int l1 = s1.length() - 1;
39-
int l2 = s2.length() - 1;
40-
int result = find_edit_distance(s1, s2, l1, l2);
41-
cout << result;
42-
cout << "\n" << calls;
43-
return 0;
1+
/*
2+
Given two strings str1 and str2 and following three operations that can performed on str1.
3+
1) Insert
4+
2) Remove
5+
3) Replace
6+
Find minimum number of operations required to convert ‘str1’ into ‘str2’.
7+
For example if input strings are CAT AND CAR the edit distance is 1.
8+
9+
Input : s1 : saturday s2 : sunday
10+
Output : 3
11+
*/
12+
// Memoized Solution : TC O(n^2)
13+
// Porgram Author : Abhisek Kumar Gupta
14+
#include<bits/stdc++.h>
15+
using namespace std;
16+
int calls = 0;
17+
int memoize[1009][1009];
18+
int find_edit_distance(string s1, string s2, int l1, int l2){
19+
calls++;
20+
if(l1 == 0)
21+
return l2;
22+
if(l2 == 0)
23+
return l1;
24+
if(memoize[l1][l2] != -1) return memoize[l1][l2];
25+
if(s1[l1] == s2[l2]){
26+
return find_edit_distance(s1, s2, l1 - 1, l2 - 1);
27+
}
28+
int del = find_edit_distance(s1, s2, l1, l2 - 1);
29+
int replace = find_edit_distance(s1, s2, l1 - 1, l2 - 1);
30+
int insert = find_edit_distance(s1, s2, l1 - 1, l2);
31+
memoize[l1][l2] = min (del, min(replace, insert)) + 1;
32+
return min (del, min(replace, insert)) + 1;
33+
}
34+
int main(){
35+
memset(memoize, -1, sizeof(memoize));
36+
string s1 = "abhisek";
37+
string s2 = "tsunade";
38+
int l1 = s1.length() - 1;
39+
int l2 = s2.length() - 1;
40+
int result = find_edit_distance(s1, s2, l1, l2);
41+
cout << result;
42+
cout << "\n" << calls;
43+
return 0;
4444
}

0 commit comments

Comments
 (0)