Sections 12-13 Hacks
The Section 12-13 Hacks
3.12 Hacks Part 1
Problem 1: Qais is writing code to calculate formulas from his math class. He's currently working on a procedure to calculate average speed, based on this formula:
Average speed = Total Distance/Total Time
Highlight which of these is the best procedure for calculating and displaying average speed:
PROCEDURE calcAvgSpeed (distance, time) { DISPLAY (distance/time) } PROCEDURE calcAvgSpeed (distance) { DISPLAY (distance/time) } PROCEDURE calcAvgSpeed (distance, time) { DISPLAY (time/distance) }
Explanation: Since the first option uses the correct formula for average speed (distance over time) and the two parameters are properly utilized (distance and time), that would be the best procedure for calculating and displaying average speed.
Problem 2: James Hunter is looking through his classmate's program and sees a procedure called heightenEmotions: PROCEDURE heightenEmotions(myEmotion). That procedure manipulates strings using two built-in procedures, CONCAT for concatenating two strings together, and UPPER for converting a string to uppercase.
James Hunter then sees this line of code:
heightenEmotions("im mad")
After that line of code runs, will nothing be displayed?
A. True B. False
Explanation: Nothing is going to be displayed, since there is no DISPLAY(__) line in the program.
Problem 3: Which of these code snippets successfully calculates and stores her total footprint? Highlight 2 answers.
1. totalFootprint ← calcFlightFootprint(2451, 118) + calcFlightFootprint(3442, 252)
- totalFootprint ← calcFlightFootprint(2451, 118 + 3442, 252)
3. totalFootprint ← calcFlightFootprint((2451, 118) + (3442, 252))
- laNyCarbon ← calcFlightFootprint(2451, 118) nyLondonCarbon ← calcFlightFootprint(3442, 252) totalFootprint ← laNyCarbon + nyLondonCarbon
Explanation: The first code snippet works, since the total footprint is outputted by adding the two flight's footprints separately and then adding them together. Then, the third code snippet also works, because it is adding the two components with each other from both flights, setting it as the entire "calcFlightFootprint", and then showing it as the "totalFootprint".
3.12 Hacks Part 2
Problem 1:
Find a.
c = 9
b = 9 * 9 = 81
a = b c = 81 9 = 729
Problem #2:
Find the cost.
'Temp' becomes equal to 110 (100 + percent taxed, and the percent taxed is 10%.)
Then, it is divided by 100. 110/100 = 1.1
Then, to find the final cost, the initial cost ($173) is multiplied by 1.1, which yields **$190.30**.
Problem #3:
Find the temperature in degrees Celsius.
Initial Temperature: 103 degrees Fahrenheit
103 - 32 = 71
71 x (5/9) = 39.44 degrees Celsius
3.13 Hacks
def replaceRByards(toprbyardspg, currentrbyards, totalGames):
if ((toprbyardspg/totalGames) < (currentrbyards/totalGames)):
toprbyardspg = currentrbyards
print(toprbyardspg)
replaceRByards(100, 1260, 12)
PROCEDURE aPlus() {
if (canMoveForward):
moveForward
else (canMoveRight):
rotateRight
moveForward
if (canMoveLeft):
rotateLeft
moveForward
}
Problem 3: Which is the correct way to define the name of a procedure?
A. PROCEDURE MYLIST B. PROCEDURE MyList C. procedure mylist
Explanation: Option B is definitely the right answer, as the procedure name is supposed to have some capital letters (not all capitals), and the word procedure should be in all caps.
PROCEDURE greenSquare {
rotateLeft
moveForward
moveForward
moveForward
moveForward
moveForward
moveForward
rotateLeft
rotateLeft
rotateLeft
}