computer science -Python
|
Total Grade (30 pts) |
|
COSC 1436 Weeks 5-6 Lab Assignment -3
Name (First Last):
Data Conversions and Control structures I
1. (3 points, each for 0.5 point ) Convert each of following numbers to Octal numbers, showing all steps.
a. (111110110)2
b. (1100010)2
c. (2001)10
d. (1492)10
e. (A9)16
f. (5FF71 )16
2. (3 points, each for 0.5 point ) Convert each of following numbers to Binary numbers, showing all steps.
a. (572) 8
b. (1604) 8
c. (1066)10
d. (99)10
e. (80E)16
f. (135AB)16
3. (3 points, each for 0.5 point ) Convert each of following numbers to Hexadecimal numbers, showing all steps.
a. (1111 0111)2
b. (1010 1010 1010)2
c. (777) 8
d. (443) 8
e. (998)10
f. (1066)10
4. (3 points, each for 0.5 point ) Evaluate the following expressions. For each expression, keep the same number system as it is. Do not convert the number systems. E.g. if the expression is written using hexadecimal number system, your answer should also be written in hexadecimal number system.
a. (1110 1111)2+(1011 1101)2
b. (11000001)2-(100010)2
c. (202) 8+( 667) 8
d. (1066)16+ (ABCD)16
e. (7766)8-(5544) 8
f. (9988)16–(AB)16
5. (1.5 points ) Write a program to ask the user to type the height of a person in inches, and convert it to feet and inches. You may assume the user only types numbers. Your program should run like this:
Enter your height in inches: 71
Your height is 5 ' 11 ".
(0.5 point) Write the program
#python program
(0.5 point) Screenshot of the output
(0.5 point) Save the program as “height.py”
6. (1.5 points) Write a program to ask the user to input the radius of a sphere, and then display the volume of the sphere. You must determine the proper variable names. The formula to compute the volume given the radius is provided:
, where is approximately 3.14159
You may assume the user only types numbers. Your program should run like this:
Enter the radius of the sphere: 10
The volume of the sphere is 4188.786666666666
(0.5 point) Write the program
#python program
(0.5 point) Screenshot of the output
(0.5 point) Save the program as “sphere.py”
7. (2point, each for 0.2 point) The following truth table shows various combinations of the values true and false connected by a logical operator (and, or, not). Complete the table by circling T or F to indicate whether the result of such a combination is true or false.
|
Logical Expression |
Result (circle T or F) |
|
|
True and False |
T |
F |
|
True and True |
T |
F |
|
False and True |
T |
F |
|
False and False |
T |
F |
|
True or False |
T |
F |
|
True or True |
T |
F |
|
False or True |
T |
F |
|
False or False |
T |
F |
|
not True |
T |
F |
|
not False |
T |
F |
8. (2 points, each for 0.2 point) Assume the variables a = 2, b = 4, c = 6, x=0, y=2, and z=4. Circle T or F for each of the following conditions to indicate whether its value is true or false.
|
Expression (Condition) |
Result (circle T or F) |
|
|
a == 4 or b > 2 |
T |
F |
|
6 <=c and a > 3 |
T |
F |
|
1 != b and c != 3 |
T |
F |
|
a >= -1 or a<=b |
T |
F |
|
not (a>2) |
T |
F |
|
x or y |
|
|
|
x and y |
|
|
|
x or z |
|
|
|
(x and y) or (y and z) |
|
|
|
not y |
|
9. (1 points, each for 0.2 point) Write logical expressions for the following conditions:
a. x is not a negative number
b. x is a multiple of 7
c. x is greater than the sum of y and z
d. x is the greatest number among x, y, and z
e. x is less than 99 but greater than 49
10. (0.5 point, each 0.25 point) What would the following code display?
a)
if 'z' < 'a':
print('z is less than a.')
else:
print('z is not less than a.')
(0.25 point) Screenshot of the output
b)
s1 = 'New York'
s2 = 'Boston'
if s1 > s2:
print(s2)
print(s1)
else:
print(s1)
print(s2)
(0.25 point) Screenshot of the output
11. (6 points, each 1 point) Draw flowcharts (only for conditions with branches as shown on the left) and write code fragments based on the flowcharts (not complete programs) to complete the following task. Note: screenshots are not required in this question.
a) if the variable x is greater than 100, assigns 20 to the variable y and assigns 40 to the variable z
|
Flowchart |
Code Fragments (python source code, not Screenshot) |
|
|
|
b) if the variable a is less than 10, assigns 0 to the variable b , and assigns 1 to the variable c
|
Flowchart |
Code Fragments (python source code, not Screenshot) |
|
|
|
c) if the variable a is less than 10, assigns 0 to the variable b. Otherwise, it should assign 99 to the variable b .
|
Flowchart |
Code Fragments (python source code, not Screenshot) |
|
|
|
d) if the variable speed is within the range of 24 to 56, displays 'Speed is normal'. Otherwise, display 'Speed is abnormal'
|
Flowchart |
Code Fragments (python source code, not Screenshot) |
|
|
|
e) Determine whether the variable points is outside the range of 9 to 51. If the variable’s value is outside this range, it should display “Invalid points.” Otherwise, it should display “Valid points.”
|
Flowchart |
Code Fragments (python source code, not Screenshot) |
|
|
|
f) If amount1 is greater than 10 and amount2 is less than 100, display the greater of amount1 and amount2 .
|
Flowchart |
Code Fragments (python source code, not Screenshot) |
|
|
|
12. (2 points) Write a program that prompts the user for two integers and then prints
a) The sum
b) The difference
c) The product
d) The average
e) The distance (absolute value of the difference)
f) The maximum (the larger of the two)
g) The minimum (the smaller of the two)
Hint: Python defines max and min functions that accept a sequence of values, each separated with a comma.
(1 point) Write the program
#python program
(0.5 point) Screenshot of the output
(0.5 points) Save the program as “Calculation.py”
13. (1.5 points) Write a program that initializes a string variable and prints the first three characters, followed by three periods, and then the last three characters. For example, if the string is initialized to ”Mississippi”, then print Mis...ppi.
(0.5 point) Write the program
#python program
(0.5 point) Screenshot of the output
(0.5 points) Save the program as “shorterString.py”