@cboseak/

WholeElasticBasenji

Python 2.7

No description

fork
loading
Files
  • main.py
  • coins.txt

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.562432349094357","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"aW1wb3J0IG1hdGgKZnJvbSByYW5kb20gaW1wb3J0IHJhbmRpbnQKaW1wb3J0IHRpbWUKaW1wb3J0IHN5cywgZ2V0b3B0LCBzaGEKaW1wb3J0IGNzdgoKI2dldCBpbnB1dCBmaWxlbmFtZSBmcm9tIGNvbW1hbmQgbGluZSBpbnB1dAoKZmlsZW5hbWUgPSBzeXMuYXJndlsxXSArICIudHh0IgoKZGVmIHByaW50VXNhZ2UoKToKCXByaW50ICJVc2FnZTogIiArIHN5cy5hcmd2WzBdICsgJyAtZiA8ZmlsZW5hbWUgd2l0aCBkZW1vbmluYXRpb25zIGFuZCBjaGFuZ2UgYW1vdW50cz4nCgp0cnk6CglvcHRzLCBhcmdzID0gZ2V0b3B0LmdldG9wdChzeXMuYXJndlsxOl0sICJoZjoiLCBbXSkKZXhjZXB0IGdldG9wdC5HZXRvcHRFcnJvcjoKCXByaW50VXNhZ2UoKQoJc3lzLmV4aXQoMikKZm9yIG9wdCwgYXJnIGluIG9wdHM6CglpZiBvcHQgPT0gJy1oJzoKCQlwcmludFVzYWdlKCkKCQlzeXMuZXhpdCgpCgllbGlmIG9wdCA9PSAnLWYnOgoJCWZpbGVuYW1lID0gYXJnCgppZiBmaWxlbmFtZSA9PSAiIjoKCXByaW50ICJmaWxlIG5hbWUgaXMgcmVxdWlyZWQiCglwcmludFVzYWdlKCkKCXN5cy5leGl0KDMpCgojcmVhZCBpbnB1dCBmaWxlIGludG8gdHdvIGFycmF5cwoKZGVub21fYXJyYXlzID0gW10KY2hhbmdlX3ZhbHVlcyA9IFtdCgp3aXRoIG9wZW4oZmlsZW5hbWUpIGFzIGlucHV0czoKCWZvciBpLCBsaW5lIGluIGVudW1lcmF0ZShpbnB1dHMpOg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N0X3Jlc3VsdAogICAgICAgIHByaW50IGdvYWwKICAgICAgICBwcmludCByZXN1bHQKICAgICAgICByZXR1cm4gcmVzdWx0CgoiIiIKZGVmIGNoYW5nZXNsb3coZGVub21fYXJyYXksIGNoYW5nZV92YWx1ZSk6CiAgICByZXN1bHQxLCByZXN1bHQyID0gaGVscGVyX2NoYW5nZXNsb3coY2hhbmdlX3ZhbHVlLCBkZW5vbV9hcnJheSkKICAgIGNvaW5zID0gWzBdICogbGVuKGRlbm9tX2FycmF5KSAgCiAgICBmb3IgaSBpbiByYW5nZSgwLCBsZW4oZGVub21fYXJyYXkpKTogICAKICAgICAgICBpZiByZXN1bHQyLmhhc19rZXkoZGVub21fYXJyYXlbaV0pOiAgICAgICAKICAgICAgICAgICAgY29pbnNbaV0gPSByZXN1bHQyLmdldChkZW5vbV9hcnJheVtpXSkgICAKICAgIHJldHVybiBjb2lucwoKZGVmIGhlbHBlcl9jaGFuZ2VzbG93KGFtb3VudCwgY29pbnMpOgogICAgbnVtY29pbnMgPSAwCiAgICByZXN1bHRzID0ge30KICAgIGlmIGxlbihjb2lucykgPT0gMToKICAgICAgICBudW1jb2lucyA9ICBhbW91bnQgLyBjb2luc1swXQogICAgICAgIHJlc3VsdHNbY29pbnNbMF1dID0gbnVtY29pbnMKICAgIGVsaWYgYW1vdW50ID09IDE6CiAgICAgICAgbnVtY29pbnMgPSAxCiAgICAgICAgcmVzdWx0c1tjb2luc1swXV0gPSAxCiAgICBlbGlmIGFtb3VudCA9PSAwOiAKICAgICAgICBudW1jb2lucyA9IDAKICAgIGVsc2U6CiAgICAgICAgaWYgKGFtb3VudCAtIGNvaW5zWy0xXT49IDApOgogICAgICAgICAgICBzb2wxID0gaGVscGVyX2NoYW5nZXNsb3coYW1vdW50LWNvaW5zWy0xXSwgY29pbnMpCiAgICAgICAgICAgIHNvbDIgPSBoZWxwZXJfY2hhbmdlc2xvdyhhbW91bnQsIGNvaW5zWzotMV0pCiAgICAgICAgICAgIG51bWNvaW5zICs9IG1pbigxKyBzb2wxWzBdLCBzb2wyWzBdKQogICAgICAgICAgICBpZiAxKyBzb2wxWzBdPHNvbDJbMF06CiAgICAgICAgICAgICAgICBpZiBjb2luc1stMV0gaW4gcmVzdWx0czoKICAgICAgICAgICAgICAgICAgICByZXN1bHRzW2NvaW5zWy0xXV0gKz0gMQogICAgICAgICAgICAgICAgZWxzZTogCiAgICAgICAgICAgICAgICAgICAgcmVzdWx0c1tjb2luc1stMV1dID0gMQogICAgICAgICAgICAgICAgcmVzdWx0cyA9IHsgeDogcmVzdWx0cy5nZXQoeCwgMCkgKyBzb2wxWzFdLmdldCh4LCAwKSBmb3IgeCBpbiBzZXQocmVzdWx0cykgfCBzZXQoc29sMVsxXSkgfQogICAgICAgICAgICBlbHNlOgogICAgICAgICAgICAgICAgcmVzdWx0cyA9IHJlc3VsdHMgKyBtaW5bMV0gCiAgICAgICAgICAgICAgICByZXN1bHRzID0geyB4OiByZXN1bHRzLmdldCh4LCAwKSArIHNvbDJbMV0uZ2V0KHgsIDApIGZvciB4IGluIHNldChyZXN1bHRzKSB8IHNldChzb2wyWzFdKSB9CiAgICAgICAgZWxzZToKICAgICAgICAgICAgc29sID0gaGVscGVyX2NoYW5nZXNsb3coYW1vdW50LCBjb2luc1s6LTFdKQogICAgICAgICAgICBudW1jb2lucyArPSBzb2xbMF0KICAgICAgICAgICAgcmVzdWx0cyA9IHsgeDogcmVzdWx0cy5nZXQoeCwgMCkgKyBzb2xbMV0uZ2V0KHgsIDApIGZvciB4IGluIHNldChyZXN1bHRzKSB8IHNldChzb2xbMV0pIH0KICAgIHJldHVybiAobnVtY29pbnMsIHJlc3VsdHMpCiIiIgoKZGVmIGNoYW5nZWdyZWVkeShkZW5vbV9hcnJheSwgY2hhbmdlX3ZhbHVlKToKCXJldHVybiBbMSwxLDEsMV0KCmRlZiBjaGFuZ2VkcChkZW5vbV9hcnJheSwgY2hhbmdlX3ZhbHVlKToKCWNvaW5fdmFsdWVzID0gWzBdICogKG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hZGVyKCkKCWZvciByIGluIHJlc3VsdHNbImNoYW5nZWdyZWVkeSJdOgoJCXdyaXRlci53cml0ZXJvdyhyKQoKd2l0aCBvcGVuKCdjaGFuZ2VzbG93X3Jlc3VsdHMuY3N2JywgJ3cnKSBhcyBydF9jc3Y6CglmaWVsZG5hbWVzID0gWydjaGFuZ2VfdmFsdWUnLCAnbnVtX2NvaW5zJywgJ3J1bnRpbWUnLCAnY29pbnMnLCAnZGVub21pbmF0aW9ucyddCgl3cml0ZXIgPSBjc3YuRGljdFdyaXRlcihydF9jc3YsIGZpZWxkbmFtZXM9ZmllbGRuYW1lcykKCgl3cml0ZXIud3JpdGVoZWFkZXIoKQoJZm9yIHIgaW4gcmVzdWx0c1siY2hhbmdlc2xvdyJdOgoJCXdyaXRlci53cml0ZXJvdyhyKQoKCgoKCgo="},"asBuffer":null},"loaded":true}}
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
264
265
import math
from random import randint
import time
import sys, getopt, sha
import csv

#get input filename from command line input

filename = sys.argv[1] + ".txt"

def printUsage():
	print "Usage: " + sys.argv[0] + ' -f <filename with demoninations and change amounts>'

try:
	opts, args = getopt.getopt(sys.argv[1:], "hf:", [])
except getopt.GetoptError:
	printUsage()
	sys.exit(2)
for opt, arg in opts:
	if opt == '-h':
		printUsage()
		sys.exit()
	elif opt == '-f':
		filename = arg

if filename == "":
	print "file name is required"
	printUsage()
	sys.exit(3)

#read input file into two arrays

denom_arrays = []
change_values = []

with open(filename) as inputs:
	for i, line in enumerate(inputs):
		if i % 2 == 0:
			sub_array = line.split()
			denom_arrays.append(map(int, sub_array))
		else:
			change_values.append(int(line))


#our three functions

def changeslow(amounts, goal):
    result = [0]*len(amounts)
    test_result = [0]*len(amounts)
    result[0] = goal # set denomination 1 to goal, maximum coins possible
    # Base case 
    if goal in amounts:
        for idx, a in enumerate(amounts):
            if a == goal:
                result[idx] = 1
                return result
    else:
    #Recursive case
        possible_coins = []
        for idx, value in enumerate(amounts):
            if value <= goal:
                test_result = changeslow(amounts, goal - value)
                test_result[idx] = test_result[idx] + 1
                if sum(test_result) < sum(result):
                    result = test_result
        print goal
        print result
        return result

"""
def changeslow(denom_array, change_value):
    result1, result2 = helper_changeslow(change_value, denom_array)
    coins = [0] * len(denom_array)  
    for i in range(0, len(denom_array)):   
        if result2.has_key(denom_array[i]):       
            coins[i] = result2.get(denom_array[i])   
    return coins

def helper_changeslow(amount, coins):
    numcoins = 0
    results = {}
    if len(coins) == 1:
        numcoins =  amount / coins[0]
        results[coins[0]] = numcoins
    elif amount == 1:
        numcoins = 1
        results[coins[0]] = 1
    elif amount == 0: 
        numcoins = 0
    else:
        if (amount - coins[-1]>= 0):
            sol1 = helper_changeslow(amount-coins[-1], coins)
            sol2 = helper_changeslow(amount, coins[:-1])
            numcoins += min(1+ sol1[0], sol2[0])
            if 1+ sol1[0]<sol2[0]:
                if coins[-1] in results:
                    results[coins[-1]] += 1
                else: 
                    results[coins[-1]] = 1
                results = { x: results.get(x, 0) + sol1[1].get(x, 0) for x in set(results) | set(sol1[1]) }
            else:
                results = results + min[1] 
                results = { x: results.get(x, 0) + sol2[1].get(x, 0) for x in set(results) | set(sol2[1]) }
        else:
            sol = helper_changeslow(amount, coins[:-1])
            numcoins += sol[0]
            results = { x: results.get(x, 0) + sol[1].get(x, 0) for x in set(results) | set(sol[1]) }
    return (numcoins, results)
"""

def changegreedy(denom_array, change_value):
	return [1,1,1,1]

def changedp(denom_array, change_value):
	coin_values = [0] * (change_value + 1)
	min_counts = [0] * (change_value + 1)
	for c in range(change_value + 1):
		num_coins = c
		coin_value = 1
		temp_coins = []
		for d in denom_array:
			if d <= c:
				temp_coins.append(d)
		for t in temp_coins:
			if min_counts[c - t] + 1 < num_coins:
				num_coins = min_counts[c - t] + 1
				coin_value = t
		min_counts[c] = num_coins
		coin_values[c] = coin_value
	#figure out which coins make the minimum change
	result = [0]*len(denom_array)
	change_left = change_value
	while change_left > 0:
		coin_to_add = coin_values[change_left]
		for idx, d in enumerate(denom_array):
			if d == coin_to_add:
				result[idx] = result[idx] + 1
				break
		change_left = change_left - coin_to_add
	return result

#write results of tests to file
write_file = filename[:-4] + "change.txt"

with open(write_file, 'w') as p2_results:

	p2_results.write("Algorithm changeslow:\n")
	for idx, d in enumerate(denom_arrays):
		result = changeslow(d, change_values[idx])
		print d
		print change_values[idx]
		print result
		#write to file per assignment format requirements
		for i in d:
			p2_results.write(str(i) + ' ')
		p2_results.write('\n')
		for i in result:
			p2_results.write(str(i) + ' ')
		p2_results.write('\n')
		p2_results.write(str(sum(result)) + '\n\n')

	p2_results.write("Algorithm changegreedy:\n")
	for idx, d in enumerate(denom_arrays):
		result = changegreedy(d, change_values[idx])
		print d
		print change_values[idx]
		print result
		#write to file per assignment format requirements
		for i in d:
			p2_results.write(str(i) + ' ')
		p2_results.write('\n')
		for i in result:
			p2_results.write(str(i) + ' ')
		p2_results.write('\n')
		p2_results.write(str(sum(result)) + '\n\n')

	p2_results.write("Algorithm changedb\n")
	for idx, d in enumerate(denom_arrays):
		result = changedp(d, change_values[idx])
		print d
		print change_values[idx]
		print result
		#write to file per assignment format requirements
		for i in d:
			p2_results.write(str(i) + ' ')
		p2_results.write('\n')
		for i in result:
			p2_results.write(str(i) + ' ')
		p2_results.write('\n')
		p2_results.write(str(sum(result)) + '\n\n')


#construct V denominations array, and A array of different change values
#for project exercises 3-5

p3v = [1, 5, 10, 25, 50]
p4v1 = [1, 2, 6, 12, 24, 48, 60]
p4v2 = [1, 6, 13, 37, 150]
p5v = [1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]

p3a = []
a = 2010
while a <= 2200:
	p3a.append(a)
	a = a + 5

p4a = []
a = 2000
while a <= 2200:
	p4a.append(a)
	a = a + 1

p5a = p4a

#run 3 algorithms and write results to files for each algorithm
results = {"changeslow": [], "changegreedy": [], "changedp": []}

#problem 3 run for each value of a for each algorithm
for a in p3a:
	start_time = time.time()
	result = changedp(p3v, a)
	time_elapsed = time.time() - start_time
	results["changedp"].append({"change_value": a, "num_coins": sum(result), "runtime": time_elapsed, "coins": result, "denominations": p3v})
	start_time = time.time()
	result = changegreedy(p3v, a)
	time_elapsed = time.time() - start_time
	results["changegreedy"].append({"change_value": a, "num_coins": sum(result), "runtime": time_elapsed, "coins": result, "denominations": p3v})
	"""start_time = time.time()
	result = changeslow(p3v, a)
	time_elapsed = time.time() - start_time
	results["changeslow"].append({"change_value": a, "num_coins": sum(result), "runtime": time_elapsed, "coins": result, "denominations": p3v})
	"""
#TO DO: problems 4a, 4b, 5 

#write runtimes, change value (A) and number of coins to csv file for analyses
with open('changedp_results.csv', 'w') as rt_csv:
	fieldnames = ['change_value', 'num_coins', 'runtime', 'coins', 'denominations']
	writer = csv.DictWriter(rt_csv, fieldnames=fieldnames)

	writer.writeheader()
	for r in results["changedp"]:
		writer.writerow(r)

with open('changegreedy_results.csv', 'w') as rt_csv:
	fieldnames = ['change_value', 'num_coins', 'runtime', 'coins', 'denominations']
	writer = csv.DictWriter(rt_csv, fieldnames=fieldnames)

	writer.writeheader()
	for r in results["changegreedy"]:
		writer.writerow(r)

with open('changeslow_results.csv', 'w') as rt_csv:
	fieldnames = ['change_value', 'num_coins', 'runtime', 'coins', 'denominations']
	writer = csv.DictWriter(rt_csv, fieldnames=fieldnames)

	writer.writeheader()
	for r in results["changeslow"]:
		writer.writerow(r)