As from the previous post, I am doing the google’s python class code and if any one comes up different solutions to the googles version or mine, please post. So here is the string2.py solutions from me.
Verbing, needs to check for string over 3 and then only compare the last 3 characters for ‘ing’
# D. verbing # Given a string, if its length is at least 3, # add 'ing' to its end. # Unless it already ends in 'ing', in which case # add 'ly' instead. # If the string length is less than 3, leave it unchanged. # Return the resulting string. def verbing(s): # +++your code here+++ if len(s) < 3: return s elif (s[-3:] =='ing'): return s + 'ly' else: return s + 'ing'
Not bad, find the ‘not’ and ‘bad’, if bad after not then replace.
# E. not_bad # Given a string, find the first appearance of the # substring 'not' and 'bad'. If the 'bad' follows # the 'not', replace the whole 'not'...'bad' substring # with 'good'. # Return the resulting string. # So 'This dinner is not that bad!' yields: # This dinner is good! def not_bad(s): # +++your code here+++ notV = s.find('not') badV = s.find('bad') if (badV > notV): return s[:notV] + 'good' + s[(badV+3):] return s
Font back, here we needed the modulus function to find out the reminder of a division.
# F. front_back # Consider dividing a string into two halves. # If the length is even, the front and back halves are the same length. # If the length is odd, we'll say that the extra char goes in the front half. # e.g. 'abcde', the front half is 'abc', the back half 'de'. # Given 2 strings, a and b, return a string of the form # a-front + b-front + a-back + b-back def front_back(a, b): # +++your code here+++ aV = len(a)/2+(len(a)%2) bV = len(b)/2+(len(b)%2) return a[:aV]+b[:bV]+a[aV:]+b[bV:]