Skip to content

Commit

Permalink
Implement new Concept Exercise: conditionals exercism#1103
Browse files Browse the repository at this point in the history
* Created all required files for python concept - conditionals

* [CI] Format code

* CUsed prettier to format the files

* Used prettier to format the files

* First draft of introduction.md done

* Initial draft completed on introduction.md and working on after.md

* Made first draft of after.md

* Update languages/exercises/concept/conditionals/.docs/after.md

David cleaned up Grammar issues

Co-authored-by: David G <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

minor grammar issues fixed from davids suggestion

Co-authored-by: David G <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

grammar corrections ahh sigh!!

Co-authored-by: Corey McCandless <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: David G <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: David G <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: David G <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: David G <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: David G <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: David G <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: David G <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: David G <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: David G <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: David G <[email protected]>

* Modifications to after.md as per review
-  The modifications made at "one line if statement"
- The inital draft had mentioned about one line if statement after the example for it
-  Now the one line if statement style guide is before showing example

* Minor corrections as stated in initial review
- Chnaged the eerenious output at line 34 for elif statement

* Created the initial draft for design.md

* Modified 'config.json'
- Misinterpreted "forked-from" now removed that and ammeded the mistake

* Update languages/exercises/concept/conditionals/.docs/introduction.md

Accepting Grammar corrections from Eric

Co-authored-by: Erik Schierboom <[email protected]>

* Cleaned up and refined the after.md for approval

The after.md has been edited and cleaned. The file seems to be
easy to port to new docs for the concept exercises

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: Corey McCandless <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: Corey McCandless <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: Corey McCandless <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: Corey McCandless <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: Corey McCandless <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: Corey McCandless <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

Co-authored-by: Corey McCandless <[email protected]>

* Update languages/exercises/concept/conditionals/.docs/after.md

* Created the instruction.md
A draft of instruction.md is made with a theme for review
Refactoring will be required later to compile everything to new folder structure

* Update languages/exercises/concept/conditionals/.docs/instructions.md

Co-authored-by: valentin-p <[email protected]>

* Second Iteration
- Removed Most of the Typos
- Made the formating more in line with standars used
- Grmmar and words changed

* added the functions definitions and the parameters

* Generated the scaffolding for test cases

* Test cases for first function added to conditionals_test.py
Minor corrections to instruction.py

* Added test cases for second function in conditional_test.py
- Minor logical corrections in instruction.py

* Added the final functions test cases

* [CI] Format code

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: David G <[email protected]>
Co-authored-by: Corey McCandless <[email protected]>
Co-authored-by: Erik Schierboom <[email protected]>
Co-authored-by: valentin-p <[email protected]>
  • Loading branch information
6 people committed Jan 29, 2021
1 parent ad06c78 commit 78c1b5d
Show file tree
Hide file tree
Showing 9 changed files with 493 additions and 0 deletions.
138 changes: 138 additions & 0 deletions exercises/concept/conditionals/.docs/after.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
## General

Programs without conditional statements run sequentially (i.e. the code is executed line by line). However, this doesn't allow for the implementation of more complex logic. Conditional statements allow the program to execute different lines of code based on some factors. Python uses `if/else` statements to achieve this.

Let's look at the basic structure of python's `if/else` .

```python
if <expression>:
<code block 1 >
else:
<code block 2>
```

Now look at the following examples

```python
x = 5
y = 10

if x > y: # expression is True
print("x is greater than y")
>>> x is greater than y

if x < y: # expression is False
print("x is lower than y")
>>> # Nothing is executed

```

Here in the first `if` statement, the expression evaluates to `True`. Therefore, the statement associated with the `if` statement gets executed. In the next example, the expression evaluates to `False`, and therefore the statement associated with it is not executed.

## The indentation and blocks

The Python examples shown below are arranged into blocks using indentation. So, if there are multiple expressions that needs to be executed from one `if` statement we can put it in a block by having a uniform indentation throughout.

```python
if x > y: # expression is True
print("x is greater than y")
print("This is part of same block")
print("This is part of same block")

print("this is not part of the if block")
```

## The else clause

So far we made a single `if` statement. What if we wanted a program to execute one statement if the expression evaluates to `True` or execute an another statement if the expression evaluates to `False`
In such scenarios we can use the `else` clause

```python

x = 5

if x > 10: # Expression evaluvates to False
print("x is greater than 10")
else:
print("x is less or equal than 10") # Expression evaluates to True
>>> x is less or equal than 10

```

In this example we see that the `if` condition evaluates to `False` so the statement in the `else` block is executed.

A real world analogy to `if/else` statement is like this. If its sunny outside, then go to park. Otherwise read a book.

## The elif clause

Now that we have understood branching for sing `if` statement. What if we need to make several such alternatives in such cases we need to rely on `elif` clause. Which literally means else if. Here is an example using `elif`

```python
x = 20

if x < 5:
print("x is less than 5")
elif x < 10:
print("x is less than 10")
elif x < 30:
print("x is less than 30")
else:
print("None of the above")
>>> x is less than 30
```

Here first the code checks the first `if` condition. It finds that the expression evaluates to `False`. Therefore it goes into `elif` statement and sees if the expression inside `elif` evaluates to `True` if not it will continue to go through each `elif` expressions. If none of them evaluates to `True` then the conditional goes to the `else` clause and executes the statement in the `else` clause. Note that in the case where `elif` is used. The `else` clause can come only at the end of the expression.

## One line If statments

It is possible to write `if` conditionals in a single line ,However, as per the [PEP8][pep8-link] standards, the use of multiple statements on same lines are discouraged
[pep8-link]: https://www.python.org/dev/peps/pep-0008/#other-recommendations

Example

```python
x = 5
if x == 5: print("Came inside the if statement "); print("x equals 5 "); print("came to this part");
>>> Came inside the if statement x equals 5 came to this part
```

Here all the statements separated by a semicolon are considered as part of a single block. We can have more complicated ones like the example shown below. This is highly discouraging as this goes against Python's strength which is readability.

```python
x = 10
if x == 10: print('x '); print(' contains '); print(' 10')
elif x == 20: print('x '); print(' contains '); print(' 20')
else: print('x '); print(' contains '); print(' something else')

```

## Additional information

In the `if` clause python strictly doesn't need a `True/False` value. This will become evident with the following example.

```python

string = ""
string_1 = "Hello,World"

if string:
print(string)
elif string_1:
print(string_1)
else:
print("All are empty")

>>> Hello,World
```

In this example we did not evaluate if condition like this `if str == "":`. Even then it's understood by Python that if a string is empty evaluate it to `False` and evaluate to `True` otherwise.

Here is another on some pythonic expressions inside `if`

```python
x = ["A", "B", "C"]

if "A" in x:
print("A is inside x")
>>> A is inside x
```
1 change: 1 addition & 0 deletions exercises/concept/conditionals/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## placeholder text for hints.md
80 changes: 80 additions & 0 deletions exercises/concept/conditionals/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!--
Date: 26 Jan 2021
This is a draft to see if this is question theme is suitable or not and will change according to
review feedback
Date : 27 Jan 2021
Second Review, the task are satisfactory. Now Improving the first review
-->

In this exercise, we will 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 becomes less than criticality it can damage the reactor.
If it goes beyond criticality it can result in a meltdown.

You have three task, all related to maintaining the criticality of the reactor.

<!-- Problem 1 to teach basic if condition -->

## 1. Check for criticality

The first thing a control system must do is to check if the reactor is critical.
A reactor is said to be critical if it satisfies the following conditions.

- Temperature less than 800
- Number of neutrons emitted per second greater than 500
- The product of temperature and neutrons emitted per second less than 500000

Implement a function called `is_criticality_balanced()` that takes in 2 arguments the temperature
and neutrons emitted per second and returns a boolean True or False

```python
>>> is_criticality_balanced(750, 600)
True
```

<!-- For teaching elif -->

## 2. Determine the Power output range

Now that the reactor has started producing power we need to determine the efficiency of
the reactor.
The efficency can be grouped into 4 bands.

- green -> 80-100% efficency
- orange -> 60-79% efficency
- red -> 30-59% efficency
- black -> <30% efficent

efficency is calculated as `(generated power/ theoretical max power)*100`
where generated `power = voltage * current`

Implement a function called `reactor_efficency()` that takes in 3 arguments: `voltage`,
`current`, and `theoretical_max_power`. The function returns the efficiency band of the
reactor either 'green', 'orange', 'red', 'black'

```python
>>> reactor_efficency(200,50,1500)
'orange'
```

<!-- Intention is to teach use of if, elif and else -->

## 3. Fail Safe Mechanism

The final part involves creating a fail-safe mechanism. We can increase/decrease or stop the
criticality state of a reactor by inserting control rods into the reactor.

Implement a function called `fail_safe()` which takes in 3 parameters: `temperature`,
`neutrons produced per second`, and `threshold` to outputs status codes.

- If `temperature * neutrons per second` < 40% of threshold, output a status code of 'LOW'
indicating that the control rods must be removed to make it go critical and produce power

- If `temperature * neutrons per second` are within plus or minus 10% of the `threshold`
the reactor is in criticality and outputs a status code of 'NORMAL' , 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
44 changes: 44 additions & 0 deletions exercises/concept/conditionals/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## Conditionals

Conditionals are the most common statements used in programming. Conditionals determine the flow of control in a program. In python the `if` statement is used to control the flow of execution. An important aspect of conditions in an `if` statement is that it will be resolved to a `boolean` type to determine the flow of execution.

The basic syntax of an `if` statement is as follows

```python
x = 10
y = 5
# Variant 1
if x > y:
print("x is greater than y")

>>> x is greater than y

# Variant 2
x = 5
y = 10

if x > y:
print("x is greater than y")
else:
print("y is greater than x")

>>> y is greater than x

# Variant 3
x = 5
y = 10
z = 20
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
```

There are mainly 3 variants of the `if` statement as seen in above example

- Single `if` statement
- `if` with `else` clause
- `if` with `elif` clause
8 changes: 8 additions & 0 deletions exercises/concept/conditionals/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"authors": [
{
"github_username": "sachsom95",
"exercism_username": "sachsom95"
}
]
}
70 changes: 70 additions & 0 deletions exercises/concept/conditionals/.meta/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<!--
written:@sachsom95
updated: 22 Sep 2020
---
Note this file developed from the github issue https://github.com/exercism/v3/issues/1103 by @DavidGerva and @BethanyG
-->

# 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

- 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 operators

## Concepts

- `conditionals`
- `if`
- `elif`
- `else`

## Prerequisites

- `basics`
- `booleans`
- `comparisons`

## Resources to refer to

- [if statement][if statement]

## Hints

NEED TO DISCUSS THIS

## After

NEED TO DISCUSS THIS

## Representer

NEED HELP WITH THIS

## Analyzer

NEED HELP WITH THIS

## Implementing

Tests should be written using unittest.TestCase and the test file named conditionals_test.py.

## 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
1 change: 1 addition & 0 deletions exercises/concept/conditionals/.meta/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# example.py here
10 changes: 10 additions & 0 deletions exercises/concept/conditionals/conditionals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def is_criticality_balanced(temprature, neutrons_emitted_per_second):
pass


def reactor_efficency(voltage, current, theoretical_max_power):
pass


def fail_safe(temperature, neutrons_produced_per_second, threshold):
pass
Loading

0 comments on commit 78c1b5d

Please sign in to comment.