Vocab
Term | Definition |
---|---|
Method/Function/Procedure | A set of code that can be run as a package, that takes in parameters and returns a return value |
Parameter | Input values for procedures |
Modularity | named group of programming instructions that serves a purpose |
Parameter | the practice of breaking a complex program into smaller, independent parts or modules that can be used and reused in different parts of the program |
Notes:
- Make your names descriptive!
- Make your program into smaller independent modules that can be used multiple times
- Utilize update functions and recursive
Activity 2
PROCEDURE rotateDecide(){
if not CAN_MOVE(right){
ROTATE_LEFT()
}
if not CAN_MOVE(left){
ROTATE_RIGHT()
}
}
PROCEDURE goForwardThenTurn(X){
for i in range x {
MOVE_FORWARD)
}
ROTATE_RIGHT()
MOVE_FORWARD()
ROTATE_LEFT()
}
PROCEDURE goForwardX(X){
for i in range x {
MOVE_FORWARD())
}
}
goForwardThenTurn(2)
goForwardThenTurn(3)
moveForwardX(3)
ROTATE_LEFT()
moveForwardX(2)
Problems 1/2/3
- The first procedure would display the value 81*9 =
729
, because c is 9, and b = 9*9 (9*2 or 81), and 81\9 - The second procedure would display
190
, as temp becomes 110, then divided by 100 to be 1.1. Then the cost (173) is multiplied by 1.1 to give 190. - The third procedure equals
39.44
, as first we subtract 32 then multiply by five-ninths.
def replaceRByards(toprbyardspg, currentrbyards, totalGames):
if ((toprbyardspg/totalGames) < (currentrbyards/totalGames)):
toprbyardspg = currentrbyards
print(toprbyardspg)
replaceRByards(100, 1260, 12)
Robot Puzzle:
PROCEDURE goForwardThenTurn(X, dir){
for i in range x {
MOVE_FORWARD)
}
if dir = "right"{
ROTATE_RIGHT()
MOVE_FORWARD()
ROTATE_LEFT()
}
else{
ROTATE_LEFT()
MOVE_FORWARD()
ROTATE_RIGHT()
}
}
PROCEDURE goForwardX(X){
for i in range x {
MOVE_FORWARD())
}
}
goForwardX(1)
ROTATE_LEFT()
goForwardThenTurn(2, right)
goForwardThenTurn(2, left)
goForwardThenTurn(1, left)
goForwardThenTurn(2, right)
goForwardX(1)
ROTATE_RIGHT()
goForwardX(2)
This will move the robot up, rotate it left. Then it moves up, rotates and moves right one extra, then the same, but to the left. Finally, it will move up one then move to the left one. Then it will move up 2 and move it right one. then it will go forwards, rotate right, then go forwards 2 through goForwardX
What is the correct name of the procedure?
A: PROCEDURE MYLIST. Technically, you can use the name MyList for a procedure, it is just that it still needs caps. using procedure
in lowercase is not how collegeboard wants people to define procedures, and so PROCEDURE MYLIST (using full caps) is the best pick.
Second Robot Problem
PROCEDURE MoveForwardsUntil(direction, number){
while number != 0{
MOVE_FORWARD()
if direction = right{
if not CAN_MOVE(right){
number -= 1
}
}
else if direction = left {
if not CAN_MOVE(left){
number -= 1
}
}
}
MOVE_FORWARD()
}
ROTATE_RIGHT()
MOVE_FORWARD()
ROTATE_LEFT()
MoveForwardsUntil(right, 3)
ROTATE_LEFT()
MOVE_FORWARD
The procedure here decrements number
by one every single time it passes a black block by checking whether it can move that way. That way, we can count to see how many black blocks we pass. Then the procedure makes the robot move one forward to ensure it is lined up.
Third Robot Problem
PROCEDURE FinishCourse(){
while not Goal_Completed{
if not CAN_MOVE(forward){
if CAN_MOVE(left){
ROTATE_LEFT()
MOVE_FORWARD()
ROTATE_RIGHT()
}
else if CAN_MOVE(right) {
ROTATE_RIGHT()
MOVE_FORWARD()
ROTATE_LEFT()
}
else{
ROTATE_RIGHT()
ROTATE_RIGHT()
MOVE_FORWARD()
}
}
MOVE_FORWARD()
}
}
FinishCourse()
This procedure works because it just asks the robot to move forward until it reaches an obstacle. If it does, then it check first to turn left (because in this case it is more optimal), then to turn right, at which it turns, moves up one (to dodge the obstacle) and then turns back to realign itself. This repeats until it reaches the top, at which point it turns left infinitely until it reaches the goal.
Extra: Creating and finishing my own course
Code to Complete Course
PROCEDURE TurnXDeg(times_To_turn){
for i in range(times_To_turn){
ROTATE_RIGHT()
}
}
PROCEDURE goForwardX(X){
for i in range x {
MOVE_FORWARD())
}
}
TurnXDeg(2)
goForwardX(2)
TurnXDeg(1)
goForwardX(4)
TurnXDeg(1)
goForwardX(3)
TurnXDeg(1)
goForwardX(2)
TurnXDeg(3)
MOVE_FORWARD()
TurnXDeg(1)
goForward(2)
This just uses some simple compound function to repeat certain actions a certain amount of times to complete tasks.
from PIL import Image
im = Image.open("../images/sugma.png")
print("Course to Finish:")
display(im)