Skip to content

Commit

Permalink
Python solution for day 10 and day 11 (#114)
Browse files Browse the repository at this point in the history
* Add @vishalshirke7 as a contributor

* Update @vishalshirke7 as a contributor

* Added python solutions for Day 4

* modified readme

* Add @Vishal

* reverted adding as a contributor

* Update README.md

* Update CONTRIBUTORS.md

* Update CONTRIBUTORS.md

* Added python solutions for day 7

* Edited python solution for day 7

* Edited python solution for day 7

* Added python solution for day 10

* Changed readme for day 10

* minor changes

* Python solution for day 11

* Python solution for day10 and day11
  • Loading branch information
vishalshirke7 authored and MadhavBahl committed Jan 3, 2019
1 parent dd79ee7 commit 9080b91
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 2 deletions.
10 changes: 10 additions & 0 deletions day10/Python/permutations1.py
@@ -0,0 +1,10 @@
from itertools import permutations
"""
@author : vishalshirke7
@date : 02/01/2019
"""

ip_str = input()
perm = permutations(list(ip_str), len(ip_str))
for i in set(list(perm)):
print("".join(map(str, i)))
24 changes: 22 additions & 2 deletions day10/README.md
Expand Up @@ -120,7 +120,7 @@ permut('123');

## Python Implementation

### [Solution](./Python/permutations.py)
### [Solution1](./Python/permutations.py)
```python

'''
Expand Down Expand Up @@ -154,8 +154,28 @@ def main():
main()
```

### [Solution2](./Python/st_permutations.py)
### [Solution 2 by @vishalshirke7](./Python/permutations1.py)
```python

from itertools import permutations
"""
@author : vishalshirke7
@date : 02/01/2019
This solution makes use of python's in-build permutations function from itertools module
It takes two arguments 1st is the list on which permutation is to be performed and
2nd is the length of the permutation
"""

ip_str = input()
perm = permutations(list(ip_str), len(ip_str))
for i in set(list(perm)):
print("".join(map(str, i)))
```

### [Solution 3 by @ashwek](./Python/st_permutations.py)

```py
"""
* @author: ashwek
* @date 2/1/2019
Expand Down
51 changes: 51 additions & 0 deletions day11/Python/lcs.py
@@ -0,0 +1,51 @@
"""
@author : vishalshirke7
@date : 03/01/2019
"""


def lcs(str1, str2):
m, n = len(str1), len(str2)

L = [[0 for y in range(n + 1)] for x in range(m + 1)]

for i in range(m + 1):
for j in range(n + 1):
if i == 0 or j == 0:
L[i][j] = 0
elif str1[i - 1] == str2[j - 1]:
L[i][j] = L[i - 1][j - 1] + 1
else:
L[i][j] = max(L[i - 1][j], L[i][j - 1])

index = L[m][n]

# Create a character array to store the lcs string
lcs_str = [""] * (index + 1)
lcs_str[index] = ""

i = m
j = n
while i > 0 and j > 0:

# If current character in X[] and Y are same, then
# current character is part of LCS
if str1[i - 1] == str2[j - 1]:
lcs_str[index - 1] = str1[i - 1]
i -= 1
j -= 1
index -= 1

# If not same, then find the larger of two and
# go in the direction of larger value
elif L[i - 1][j] > L[i][j - 1]:
i -= 1
else:
j -= 1

print("LCS of " + str1 + " and " + str2 + " is -" + "".join(lcs_str))


lcs(*(input().split()))


58 changes: 58 additions & 0 deletions day11/README.md
Expand Up @@ -192,3 +192,61 @@ public class longestCommonSubstring {
}
}
```

## Python Implementation

### [Solution using dynamic programming](./Python/lcs.py)

```python
"""
@author : vishalshirke7
@date : 03/01/2019
"""


def lcs(str1, str2):
m, n = len(str1), len(str2)

L = [[0 for y in range(n + 1)] for x in range(m + 1)]

for i in range(m + 1):
for j in range(n + 1):
if i == 0 or j == 0:
L[i][j] = 0
elif str1[i - 1] == str2[j - 1]:
L[i][j] = L[i - 1][j - 1] + 1
else:
L[i][j] = max(L[i - 1][j], L[i][j - 1])

index = L[m][n]

# Create a character array to store the lcs string
lcs_str = [""] * (index + 1)
lcs_str[index] = ""

# Start from the right-most-bottom-most corner and
# one by one store characters in lcs[]
i = m
j = n
while i > 0 and j > 0:

# If current character in X[] and Y are same, then
# current character is part of LCS
if str1[i - 1] == str2[j - 1]:
lcs_str[index - 1] = str1[i - 1]
i -= 1
j -= 1
index -= 1

# If not same, then find the larger of two and
# go in the direction of larger value
elif L[i - 1][j] > L[i][j - 1]:
i -= 1
else:
j -= 1

print("LCS of " + str1 + " and " + str2 + " is -" + "".join(lcs_str))


lcs(*(input().split()))
```

0 comments on commit 9080b91

Please sign in to comment.