From 1fad755ddcc0c8f9090530c7e3187715631eccec Mon Sep 17 00:00:00 2001 From: BethanyG Date: Fri, 14 May 2021 16:29:03 -0700 Subject: [PATCH 1/8] Created about, introduction and links files. --- concepts/conditionals/about.md | 2 +- concepts/conditionals/introduction.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/concepts/conditionals/about.md b/concepts/conditionals/about.md index 60d7eeb662..a6062008ec 100644 --- a/concepts/conditionals/about.md +++ b/concepts/conditionals/about.md @@ -1,6 +1,6 @@ # About -In Python, [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` statements are used in Python to [control the flow][control flow tools] of execution and make decisions in a program. +In Python, [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` statements are used to [control the flow][control flow tools] of execution and make decisions in a program. Unlike many other programming languages, Python versions 3.9 and below do not offer a formal case-switch statement, instead using multiple `elif` statements to serve a similar purpose. Python 3.10 introduces a variant case-switch statement called `pattern matching`, which will be covered separately in another concept. diff --git a/concepts/conditionals/introduction.md b/concepts/conditionals/introduction.md index 2d07471a41..e41a93e08f 100644 --- a/concepts/conditionals/introduction.md +++ b/concepts/conditionals/introduction.md @@ -1,6 +1,6 @@ # Introduction -In Python, [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` statements are used in Python to [control the flow][control flow tools] of execution and make decisions in a program. +In Python, [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` statements are used to [control the flow][control flow tools] of execution and make decisions in a program. Unlike many other programming languages, Python versions 3.9 and below do not offer a formal case-switch statement, instead using multiple `elif` statements to serve a similar purpose. Python 3.10 introduces a variant case-switch statement called `pattern matching`, which will be covered separately in another concept. From 39c284786d42d193259c5957ba5291fc96e8677c Mon Sep 17 00:00:00 2001 From: BethanyG Date: Sat, 29 May 2021 14:37:46 -0700 Subject: [PATCH 2/8] Attempted to remove hidden CLRF chars in code example. May have failed. --- concepts/conditionals/about.md | 316 ++++++++++++++++----------------- 1 file changed, 158 insertions(+), 158 deletions(-) diff --git a/concepts/conditionals/about.md b/concepts/conditionals/about.md index a6062008ec..9a7220c768 100644 --- a/concepts/conditionals/about.md +++ b/concepts/conditionals/about.md @@ -1,158 +1,158 @@ -# About - -In Python, [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` statements are used to [control the flow][control flow tools] of execution and make decisions in a program. -Unlike many other programming languages, Python versions 3.9 and below do not offer a formal case-switch statement, instead using multiple `elif` statements to serve a similar purpose. - -Python 3.10 introduces a variant case-switch statement called `pattern matching`, which will be covered separately in another concept. - -Conditional statements use expressions that must resolve to `True` or `False` -- either by returning a `bool` directly, or by evaluating ["truthy" or "falsy"][truth value testing]. - - - -```python -x = 10 -y = 5 - -# The comparison '>' returns the bool 'True', -# so the statement is printed. -if x > y: - print("x is greater than y") -... ->>> x is greater than y -``` - -When paired with `if`, an optional `else` code block will execute when the original `if` condition evaluates to `False`: - -```python -x = 5 -y = 10 - -# The comparison '>' here returns the bool False, -# so the 'else' block is executed instead of the 'if' block. -if x > y: - print("x is greater than y") -else: - print("y is greater than x") -... ->>> y is greater than x -``` - -`elif` allows for multiple evaluations/branches. - -```python -x = 5 -y = 10 -z = 20 - -# The elif statement allows for the checking of more conditions. -if x > y: - print("x is greater than y and z") -elif y > z: - print("y is greater than x and z") -else: - print("z is great than x and y") -... ->>> z is great than x and y -``` - -[Boolen operations][boolean operations] and [comparisons][comparisons] can be combined with conditionals for more complex testing: - -```python - ->>> def classic_fizzbuzz(number): - if number % 3 == 0 and number % 5 == 0: - return 'FizzBuzz!' - elif number % 5 == 0: - return 'Buzz!' - elif number % 3 == 0: - return 'Fizz!' - else: - return str(number) - ->>> classic_fizzbuzz(15) -'FizzBuzz!' - ->>> classic_fizzbuzz(13) -'13' -``` - -Conditionals can also be nested. - -```python ->>> def driving_status(driver_age, test_score): - if test_score >= 80: - if 18 > driver_age >= 16: - return "Student driver, needs supervision." - elif driver_age == 18: - return "Permitted driver, on probation." - elif driver_age > 18: - return "Fully licensed driver." - else: - return "Unlicensed!" - - ->>> driving_status(63, 78) -'Unlicsensed!' - ->>> driving_status(16, 81) -'Student driver, needs supervision.' - ->>> driving_status(23, 80) -'Fully licsensed driver.' -``` - -## Conditional expressions or "ternary operators" - -While Python has no specific `?` ternary operator, it is possible to write single-line `conditional expressions`. -These take the form of `` if `` else ``. -Since these expressions can become hard to read, it's recommended to use this single-line form only if it shortens code and helps readability. - - -```python -def just_the_buzz(number): - return 'Buzz!' if number % 5 == 0 else str(number) - ->>> just_the_buzz(15) -'Buzz!' - ->>> just_the_buzz(10) -'10' -``` - -## Truthy and Falsy - -In Python, any object can be tested for [truth value][truth value testing], and can therefore be used with a conditional, comparison, or boolean operation. -Objects that are evaluated in this fashion are considered "truthy" or "falsy", and used in a `boolean context`. - -```python ->>> def truthy_test(thing): - if thing: - print('This is Truthy.') - else: - print("Nope. It's Falsey.") - - -# Empty container objects are considered Falsey. ->>> truthy_test([]) -Nope. It's Falsey. - ->>> truthy_test(['bear', 'pig', 'giraffe']) -This is Truthy. - -# Empty strings are considered Falsey. ->>> truthy_test('') -Nope. It's Falsey. - ->>> truthy_test('yes') -This is Truthy. - -# 0 is also considered Falsey. ->>> truthy_test(0) -Nope. It's Falsey. -``` - -[if statement]: https://docs.python.org/3/reference/compound_stmts.html#the-if-statement -[control flow tools]: https://docs.python.org/3/tutorial/controlflow.html#more-control-flow-tools -[truth value testing]: https://docs.python.org/3/library/stdtypes.html#truth-value-testing -[boolean operations]: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not -[comparisons]: https://docs.python.org/3/library/stdtypes.html#comparisons +# About + +In Python, [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` statements are used to [control the flow][control flow tools] of execution and make decisions in a program. +Unlike many other programming languages, Python versions 3.9 and below do not offer a formal case-switch statement, instead using multiple `elif` statements to serve a similar purpose. + +Python 3.10 introduces a variant case-switch statement called `pattern matching`, which will be covered separately in another concept. + +Conditional statements use expressions that must resolve to `True` or `False` -- either by returning a `bool` directly, or by evaluating ["truthy" or "falsy"][truth value testing]. + + + +```python +x = 10 +y = 5 + +# The comparison '>' returns the bool 'True', +# so the statement is printed. +if x > y: + print("x is greater than y") +... +>>> x is greater than y +``` + +When paired with `if`, an optional `else` code block will execute when the original `if` condition evaluates to `False`: + +```python +x = 5 +y = 10 + +# The comparison '>' here returns the bool False, +# so the 'else' block is executed instead of the 'if' block. +if x > y: + print("x is greater than y") +else: + print("y is greater than x") +... +>>> y is greater than x +``` + +`elif` allows for multiple evaluations/branches. + +```python +x = 5 +y = 10 +z = 20 + +# The elif statement allows for the checking of more conditions. +if x > y: + print("x is greater than y and z") +elif y > z: + print("y is greater than x and z") +else: + print("z is great than x and y") +... +>>> z is great than x and y +``` + +[Boolen operations][boolean operations] and [comparisons][comparisons] can be combined with conditionals for more complex testing: + +```python + +>>> def classic_fizzbuzz(number): + if number % 3 == 0 and number % 5 == 0: + return 'FizzBuzz!' + elif number % 5 == 0: + return 'Buzz!' + elif number % 3 == 0: + return 'Fizz!' + else: + return str(number) + +>>> classic_fizzbuzz(15) +'FizzBuzz!' + +>>> classic_fizzbuzz(13) +'13' +``` + +Conditionals can also be nested. + +```python +>>> def driving_status(driver_age, test_score): + if test_score >= 80: + if 18 > driver_age >= 16: + return "Student driver, needs supervision." + elif driver_age == 18: + return "Permitted driver, on probation." + elif driver_age > 18: + return "Fully licensed driver." + else: + return "Unlicensed!" + + +>>> driving_status(63, 78) +'Unlicsensed!' + +>>> driving_status(16, 81) +'Student driver, needs supervision.' + +>>> driving_status(23, 80) +'Fully licsensed driver.' +``` + +## Conditional expressions or "ternary operators" + +While Python has no specific `?` ternary operator, it is possible to write single-line `conditional expressions`. +These take the form of `` if `` else ``. +Since these expressions can become hard to read, it's recommended to use this single-line form only if it shortens code and helps readability. + + +```python +def just_the_buzz(number): + return 'Buzz!' if number % 5 == 0 else str(number) + +>>> just_the_buzz(15) +'Buzz!' + +>>> just_the_buzz(10) +'10' +``` + +## Truthy and Falsy + +In Python, any object can be tested for [truth value][truth value testing], and can therefore be used with a conditional, comparison, or boolean operation. +Objects that are evaluated in this fashion are considered "truthy" or "falsy", and used in a `boolean context`. + +```python +>>> def truthy_test(thing): + if thing: + print('This is Truthy.') + else: + print("Nope. It's Falsey.") + + +# Empty container objects are considered Falsey. +>>> truthy_test([]) +Nope. It's Falsey. + +>>> truthy_test(['bear', 'pig', 'giraffe']) +This is Truthy. + +# Empty strings are considered Falsey. +>>> truthy_test('') +Nope. It's Falsey. + +>>> truthy_test('yes') +This is Truthy. + +# 0 is also considered Falsey. +>>> truthy_test(0) +Nope. It's Falsey. +``` + +[if statement]: https://docs.python.org/3/reference/compound_stmts.html#the-if-statement +[control flow tools]: https://docs.python.org/3/tutorial/controlflow.html#more-control-flow-tools +[truth value testing]: https://docs.python.org/3/library/stdtypes.html#truth-value-testing +[boolean operations]: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not +[comparisons]: https://docs.python.org/3/library/stdtypes.html#comparisons From 06085ad3bf29d31a24a72f8adba326898fdea691 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Sat, 29 May 2021 22:52:46 -0700 Subject: [PATCH 3/8] Rewrote/added instructions and introduction files. --- .../meltdown-mitigation/.docs/instructions.md | 72 +++++++++++++++++ .../meltdown-mitigation/.docs/introduction.md | 81 +++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 exercises/concept/meltdown-mitigation/.docs/instructions.md create mode 100644 exercises/concept/meltdown-mitigation/.docs/introduction.md diff --git a/exercises/concept/meltdown-mitigation/.docs/instructions.md b/exercises/concept/meltdown-mitigation/.docs/instructions.md new file mode 100644 index 0000000000..809b93a571 --- /dev/null +++ b/exercises/concept/meltdown-mitigation/.docs/instructions.md @@ -0,0 +1,72 @@ +# Instructions + +In this exercise, we'll develop a simple control system for a nuclear reactor. + +For a reactor to produce the power it must be in a state of _criticality_. +If the reactor is in a state less than criticality, it can become damaged. +If the reactor state goes beyond criticality, it can overload and result in a meltdown. +We want to mitigte the chances of meltdown and correctly manage reactor state. + +The following three tasks are all related to writing code for maintaining ideal reactor state. + +## 1. Check for criticality + +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 less than 800. +- The number of neutrons emitted per second greater than 500. +- The product of temperature and neutrons emitted per second 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. + +```python +>>> is_criticality_balanced(750, 600) +True +``` + +## 2. Determine the Power output range + +Once the reactor has started producing power its efficiency needs to be determined. +Efficiency can be grouped into 4 bands: + +1. green -> 80-100% efficiency +2. orange -> 60-79% efficiency +3. red -> 30-59% efficiency +4. black -> <30% efficient + +These percentage ranges are calculated as `(generated_power/ theoretical_max_power)*100` +where generated `power = voltage * current` + +Implement the function `reactor_efficency()`, with three parameters: `voltage`, +`current`, and `theoretical_max_power`. +This function should return the efficiency band of the reactor : 'green', 'orange', 'red', or 'black'. + +```python +>>> reactor_efficency(200,50,1500) +'orange' +``` + +## 3. Fail Safe Mechanism + +Your final task involves creating a fail-safe mechanism to avoid overload and meltdown. +This mechanism will determine if the reactor is below, at, or above the ideal criticality threshold. +Criticality can then be increased, decreased, or stopped by inserting (or removing) control rods into the reactor. + +Implement the function called `fail_safe()`, which takes 3 parameters: `temperature`, +`neutrons_produced_per_second`, and `threshold`, and outputs a status code for the reactor. + +- If `temperature * neutrons_per_second` < 40% of threshold, output a status code of 'LOW' + indicating that control rods must be removed to produce power. + +- If `temperature * neutrons_per_second` are within plus or minus 10% of the `threshold` + the reactor is in _criticality_ and the status code of 'NORMAL' should be output, indicating that the + reactor is in optimum condition and control rods are in an idea position. + +- If `temperature * neutron_per_second` is not in the above-stated ranges, the reactor is + going into meltdown and a status code of 'DANGER' must be passed to immediately shut down the reactor. + +```python +>>> fail_safe(temperature=1000, neutrons_produced_per_second=30, threshold=5000) +'DANGER' +``` diff --git a/exercises/concept/meltdown-mitigation/.docs/introduction.md b/exercises/concept/meltdown-mitigation/.docs/introduction.md new file mode 100644 index 0000000000..16aceb4deb --- /dev/null +++ b/exercises/concept/meltdown-mitigation/.docs/introduction.md @@ -0,0 +1,81 @@ +# Conditionals + +In Python, [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` statements are used to [control the flow][control flow tools] of execution and make decisions in a program. +Unlike many other programming langauges, Python versions 3.9 and below do not offer a formal case-switch statement, instead using multiple `elif` statements to serve a similar purpose. + +Python 3.10 introduces a variant case-switch statement called `pattern matching`, which will be covered separately in another concept. + +Conditional statements use expressions that must resolve to `True` or `False` -- either by returning a `bool` directly, or by evaluating ["truthy" or "falsy"][truth value testing]. + +```python +x = 10 +y = 5 + +# The comparison '>' returns the bool 'True', +# so the statement is printed. +if x > y: + print("x is greater than y") +... +>>> x is greater than y +``` + +When paired with `if`, an optional `else` code block will execute when the original `if` condition evaluates to `False`: + +```python +x = 5 +y = 10 + +# The comparison '>' here returns the bool False, +# so the 'else' block is executed instead of the 'if' block. +if x > y: + print("x is greater than y") +else: + print("y is greater than x") +... +>>> y is greater than x +``` + +`elif` allows for multiple evaluations/branches. + +```python +x = 5 +y = 10 +z = 20 + +# The elif statement allows for the checking of more conditions. +if x > y: + print("x is greater than y and z") +elif y > z: + print("y is greater than x and z") +else: + print("z is great than x and y") +... +>>> z is great than x and y +``` + +[Boolen operations][boolean operations] and [comparisons][comparisons] can be combined with conditionals for more complex testing: + +```python + +>>> def classic_fizzbuzz(number): + if number % 3 == 0 and number % 5 == 0: + return 'FizzBuzz!' + elif number % 5 == 0: + return 'Buzz!' + elif number % 3 == 0: + return 'Fizz!' + else: + return str(number) + +>>> classic_fizzbuzz(15) +'FizzBuzz!' + +>>> classic_fizzbuzz(13) +'13' +``` + +[if statement]: https://docs.python.org/3/reference/compound_stmts.html#the-if-statement +[control flow tools]: https://docs.python.org/3/tutorial/controlflow.html#more-control-flow-tools +[truth value testing]: https://docs.python.org/3/library/stdtypes.html#truth-value-testing +[boolean operations]: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not +[comparisons]: https://docs.python.org/3/library/stdtypes.html#comparisons From 83a228aa451abce72da58c6bf10b3b1111122793 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Sat, 29 May 2021 22:53:39 -0700 Subject: [PATCH 4/8] Added config design, and exemplar files. --- .../meltdown-mitigation/.meta/config.json | 10 +++ .../meltdown-mitigation/.meta/design.md | 79 +++++++++++++++++++ .../meltdown-mitigation/.meta/exemplar.py | 72 +++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 exercises/concept/meltdown-mitigation/.meta/config.json create mode 100644 exercises/concept/meltdown-mitigation/.meta/design.md create mode 100644 exercises/concept/meltdown-mitigation/.meta/exemplar.py diff --git a/exercises/concept/meltdown-mitigation/.meta/config.json b/exercises/concept/meltdown-mitigation/.meta/config.json new file mode 100644 index 0000000000..9348eabdb6 --- /dev/null +++ b/exercises/concept/meltdown-mitigation/.meta/config.json @@ -0,0 +1,10 @@ +{ + "blurb": "Learn about conditionals and avoid a meltdown by developing a simple control system for a Nuclear Reactor.", + "icon": "circular-buffer", + "authors": ["sachsom95", "BethanyG"], + "files": { + "solution": ["conditionals.py"], + "test": ["conditionals_test.py"], + "exemplar": [".meta/exemplar.py"] + } +} diff --git a/exercises/concept/meltdown-mitigation/.meta/design.md b/exercises/concept/meltdown-mitigation/.meta/design.md new file mode 100644 index 0000000000..cdc482c6d0 --- /dev/null +++ b/exercises/concept/meltdown-mitigation/.meta/design.md @@ -0,0 +1,79 @@ +# Design + + +## Goal + +The goal of this exercise is to teach the student what is a `conditional` and how they are used in Python. + +## Learning objectives + +- learn some general things about `control flow` in python +- create a `conditional` structure to choose something, take a decision +- use an `if...else` structure +- use an `if..elif...else` structure + +## Out of scope + +- `ternary expressions` + +## Concepts + +- `conditionals` +- `if` +- `elif` +- `else` + +## Prerequisites + +- `basics` +- `bools` +- `comparisons` + +## Resources to refer to + +- [if statement][if statement] +- [control flow tools][control flow tools] +- [Real Python : Conditional Statements in Python][conditional statements in python] + +## Hints + +For more information on writing hints see [hints](https://github.com/exercism/docs/blob/main/anatomy/tracks/concept-exercises.md#file-docshintsmd) + + * You can refer to one or more of the resources linked , or analogous resources from a trusted source. We prefer using links within the [Python Docs](https://docs.python.org/3/) as the primary go-to, but other resources listed above are also good. Please try to avoid paid or subscription-based links if possible. + +* ### `links.json` + + For more information, see [concept links file](https://github.com/exercism/docs/blob/main/anatomy/tracks/concepts.md#file-linksjson) + + - The same resources listed can be used as a starting point for the [ `concepts/links.json`](https://github.com/exercism/docs/blob/main/anatomy/tracks/concepts.md#file-linksjson) file, if it doesn't already exist. + - If there are particularly good/interesting information sources for this concept that extend or supplement the concept exercise material & the resources already listed -- please add them to the `links.json` document. + +## Representer + +No changes needed + +## Analyzer + +No changes needed at this time. + +## Implementing + +- Code in the `.meta/examplar.py` file should **only use syntax & concepts introduced in this exercise or one of its prerequisite exercises.** + +- Please **do not use** comprehensions, generator expressions, or other syntax not previously covered. Please also follow [PEP8](https://www.python.org/dev/peps/pep-0008/) guidelines. + +- In General, tests should be written using `unittest.TestCase` and the test file should be named `_test.py` or `_test.py`. + + +- While we do use [PyTest](https://docs.pytest.org/en/stable/) as our test runner and for some implementation tests, please check with a maintainer before using a PyTest test method, fixture, or feature. + +- Our markdown and JSON files are checked against [prettier](https://prettier.io/) . We recommend [setting prettier up locally](https://prettier.io/docs/en/install.html) and running it prior to submitting your PR to avoid any CI errors. + +## Edits + +edited for updated naming by @yawpitch +edited for prerequisites and learning objectives detail by @BethanyG + +[if statement]: https://docs.python.org/3/reference/compound_stmts.html#the-if-statement +[control flow tools]: https://docs.python.org/3/tutorial/controlflow.html#more-control-flow-tools +[conditional statements in python]: https://realpython.com/python-conditional-statements/ diff --git a/exercises/concept/meltdown-mitigation/.meta/exemplar.py b/exercises/concept/meltdown-mitigation/.meta/exemplar.py new file mode 100644 index 0000000000..e001afcea7 --- /dev/null +++ b/exercises/concept/meltdown-mitigation/.meta/exemplar.py @@ -0,0 +1,72 @@ +def is_criticality_balanced(temprature, neutrons_emitted): + ''' + + :param temprature: int + :param neutrons_emitted: int + :return: boolen True if conditions met, False if not + + A reactor is said to be critical if it satisfies the following conditions: + - The temperature less than 800. + - The number of neutrons emitted per second greater than 500. + - The product of temperature and neutrons emitted per second less than 500000. + ''' + output = temprature * neutrons_emitted + + if (temprature < 800 and neutrons_emitted > 500) and output < 500000: + return True + else: + return False + + +def reactor_efficency(voltage, current, theoretical_max_power): + ''' + + :param voltage: int + :param current: int + :param theoretical_max_power: int + :return: str one of 'green', 'orange', 'red', or 'black' + + Efficency can be grouped into 4 bands: + + 1. green -> 80-100% efficency + 2. orange -> 60-79% efficency + 3. red -> 30-59% efficency + 4. black -> <30% efficent + + These percentage ranges are calculated as + (generated power/ theoretical max power)*100 + where generated power = voltage * current + ''' + generated_power = voltage * current + percentage_range = (generated_power/theoretical_max_power)*100 + + if 80 <= percentage_range <= 100: + return 'green' + elif 60 <= percentage_range <= 79: + return 'orange' + elif 30 <= percentage_range <= 59: + return 'red' + else: + return 'black' + +def fail_safe(temperature, neutrons_produced_per_second, threshold): + ''' + + :param temperature: + :param neutrons_produced_per_second: + :param threshold: + :return: str one of: 'LOW', 'NORMAL', 'DANGER' + + - `temperature * neutrons per second` < 40% of threshold == 'LOW' + - `temperature * neutrons per second` +/- 10% of `threshold` == 'NORMAL' + - `temperature * neutron per second` is not in the above-stated ranges == 'DANGER' + ''' + output = temperature * neutrons_produced_per_second + operational_percentage = int((output/threshold) * 100) + + if operational_percentage < 40: + return 'LOW' + elif 90 <= operational_percentage <= 110: + return 'NORMAL' + else: + return 'DANGER' From 0fe66b75149ac6c07ef80f3754674115e7f34397 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Sat, 29 May 2021 22:54:37 -0700 Subject: [PATCH 5/8] Added stub file. --- .../meltdown-mitigation/conditionals.py | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 exercises/concept/meltdown-mitigation/conditionals.py diff --git a/exercises/concept/meltdown-mitigation/conditionals.py b/exercises/concept/meltdown-mitigation/conditionals.py new file mode 100644 index 0000000000..238b4e87d5 --- /dev/null +++ b/exercises/concept/meltdown-mitigation/conditionals.py @@ -0,0 +1,50 @@ +def is_criticality_balanced(temprature, neutrons_emitted): + ''' + + :param temprature: int + :param neutrons_emitted: int + :return: boolen True if conditions met, False if not + + A reactor is said to be critical if it satisfies the following conditions: + - The temperature less than 800. + - The number of neutrons emitted per second greater than 500. + - The product of temperature and neutrons emitted per second less than 500000. + ''' + pass + + +def reactor_efficency(voltage, current, theoretical_max_power): + ''' + + :param voltage: int + :param current: int + :param theoretical_max_power: int + :return: str one of 'green', 'orange', 'red', or 'black' + + Efficency can be grouped into 4 bands: + + 1. green -> 80-100% efficency + 2. orange -> 60-79% efficency + 3. red -> 30-59% efficency + 4. black -> <30% efficent + + These percentage ranges are calculated as + (generated power/ theoretical max power)*100 + where generated power = voltage * current + ''' + pass + + +def fail_safe(temperature, neutrons_produced_per_second, threshold): + ''' + + :param temperature: + :param neutrons_produced_per_second: + :param threshold: + :return: str one of: 'LOW', 'NORMAL', 'DANGER' + + - `temperature * neutrons per second` < 40% of threshold == 'LOW' + - `temperature * neutrons per second` +/- 10% of `threshold` == 'NORMAL' + - `temperature * neutron per second` is not in the above-stated ranges == 'DANGER' + ''' + pass From f17fca1f0e52d300a6e6f83a03028b6b13c59576 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Sat, 29 May 2021 22:55:19 -0700 Subject: [PATCH 6/8] Edited tests file and added task numbering. --- .../meltdown-mitigation/conditionals_test.py | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 exercises/concept/meltdown-mitigation/conditionals_test.py diff --git a/exercises/concept/meltdown-mitigation/conditionals_test.py b/exercises/concept/meltdown-mitigation/conditionals_test.py new file mode 100644 index 0000000000..9005e72f1e --- /dev/null +++ b/exercises/concept/meltdown-mitigation/conditionals_test.py @@ -0,0 +1,163 @@ +# unit test here +import unittest +import pytest +from conditionals import (is_criticality_balanced, + reactor_efficency, + fail_safe + ) + + +class TestConditionals(unittest.TestCase): + # Checking the first condition using assertTrue and assertFalse + # The values for arguments is not final and should be considered as placeholders + # More test-cases required for full testing + + @pytest.mark.task(taskno=1) + def test_is_criticality_balanced_set1(self): + + self.assertTrue(is_criticality_balanced( + temprature=750, neutrons_emitted=650), msg="Expected True but returned False") + + @pytest.mark.task(taskno=1) + def test_is_criticality_balanced_set2(self): + + self.assertTrue(is_criticality_balanced( + temprature=799, neutrons_emitted=501), msg="Expected True but returned False") + + @pytest.mark.task(taskno=1) + def test_is_criticality_balanced_set3(self): + + self.assertTrue( + is_criticality_balanced(temprature=500, neutrons_emitted=600), msg="Expected True but returned False" + ) + + @pytest.mark.task(taskno=1) + def test_is_criticality_balanced_set4(self): + + self.assertFalse( + is_criticality_balanced(temprature=800, neutrons_emitted=500), msg="Expected False but returned True" + ) + +# End of first functions testing + +# Test case for reactor_efficency() + # Checking the second condition using assertEqual + # The values for arguments is not final and should be considered as placeholders + # More test-cases required for full testing + # need to add more info to messages + # Need to verify if f-string based errors allowed + + @pytest.mark.task(taskno=2) + def test_reactor_efficency_set1(self): + + test_return = reactor_efficency( + voltage=100, current=50, theoretical_max_power=5000) + self.assertEqual( + test_return, 'green', msg=f"Expected green but returned {test_return}" + ) + + @pytest.mark.task(taskno=2) + def test_reactor_efficency_set2(self): + test_return = reactor_efficency( + voltage=100, current=30, theoretical_max_power=5000) + self.assertEqual( + test_return, 'orange', msg=f"Expected orange but returned {test_return}" + ) + + @pytest.mark.task(taskno=2) + def test_reactor_efficency_set3(self): + test_return = reactor_efficency( + voltage=100, current=28, theoretical_max_power=5000) + self.assertEqual( + test_return, 'red', msg=f"Expected red but returned {test_return}" + ) + + @pytest.mark.task(taskno=2) + def test_reactor_efficency_set4(self): + test_return = reactor_efficency( + voltage=100, current=10, theoretical_max_power=5000) + self.assertEqual( + test_return, 'black', msg=f"Expected black but returned {test_return}" + ) + +# End of second function testing + + +# Test case for fail_safe() + # Checking the third condition using assertEqual + # The values for arguments is not final and should be considered as placeholders + # More test-cases required for full testing + # need to add more info to messages + # Need to verify if f-string based errors allowed + + @pytest.mark.task(taskno=3) + def test_fail_safe_set1(self): + test_return = fail_safe( + temperature=100, neutrons_produced_per_second=18, threshold=5000) + self.assertEqual( + test_return, 'LOW', msg=f"Expected LOW but returned {test_return}" + ) + + @pytest.mark.task(taskno=3) + def test_fail_safe_set2(self): + test_return = fail_safe( + temperature=100, neutrons_produced_per_second=12, threshold=4000) + self.assertEqual( + test_return, 'LOW', msg=f"Expected LOW but returned {test_return}" + ) + + @pytest.mark.task(taskno=3) + def test_fail_safe_set3(self): + test_return = fail_safe( + temperature=100, neutrons_produced_per_second=10, threshold=3000) + self.assertEqual( + test_return, 'LOW', msg=f"Expected LOW but returned {test_return}" + ) + + @pytest.mark.task(taskno=3) + def test_fail_safe_set4(self): + test_return = fail_safe( + temperature=100, neutrons_produced_per_second=55, threshold=5000) + self.assertEqual( + test_return, 'NORMAL', msg=f"Expected NORMAL but returned {test_return}" + ) + + @pytest.mark.task(taskno=3) + def test_fail_safe_set5(self): + test_return = fail_safe( + temperature=100, neutrons_produced_per_second=45, threshold=5000) + self.assertEqual( + test_return, 'NORMAL', msg=f"Expected NORMAL but returned {test_return}" + ) + + @pytest.mark.task(taskno=3) + def test_fail_safe_set6(self): + test_return = fail_safe( + temperature=100, neutrons_produced_per_second=50, threshold=5000) + self.assertEqual( + test_return, 'NORMAL', msg=f"Expected NORMAL but returned {test_return}" + ) + + @pytest.mark.task(taskno=3) + def test_fail_safe_set7(self): + test_return = fail_safe( + temperature=1000, neutrons_produced_per_second=35, threshold=5000) + self.assertEqual( + test_return, 'DANGER', msg=f"Expected DANGER but returned {test_return}" + ) + + @pytest.mark.task(taskno=3) + def test_fail_safe_set8(self): + test_return = fail_safe( + temperature=1000, neutrons_produced_per_second=30, threshold=5000) + self.assertEqual( + test_return, 'DANGER', msg=f"Expected DANGER but returned {test_return}" + ) + + @pytest.mark.task(taskno=3) + def test_fail_safe_set9(self): + test_return = fail_safe( + temperature=1000, neutrons_produced_per_second=25, threshold=5000) + self.assertEqual( + test_return, 'DANGER', msg=f"Expected DANGER but returned {test_return}" + ) From 73bb172f1022a6df5a79079a8d86c1ac448e5646 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Sat, 29 May 2021 22:55:39 -0700 Subject: [PATCH 7/8] Added hints file. --- .../meltdown-mitigation/.docs/hints.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 exercises/concept/meltdown-mitigation/.docs/hints.md diff --git a/exercises/concept/meltdown-mitigation/.docs/hints.md b/exercises/concept/meltdown-mitigation/.docs/hints.md new file mode 100644 index 0000000000..fb2be75c3a --- /dev/null +++ b/exercises/concept/meltdown-mitigation/.docs/hints.md @@ -0,0 +1,48 @@ +# Hints + +## General + +- The Python Docs on [Control Flow Tools][control flow tools] and the Real Python tutorial on [conditionals][real python conditionals] are great places to start. +- The Python Docs on [Boolean Operations][boolean operations] can be a great refresher on `bools`, as can the Real Python tutorial on [booleans][python booleans]. +- The Python Docs on [Comparisons][comparisons] and [comparisons examples][python comparisons examples] can be a great refresher for comparisons. + +## 1. Check for criticality + +- Comparison operators and boolean operations can be combined and used with conditionals. +- Conditional expressions must evaluate to `True` or `False`. +- `else` can be used for a code block that will execute when a conditional test returns `False`. + + ```python + >>> item = 'blue' + >>> item_2 = 'green' + + >>> if len(item) >=3 and len(item_2) < 5: + print('Both pass the test!') + elif len(item) >=3 or len(item_2) < 5: + print('One passes the test!') + else: + print('None pass the test!') + ... + One passes the test! + ``` + +## 2. Determine the Power output range + +- Comparison operators can be combined and used with conditionals. +- Any number of `elif` statements can be used as "branches". +- Each "branch" can have a separate `return` + +## 3. Fail Safe Mechanism + +- Comparison operators can be combined and used with conditionals. +- Any number of `elif` statements can be used as "branches". +- Each "branch" can have a separate `return` + + +[python comparisons examples]: https://www.tutorialspoint.com/python/comparison_operators_example.htm +[boolean operations]: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not +[comparisons]: https://docs.python.org/3/library/stdtypes.html#comparisons +[python booleans]: https://realpython.com/python-boolean/ +[real python conditionals]: https://realpython.com/python-conditional-statements/ +[control flow tools]: https://docs.python.org/3/tutorial/controlflow.html + From 44d0d3030686c67e97bd0646b1ce482295688eb3 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Sat, 29 May 2021 22:56:05 -0700 Subject: [PATCH 8/8] Added exercise entry in track config.json. --- config.json | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/config.json b/config.json index e5cdf23da3..3ff31cc880 100644 --- a/config.json +++ b/config.json @@ -34,6 +34,14 @@ "prerequisites": ["basics"], "status": "wip" }, + { + "slug": "meltdown-mitigation", + "name": "Meltdown Mitigation", + "uuid": "50c88b36-e2e2-46e5-b6c4-bb7e714c375a", + "concepts": ["conditionals"], + "prerequisites": ["basics", "booleans", "comparisons"], + "status": "wip" + }, { "slug": "tisbury-treasure-hunt", "name": "Tisbury Treasure Hunt",