Chap 4: My - Thermo - Stat
Chap 4: My - Thermo - Stat
x = 3
if x > 1 and x < 2:
y = 2
elif x > 2 and x < 4:
y = 4
else:
y = 0
print(y)
4)
Note that if you want the logical statement a < x < b, this is two conditional
statements, a < x and x < b. In Python, you can type a < x < b as well. For example:
x = 3
if 1 < x < 2:
y = 2
elif 2 < x < 4:
y = 4
else:
y = 0
print(y)
5) Think about what will happen when the following code is executed. What are all the
possible outcomes based on the input values of x and y?
def my_nested_branching(x,y):
"""
Nested Branching Statement Example
author
date
:type x: Int
:type y: Int
:rtype: Int
"""
if x > 2:
if y < 2:
out = x + y
else:
out = x - y
else:
if y > 2:
out = x*y
else:
out = 0
return out
6) Modify my_adder to throw out a warning if the user does not input numerical
values. Try your function for non-numerical inputs to show that the check works.
When a statement is too long, we can use ‘' symbol to break a line to multiple lines.
def is_odd(number):
"""
function returns 'odd' if the input is odd,
'even' otherwise
author
date
:type number: Int
:rtype: String
"""
# use modulo to check if the input is divisible by 2
if number % 2 == 0:
# if it is divisible by 2, then input is not odd
return 'even'
else:
return 'odd'
print(is_odd(1))
import numpy as np
9) : Ternary operator
is_student = True
person = 'student' if is_student else 'not student'
print(person)
is_student = True
if is_student:
person = 'student'
else:
person = 'not student'
print(person)
10)Write a function my_tip_calc(bill, party), where bill is the total cost of a meal and party is
the number of people in the group. The tip should be calculated as 15% for a party
strictly less than six people, 18% for a party strictly less than eight, 20% for a party less
than 11, and 25% for a party 11 or more. A couple of test cases are given below.
11)
Write a function my_mult_operation(a,b,operation)
x = np.array([1, 2, 3, 4])
y = np.array([2, 3, 4, 5])
# Output: [3, 5, 7, 9]
print(my_mult_operation(x, y, 'plus'))
# Output: 'inside'
print(my_inside_triangle(0.25, 0.25))
# Output: 'outside'
print(my_inside_triangle(5, 5))
import numpy as np
def my_make_size10(x):
# convert x to a numpy array
x = np.array(x)
# if x has more than 10 elements, take the first 10
if len(x) > 10:
return x[:10]
# if x has less than 10 elements, pad it with zeros
else:
return np.pad(x, (0, 10-len(x)), mode='constant')
print(my_make_size10(range(1,2))) # Output: [1 2 0 0 0 0 0 0 0 0]
print(my_make_size10(range(1,15))) # Output: [ 1 2 3 4 5 6 7 8 9 10]
14) Can you write my_make_size10 without using if-statements (i.e., using only logical
and array operations)?
1. import numpy as np
2.
3. def my_make_size10(x):
4. size10 = np.zeros(10, dtype=int)
5. size10[:min(10, len(x))] = x[:min(10, len(x))]
6. return size10
def my_letter_grader(percent):
if percent > 97:
return 'A+'
elif percent > 93:
return 'A'
elif percent > 90:
return 'A-'
elif percent > 87:
return 'B+'
elif percent > 83:
return 'B'
elif percent > 80:
return 'B-'
elif percent > 77:
return 'C+'
elif percent > 73:
return 'C'
elif percent > 70:
return 'C-'
elif percent > 67:
return 'D+'
elif percent > 63:
return 'D'
elif percent > 60:
return 'D-'
else:
return 'F'
# Output: 'A+'
print(my_letter_grader(97))
# Output: 'B'
print(my_letter_grader(84))
# Output: 'F'
print(my_letter_grader(50))
# Output: 'A+'
print(my_letter_grader(97))
# Output: 'B'
print(my_letter_grader(84))
# Output: 'F'
print(my_letter_grader(50))
# Output: 'A-'
print(my_letter_grader(91))
# Output: 'D-'
print(my_letter_grader(61))
# Output: 'A-'
print(my_letter_grader(91))
# Output: 'D-'
print(my_letter_grader(61))
1. 16) Most engineering systems have redundancy. That is, an engineering system has
more than is required to accomplish its purpose. Consider a nuclear reactor whose
temperature is monitored by three sensors. An alarm should go off if any two of the
sensor readings disagree. Write a function my_nuke_alarm(s1,s2,s3) where s1, s2,
and s3 are the temperature readings for sensor 1, sensor 2, and sensor 3,
respectively. The output should be the string ‘alarm!’ if any two of the temperature
readings disagree by strictly more than 10 degrees and ‘normal’ otherwise.
2. def my_nuke_alarm(s1, s2, s3):
3. diff1 = abs(s1 - s2)
4. diff2 = abs(s1 - s3)
5. diff3 = abs(s2 - s3)
6.
7. if diff1 > 10 and diff2 > 10:
8. return 'alarm!'
9. elif diff1 > 10 and diff3 > 10:
10. return 'alarm!'
11. elif diff2 > 10 and diff3 > 10:
12. return 'alarm!'
13. else:
14. return 'normal'
15.print(my_nuke_alarm(94, 96, 90)) # Output: 'normal'
16.print(my_nuke_alarm(94, 96, 80)) # Output: 'alarm!'
17.print(my_nuke_alarm(100, 96, 90)) # Output: 'normal'
18.
17) Write a function my_split_function(f,g,a,b,x), where f and g are handles to
functions f(x) and g(x), respectively. The output should be f(x) if x≤a, g(x)
if x≥band 0 otherwise. You may assume that b>a.
import numpy as np
# Example usage
print(my_split_function(np.exp, np.sin, 2, 4, 1)) # Output: 2.718281828459045
print(my_split_function(np.exp, np.sin, 2, 4, 3)) # Output: -0.9589242746631385
print(my_split_function(np.exp, np.sin, 2, 4, 5)) # Output: 0
CHAP 5
n = 0
for i in range(1, 4):
n = n + i
print(n)
2) Print all the characters in the string "banana".
for c in "banana":
print(c)
s = "banana"
for i in range(len(s)):
print(s[i])
3) Given a list of integers, a, add all the elements of a.
s = 0
a = [2, 3, 1, 3, 3]
for i in a:
s += i # note this is equivalent to s = s + i
print(s)
4) Define a dictionary and loop through all the keys and values.
def have_digits(s):
out = 0
# loop through the string
for c in s:
# check if the character is a digit
if c.isdigit():
out = 1
break
return out
out = have_digits('only4you')
print(out)
6) : Let the function my_dist_2_points(xy_points, xy), where the input
argument xy_points is a list of x-y coordinates of a point in Euclidean space, xy is a list
that contain an x-y coordinate, and the output d is a list containing the distances
from xy to the points contained in each row of xy_points.
import math
import numpy as np
x = np.array([[5, 6], [7, 8]])
n, m = x.shape
s = 0
for i in range(n):
for j in range(m):
s += x[i, j]
print(s)
8) Determine the number of times 8 can be divided by 2 until the result is less than 1.
i = 0
n = 8
while n >= 1:
n /= 2
i += 1
print(f'n = {n}, i = {i}')
9)
n = 0
while n > -1:
n += 1
print(n)
10)If x = range(5), square each number in x, and store it in a list y.
x = range(5)
y = []
for i in x:
y.append(i**2)
print(y)
y = [i**2 for i in x]
print(y)
y = [i**2 for i in x if i%2 == 0]
print(y)
y = []
for i in range(5):
for j in range(2):
y.append(i + j)
print(y)