Fork me on GitHub

Bdferr Strings and Lists

by Brendan Ferreri-Hanberry

31 Jan 2014

I am including a few exercises I did in the strings chapter beyond the ones we were required to do, although not the optional ones you mentioned (I did not start early enough and now I am almost out of time). To begin with, here is #2:

Here is its output:

And here is #3:

Here is the output from #3:

7. Am I allowed to copy the code which was already in the textbook as the answer to this problem? It was surprisingly concise and I don't see how I could improve upon it, except to try different words with the "testEqual" function. All the tests yielded "Pass".

`

  1. This next one is again the code from the text, which finally explained to me how to do this type of thing after a long period of confusion. All the tests yielded "Pass", of course.

  1. Here is exercise #12 for the strings chapter; it does not quite get unlimited instances of a substring out of a string, but I did figure out how to take two instances out, which was something, at least.

The output was "ba", "bile", and " palindrome" (I had not worked out anything to remove spaces).

Here are the exercises from the lists chapter:

  1. Here is the code provided by the text, slightly modified:

The output is of course 4.

Here is #8:

The output is 16.

Here is #9:

The output from #9 was -15.

14: I don't see what this has to do with the split and join methods, although if I were to try to replace occurences of a particular word rather than a character, I would use the "split" function. It would be easier, though, to make it replace particular strings rather than words. In this version I am making it replace particular characters, which was also exercise #8 in the strings chapter.

The output is like so:

Here I am attempting to replace strings, but right now it does not allow more than two instances of the substring. It also does not use "split" or "join;" my experiments were those have not been successful so far. I am running out of time though, so here is what I have now.

The output is like so:

Brendan is an MSIS student from Virginia. In his free time, he writes articles about drug policy at ssdp.org/about/blog-contributors/brendan-ferreri-hanberry/ and at the-libertarian.co.uk, where he serves as an editor. Find Brendan Ferreri-Hanberry on Twitter, Github, and on the web.
comments powered by Disqus