elz: (ada-tubes)
[personal profile] elz posting in [community profile] intro_to_cs
Post and discuss your answers in the comments! (And yay for everybody who posted problem set 1 answers!)

#4

Date: 2009-11-20 12:39 am (UTC)
medrin: matlab code with everything but 'hold on' blurred (Default)
From: [personal profile] medrin
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.


Profile

Introduction to Computer Science

July 2010

S M T W T F S
    123
45678910
11121314151617
18192021222324
2526272829 3031

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Aug. 10th, 2025 11:18 pm
Powered by Dreamwidth Studios