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
None
of typeNone
.None
is 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
ord
function, e.g.,ord('a')
returns97
. - To convert a character value into a character, use the
chd
function, 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 ofn
spaces.is_true(s)
: returnsTrue
iffs
is the stringtrue
with any sort of case and with any amount of whitespace on either end of the string.contains_rep(s, t)
: returnsTrue
iff the strings
containst
or some non-zero replication oft
, e.g.,t
,tt
,ttt
.shift_char(ch, n)
: returns a new lowercase character that isn
characters ahead ofch
in the alphabet. For exampleshift_char('c', 5)
produces'h'
. This addition operation should be performed modulo 26, i.e., the calculation should wrap fromz
back toa
. For exampleshift_char('y', 3)
produces'b'
. Finally, negative values ofn
should result in “subtracting” fromch
rather 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 ofs
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!
occurrences(s, t)
: returns the number of times the substringt
occurs ins
. For example,occurrences('mississippi', 'ss')
returns2
andoccurrences('aaaaa', 'aa')
returns4
.
sum_numbers(s)
: returns the sum of the integers found in the strings
.s
is 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 interspersingt
among 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 ofs
at 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 thats
is a string only containing digits and the characters+
and-
.