Saturday, February 7, 2009

Week 2

Note to self: "Read the assigned readings more thoroughly, pay attention to a few obscure things from the readings, and be prepared to answer rather vague questions over said readings for the quizzes." I'm quickly realizing that the daily quizzes are going to be my biggest problem in the course because the questions tend to be very vague.

On another not
e, string indexing and capturing a substring is rather interesting in Python.

Substrings can be captured in a manner that probably seems normal to most people.
The indexing is an inclusive/exclusive index (the beginning index is included, but the ending index is not).

        s = "abCbA"
self.assert_(s[1:4] == "bCb")
self.assert_(s[1:4] is not "bCb")
self.assert_(s[1: ] == "bCbA")
self.assert_(s[1: ] is not "bCbA")
self.assert_(s[ :4] == "abCb")
self.assert_(s[ :4] is not "abCb")
self.assert_(s[0:5] == s)
self.assert_(s[0:5] is s)
self.assert_(s[ : ] == s)
self.assert_(s[ : ] is s)
Here's where things are slightly different. Python allows you to specify a step size in your indexing.

        s = "abCbA"
self.assert_(s[1:4: 2] == "bb")
self.assert_(s[0:5: 2] == "aCA")
self.assert_(s[ : : 2] == "aCA")
Stranger still, Python allows negative indexing. The strings do not, however, function as a circularly linked list. For this example an index of -7 as an ending index would be invalid.

        s = "abCbA"
self.assert_(s[-1] is "A")
self.assert_(s[-4] is "b")
self.assert_(s[-5] is "a")
# self.assert_(s[-6] is "") // out of range

s = "abCbA"
self.assert_(s[-2:-5:-2] == "bb")
self.assert_(s[-1:-6:-2] == "ACa")
self.assert_(s[ : :-2] == "ACa")

No comments:

Post a Comment