Python Forum
Meltdown Mitigation (operators + conditionals) beware: Exercism potential spoiler
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Meltdown Mitigation (operators + conditionals) beware: Exercism potential spoiler
#1
I’m working on a coding challenge courtesy of exercism.org. It’s self-paced study courseware. Even though it is not for-credit, I’d still like to preserve an element of self-discovery so if you are reading this, I am looking for as many hints as you people can provide (the more hints the better) but without providing the complete solution.

The challenge involves conditionals and precedence operators and basic math.

Here is the task as outlined by the author of this challenge:

Quote:The first thing a control system has to do is check if the reactor is balanced in criticality. A reactor is said to be critical if it satisfies the following conditions:
  • The temperature is less than 800.
  • The number of neutrons emitted per second is greater than 500.
  • The product of temperature and neutrons emitted per second is less than 500000.
Implement the function is_criticality_balanced() that takes temperature and neutrons_emitted as parameters, and returns True if the criticality conditions are met, False if not.

Here is my best attempt:

def is_criticality_balanced(temperature, neutrons_emitted):
   """ Verify criticality is balanced.
 
   :param temperature: temperature value (integer or float)
   :param neutrons_emitted: number of neutrons emitted per second (integer or float)
   :return:  boolean True if conditions met, False if not
 
   A reactor is said to be critical if it satisfies the following conditions:
   - The temperature is less than 800.
   - The number of neutrons emitted per second is greater than 500.
   - The product of temperature and neutrons emitted per second is less than 500 000.
   """
   if (temperature <= 800.00) and (neutrons_emitted > 500.00) and (temperature * neutrons_emitted < 500000.00):
       return True
   else:
       return False
  
The Doc String is very helpful. When I run the pytest against this solution I’ve written, here is the traceback I see:

Error:
FAILED conditionals_test.py::MeltdownMitigationTest::test_is_criticality_balanced - AssertionError: True != False : Expected False but returned True with T=800 and neutrinos=500.01
What this unit test says, according to my understanding, is that when the temperature argument is set to 800.00 and the neutrinos emitted argument is set to 500.01, the expected output is False but in my current iteration above, the function is returning True. That’s what the unit test is saying for those specific parameters. Since this is basic algebra, let’s interpolate those values as arguments into the conditional / algorithm I wrote:

Quote:if (800 <= 800.00) and (500.01 > 500) and (800*500.01 <500000):
return True

To evaluate that equation, it becomes:

Quote:if True and True and True:
return True

So according to my understanding, as far as I can tell, in this unit test, for these specific arguments (with 800 as temperature and 500.01 as neutrons emitted), the algorithm should evaluate to True but the unit test is saying it is expecting a False end result.

Therefore, my question for all of you is: What am I missing here?

What do I need to change so that my equation evaluates to False with those parameters?

I’ve leveraged a guide on Programiz to learn more about “Precedence and Associativity of Operators in Python”. The most helpful piece of information in that guide identifies how Python evaluates certain math and other symbols from first priority at the top (like (/) down to least priority at the bottom (like not/and/or). There is a chart which shows the hierarchy of Python symbols and precedence. I feel like I have a good understanding of that. But I am clearly still missing something here.

Here is the full unit test for this particular function:

class MeltdownMitigationTest(unittest.TestCase):
   """Test cases for Meltdown mitigation exercise.
   """
 
   @pytest.mark.task(taskno=1)
   def test_is_criticality_balanced(self):
       """Testing border cases around typical points.
 
       T, n == (800, 500), (625, 800), (500, 1000), etc.
 
       """
 
       test_data = ((750, 650, True), (799, 501, True), (500, 600, True),
                    (1000, 800, False), (800, 500, False), (800, 500.01, False),
                    (799.99, 500, False), (500.01, 999.99, False), (625, 800, False),
                    (625.99, 800, False), (625.01, 799.99, False), (799.99, 500.01, True),
                    (624.99, 799.99, True), (500, 1000, False), (500.01, 1000, False),
                    (499.99, 1000, True))
 
       for variant, data in enumerate(test_data, start=1):
           temp, neutrons_emitted, expected = data
           with self.subTest(f'variation #{variant}', temp=temp, neutrons_emitted=neutrons_emitted, expected=expected):
 
               # pylint: disable=assignment-from-no-return
               actual_result = is_criticality_balanced(temp, neutrons_emitted)
               failure_message = (f'Expected {expected} but returned {actual_result} '
                                  f'with T={temp} and neutrinos={neutrons_emitted}')
               self.assertEqual(actual_result, expected, failure_message)
 
Above is just the first function provided by the author of this challenge that I am working with right now. But for what it is worth, here are the additional two, in full:

""" Meltdown Mitigation exercise """


def is_criticality_balanced(temperature, neutrons_emitted):
    """Verify criticality is balanced.

    :param temperature: temperature value (integer or float)
    :param neutrons_emitted: number of neutrons emitted per second (integer or float)
    :return:  boolean True if conditions met, False if not

    A reactor is said to be critical if it satisfies the following conditions:
    - The temperature is less than 800.
    - The number of neutrons emitted per second is greater than 500.
    - The product of temperature and neutrons emitted per second is less than 500000.
    """

    pass


def reactor_efficiency(voltage, current, theoretical_max_power):
    """Assess reactor efficiency zone.

    :param voltage: voltage value (integer or float)
    :param current: current value (integer or float)
    :param theoretical_max_power: power that corresponds to a 100% efficiency (integer or float)
    :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
    """

    pass


def fail_safe(temperature, neutrons_produced_per_second, threshold):
    """Assess and return status code for the reactor.

    :param temperature: value of the temperature (integer or float)
    :param neutrons_produced_per_second: neutron flux (integer or float)
    :param threshold: threshold (integer or float)
    :return: str one of: 'LOW', 'NORMAL', 'DANGER'

    - `temperature * neutrons per second` < 90% of `threshold` == 'LOW'
    - `temperature * neutrons per second` +/- 10% of `threshold` == 'NORMAL'
    - `temperature * neutrons per second` is not in the above-stated ranges ==  'DANGER'
    """

    pass
Reply
#2
So condition is:

Quote:The temperature is less than 800.

You express it in Python:

if (temperature <= 800.00)
Do you see the problem now?
BashBedlam and Drone4four like this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
It is fairly rare that you should type "return True" or "return False". Not fixing anything, just reducing.
def is_criticality_balanced(temperature, neutrons_emitted):
   return temperature <= 800.0 and neutrons_emitted > 500.0 and temperature * neutrons_emitted < 500000.0
ndc85430 likes this post
Reply
#4
What Dean said. It makes sense if you think about it: the expression you use in an if is checked for its truth value and an expression like x < y obviously evaluates to True or False, so the if and else are just redundant.
Reply
#5
Changing the <= operator to < passed the unit test! When I was wrangling in my Python shell, I thought I tried both those possibilities already, along with other combinations elsewhere in the code block. It’s a lot of trial-and-error and sometimes it’s as if I haven’t saved my script properly before executing it, causing me to overlook the correct answer. Anyways, it works now. Dance
Reply
#6
Another error is that a neutron and a neutrino are not the same particle. Neutrino should be neutron in the error message.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tracking leap.py years for gregorian calendar (Exercism org) Drone4four 11 3,810 Oct-14-2022, 03:20 PM
Last Post: betinajessen
  Exercism with Python Gyga_Hawk 8 123,913 Mar-18-2022, 08:53 AM
Last Post: ndc85430
  Need help with a homework problem on logical operators voodoo 3 4,593 Jun-28-2019, 03:45 PM
Last Post: jefsummers
  Functions with conditionals and comparison operators Drone4four 9 12,926 Jan-01-2019, 06:48 PM
Last Post: Drone4four
  Guessing game with comparison operators Drone4four 9 13,781 Dec-02-2018, 06:12 PM
Last Post: ichabod801
  Nested Conditionals Elero 3 7,935 Apr-16-2018, 07:58 PM
Last Post: Elero
  Error with conditionals RiceGum 17 7,210 Oct-18-2017, 01:45 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020