This assignment is designed to build your competence with basic string operations. We are going to "tie one arm behind our backs" by not making use of any of the Python string methods, so as to focus on the basic operations.
In addition, this is the first assignment in which you are accountable for program style. You should observe all the style rules in Coding Standards through Chapter 3.
Define and test three functions on strings:
ends function takes a string as argument; if the string has two or more characters, it returns a string consisting of the first and last character of the given string; otherwise, it returns the given string.
>>> ends("ab") 'ab' >>> ends("abc") 'ac' >>> ends("a long sentence") 'ae' >>> ends("") '' >>> ends("*") '*'
butends function takes a string argument; if the string has two or more characters, it returns a string consisting of all but the first and last character of the string; otherwise, it returns the given string.
>>> butends("abcde") 'bcd' >>> butends("abc") 'b' >>> butends("a long sentence") ' long sentenc' >>> butends("") '' >>> butends("a") 'a'
repl function takes three arguments:
oldis a single character;
newis a string of 0 or more characters;
sis any string.
It returns a new string formed by replacing every occurrence of
>>> repl('a', 'A', 'fast faces react snappily') 'fAst fAces reAct snAppily' >>> repl('*', '+++', 'a*b = c*d') 'a+++b = c+++d' >>> repl(' ', '\n', 'Practice every day.') 'Practice\nevery\nday.' >>> print(repl(' ', '\n', 'Practice every day.')) Practice every day. >>> repl(",", ":", "a,b,cde,fghi") 'a:b:cde:fghi'
You may not use any string methods. (In particular, you may not use the
replace method for the
repl function in problem 3—that would make it much too easy!) String methods include those described in Table 3.2 on page 90 of the textbook, but are not limited to those in the table. In general, a string followed by a dot (
.), method name, and method arguments make a string method call; so don't do anything in this form:
Follow the Coding Standards for chapters 1–3.
You may use the string
len function, and the string operations
*, indexing, slicing, and
== for comparing strings or characters. You will probably need to use most of these, but not all. Remember that in indexing and slicing, a negative index specifies a position relative to the end of the string.
repl function, use the accumulator pattern to build up the new string.
repl function, if
old does not occur in
s, then the returned value is simply a copy of
s; but this case does not require any special treatment: it should emerge automatically from the general solution.
>>> repl("X", "Y", "abc") 'abc'
Turn this assignment in through Oncourse Assignments. by attaching two files and writing in "Submitted text."
Attach a copy of your Python source file (the file name must end in ".py").
Run the program in a fresh IDLE Python Shell (call the main function with appropriate arguments) for the following test cases.
>>> # Problem 1 >>> ends("x") >>> ends("horses") >>> ends("A long way.") >>> # Problem 2 >>> butends("x") >>> butends("horses") >>> butends("Right the first time") >>> # Problem 3 >>> repl('s', 'th', 'Mississippi') >>> repl(' ', '---', 'a bee in her bonnet') # replace space with ---
Save the Python shell in a file name ending in ".txt". Attach this file as your test results.
In the "Submitted text" box, describe any problems you encountered in developing the program, and explain how you solved them. If you had no problems, just say "No problems."
(CA = Competency Assessment)
STRING CA SUBTOTAL: 9 points, 6 to "pass". This is not recorded as a separate score, and it is not "all or nothing." If you earn significantly less than 6, you may get a chance to resubmit; but do try to get it right the first time!
TOTAL: 12 points