Starting from:

$29

ECE-203 Laboratory Experiment Week 5

ECE-203 Programming for Engineers
Laboratory Experiment Week 5
Name:
Lab Assignments (Due end of this lab session)
(5 Points) Write a function that accepts a floating-point number and prints it to the display as a
currency value (with a $ sign and two decimal places). Demonstrate your working program to the
TA.
TA Initials
(5 Points) We have introduced two variable scopings in Python: global and local. For the following
program, list all the variables that are accessible from the following namespaces: global, f(), g(),
and main(). Next to each variable, write which namespace it belongs to.
1 a = 0
2 b = 5
3
4 def f(i):
5 n = 0
6 while n * n <= i:
7 n = n + 1
8 return n - 1
9
10 def g(a):
11 b = 0
12 for n in range(a):
13 i = f(n)
14 b = b + i
15 return b
16
17 def main():
18 global a, b
19 i = 10
20 b = g(i)
21 print(a + b + i)
22
23 main()
global f() g() main()
1
(10 Points) The effective focal length f of a double sided convex lens of thickness d with surface
curvature radii R1 and R2 is given by:
1
f
= (n − 1) [
1
R1

1
R2
+
(n − 1)d
nR1R2
]
where n is the refractive index of the lens material. Write a function that computes f for a given
set of parameters R1, R2, d, and n. Demonstrate your working function to the TA.
TA Initials
(20 Points) It is a well-known phenomenon that most people are easily able to read text where
the words have two letters flipped — as long as the first and last letter of the word are unchanged.
For example:
“I dn’ot gvie a dman for a man taht can olny sepll a wrod one way. (Mrak Taiwn)
Write a function scramble(word) that constructs a scrambled version of a given word, randomly
flipping two characters other than the first and last one. Test this function for several words of
different lengths and demonstrate for the TA.
TA Initials
Now, you will write a second function build sentence(string) that will accept a string containing
an entire sentence. Design your function to use scramble(word) from the previous step to construct
your own transposed sentence similar to the example. The function build sentence(string) should
return a string containing the full sentence where the letters of each word have been scrambled by
scramble(word). Demonstrate that your build sentence() function works to the TA.
TA Initials
2

More products