After been talking with some friends, they said about putting some of the google code from there classes online encase other people come up with any other versions. So from the first of the classes that they was talking about was Python , so here is my code for these, may be other people may be doing and come up with different code ?
For first string1.py test there is 4 functions that you have code up and in order from the source code.
Donut, as from the below you just need do a compare on the parameter in.
# A. donuts # Given an int count of a number of donuts, return a string # of the form 'Number of donuts:
', where is the number # passed in. However, if the count is 10 or more, then use the word 'many' # instead of the actual count. # So donuts(5) returns 'Number of donuts: 5' # and donuts(23) returns 'Number of donuts: many' def donuts(count): # +++your code here+++ if count >= 10: return 'Number of donuts: many' return 'Number of donuts: ' + str(count)
Both end, it is string manipulation and returning part of the string if longer enough.
# B. both_ends # Given a string s, return a string made of the first 2 # and the last 2 chars of the original string, # so 'spring' yields 'spng'. However, if the string length # is less than 2, return instead the empty string. def both_ends(s): # +++your code here+++ if len(s) < 2: return '' return s[:2] + s[-2:]
Fix start, replace the characters within the string with a * that the same as the first character, so just need to concatenate the first character with the rest of the characters with any replaced 🙂
# C. fix_start # Given a string s, return a string # where all occurences of its first char have # been changed to '*', except do not change # the first char itself. # e.g. 'babble' yields 'ba**le' # Assume that the string is length 1 or more. # Hint: s.replace(stra, strb) returns a version of string s # where all instances of stra have been replaced by strb. def fix_start(s): # +++your code here+++ return s + s[1:].replace(s,'*')
Mix up, change the first 2 characters from the two parameters around.
# D. MixUp # Given strings a and b, return a single string with a and b separated # by a space ' ', except swap the first 2 chars of each string. # e.g. # 'mix', pod' -> 'pox mid' # 'dog', 'dinner' -> 'dig donner' # Assume a and b are length 2 or more. def mix_up(a, b): # +++your code here+++ return b[:2]+a[2:]+' '+a[:2]+b[2:]