elz: (ada-tubes)
elz ([personal profile] elz) wrote in [community profile] intro_to_cs2009-11-16 09:41 pm
Entry tags:

Problem Set 2

Post and discuss your answers in the comments! (And yay for everybody who posted problem set 1 answers!)
yhlee: Alto clef and whole note (middle C). (Default)

Problem 4

[personal profile] yhlee 2009-11-20 02:05 am (UTC)(link)
Very similar to problem 3. I didn't bother with the other cases they suggested trying, but it would be simple to make the changes.

packages = (6,9,20)	 # variable that contains package sizes

# helper function #1
def consecutiveValues(n1, n2, n3, n4, n5, n6):
	value = (n1 + 1 == n2) and (n2 + 1 == n3) and (n3 + 1 == n4) and (n4 + 1 == n5) and (n5 + 1 == n6)
	return value
	
# helper function #2
def nuggetsTest(n):
	answer = False
	for a in range(0,34):
		for b in range(0,34):
			for c in range(0,34):
				if (a*packages[0] + b*packages[1] + c*packages[2]) == n:
					return True
	return answer

def mcnuggets_search():
	n = 1
	
	# Stores successful values of n.
	successful_n = [0,0,0,0,0,0]
	
	bestSoFar = [0]	# variable that keeps track of largest
				# number of McNuggets that cannot be
				# bought in exact quantity

	while consecutiveValues(successful_n[-6], successful_n[-5], successful_n[-4], successful_n[-3], successful_n[-2], successful_n[-1]) == False and n < 200:
		if nuggetsTest(n) == True:
			successful_n = successful_n + [n]
			print n
		else:
			bestSoFar = bestSoFar + [n]
		n = n + 1
		
	print "Given package sizes " + str(packages[0]) + ", " + str(packages[1]) + ", and " + str(packages[2]) + ", the largest number of McNuggets that cannot be bought in exact quantity is: " + str(bestSoFar[-1]) + "."

nPtLQmypptMQSGzKShM

(Anonymous) 2012-01-09 04:20 am (UTC)(link)
Sharp tihnikng! Thanks for the answer.