Return Values and String Manipulation
Summary (Notes)
Return Values
- A function can produces a value to be used in further computation, i.e., as as an expression, by returning a value.
- The return statement causes evaluation to return from the current function. The function call evaluates to the value that is returned.
return <expression>- A return statement causes execution of the function to end, so it does not make any sense to have additional statements after the return because they will not be executed.
- In fact, all function calls are actually expressions—they just happen to also act as statements as well!
- A function that does not return a value explicitly returns the unique value
Noneof typeNone.Noneis a placeholder value that stands in for “there is no value here”.
Character Representation
- In a computer, we directly represent integers using collections of bits. This is due to the electromagnetic switches that we use to store data in a computer. They can either hold a charge (1) or not hold a charge (0).
- A character is represented by an integer called its character value.
- To obtain the character value of a character, use the
ordfunction, e.g.,ord('a')returns97. - To convert a character value into a character, use the
chdfunction, e.g.chr(97)returns'a'.
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.
indent(n): returns a string consisting ofnspaces.is_true(s): returnsTrueiffsis the stringtruewith any sort of case and with any amount of whitespace on either end of the string.contains_rep(s, t): returnsTrueiff the stringscontainstor some non-zero replication oft, e.g.,t,tt,ttt.shift_char(ch, n): returns a new lowercase character that isncharacters ahead ofchin the alphabet. For exampleshift_char('c', 5)produces'h'. This addition operation should be performed modulo 26, i.e., the calculation should wrap fromzback toa. For exampleshift_char('y', 3)produces'b'. Finally, negative values ofnshould result in “subtracting” fromchrather than adding.average_ch(s): returns the “average” character found ins. This character corresponds to the average of the character values of the characters found ins.mixed_case(s): returns a string that is the result of mixing the case ofs. To mix the case of a string, we make the first letter ofslowercase, 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!occurrences(s, t): returns the number of times the substringtoccurs ins. For example,occurrences('mississippi', 'ss')returns2andoccurrences('aaaaa', 'aa')returns4.
sum_numbers(s): returns the sum of the integers found in the strings.sis a string of tokens separated by arbitrary amounts of whitespace. The function ignores any such token that is not an integer value.
intersperse(s, t): returns a new string that is the result of interspersingtamong the letters ofs. For exampleintersperse('hello world', '-')returnsh-e-l-l-o- -w-o-r-l-d. Note that there is no dash after thed!swap_at(s, n): returns a string string that is the result of swapping the substrings ofsat character range(0-(n-1)]to(n-len(s)]. For example,swap_at('hello world!', 5)returnsworld!hello(note the space in front of thew).evaluate(s): returns an integer that is the result of evaluating the arithmetic expression denoted bys. Assume thatsis a string only containing digits and the characters+and-.
