Here it is
Step 1
def is_criticality_balanced(temperature, neutrons_emitted):
“”"Verify criticality is balanced.
:param temperature: int or float - temperature value in kelvin.
:param neutrons_emitted: int or float - number of neutrons emitted per second.
:return: bool - is criticality balanced?
A reactor is said to be critical if it satisfies the following conditions:
- The temperature is less than 800 K.
- The number of neutrons emitted per second is greater than 500.
- The product of temperature and neutrons emitted per second is less than 500000.
"""
if temperature < 800 and neutrons_emitted > 500 and (temperature * neutrons_emitted) < 500000:
return True
else:
return False
Step 2
def reactor_efficiency(voltage: int, current: int, theoretical_max_power: int) → str:
"""Assess reactor efficiency zone.
:param voltage: int or float - voltage value.
:param current: int or float - current value.
:param theoretical_max_power: int or float - power that corresponds to a 100% efficiency.
:return: str - one of ('green', 'orange', 'red', or 'black').
Efficiency can be grouped into 4 bands:
1. green -> efficiency of 80% or more,
2. orange -> efficiency of less than 80% but at least 60%,
3. red -> efficiency below 60%, but still 30% or more,
4. black -> less than 30% efficient.
The percentage value is calculated as
(generated power/ theoretical max power)*100
where generated power = voltage * current
"""
generated_power = voltage * current
efficiency = (generated_power / theorical_max_power) * 100
if efficiency >= 80 and efficiency <= 100:
return 'green'
elif efficiency >= 60 and efficiency < 80:
return 'orange'
elif efficiency >= 30 and efficiency < 60:
return 'red'
elif efficiency < 30:
return 'black'
else:
return 'Out of range'
Step 3
def fail_safe(temperature, neutrons_produced_per_second, threshold):
“”"Assess and return status code for the reactor.
:param temperature: int or float - value of the temperature in kelvin.
:param neutrons_produced_per_second: int or float - neutron flux.
:param threshold: int or float - threshold for category.
:return: str - one of ('LOW', 'NORMAL', 'DANGER').
1. 'LOW' -> `temperature * neutrons per second` < 90% of `threshold`
2. 'NORMAL' -> `temperature * neutrons per second` +/- 10% of `threshold`
3. 'DANGER' -> `temperature * neutrons per second` is not in the above-stated ranges
"""
criticality = 100 * (1 - temperature * neutrons_produced_per_seconds / threshold)
if -10 <= criticality <= 10:
return "NORMAL"
elif 10 < criticality <= 100:
return "LOW"
else:
return "DANGER"
Error Step 2
CODE RUN
voltage = 10
theoretical_max_power = 10000
# The numbers are chosen so that current == 10 x percentage
test_data = ((1000, 'green'), (999, 'green'), (800, 'green'),
(799, 'orange'), (700, 'orange'), (600, 'orange'),
(599, 'red'), (560, 'red'), (400, 'red'), (300, 'red'),
(299, 'black'), (200, 'black'), (0, 'black'))
for variant, data in enumerate(test_data, start=1):
current, expected = data
with self.subTest(f'variation #{variant}', voltage=voltage, current=current,
theoretical_max_power=theoretical_max_power, expected=expected):
# pylint: disable=assignment-from-no-return
actual_result = reactor_efficiency(voltage, current, theoretical_max_power)
failure_message = (f'Expected {expected} but returned {actual_result} '
f'with voltage={voltage}, current={current}, max_pow={theoretical_max_power}')
self.assertEqual(actual_result, expected, failure_message)
TEST FAILURE
One or more variations of this test failed. Details can be found under each [variant#].
Error Step 3
CODE RUN
temp = 10
threshold = 10000
test_data = ((399, 'LOW'), (300, 'LOW'), (1, 'LOW'),
(0, 'LOW'), (901, 'NORMAL'), (1000, 'NORMAL'),
(1099, 'NORMAL'), (899, 'LOW'), (700, 'LOW'),
(400, 'LOW'), (1101, 'DANGER'), (1200, 'DANGER'))
for variant, (neutrons_per_second, expected) in enumerate(test_data, start=1):
with self.subTest(f'variation #{variant}', temp=temp, neutrons_per_second=neutrons_per_second,
threshold=threshold, expected=expected):
# pylint: disable=assignment-from-no-return
actual_result = fail_safe(temp, neutrons_per_second, threshold)
failure_message = (f'Expected {expected} but returned {actual_result} with T={temp}, '
f'neutrons={neutrons_per_second}, threshold={threshold}')
self.assertEqual(actual_result, expected, failure_message)
TEST FAILURE
One or more variations of this test failed. Details can be found under each [variant#].