File tree Expand file tree Collapse file tree 4 files changed +141
-2
lines changed
Expand file tree Collapse file tree 4 files changed +141
-2
lines changed Original file line number Diff line number Diff line change 1+ from itertools import permutations
2+ """
3+ @author : vishalshirke7
4+ @date : 02/01/2019
5+ """
6+
7+ ip_str = input ()
8+ perm = permutations (list (ip_str ), len (ip_str ))
9+ for i in set (list (perm )):
10+ print ("" .join (map (str , i )))
Original file line number Diff line number Diff line change @@ -120,7 +120,7 @@ permut('123');
120120
121121## Python Implementation
122122
123- ### [ Solution ] ( ./Python/permutations.py )
123+ ### [ Solution1 ] ( ./Python/permutations.py )
124124``` python
125125
126126'''
@@ -154,8 +154,28 @@ def main():
154154main()
155155```
156156
157- ### [ Solution2 ] ( ./Python/st_permutations .py )
157+ ### [ Solution 2 by @ vishalshirke7 ] ( ./Python/permutations1 .py )
158158``` python
159+
160+ from itertools import permutations
161+ """
162+ @author : vishalshirke7
163+ @date : 02/01/2019
164+
165+ This solution makes use of python's in-build permutations function from itertools module
166+ It takes two arguments 1st is the list on which permutation is to be performed and
167+ 2nd is the length of the permutation
168+ """
169+
170+ ip_str = input ()
171+ perm = permutations(list (ip_str), len (ip_str))
172+ for i in set (list (perm)):
173+ print (" " .join(map (str , i)))
174+ ```
175+
176+ ### [ Solution 3 by @ashwek ] ( ./Python/st_permutations.py )
177+
178+ ``` py
159179"""
160180 * @author: ashwek
161181 * @date 2/1/2019
Original file line number Diff line number Diff line change 1+ """
2+ @author : vishalshirke7
3+ @date : 03/01/2019
4+ """
5+
6+
7+ def lcs (str1 , str2 ):
8+ m , n = len (str1 ), len (str2 )
9+
10+ L = [[0 for y in range (n + 1 )] for x in range (m + 1 )]
11+
12+ for i in range (m + 1 ):
13+ for j in range (n + 1 ):
14+ if i == 0 or j == 0 :
15+ L [i ][j ] = 0
16+ elif str1 [i - 1 ] == str2 [j - 1 ]:
17+ L [i ][j ] = L [i - 1 ][j - 1 ] + 1
18+ else :
19+ L [i ][j ] = max (L [i - 1 ][j ], L [i ][j - 1 ])
20+
21+ index = L [m ][n ]
22+
23+ # Create a character array to store the lcs string
24+ lcs_str = ["" ] * (index + 1 )
25+ lcs_str [index ] = ""
26+
27+ i = m
28+ j = n
29+ while i > 0 and j > 0 :
30+
31+ # If current character in X[] and Y are same, then
32+ # current character is part of LCS
33+ if str1 [i - 1 ] == str2 [j - 1 ]:
34+ lcs_str [index - 1 ] = str1 [i - 1 ]
35+ i -= 1
36+ j -= 1
37+ index -= 1
38+
39+ # If not same, then find the larger of two and
40+ # go in the direction of larger value
41+ elif L [i - 1 ][j ] > L [i ][j - 1 ]:
42+ i -= 1
43+ else :
44+ j -= 1
45+
46+ print ("LCS of " + str1 + " and " + str2 + " is -" + "" .join (lcs_str ))
47+
48+
49+ lcs (* (input ().split ()))
50+
51+
Original file line number Diff line number Diff line change @@ -192,3 +192,61 @@ public class longestCommonSubstring {
192192 }
193193}
194194```
195+
196+ ## Python Implementation
197+
198+ ### [ Solution using dynamic programming] ( ./Python/lcs.py )
199+
200+ ``` python
201+ """
202+ @author : vishalshirke7
203+ @date : 03/01/2019
204+ """
205+
206+
207+ def lcs (str1 , str2 ):
208+ m, n = len (str1), len (str2)
209+
210+ L = [[0 for y in range (n + 1 )] for x in range (m + 1 )]
211+
212+ for i in range (m + 1 ):
213+ for j in range (n + 1 ):
214+ if i == 0 or j == 0 :
215+ L[i][j] = 0
216+ elif str1[i - 1 ] == str2[j - 1 ]:
217+ L[i][j] = L[i - 1 ][j - 1 ] + 1
218+ else :
219+ L[i][j] = max (L[i - 1 ][j], L[i][j - 1 ])
220+
221+ index = L[m][n]
222+
223+ # Create a character array to store the lcs string
224+ lcs_str = [" " ] * (index + 1 )
225+ lcs_str[index] = " "
226+
227+ # Start from the right-most-bottom-most corner and
228+ # one by one store characters in lcs[]
229+ i = m
230+ j = n
231+ while i > 0 and j > 0 :
232+
233+ # If current character in X[] and Y are same, then
234+ # current character is part of LCS
235+ if str1[i - 1 ] == str2[j - 1 ]:
236+ lcs_str[index - 1 ] = str1[i - 1 ]
237+ i -= 1
238+ j -= 1
239+ index -= 1
240+
241+ # If not same, then find the larger of two and
242+ # go in the direction of larger value
243+ elif L[i - 1 ][j] > L[i][j - 1 ]:
244+ i -= 1
245+ else :
246+ j -= 1
247+
248+ print (" LCS of " + str1 + " and " + str2 + " is -" + " " .join(lcs_str))
249+
250+
251+ lcs(* (input ().split()))
252+ ```
You can’t perform that action at this time.
0 commit comments