-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_030_06.py
More file actions
263 lines (233 loc) · 11.9 KB
/
Copy pathproblem_030_06.py
File metadata and controls
263 lines (233 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
##In England the currency is made up of pound, £, and pence, p,
##and there are eight coins in general circulation:
##
##1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
##It is possible to make £2 in the following way:
##
##1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
##How many different ways can £2 be made using any number of coins?
import numpy as np
import time
import functools
import math
import itertools
valmax = 5
COINVALUE = {"1p":1, "2p":2, "5p":5, "10p": 10,
"20p":20, "50p":50, "1P":100, "2P":200}
potcomb = set()
####k = iterator.COINVALUE.keys()
##b = k
##
##a, b, c, d, e, f, g, h = "1p", "2p", "5p", "10p","20p", "50p", "1P", "2P"
####for i in range(0,len(vals)):
##print(a,b,c,d,e,f,g,h)
def testb(valmax):
a, b, c, d, e, f, g, h = "1p", "2p", "5p", "10p","20p", "50p", "1P", "2P"
print(a,b,c,d,e,f,g,h)
for n1pi in range(0, int(valmax/COINVALUE[a])+1):
for n2pi in range(0, int(valmax/COINVALUE[b])+1):
for n5pi in range(0, int(valmax/COINVALUE[c])+1):
for n10pi in range(0, int(valmax/COINVALUE[d])+1):
for n20pi in range(0, int(valmax/COINVALUE[e])+1):
for n50pi in range(0, int(valmax/COINVALUE[f])+1):
for n1Pi in range(0, int(valmax/COINVALUE[g])+1):
for n2Pi in range(0, int(valmax/COINVALUE[h])+1):
newvala = n1pi * COINVALUE[a]
newvalb = n2pi * COINVALUE[b]
newvalc = n5pi * COINVALUE[c]
newvald = n10pi * COINVALUE[d]
newvale = n20pi * COINVALUE[e]
newvalf = n50pi * COINVALUE[f]
newvalg = n1Pi * COINVALUE[g]
newvalh = n2Pi * COINVALUE[h]
newval = newvala +newvalb+newvalc+newvald+newvale+newvalf+newvalg+newvalh
combination = (str(n1pi) +" 1p", str(n2pi) + " 2p", str(n5pi) +" 5p",
str(n10pi) +" 10p", str(n20pi) +" 20p", str(n50pi) +" 50p",
str(n1Pi) +" 1P", str(n2Pi) +" 2P")
## print(newval, combination)
if int(newval) == valmax:
## print("hit found:")
## print(combination)
## input()
potcomb.add(combination)
## print(potcomb)
print(len(potcomb))
def lh(valmax, current_value, coin, seq):
for n in range(0, int(valmax/COINVALUE[coin])+1):
coin_number = n
newvalue, combination = ch(current_value, coin, coin_number, seq)
## if new_val == target:
## if lastcoin:
## end
##
## else:
## next lh
#### elif new_val < target:
#### next lh
#### elif new_val > target:
#### break
##
## if n == 0:
## return 0
## else:
## return f((n-1))
##
## if len(lc) = 1:
## return a
## else:
## return b
## for i in range(0, len(lc)):
def ch(current_value, coin, coin_number, seq):
print("coin =", coin, "coin_number =", coin_number, "seq =",seq,"update\t", end = "\n")
cn = coin_number
valadd = cn * COINVALUE[coin]
new_val = current_value + valadd
print("\tcurrent_value= " , current_value ,"\tvaladd", valadd, "\tnewval =", new_val,"newseq = ", str(seq + str(cn) + " "+coin))
return new_val, str(seq+"-" + str(cn) + " "+coin)
def testc(valmax):
lc = ("1p", "2p", "5p", "10p","20p", "50p", "1P", "2P")
a, b, c, d, e, f, g, h = [x for x in lc]
print(a,b,c,d,e,f,g,h)
val = 0
count = 0
seq =""
## lh(valmax,val,lc[0], seq)
## while len(lc) > 0:
## x = pop(lc[0])
## newvalue,seq = lh(valmax, val, x,seq)
nv = 0
for n1pi in range(0, int(valmax/COINVALUE[a])+1):
val = 0
seq =""
coin_number = n1pi
coin = a
nv, seq = ch(val,coin, coin_number, seq)
val = nv
if int(val) < int(valmax + 1):
for n2pi in range(0, int(valmax/COINVALUE[b])+1):
coin_number = n2pi
coin = b
nv, seq = ch(val,coin, coin_number, seq)
val = nv
if int(val) < int(valmax + 1):
for n5pi in range(0, int(valmax/COINVALUE[c])+1):
coin_number = n5pi
coin = c
nv, seq = ch(val,coin, coin_number, seq)
val = nv
if int(val) < int(valmax + 1):
for n10pi in range(0, int(valmax/COINVALUE[d])+1):
coin_number = n10pi
coin = d
nv, seq = ch(val,coin, coin_number, seq)
val = nv
if int(val) < int(valmax + 1):
for n20pi in range(0, int(valmax/COINVALUE[e])+1):
coin_number = n20pi
coin = e
nv, seq = ch(val,coin, coin_number, seq)
val = nv
if int(val) < int(valmax + 1):
for n50pi in range(0, int(valmax/COINVALUE[f])+1):
coin_number = n50pi
coin = f
nv, seq = ch(val,coin, coin_number, seq)
val = nv
if int(val) < int(valmax + 1):
for n1Pi in range(0, int(valmax/COINVALUE[g])+1):
coin_number = n1Pi
coin = g
nv, seq = ch(val,coin, coin_number, seq)
val = nv
if int(val) < int(valmax + 1):
for n2Pi in range(0, int(valmax/COINVALUE[h])+1):
coin_number = n2Pi
coin = h
nv, seq = ch(val,coin, coin_number, seq)
val = nv
if int(val) == int(valmax):
potcomb.add(seq)
print(potcomb)
print(len(potcomb))
def validator(testarr):
valarra=np.array([1,2,5,10,20,50,100,200])
check = testarr*valarra
return check.sum()
def testd(target):
lc = ("1p", "2p", "5p", "10p","20p", "50p", "1P", "2P")
a, b, c, d, e, f, g, h = [x for x in lc]
print(a,b,c,d,e,f,g,h)
valarra=np.array([1,2,5,10,20,50,100,200])
valmax = target
count = 0
for n1pi in range(0, int(valmax/COINVALUE[a])+1):
count += 1
testarr = np.zeros(8, dtype = int)
testarr[0] = n1pi
if validator(testarr) <= target:
print("printing", count, "\n",testarr)
for n2pi in range(0, int(valmax/COINVALUE[b])+1):
testarr[1] = n2pi
if validator(testarr) <= target:
for n5pi in range(0, int(valmax/COINVALUE[c])+1):
testarr[2] = n5pi
if validator(testarr) <= target:
for n10pi in range(0, int(valmax/COINVALUE[d])+1):
testarr[3] = n10pi
if validator(testarr) <= target:
for n20pi in range(0, int(valmax/COINVALUE[e])+1):
testarr[4] = n20pi
if validator(testarr) <= target:
for n50pi in range(0, int(valmax/COINVALUE[f])+1):
testarr[5] = n50pi
if validator(testarr) <= target:
for n1Pi in range(0, int(valmax/COINVALUE[g])+1):
testarr[6] = n1Pi
if validator(testarr) <= target:
for n2Pi in range(0, int(valmax/COINVALUE[h])+1):
testarr[7] = n2Pi
## print(testarr)
if validator(testarr) == target:
combination = ""
for x in range(0,len(lc)):
combination += " "+ str(testarr[x])+"x"+ str(lc[x])
print("hit\n",testarr)
print(combination)
potcomb.add(combination)
## print(potcomb)
print(len(potcomb))
def teste(target):
lc = ("1p", "2p", "5p", "10p","20p", "50p", "1P", "2P")
a, b, c, d, e, f, g, h = [x for x in lc]
print(a,b,c,d,e,f,g,h)
valarra=np.array([1,2,5,10,20,50,100,200], dtype = int)
valmax = int(target)
count = 0
testarr = np.zeros(8, dtype = int)
while validator(testarr)<(valmax+1):
for n1pi in range(0, int(valmax/COINVALUE[a])+1):
testarr[0] = n1pi
for n2pi in range(0, int(valmax/COINVALUE[b])+1):
testarr[1] = n2pi
for n5pi in range(0, int(valmax/COINVALUE[c])+1):
testarr[2] = n5pi
for n10pi in range(0, int(valmax/COINVALUE[d])+1):
testarr[3] = n10pi
for n20pi in range(0, int(valmax/COINVALUE[e])+1):
testarr[4] = n20pi
for n50pi in range(0, int(valmax/COINVALUE[f])+1):
testarr[5] = n50pi
for n1Pi in range(0, int(valmax/COINVALUE[g])+1):
testarr[6] = n1Pi
for n2Pi in range(0, int(valmax/COINVALUE[h])+1):
testarr[7] = n2Pi
if validator(testarr) == target:
combination = ""
for x in range(0,len(lc)):
combination += " "+ str(testarr[x])+"x"+ str(lc[x])
## print("hit\n",testarr)
## print(combination)
potcomb.add(combination)
## print(potcomb)
print(len(potcomb))
teste(200)