medrin: matlab code with everything but 'hold on' blurred (Default)
medrin ([personal profile] medrin) wrote in [community profile] intro_to_cs 2009-11-20 12:39 am (UTC)

#4

Here is my solution! (but I could not get the indentations to work, anyone know how?)

If anyone have any questions, just ask!


#packets = (6,9,20)
no_right = 0
n=0
highest=0
corr_pack=False
print'Hi!'


#asks for the diffrent package sizes and checks that they are suitable (error handling)
while corr_pack==False:
print('What are the three packet sizes (in rising order)?')
packets=input()
if len(packets)!=3:
print ('There must be three and only three packet sizes')
elif type(packets[0])!=int or type(packets[1])!=int or type(packets[2])!=int:
print ('All packet sizes must be given in integrers')
elif packets[0]<0 or packets[1]<0 or packets[2]<0:
print ('The packet sizes must be positive numbers')
elif packets[0]>packets[1] or packets [0]>packets[2] or packets[1]>packets[2]:
print ('The packets must be given in a rising order')
else:
corr_pack=True

while n<200 and no_right< packets[0] : #n loops all possible combinations from 1 and up to a
works=False #consecutive number of possibles the same length as the minimum package
n+=1 #works turns true as soon as a solution is found,
c=0 #a,b,c is how many packages of each size are needed
while c*packets[2]<=n and works==False: #each loop goes until the combined number of nuggets is bigger
b=0 #than the wanted number.
while c*packets[2]+b*packets[1]<=n and works==False:
a=0
while c*packets[2]+b*packets[1]+a*packets[0]<=n and works==False:#Goes though all combinations
if c*packets[2]+b*packets[1]+a*packets[0]==n: #of a,b&c till a solution is
works=True #found or all combinations are tried.
else: #starts with a and works it way out.
a+=1 #if a correct solution are found works turns True
if c*packets[2]+b*packets[1]+a*packets[0]==n:
works=True
else:
b+=1
if c*packets[2]+b*packets[1]+a*packets[0]==n:
works=True
else:
c+=1
if works==True: #if there exists a correct solution the n_right counter incerases
no_right+=1
#print n,a,b,c
else: #if there isn't, it resets to zero and that number is stored in highest.
highest=n
no_right=0
#print n,no_right
print highest #the highest non-solvable number is printed.



Post a comment in response:

This community only allows commenting by members. You may comment here if you're a member of intro_to_cs.
(will be screened if not on Access List)
(will be screened if not on Access List)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting