Trux Falsy
Min 3
Write the function min_3(number_1, number_2,
number_3)
that returns the minimum of the 3 parameters to
the function, number_1
, number_2
,
number_3
. The function should not use the
built-in min
function. The function is
the same as the min_3
function you previously
wrote. This version of it should use logical operators to
simplify the code.
Test Cases
print('Input: 0, 1, 2\tActual:', min_3(0, 1, 2), '\tExpected: 0') print('Input: 0, 2, 1\tActual:', min_3(0, 2, 1), '\tExpected: 0') print('Input: 1, 0, 2\tActual:', min_3(1, 0, 2), '\tExpected: 0') print('Input: 2, 0, 1\tActual:', min_3(2, 0, 1), '\tExpected: 0') print('Input: 1, 2, 0\tActual:', min_3(1, 2, 0), '\tExpected: 0') print('Input: 2, 1, 0\tActual:', min_3(2, 1, 0), '\tExpected: 0')
Single Digit
Write the function is_single_digit(integer)
that
returns True
if the parameter integer is a
single digit number and returns False
otherwise.
Note, the function should not use any conditional
statemetns.
Test Cases
print('Input: 0\tActual:', is_single_digit(0), '\tExpected: True') print('Input: 1\tActual:', is_single_digit(1), '\tExpected: True') print('Input: 9\tActual:', is_single_digit(9), '\tExpected: True') print('Input: 10\tActual:', is_single_digit(10), '\tExpected: False') print('Input: 100\tActual:', is_single_digit(100), '\tExpected: False') print('Input: -1\tActual:', is_single_digit(-1), '\tExpected: True') print('Input: -9\tActual:', is_single_digit(-9), '\tExpected: True') print('Input: -10\tActual:', is_single_digit(-10), '\tExpected: False')
Point in Rectangle
Creating animations where different elements interact, requires being able to tell if two elements overlap. There are many different ways to determine whether two entities are colliding. One of them involves circles, similar to the point in circle program from last class. Another, is determining if a point is located within a rectangle.
Details
Create a Python program that defines the function
is_point_in_rectangle(point_x, point_y,
rectangle_center_x, rectangle_center_y, rectangle_width,
rectangle_height)
. The function has 6
parameters. The first two parameters, point_x
and point_y are the x and y coordinates of a point.
The next two parameters rectangle_center_x
and rectangle_center_y) the x and y coordinates of the
center of a rectangle. The last two
parameters rectangle_width
and rectangle_height the width and height of the
rectangle.
The function should return True if the point is inside the rectangle. Assume a point exacty on the border of the rectangle is inside the rectangle. The function should return False if the point is not inside the rectangle.
Test Cases
print('Input: 0, 0, 0, 0, 2, 2\t\tActual:', is_point_in_rectangle(0, 0, 0, 0, 2, 2), '\tExpected: True') print('Input: 1, 0, 0, 0, 2, 2\t\tActual:', is_point_in_rectangle(1, 0, 0, 0, 2, 2), '\tExpected: True') print('Input: -1, 0, 0, 0, 2, 2\tActual:', is_point_in_rectangle(-1, 0, 0, 0, 2, 2), '\tExpected: True') print('Input: 0, 1, 0, 0, 2, 2\t\tActual:', is_point_in_rectangle(0, 1, 0, 0, 2, 2), '\tExpected: True') print('Input: 0, -1, 0, 0, 2, 2\tActual:', is_point_in_rectangle(0, -1, 0, 0, 2, 2), '\tExpected: True') print('Input: 1, 1, 0, 0, 2, 2\t\tActual:', is_point_in_rectangle(1, 1, 0, 0, 2, 2), '\tExpected: True') print('Input: -1, -1, 0, 0, 2, 2\tActual:', is_point_in_rectangle(-1, -1, 0, 0, 2, 2), '\tExpected: True') print('Input: -1, 1, 0, 0, 2, 2\tActual:', is_point_in_rectangle(-1, 1, 0, 0, 2, 2), '\tExpected: True') print('Input: 1, -1, 0, 0, 2, 2\tActual:', is_point_in_rectangle(1, -1, 0, 0, 2, 2), '\tExpected: True') print('Input: 2, 0, 0, 0, 2, 2\t\tActual:', is_point_in_rectangle(2, 0, 0, 0, 2, 2), '\tExpected: False') print('Input: -2, 0, 0, 0, 2, 2\tActual:', is_point_in_rectangle(-2, 0, 0, 0, 2, 2), '\tExpected: False') print('Input: 0, 2, 0, 0, 2, 2\t\tActual:', is_point_in_rectangle(0, 2, 0, 0, 2, 2), '\tExpected: False') print('Input: 0,-2, 0, 0, 2, 2\t\tActual:', is_point_in_rectangle(0,-2, 0, 0, 2, 2), '\tExpected: False') print('Input: -3, 2, -3, 2, 1, 1\tActual:', is_point_in_rectangle(-3, 2, -3, 2, 1, 1), '\tExpected: True') print('Input: 0, 1, -3, 2, 1, 1\tActual:', is_point_in_rectangle(0, 1, -3, 2, 1, 1), '\tExpected: False') print('Input: 2, 0, 0, 0, 4, 2\t\tActual:', is_point_in_rectangle(2, 0, 0, 0, 4, 2), '\tExpected: True') print('Input: 0, 2, 0, 0, 4, 2\t\tActual:', is_point_in_rectangle(0, 2, 0, 0, 4, 2), '\tExpected: False')
Hint
- To test if a point is in a rectangle, you must compare the point's location to the location of each of the sides of the rectangle.
- You are given the center point of a rectangle, but to check if a point is inside of a given rectangle you need to be able to compute the x coordinate for the left and right sides of the rectangle, and the y coordinate of the top and bottom of the rectangle. Create variables for these, so your logical expressions will be more readable. Don't forget that y decreasing is up!
Challenge
Modify the program to be visual like the Point in Circle program from last class. The program should draw a rectangle in the center of the screen that takes up about half of the window. Allow the user to click on the screen for up to 10 points. If the point the user clicked was inside the rectangle, it should be colored green. Otherwise it should be colored red.
Paint
One of the more interesting things that conditionals give us is the ability to interact with a graphical program in different ways. We can tell the difference between a mouse button being down vs. up, we can tell when a keyboard key is pressed, etc. This can also allow us to create programs which allows a user to express their...creativity!
Details
Create a Python program that uses the graphics module to allow the user to draw in a window with the mouse. When the user clicks and drags the mouse, the program should use the mouse position to repeatedly draw lines.
Example
Hint
-
The graphics module has a function
called
button_down
that returns whether a mouse button is pressed. Use 1 as the parameter to this function, to check for the left mouse button. Put this function inside of a for loop to repeatedly check whether the mouse button is down. The for loop can run any number of times, but it should have a call to thewait
function so that the loop doesn't run too fast. -
The program should keep track of two
values,
previous_x
andprevious_y
. The variables should be updated at the end of every iteration of the loop using the values ofmouse_x
andmouse_y
respectively. - When the mouse button is down, the program should use the current position of the mouse to draw a line from the previous location to the current location.
Challenge
Modify the program so that the user can chose between different line colors by clicking on a rectangle defining that color.