lab9-extra¶
make_oscillator(frequency)
¶
Write a function make_oscillator(frequency)
that returns a function
which takes a floating point value t
to represent time, and returns
sin(t * frequency)
.
Examples
In [ ]: osc1 = make_oscillator(1)
In [ ]: osc1(0 * math.pi)
Out[ ]: 0.0
In [ ]: osc1(0.25 * math.pi) # expect sqrt(2)/2 for sin(45 deg)
Out[ ]: 0.7071067811865475
In [ ]: osc1(0.5 * math.pi) # expect 1 for sin(90 deg)
Out[ ]: 1.0
In [ ]: osc2 = make_oscillator(2)
In [ ]: osc2(0 * math.pi)
Out[ ]: 0.0
In [ ]: osc2(0.25 * math.pi)
Out[ ]: 1.0
is_palindrome(s)
¶
Implement a function is_palindrome(s)
which takes a string s
and
returns the value True
if s
is a palindrome, and returns
False
otherwise. (Note that the return value is not the string
“True”
but the special Python value True
— see the section True
and False
in the appendix below. The same applies to False
.)
A palindrome is a word that reads the same backwards as forwards, such as “madam”, “kayak”, “radar” and “rotator”.
Examples:
In [ ]: is_palindrome('rotator')
Out[ ]: True
In [ ]: is_palindrome('radiator')
Out[ ]: False
In [ ]: is_palindrome('ABBA')
Out[ ]: True
We treat small letters (e.g. a
) and capital letters (e.g. A
) as
different letters for this exercise: the string ABba
is thus not a
palindrome.
Hints for implementing a solution (Option 1):
if
s
is an empty string, then it is a palindrome.if
s
is a string with one character, then it is a palindrome.if the first letter of
s
is the same as the last letter ofs
, thens
is a palindrome if the remaining letters ofs
(i.e.s[1:-1]
) are a palindrome. This is algorithm is particularly easy to implement using recursion. (But you can also use a for loop.)Recursion: to learn about recursion, take some time to study the output of this recursive factorial computation in the appendix below. Or check out the first 2 minutes and 40 seconds in this Socratica video on recursion.
Hints for implementing a solution (Option 2):
A completely different strategy would be to create a second string that contains the letters of
s
in reverse orderand compare that second string against
s
.
Appendix¶
True and False¶
True and False are special boolean values (of Python type bool), and different from strings. Here is some demonstration of this:
In [ ]: a = True
In [ ]: b = "True"
In [ ]: type(a)
Out[ ]: bool
In [ ]: type(b)
Out[ ]: str
In the function is_palindrome()
above, you must return the bool
value True
or False
, but not the string “True” or the string
“False”.
Verbose recursive factorial implementation¶
We implement the factorial funcion $f(n) = n!$. We use recursion, i.e. the fact that $f(n) = n * f(n -1)$ and our knowledge that $f(1)=1$.
def f(n):
"""returns n*(n-1)*(n-2)*...*2*1"""
if n == 1:
return 1
else:
return n * f(n-1)
Here is a different implementation that prints more diagnostic output to the screen. Useful to understand how recursion works.
def f(n):
"""returns n*(n-1)*(n-2)*...*2*1
As fsilent(n), but includes many print statements to explain flow of
control"""
print(f"=> Starting f({n})")
if n == 1:
print("if n==1 TRUE => Base case (n==1). Will return 1 and " +
"leave function f(1)")
return 1
else:
print(f"if n==1 FALSE => (n={n}), need to compute n*f(n-1) =" +
f" {n}*f({n-1})")
print(f"Will call f({n-1}) now ...")
tmp1 = f(n-1)
result = tmp1*n
print(f"...returned from calling f({n-1}), back in f({n})")
print(f"Multiply n*f(n-1) = {n}*f({n-1}) = {n}*{tmp1} = {result}," +
f" leaving function f({n})")
return result
# You may want to try different calls here, say f(1), f(2), f(3)
print(f(3))
Please include the extra tasks in your file lab9.py
and submit as lab9 assignment.
Back to lab9.
End of lab9-extra.