2018-03-09 HW

posted Mar 9, 2018, 5:46 AM by Konstantinovich Samuel   [ updated Mar 9, 2018, 10:29 AM ]
HW: Complete the 3 problems from class, there will be coding bat slots posted on  /intro

Goal Lists and Strings

Do Now 
On paper outline how you will write the 1st function below:
-Make a diagram of an example list/string with labeled indices.
-Make a label for a variable and point at what numbers it needs to loop through
-Draw how you think this function would create an answer, then you can write an expression to model your diagram.

1. makeSentence(L) Take a list of strings and join them into one string. Place spaces between the words. do not use the function join().
# returns:  'coding you must do'  (no space at end) 

makeSentence( []) 
#  returns ''  (empty string)

makeSentence(["wu tang","clams "]) 
#returns:  'wu tang clams '  (space at end from 'clams ') 

Classwork:  Complete 1,2 and 3. You may use IDLE, and you must come up with your own test cases. 

2. removeOuterSpaces(s) Returns a new string that is like the parameter string s, but without spaces at the beginning or end of the string. You may not use the strip,rstrip, or lstrip methods.

removeOuterSpaces("   thanks for all the fish") 
# returns 'thanks for all the fish'

removeOuterSpaces("  x x  ") 
# returns 'x x'

removeOuterSpaces("     ") 
# returns ''    #note that all of the chars are removed


3. breakSentence(s) Take a string and return a list of all of the substrings that are separated by spaces. You may not use the split method.
breakSentence('coding you must do') #returns : ["coding","you","must","do"]) 
breakSentence('the fish are here') #returns : "the","fish","are","here"]) 

For simplicity sake, 
-assume no spaces at start/end of the sentence.
-assume no spaces are next to other spaces.

We will learn the new string methods on Monday:
strip() / rstrip / lstrip