6) a. Introduction to PEP8 Examples




You are trying to escape from a maze and you have only 10 minutes to get out until it explodes.
The exit will be the closest point between your, the maze’s center and it’s corners location.
Given a python function that accepts the GPS location, the maze’s 4 corner GPS location, the GPS location of the center of the maze (12 parameters), correct it with respect to the PEP 8 standards.

Do not bother executing this code. This is just for illustrating the PEP8 code style.

Given code:

1
2
3
4
5
6
7
8
9
import numpy as np

def maze_out(your_x_loc, your_y_loc, center_x_loc, center_y_loc, left_up_corner_x, left_up_corner_y, right_up_corner_x, right_up_corner_y, left_down_corner_x, left_down_corner_y, right_down_corner_x, right_down_corner_y):
   
    x_middle = np.sum([your_x_loc, center_x_loc, left_up_corner_x, right_up_corner_x, left_down_corner_x, right_down_corner_x]) / 5.0
   
    y_middle = np.sum([your_y_loc, center_y_loc, left_up_corner_y, right_up_corner_y, left_down_corner_y, right_down_corner_y]) / 5.0

    return (x_middle, y_middle)


After fixing as per PEP8 standards:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
After fixing as per PEP8
def maze_out(your_x_loc, your_y_loc, center_x_loc, center_y_loc,
        left_up_corner_x, left_up_corner_y, right_up_corner_x,
        right_up_corner_y, left_down_corner_x, left_down_corner_y,
        right_down_corner_x, right_down_corner_y):

        x_middle = np.sum([your_x_loc, center_x_loc, left_up_corner_x,
                            right_up_corner_x, left_down_corner_x,
                            right_down_corner_x]) / 5.0
        y_middle = np.sum([your_y_loc, center_y_loc, left_up_corner_y,
                            right_up_corner_y, left_down_corner_y,
                            right_down_corner_y]) / 5.0

        return (x_middle, y_middle)




You are in a forest, but the sunset is coming. Your only way out is to find the cave in the middle of the forest. You know the forest’s four corners’ position. You don’t have much time to calculate the exact
position of the cave.
Given a program that calculates the position of the cave. Please correct it to comply with the PEP 8 standard, otherwise, you won’t be able to enter the cave. The forest’s corners are at (0,0), (4,0), (4,4), (0,4).

Do not bother executing this code. This is just for illustrating the PEP8 code style.

Given code:

1
2
3
4
5
6
7
8
9
forest_left_up = (0,4)
forest_right_up = (4,4)
forest_left_bottom = (0,0)
forest_right_bottom = (4,0)

forest_middle_x = (forest_left_up[0] + forest_right_up[0] + forest_left_bottom[0] + forest_right_bottom[0]) / 4
forest_middle_y = (forest_left_up[1] + forest_right_up[1] + forest_left_bottom[1] + forest_right_bottom[1]) / 4

print("The cave is at:", (forest_middle_x, forest_middle_y))


After fixing as per PEP8 standards:

1
2
3
4
5
6
7
8
9
forest_left_up = (0,4)
forest_right_up = (4,4)
forest_left_bottom = (0,0)
forest_right_bottom = (4,0)

forest_middle_x = (forest_left_up[0] + forest_right_up[0] + forest_left_bottom[0] + forest_right_bottom[0]) / 4
forest_middle_y = (forest_left_up[1] + forest_right_up[1] + forest_left_bottom[1] + forest_right_bottom[1]) / 4

print("The cave is at:", (forest_middle_x, forest_middle_y))




Once upon a time, you were walking on the streets and somebody robbed you. The only way to get back your money is to list the whole city. Usually, the one in the middle is the thief.
Write a program that calculates the middle of a given list by you.
Do not bother executing this code. This is just for illustrating the PEP8 code style.

Given code:

1
2
people = ["Frank", "Emil", "Josh", "Jack", "Carol", "Botond", "Esmeralda"]
print("The thief is:", people[int(len(people) / 2)])


After fixing as per PEP8 standards:

1
2
3
people = ["Frank", "Emil", "Josh", "Jack",
          "Carol", "Botond", "Esmeralda"]
print("The thief is:", people[int(len(people) / 2)])




You are in a spaceship that has some errors. You have 5 minutes to solve the problem, which makes you a very short amount of time to actually find the problem. The computer has 2 error messages, but the system has a limitation of 10000 lines per file. In the system, there is only one file with 9999 lines, which means your only chance is to write out to the console the 2 error messages. Please keep in mind, that the computer can only print one message with a single print command, which means you will need to use two of them. Also, this particular computer is able to run only files which respect the PEP8 coding standard.
Write a line that prints the two error messages, and repair the ship. Do not bother executing this code. This is just for illustrating the PEP8 code style.

Given code:

1
2
3
error_1 = "The ship is out of fuel"
error_2 = "Module 3 of the east section has leaking! The ship will explode in 5 minutes"
print(error_1);print(error_2)


After fixing as per PEP8 standards:

1
2
3
4
5
error_1 = "The ship is out of fuel"
error_2 = "Module 3 of the east section has leaking! \
The ship will explode in 5 minutes"

print(error_1)
print(error_2)




You are working for a serial killer and he asked you to write documentation for his program. If you don’t do it in 1 minute he will kill you.
Write a 3 line documentation to a hypothetical function that randomizes the next target from an input list.
You should write about the input and output of the function. Don’t forget! Your employer is also a fanatic of clean coding, so please respect the PEP8 coding standard.
Do not bother executing this code. This is just for illustrating the PEP8 code style.

Given code:

1
2
def hypothetical_function():
    pass


After fixing as per PEP8 standards:

1
2
3
4
5
6
7
def hypothetical_function():
   """random_target_generator
    This function generates a random target for the most known serial killer.
    Input: a list of targets, from where the target will be chosen.
    Output: the selected target
    """

    pass