diff options
author | RossTheRoss <mstrapp@protonmail.com> | 2020-01-30 16:55:04 -0600 |
---|---|---|
committer | RossTheRoss <mstrapp@protonmail.com> | 2020-01-30 16:55:04 -0600 |
commit | 175721a63b426355274fa9e8063f762020ab8362 (patch) | |
tree | cf2c1b33233d660c9f50de5e659b9343bb264984 /OLD/csci1913/Python/lab2_strap012.py | |
parent | Make Python thing in Python (diff) | |
download | homework-175721a63b426355274fa9e8063f762020ab8362.tar homework-175721a63b426355274fa9e8063f762020ab8362.tar.gz homework-175721a63b426355274fa9e8063f762020ab8362.tar.bz2 homework-175721a63b426355274fa9e8063f762020ab8362.tar.lz homework-175721a63b426355274fa9e8063f762020ab8362.tar.xz homework-175721a63b426355274fa9e8063f762020ab8362.tar.zst homework-175721a63b426355274fa9e8063f762020ab8362.zip |
R E A R R A N G E
Diffstat (limited to 'OLD/csci1913/Python/lab2_strap012.py')
-rw-r--r-- | OLD/csci1913/Python/lab2_strap012.py | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/OLD/csci1913/Python/lab2_strap012.py b/OLD/csci1913/Python/lab2_strap012.py new file mode 100644 index 0000000..511e6bf --- /dev/null +++ b/OLD/csci1913/Python/lab2_strap012.py @@ -0,0 +1,137 @@ +class Zillion: + def __init__(self,digits): + self.digits = digits + self.List = [] + self.toList() + + def toList(self): + badBoolean=False #Kludge + if len(self.digits) == 0 : + raise RuntimeError #Cannot make a list of nothing + for n in range (0,len(self.digits)): + if self.digits[n] is not ',': + if self.digits[n] is not ' ': + try: + self.List+=[int(self.digits[n])] + badBoolean=True + except ValueError: #Non-comma character in string + raise RuntimeError + if badBoolean is False: #No numbers in string + raise RuntimeError + else: + return self.List + + + def increment(self): + length = len(self.List) - 1 + newList = self.List + newList[length] += 1 + if newList[length]>=10: #Loop to increment all 10s to 0s with carry + for n in range (0,length): + newList[length-n]=0 + newList[length-n-1]+=1 + + def isZero(self): + for p in range (0,len(self.List)-1): + if self.List[p] is not 0: + return False + return True + + def toString(self): + string='' + for q in range (0,len(self.List)): + string+=str(self.List[q]) #Make string by concatenation + return string + + + +# +# TESTS. Test the class Zillion for CSci 1913 Lab 2. +# +# James Moen +# 18 Sep 17 +# +# Every test is followed by a comment which shows what must be printed if your +# code works correctly. It also shows how many points the test is worth. +# + +try: + z = Zillion('') +except RuntimeError: + print('Empty string') + +# It must print 'Empty string' without apostrophes. 2 points. + +try: + z = Zillion(' , ') +except RuntimeError: + print('No digits in the string') + +# It must print 'No digits in the string' without apostrophes. 2 points. + +try: + z = Zillion('1+0') +except RuntimeError: + print('Non-digit in the string') + +# It must print 'Non-digit in the string' without apostrophes. 2 points. + +try: + z = Zillion('0') +except RuntimeError: + print('This must not be printed') + +# It must print nothing. 2 points. + +print(z.isZero()) # It must print True. 2 points. + +try: + z = Zillion('000000000') +except RuntimeError: + print('This must not be printed') + +# It must print nothing. 2 points. + +print(z.isZero()) # It must print True. 2 points. + +try: + z = Zillion('000 000 000') +except RuntimeError: + print('This must not be printed') + +# It must print nothing. 2 points. + +print(z.isZero()) # It must print True. 2 points. + +try: + z = Zillion('997') +except RuntimeError: + print('This must not be printed') + +# It must print nothing. 2 points. + +print(z.isZero()) # It must print False. 2 points. + +print(z.toString()) # It must print 997. 2 points. + +z.increment() + +print(z.toString()) # It must print 998. 2 points. + +z.increment() + +print(z.toString()) # It must print 999. 2 points. + +z.increment() + +print(z.toString()) # It must print 1000. 2 points. + +try: + z = Zillion('0 9,9 9') +except: + print('This must not be printed') + +# It must print nothing. 3 points. + +z.increment() +print(z.toString()) # It must print 1000. 2 points. |