Skip to content

Commit 31df9f2

Browse files
authored
Add files via upload
1 parent 43c1443 commit 31df9f2

4 files changed

+294
-0
lines changed

coin change dynamic programming.jl

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# coding: utf-8
2+
3+
4+
# In[1]:
5+
6+
#coin change is a straight forward dynamic programming problem
7+
#you are given one note and a set of coins of different values
8+
#assuming you have infinite supply of coins of different values
9+
#we need to compute different ways of making change
10+
#the order of the coins doesnt matter in this case
11+
#more details can be found in the following link
12+
# https://www.geeksforgeeks.org/coin-change-dp-7/
13+
#the script can also be done in recursion
14+
# https://github.com/je-suis-tm/recursion-and-dynamic-programming/blob/master/coin%20change%20recursion.jl
15+
16+
17+
# In[2]:
18+
19+
20+
#to solve this problem via tabulation
21+
#we divide one big problem into two sub problems
22+
#the case where coin of η value is excluded in the solutions
23+
#and the case where at least one coin of η value is included
24+
function coin_change(num,choice)
25+
26+
#create matrix (num+1)*length(choice)
27+
#the raison d'être is the computation starts from 0 to num
28+
#0 is when num is perfectly substituted by coins
29+
tabulation=[[0 for _ in 1:length(choice)] for _ in 1:num+1]
30+
31+
#initialize
32+
#when the remain value happens to be η
33+
#one solution is found
34+
for i in 1:length(choice)
35+
36+
tabulation[1][i]=1
37+
38+
end
39+
40+
#since we initialize the null case
41+
#the outerloop starts from 2
42+
#i actually refers to i-1
43+
for i in 2:num+1
44+
45+
for j in 1:length(choice)
46+
47+
#annoying part of julia
48+
#if index starts at zero
49+
#will be a lot easier
50+
if i-choice[j]>=1
51+
52+
#the case where at least one coin of η value is included
53+
#we just need the computation where η is deducted
54+
include=tabulation[i-choice[j]][j]
55+
56+
else
57+
58+
include=0
59+
60+
end
61+
62+
#the case where coin of η value is excluded in the solutions
63+
if j>=2
64+
65+
exclude=tabulation[i][j-1]
66+
67+
else
68+
69+
exclude=0
70+
71+
end
72+
73+
#two sub problems merge into a big one
74+
tabulation[i][j]=exclude+include
75+
76+
end
77+
78+
end
79+
80+
#get the final answer
81+
return tabulation[num+1][length(choice)]
82+
83+
end
84+
85+
86+
# In[3]:
87+
88+
89+
coin_change(10,[1,2,5])

coin change dynamic programming.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
# coding: utf-8
3+
4+
# In[1]:
5+
6+
7+
#coin change is a straight forward dynamic programming problem
8+
#you are given one note and a set of coins of different values
9+
#assuming you have infinite supply of coins of different values
10+
#we need to compute different ways of making change
11+
#the order of the coins doesnt matter in this case
12+
#more details can be found in the following link
13+
# https://www.geeksforgeeks.org/coin-change-dp-7/
14+
#the script can also be done in recursion
15+
# https://github.com/je-suis-tm/recursion-and-dynamic-programming/blob/master/coin%20change%20recursion.py
16+
17+
18+
# In[2]:
19+
20+
21+
#to solve this problem via tabulation
22+
#we divide one big problem into two sub problems
23+
#the case where coin of η value is excluded in the solutions
24+
#and the case where at least one coin of η value is included
25+
def coin_change(num,choice):
26+
27+
#create matrix (num+1)*leng(choice)
28+
#the raison d'être is the computation starts from 0 to num
29+
#0 is when num is perfectly substituted by coins
30+
tabulation=[[0 for _ in range(len(choice))] for _ in range(num+1)]
31+
32+
#initialize
33+
#when the remain value happens to be η
34+
#one solution is found
35+
for i in range(len(choice)):
36+
37+
tabulation[0][i]=1
38+
39+
#since we initialize the null case
40+
#the outerloop starts from 1
41+
for i in range(1,num+1):
42+
43+
for j in range(len(choice)):
44+
45+
if i-choice[j]>=0:
46+
47+
#the case where at least one coin of η value is included
48+
#we just need the computation where η is deducted
49+
include=tabulation[i-choice[j]][j]
50+
51+
else:
52+
53+
include=0
54+
55+
#the case where coin of η value is excluded in the solutions
56+
if j>=1:
57+
58+
exclude=tabulation[i][j-1]
59+
60+
else:
61+
62+
exclude=0
63+
64+
#two sub problems merge into a big one
65+
tabulation[i][j]=exclude+include
66+
67+
#get the final answer
68+
return tabulation[num][len(choice)-1]
69+
70+
71+
# In[3]:
72+
73+
74+
coin_change(10,[1,2,3])
75+

coin change recursion.jl

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# coding: utf-8
2+
3+
4+
# In[1]:
5+
6+
#coin change is a straight forward dynamic programming problem
7+
#you are given one note and a set of coins of different values
8+
#assuming you have infinite supply of coins of different values
9+
#we need to compute different ways of making change
10+
#the order of the coins doesnt matter in this case
11+
#more details can be found in the following link
12+
# https://www.geeksforgeeks.org/coin-change-dp-7/
13+
#the script can also be done in dynamic programming
14+
# https://github.com/je-suis-tm/recursion-and-dynamic-programming/blob/master/coin%20change%20dynamic%20programming.jl
15+
16+
17+
# In[2]:
18+
19+
20+
#to solve this problem via recursion
21+
#we divide one big problem into two sub problems
22+
#the case where coin of η value is excluded in the solutions
23+
#and the case where at least one coin of η value is included
24+
function coin_change(num,choice)
25+
26+
#base case
27+
if num==0
28+
29+
return 1
30+
31+
end
32+
33+
#prevent stack overflow
34+
if num<0
35+
36+
return 0
37+
38+
end
39+
40+
output=0
41+
42+
#iterate through cases of exclusion
43+
for i in 1:length(choice)
44+
45+
#case of inclusion
46+
include=coin_change(num-choice[i],choice)
47+
exclude=0
48+
49+
#case of exclusion
50+
if i>=2
51+
52+
exclude=coin_change(num,choice[1:(i-1)])
53+
54+
end
55+
56+
#two sub problems merge into a big one
57+
output=include+exclude
58+
59+
end
60+
61+
return output
62+
63+
end
64+
65+
66+
# In[3]:
67+
68+
69+
coin_change(10,[1,2,3])

coin change recursion.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
# coding: utf-8
3+
4+
# In[1]:
5+
6+
7+
#coin change is a straight forward dynamic programming problem
8+
#you are given one note and a set of coins of different values
9+
#assuming you have infinite supply of coins of different values
10+
#we need to compute different ways of making change
11+
#the order of the coins doesnt matter in this case
12+
#more details can be found in the following link
13+
# https://www.geeksforgeeks.org/coin-change-dp-7/
14+
#the script can also be done in dynamic programming
15+
# https://github.com/je-suis-tm/recursion-and-dynamic-programming/blob/master/coin%20change%20dynamic%20programming.py
16+
17+
18+
# In[2]:
19+
20+
21+
#to solve this problem via recursion
22+
#we divide one big problem into two sub problems
23+
#the case where coin of η value is excluded in the solutions
24+
#and the case where at least one coin of η value is included
25+
def coin_change(num,choice):
26+
27+
#base case
28+
if num==0:
29+
30+
return 1
31+
32+
#prevent stack overflow
33+
if num<0:
34+
35+
return 0
36+
37+
output=0
38+
39+
#iterate through cases of exclusion
40+
for i in range(len(choice)):
41+
42+
#case of inclusion
43+
include=coin_change(num-choice[i],choice)
44+
exclude=0
45+
46+
#case of exclusion
47+
if i>=1:
48+
49+
exclude=coin_change(num,choice[:i])
50+
51+
#two sub problems merge into a big one
52+
output=include+exclude
53+
54+
return output
55+
56+
57+
# In[3]:
58+
59+
60+
coin_change(4,[1,2,3])
61+

0 commit comments

Comments
 (0)