![]() |
![]() |
|
home—info—labs—exams—hws
—D2L—MediaSamples/—breeze (snow day)—tutor/PIs
We'll start by discussion exam01.
Last time we talked about boolean expressions
(and how operators like
Although JES treats booleans as 0 or 1
they're not — actual python uses
For the Codingbat.com > Python > Warmup-1 > sum_double, we talked about four ways to compute the answer:
def sum_double(a, b): if (a == b): return 2*(a+b) else: return a+b |
def sum_double(a, b): if (a != b): return a+b else: return 2*(a+b) |
def sum_double(a, b): if (a!=b): answer = a+b else: answer = 2*(a+b) return answer |
def sum_double(a, b): sum = a+b if (a!=b): return sum else: return 2*sum |
Task: We will work through the following codingbat problems:
# substring : string, int, int → string # Given some text, a start-index, and a stop-index, # return the characters of `txt` from `start` up until (but not including) `stop`. # Indices are 0-based. # Examples: substring( "radford", 3, 6) = "for" # substring( "radford", 0, 3) = "rad" # def substring(txt, start, stop): if (0 <= start and start <= stop and stop <= len(txt)): return txt[start:stop] else: raise IndexError("substring: bounds out of range: " + str(start) + "," + str(stop) + " for '" + txt + "'") |
1However,
2Even if it's
home—info—labs—exams—hws
—D2L—MediaSamples/—breeze (snow day)—tutor/PIs
©2014, Ian Barland, Radford University Last modified 2014.Apr.10 (Thu) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |