Math Practice
Let’s try some simple practice problems. These problems are not graded - they are just for you to practice before doing the real exercises in the lab itself. You can find the answers below each question by clicking the button below each question.
2.1 Reading Code
Write the output that is displayed to the user after running the following Python code:
x = 13
z = "5"
y = int(z)
var = (x + y) % y * y - x
var = var / 2
print(var)
Pay special attention to data types! Make sure the answer is presented as the correct type.
2.2 Reading Code
Write the output that is displayed to the user after running the following Python code:
x = 9
y = 3.0
z = 5
ans = (x - z) % 2 // y + z ** (y - 1)
print(ans)
Pay special attention to data types! Make sure the answer is presented as the correct type.
2.3 Writing Code
You are teaching a class and would like to put your students into a number of groups. You know how many students are in the class and the number of groups to create, but you aren’t sure how many students should be in each group.
We’ll assume that these values are stored in the students
and groups
variables, respectively.
Write a Python program to compute the ideal group size for each group in the class. When divided, the groups in the class should have no fewer than size
people, and no more than size+1
people, and there should be exactly groups
groups total. For example, if there are
$ 15 $ people and the desired number of groups is
$ 4 $, then code should start with the following two variable assignments.
students = 15
groups = 4
# more code goes here
Your code should produce the following output for these values:
groups of 3 or 4
This is because the ideal group size for $ 4 $ groups out of $ 15 $ people is $ 3 $, and all groups should have either $ 3 $ or $ 4 $ members (in this case, one group of $ 3 $ and the rest groups of $ 4 $).
Write the rest of this Python program. Try different values for the students
and groups
variables to make sure that your answers are correct!