Python resolve

Tutor anglina
pythonexam.docx

Q1 a.)

1. Declare offices array, lecture_halls array, night_time string

2. Set night_time as the estimated time where no one is moving around the campus (string)

3. Loop through the 15 offices and set each offices array element as the status of the lights in the office number (Boolean)

4. Loop through the 10 offices and set each lecture_halls array element as the status of the lights in the lecture hall number (Boolean)

5. In an update loop:

6. Get the current time in the same format as night_time

7. If (the current time == night_time) then

8. Loop through offices and check if the array element is True, if true then switch off the light for that office number

9. Loop through all the lecture_halls and check if the array element is True, if true then switch off the light for that lecture hall number.

Q1 b.)

from datetime import datetime

offices = []

lecture_halls = []

night_time = "" # estimated night time (string)

for i in 15:

offices[i] = get_office_lights(i) # returns true or false

for i in 10:

lecture_halls[i] = get_lecture_lights(i) # returns true or false

while True:

current_time = datetime.utcnow().strftime("%H:%M")

if (current_time == night_time):

for i in offices:

if (offices[i] == True):

offices[i] = switch_office_lights(i, False)

for i in lecture_halls:

if (lecture_halls[i] == True):

lecture_halls[i] = switch_lecture_lights(i, False)

Q2 a.)

1. Declare concentration, safe_level, time and rate

2. Set safe_level as the safe concentration for release

3. Set concentration as the measured concentration of the waste in the compound,

4. In a while loop:

5. Increment time,

6. If time > 1 second then break the loop,

7. Measure the concentration of the waste in the compound

8. Set rate as – ((new measurement – concentration) / 1 second)

9. Set time = 0

10. In another while loop:

11. Increment time,

12. If time > (concentration * rate / safe_level * 1 day) then release the compound and break the loop

Q2 b.)

concentration = 0

safe_level = 0

time = 0

rate = 0

while True:

time += 1

if (time == 1e9):

break

newMeasurement = 0

rate = (newMeasurement - concentration) / 1e9

while True:

time += 1

if (time > concentration * rate / safe_level * 8.64e+13):

#release

Break

Q3 a.)

1. Declare max_level, water_level

2. Set max_level to the desired maximum water level,

3. In an update loop:

4. Set water_level to the measured current level of water,

5. If (water_level > max_level) then release water

6. If (water_level < max_level) then stop releasing water

Q3 b.)

max_level = 0

water_level = 0

# update loop

while True:

water_level = get_water_level()

if (water_level > max_level):

open_valves()

if (water_level > max_level):

close_valves()

Q4.)

time = 0

pressureValueA = 0

pressureValueB = 0

refresh_rate = 1e6 # in nanoseconds

while True:

time += 1

if (time % refresh_rate == 0):

pressureValueB = pressureValueA

pressureValueA = get_current_pressure()

draw_graph_line(pressureValueA, pressureValueB)