Skip to content

Commit 3b8b1b9

Browse files
authored
Merge branch 'master' into restructure-index
2 parents 07d639e + b8d76df commit 3b8b1b9

14 files changed

Lines changed: 286 additions & 38 deletions

.github/pull_request_template.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Description
2+
3+
Please include a summary of the change and which issue is fixed or what question/feature you have added.
4+
5+
Fixes # (issue)
6+
7+
## Type of change
8+
9+
Please delete options that are not relevant.
10+
11+
- [ ] Bug fix (non-breaking change which fixes an issue)
12+
- [ ] New feature/question
13+
- [ ] This change requires a documentation update
14+
- [ ] Bookmark link
15+
16+
# Checklist:
17+
18+
- [ ] My code follows the style guidelines of this project i.e. [Pep8](https://www.python.org/dev/peps/pep-0008/)
19+
- [ ] I have performed a self-review of my own code
20+
- [ ] I have commented my code, particularly in hard-to-understand areas
21+
- [ ] I have made corresponding changes to the documentation
22+
- [ ] Any dependent changes have been merged and published in downstream modules
23+
- [ ] I have squashed unnecessary commits

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ This directory contains various types of algorithm questions like Dynamic Progra
3131
1. Dynamic Programming
3232
2. Math
3333
3. Sorting
34+
4. Greedy
35+
5. Misc
3436

3537
### Bookmarks
3638

@@ -65,4 +67,4 @@ To follow the guidelines, refer to [Contributing.md](CONTRIBUTING.md)
6567

6668
## :page_facing_up: License
6769

68-
MIT @ Prabhu Pant
70+
MIT @ Prabhu Pant

algorithms/dynamic_programming/climbing_stairs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""
22
High Level Description:
33
You are climbing a stair case. It takes n steps to reach to the top.
4-
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
4+
Each time you can either climb 1 or 2 steps. In how many distinct ways can you
5+
climb to the top?
56
67
Time Complexity:
78
O(n)
Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
"""
22
High Level Description:
3-
Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.
4-
You have the following 3 operations permitted on a word:
3+
Given two words word1 and word2, find the minimum number of operations
4+
required to convert word1 to word2. You have the following 3 operations
5+
permitted on a word:
56
Insert a character
67
Delete a character
78
Replace a character
89
910
Time Complexity:
1011
O(m*n)
1112
"""
13+
14+
1215
def edit_dist(str1, str2, m, n):
13-
dp = [[0 for x in range(n+1)] for x in range(m+1)]
14-
15-
for i in range(m+1):
16-
for j in range(n+1):
17-
if i == 0:
18-
dp[i][j] = j
19-
elif j == 0:
20-
dp[i][j] = i
21-
elif str1[i-1] == str2[j-1]:
22-
dp[i][j] = dp[i-1][j-1]
23-
else:
24-
dp[i][j] = 1 + min(dp[i][j-1], # Insert
25-
dp[i-1][j], # Remove
26-
dp[i-1][j-1]) # Replace
27-
28-
return dp[m][n]
16+
dp = [[0 for x in range(n+1)] for x in range(m+1)]
17+
18+
for i in range(m+1):
19+
for j in range(n+1):
20+
if i == 0:
21+
dp[i][j] = j
22+
elif j == 0:
23+
dp[i][j] = i
24+
elif str1[i-1] == str2[j-1]:
25+
dp[i][j] = dp[i-1][j-1]
26+
else:
27+
dp[i][j] = 1 + min(dp[i][j-1], # Insert
28+
dp[i-1][j], # Remove
29+
dp[i-1][j-1]) # Replace
30+
31+
return dp[m][n]

algorithms/dynamic_programming/longest_common_subsequence.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
longest common subsequence
55
'''
66

7+
78
def lcs(S1, S2):
89
m = len(S1)
910
n = len(S2)
@@ -12,7 +13,7 @@ def lcs(S1, S2):
1213

1314
for i in range(m + 1):
1415
for j in range(n + 1):
15-
if i == 0 or j == 0 :
16+
if i == 0 or j == 0:
1617
LCS[i][j] = 0
1718
elif S1[i-1] == S2[j-1]:
1819
LCS[i][j] = LCS[i-1][j-1]+1

algorithms/dynamic_programming/max_subarray.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
"""
22
High Level Description:
3-
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
3+
Given an integer array nums, find the contiguous subarray (containing at least
4+
one number) which has the largest sum and return its sum.
45
56
Time Complexity:
67
O(n)
78
"""
9+
10+
811
# Iterative Solution
912
def max_sub_array(self, arr):
1013
if not arr:
@@ -15,4 +18,4 @@ def max_sub_array(self, arr):
1518
cur_sum = max(num, cur_sum + num)
1619
max_sum = max(max_sum, cur_sum)
1720

18-
return max_sum
21+
return max_sum

algorithms/dynamic_programming/rod_cutting.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44
of all pieces of size less than n. We
55
need to find the maximum maximum price
66
obtainable by cutting the rod and
7-
selling it.
7+
selling it.
88
'''
99
INT_MIN = -32767
1010

11+
1112
def cutRod(price, n):
1213
values = [0 for i in range(n+1)]
1314
values[0] = 0
1415

1516
for i in range(1, n+1):
1617
max_value = INT_MIN
1718
for j in range(i):
18-
max_value = max(max_value, price[j] + values[i-j-1])
19+
max_value = max(max_value, price[j] + values[i-j-1])
1920
values[i] = max_value
2021

2122
return values[n]
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
"""
22
High Level Description:
3-
A preson is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
4-
The preson can only move either down or right at any point in time.
5-
The preson is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
3+
A preson is located at the top-left corner of a m x n grid (marked 'Start' in
4+
the diagram below). The preson can only move either down or right at any point
5+
in time. The preson is trying to reach the bottom-right corner of the grid
6+
(marked 'Finish' in the diagram below).
67
How many possible unique paths are there?
78
89
Time Complexity:
910
O(m*n)
1011
"""
11-
def number_of_paths(m, n):
12-
count = [[0 for x in range(m)] for y in range(n)]
1312

14-
for i in range(m):
15-
count[i][0] = 1;
1613

17-
for j in range(n):
18-
count[0][j] = 1;
14+
def number_of_paths(m, n):
15+
count = [[0 for x in range(m)] for y in range(n)]
1916

20-
for i in range(1, m):
21-
for j in range(n):
22-
count[i][j] = count[i-1][j] + count[i][j-1]
23-
return count[m-1][n-1]
17+
for i in range(m):
18+
count[i][0] = 1
19+
20+
for j in range(n):
21+
count[0][j] = 1
22+
23+
for i in range(1, m):
24+
for j in range(n):
25+
count[i][j] = count[i-1][j] + count[i][j-1]
26+
return count[m-1][n-1]

algorithms/math/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
* [Fibonacci Number](https://github.com/prabhupant/python-ds/blob/master/algorithms/math/fibonacci_number.py)
55
* [Nth Prime Number](https://github.com/prabhupant/python-ds/blob/master/algorithms/math/prime.py)
66
* [Sieve of Erastothenes](https://github.com/prabhupant/python-ds/blob/master/algorithms/math/sieve_of_eratosthenes.py)
7+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Description:
3+
Sieve of Eratosthenes is a very fast method (loglog(n)) of finding primes upto a given number.
4+
"""
5+
6+
7+
def sieve(n):
8+
prime_list = []
9+
for i in range(2, n+1):
10+
if i not in prime_list:
11+
print(i)
12+
for j in range(i*i, n+1, i):
13+
prime_list.append(j)
14+
15+
16+
if __name__ == '__main__':
17+
input_number = int(input("Provide a number upto which primes are to be found :"))
18+
sieve(input_number)

0 commit comments

Comments
 (0)