Return Values and String Manipulation

Summary (Notes)

Return Values

return <expression>

Character Representation

String Operations

Stringercises

In a file called stringercises.py, implement the following functions. For each function, your main function should demonstrate that the function works on a variety of inputs. Your tests should include both standard and corner cases.

  1. indent(n): returns a string consisting of n spaces.
  2. is_true(s): returns True iff s is the string true with any sort of case and with any amount of whitespace on either end of the string.
  3. contains_rep(s, t): returns True iff the string s contains t or some non-zero replication of t, e.g., t, tt, ttt.
  4. shift_char(ch, n): returns a new lowercase character that is n characters ahead of ch in the alphabet. For example shift_char('c', 5) produces 'h'. This addition operation should be performed modulo 26, i.e., the calculation should wrap from z back to a. For example shift_char('y', 3) produces 'b'. Finally, negative values of n should result in “subtracting” from ch rather than adding.
  5. average_ch(s): returns the “average” character found in s. This character corresponds to the average of the character values of the characters found in s.
  6. mixed_case(s): returns a string that is the result of mixing the case of s. To mix the case of a string, we make the first letter of s lowercase, the second uppercase, the third lowercase and so forth. Non-letter characters are not affected although they count with respect to the alternating case. For example, mixed_case('hello world!') returns 'hElLo wOrLd!
  7. occurrences(s, t): returns the number of times the substring t occurs in s. For example, occurrences('mississippi', 'ss') returns 2 and occurrences('aaaaa', 'aa') returns 4.
  1. intersperse(s, t): returns a new string that is the result of interspersing t among the letters of s. For example intersperse('hello world', '-') returns h-e-l-l-o- -w-o-r-l-d. Note that there is no dash after the d!
  2. swap_at(s, n): returns a string string that is the result of swapping the substrings of s at character range (0-(n-1)] to (n-len(s)]. For example, swap_at('hello world!', 5) returns world!hello (note the space in front of the w).
  3. evaluate(s): returns an integer that is the result of evaluating the arithmetic expression denoted by s. Assume that s is a string only containing digits and the characters + and -.