Skip to content

Commit

Permalink
Adding k_factor.py in dp folder in add_K_FactorofStrings branch (keon…
Browse files Browse the repository at this point in the history
…#743)

* Adding k_factor.py in dp folder in add_K_FactorofStrings branch

* Added tests for K_factor of strings

* updated README by adding kfactor into dp section

* updated __init__.py in dp folder

Co-authored-by: Aditya <[email protected]>
  • Loading branch information
TheCodeYoda and Aditya authored Dec 4, 2020
1 parent 6ce5fea commit 162024f
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ If you want to uninstall algorithms, it is as simple as:
- [word_break](algorithms/dp/word_break.py)
- [fibonacci](algorithms/dp/fib.py)
- [hosoya triangle](algorithms/dp/hosoya_triangle.py)
- [K-Factor_strings](algorithms/dp/k_factor.py)
- [graph](algorithms/graph)
- [check_bipartite](algorithms/graph/check_bipartite.py)
- [strongly_connected](algorithms/graph/check_digraph_strongly_connected.py)
Expand Down
1 change: 1 addition & 0 deletions algorithms/dp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
from .rod_cut import *
from .word_break import *
from .int_divide import *
from .k_factor import *
68 changes: 68 additions & 0 deletions algorithms/dp/k_factor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'''The K factor of a string is defined as the number of times 'abba' appears as a substring.
Given two numbers N and k,​ find the number of strings of length N with 'K factor' = k.
The algorithms is as follows:
dp[n][k] will be a 4 element array, wherein each element can be the number of strings of length n and 'K factor' = k which belong to the criteria represented by that index:
dp[n][k][0] can be the number of strings of length n and K-factor = k which end with substring 'a'
dp[n][k][1] can be the number of strings of length n and K-factor = k which end with substring 'ab'
dp[n][k][2] can be the number of strings of length n and K-factor = k which end with substring 'abb'
dp[n][k][3] can be the number of strings of length n and K-factor = k which end with anything other than the above substrings (anything other than 'a' 'ab' 'abb')
Example inputs
n=4 k=1 no of strings = 1
n=7 k=1 no of strings = 70302
n=10 k=2 no of strings = 74357
'''

def find_k_factor(n,k):
dp=[[[0 for i in range(4)]for j in range((n-1)//3+2)]for k in range(n+1)]
if(3*k+1>n):
return 0
#base cases
dp[1][0][0]=1;
dp[1][0][1]=0;
dp[1][0][2]=0;
dp[1][0][3]=25;

for i in range(2,n+1):
for j in range((n-1)//3+2):
if(j==0):
#adding a at the end
dp[i][j][0]=dp[i-1][j][0]+dp[i-1][j][1]+dp[i-1][j][3]

#adding b at the end
dp[i][j][1]=dp[i-1][j][0]
dp[i][j][2]=dp[i-1][j][1]

#adding any other lowercase character
dp[i][j][3]=dp[i-1][j][0]*24+dp[i-1][j][1]*24+dp[i-1][j][2]*25+dp[i-1][j][3]*25

elif(3*j+1<i):
#adding a at the end
dp[i][j][0]=dp[i-1][j][0]+dp[i-1][j][1]+dp[i-1][j][3]+dp[i-1][j-1][2]

#adding b at the end
dp[i][j][1]=dp[i-1][j][0]
dp[i][j][2]=dp[i-1][j][1]

#adding any other lowercase character
dp[i][j][3]=dp[i-1][j][0]*24+dp[i-1][j][1]*24+dp[i-1][j][2]*25+dp[i-1][j][3]*25

elif(3*j+1==i):
dp[i][j][0]=1
dp[i][j][1]=0
dp[i][j][2]=0
dp[i][j][3]=0

else:
dp[i][j][0]=0
dp[i][j][1]=0
dp[i][j][2]=0
dp[i][j][3]=0

return sum(dp[n][k])

29 changes: 28 additions & 1 deletion tests/test_dp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
longest_increasing_subsequence,
longest_increasing_subsequence_optimized,
longest_increasing_subsequence_optimized2,
int_divide
int_divide,find_k_factor
)


Expand Down Expand Up @@ -155,6 +155,33 @@ def test_int_divide(self):
self.assertEqual(42, int_divide(10))
self.assertEqual(204226, int_divide(50))

class Test_dp_K_Factor(unittest.TestCase):
def test_kfactor(self):
#Test 1
n1=4
k1=1
self.assertEqual(find_k_factor(n1,k1),1)

#Test 2
n2=7
k2=1
self.assertEqual(find_k_factor(n2,k2),70302)

#Test 3
n3=10
k3=2
self.assertEqual(find_k_factor(n3,k3),74357)

#Test 4
n4=8
k4=2
self.assertEqual(find_k_factor(n4,k4),53)

#Test 5
n5=9
k5=1
self.assertEqual(find_k_factor(n5,k5),71284044)


if __name__ == '__main__':
unittest.main()

0 comments on commit 162024f

Please sign in to comment.