''' Created on Nov 13, 2012 @author: lorensen ''' """*********************** PROBLEM 1 *********************************""" '''Part B ''' import math import string def sphereVolume( radius ): return (4/3)*math.pi*radius**3 '''Part C ''' upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" lowerCase = "abcdefghijklmnopqrstuvwxyz" def reverseCase( word ): toBePrinted = "" for eachLetter in word: if eachLetter in upperCase: toBePrinted = toBePrinted + eachLetter.lower() elif eachLetter in lowerCase: toBePrinted = toBePrinted + eachLetter.upper() return toBePrinted """*********************** PROBLEM 2 *********************************""" '''Part B ''' def is_deficient( num ): result = False # Default value to be returned sumDiv = [] #A list variable to hold the sum of divisors for n in range( 1 , num ): #Ex. range(4) gives [1,2,3]. Alternative range( 1, num/2 ) is more efficient! if num % n == 0: # If there are no remainder after dividing n with num... sumDiv.append( n ) #...add n to the sum of divisors in the list # sumDiv = [1, 2] if sum( sumDiv ) < num: # If the sum of all the divisors are less than the number result = True #, then we have a deficient number return result def is_deficient( num ): # result = False # Default value to be returned sumDiv = [] #A list variable to hold the sum of divisors for n in range( 1 , num ): #Ex. range(4) gives [1,2,3]. Alternative range( 1, num/2 ) is more efficient! if num % n == 0: # If there are no remainder after dividing n with num... sumDiv.append( n ) #...add n to the sum of divisors in the list # sumDiv = [1, 2] # if sum( sumDiv ) < num: # If the sum of all the divisors are less than the number # result = True #, then we have a deficient number return sum( sumDiv ) < num """ Alternative compressed solution:""" def is_deficient( num ): return sum( [ n for n in range( 1 , num ) if num % n == 0 ] ) < num '''Part C ''' def deficient_count( start, end ): count = [] # default list counter to hold the number of deficent numbers for num in range(start, end+1): #Ex. range(1,4+1) gives [1,2,3,4] to loop over if is_deficient(num): # The method call is_deficient(num) returns either True or False count.append( 1 ) # increase the counter by 1 return sum( count ) """ Alternative compressed solution: """ def deficient_count( start, end ): return sum( [ 1 for num in range(start, end+1) if is_deficient(num) ] ) """*********************** PROBLEM 3 *********************************""" '''Part A ''' def cutStart(word, remove): return word[remove:] '''Part B ''' def cutEnd(word, remove): return word[:len(word)-remove] """ ALTERNATIVE SOLUTION """ def cutEnd(word, remove): return word[:-remove ] #[:remove*(-1)] #Using the negative index '''Part C ''' def greedGetsMe( crewList, blackNum, itsNewest ): result = [] for section in crewList: #section contains "MOREDV" fist time around if itsNewest: #Since itsNewest is a bool this is unnecessary: itsNewest==True result.append( cutEnd(section, blackNum) ) else: result.append( cutStart(section, blackNum) ) return result """*********************** PROBLEM 4 *********************************""" '''Part A ''' def email_isCorrect( suspect ): countSpace = 0 #variable to hold number of space countAt = 0 #how many @ signs are there? ''' Collecting data: Running through the suspect and counting @ and " " spaces: ''' for char in suspect: if char == '@': countAt += 1 #countAt = countAt + 1 if char == " ": #checks for space countSpace += 1 #countSpace = countSpace + 1 """Looking at the data: only 1 @ allowed and no " " spaces.""" if countAt == 1 and countSpace == 0: return True else: return False """ ALTERNATIVE SOLUTIONS """ def email_isCorrect( suspect ): return suspect.count("@")== 1 and suspect.count(" ")==0 '''Part B ''' def pass_isOK( passAttempt ): result = 0 capital = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" number = "0123456789" containsCap = False containsNum = False if len( passAttempt ) <= 5: result = result + 1 #Rule 1 tested here for character in passAttempt: # Collecting info for test of rule 2,3 if character in capital: containsCap = True if character in number: containsNum = True if containsCap == False: result = result + 2 #Rule 2 tested here if containsNum == False: result = result + 5 #Rule 3 tested here return result """ ALTERNATIVE SOLUTIONS """ def pass_isOK( passAttempt ): score = 0 '''Rule 1 tested here. Length of password''' if len(passAttempt)<6: score = score + 1 '''Rule 2 tested here. Convert all characters to lower case and if they are the same, then there were no capital characters.''' if passAttempt == passAttempt.lower() score = score + 2 '''Rule 3 tested here. Run through [0,1,2,3,4,5,6,7,8,9] and see if they are in the password. A negative approached is used here: Is is assumed that the password is wrong and 5 is added to the score. ONLY if a number is found is the 5 removed again. ''' score = score + 5 #Assuming passsAttempt is wrong! for number in range(10): if str(number) in passAttempt: score = score - 5 #Correcting the initial assumption and removing the 5. break #This takes us out of the for loop immediately, first time we find a number. return score '''Part C ''' def accountCreation( mail, password ): if email_isCorrect( mail ) and pass_isOK(password)==0: return "OK" if not email_isCorrect(mail): return "Email error" if pass_isOK( password )==1: return "Email OK, password too short" if pass_isOK( password )==2: return "Email OK, password missing capital letter!" if pass_isOK( password )==5: return "Email OK, password missing a number!" if pass_isOK( password )==1+2: return "Email OK, password too short and missing capital" if pass_isOK( password )==1+5: return "Email OK, password too short and missing number" if pass_isOK( password )==2+5: return "Email OK, password is missing capital letter and a number" if pass_isOK( password )==1+2+5: return "Email OK, password is too short and missing capital letter and a number" """The logic of this function starts with all the bad cases and ends with the correct case.""" def accountCreation( mail, password ): result = "" if not email_isCorrect(mail): #Case: The mail is wrong. Password doesn't matter result = "Email error" else: #Case: Mail is right. Password is wrong: passERROR = pass_isOK( password ) if passERROR==1: result = "Email OK, password too short" elif passERROR==2: result = "Email OK, password missing capital letter" elif passERROR==5: result = "Email OK, password missing a number" elif passERROR==1+2:result = "Email OK, password too short and missing capital" elif passERROR==1+5:result = "Email OK, password too short and missing number" elif passERROR==2+5:result = "Email OK, password is missing capital letter and a number" elif passERROR==1+2+5:result = "Email OK, password is too short and missing capital letter and a number" else # if email_isCorrect( mail ) and pass_isOK(password)==0: result = "OK" #Case: Both mail and password OK. return result """alternative solutions Part 2 B C """ def is_deficient( num ): return sum( [ x for x in range(1,num) if num % x == 0 ] ) < num def deficient_count( start, end ): return sum( [ 1 for x in range(start, end+1) if is_deficient(num)] )