Hannah's Chapter 6 Exercises

by Hannah Wang

07 Feb 2016

Exercise 1

For this exercise, you just had to take the code here and adapt it to go through the string from end to beginning, rather than beginning to end. This required starting the index at the end, changing the “while” loop to work while index>=0, and have it traverse the string backwards.

Exercise 3

Here, you had to take the code here and turn it into a generalized function that takes two arguments. My two variables were ‘s’ for ‘string’ and ‘l’ for ‘letter’. I essentially copied the original code and replaced ‘word’ with ‘s’ and the letter ‘a’ with the generalized ‘l’. I realized that I also had to add another variable for the ‘for’ loop, to stand in for ‘letter’, so I used ‘ltr’.

Exercise 4

At first, I wasn’t exactly sure how to implement this string method. After some trial and error, I realized that I just had to define ‘str’ and ‘sub’ as the word and the letter, respectively, and that the ‘start’ and ‘end’ arguments were unnecessary for this implementation.

Exercise 5

For this exercise, I used ‘find’ to find the colon in the string, which I defined as ‘x’. I was then able to use x as an index number to do string slicing. However, the string slice had to start 2 positions after the colon, so I used ‘x+2’ to start the slice, and had it go to the end of the string.

Hannah Wang is a second semester MSIS student at SILS. Find Hannah Wang on Twitter, Github, and on the web.