Skip to content

Commit 0e8b832

Browse files
mohanrajanrBethanyG
authored andcommitted
Implement new Concept Exercise: loops exercism#1430 (exercism#2302)
* Loops Concept Exercise First Change * Review Change 1 * Update languages/exercises/concept/loops/.docs/instructions.md * Update languages/exercises/concept/loops/.docs/instructions.md * Update languages/exercises/concept/loops/.docs/instructions.md * Update languages/exercises/concept/loops/.docs/instructions.md * Update languages/exercises/concept/loops/.docs/instructions.md * Update languages/exercises/concept/loops/.docs/instructions.md * Update languages/exercises/concept/loops/.docs/instructions.md * Update languages/exercises/concept/loops/.docs/introduction.md * Update languages/exercises/concept/loops/.docs/introduction.md * Update languages/exercises/concept/loops/.docs/introduction.md Co-authored-by: BethanyG <[email protected]>
1 parent b874f00 commit 0e8b832

File tree

9 files changed

+415
-0
lines changed

9 files changed

+415
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
## Loops in Python
2+
3+
There is always a scenario where you would need to go through a [`list` or `group`] of [`things` or `object` or `values`].
4+
When you have an `array` of objects or values to process, you need to `iterate` on the array and process them. You are going to do a repeated set of instructions on every single object or value in an array.
5+
6+
There are 2 ways to loop through objects.
7+
8+
- `while` loop
9+
- `for` loop
10+
11+
## While loops
12+
13+
While loops are based on `conditional` statement for looping through objects.
14+
15+
```
16+
while expression:
17+
set_of_statements
18+
```
19+
20+
When the expression evaluates to `True` (truthiness) the loop will execute the set of statements till it reaches the end of the indent block. It will execute the loop again until the expression evaluates to `False`.
21+
22+
```python
23+
i = 0
24+
while i < 3:
25+
print(i)
26+
# => 0
27+
# => 1
28+
# => 2
29+
```
30+
31+
## For Loops
32+
33+
While loops are based on `Iterational` statement. The Loop will iterate over a `iterable` object.
34+
35+
```
36+
for item in iterable_object:
37+
set_of_statements
38+
```
39+
40+
```python
41+
list_of_items = [1, 2, 3]
42+
for item in list_of_items:
43+
print(item)
44+
# => 1
45+
# => 2
46+
# => 3
47+
```
48+
49+
## Breaking from loops
50+
51+
Where you have a large set of objects that you want to loop through and you dont want to go through all the objects in the loop but rather go through a certain number of desired objects and then stop the execution, then `break` comes to the rescue.
52+
53+
When you want to break from an iterable loop on any condition, you can use the `break` statement
54+
55+
```python
56+
list_of_items = [1, 2, 3, 4, 5, 6, 7, 8]
57+
for item in list_of_items:
58+
if item > 5:
59+
break
60+
print(item)
61+
# => 1
62+
# => 2
63+
# => 3
64+
# => 4
65+
# => 5
66+
```
67+
68+
## Continuing in loops
69+
70+
You will have important objects as well as non important ones. When you want to skip over the objects that you dont need to process you use the continue statement.
71+
72+
Example: you want to process only odd numbers in a loop and not the event numbers.
73+
74+
```python
75+
all_numbers = [1, 2, 3, 4, 5, 6, 7, 8]
76+
for num in all_numbers:
77+
if num % 2 == 0:
78+
continue
79+
print(num)
80+
# => 1
81+
# => 3
82+
# => 5
83+
# => 7
84+
```
85+
86+
## Else in Loops
87+
88+
An `else` clause in a loop will get executed when there is no `break` that happebed while you were processing your loop.
89+
This gets useful if you want to go over fail over scenarios.
90+
91+
```python
92+
x = None
93+
for i in list_of_items:
94+
if i > 10:
95+
x = i
96+
break
97+
else:
98+
x = 10
99+
```
100+
101+
What the above code does is it sets the variable x to 10 if the loop inside was not able to find an item that is more than 10.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## General
2+
3+
- Make sure you are comfortable with using the `in` concept while looping through in for loops.
4+
5+
You are a teacher and you are correcting papers of your students who wrote an exam you just conducted. You are tasked with the following activities.
6+
7+
## 1. Failed Students
8+
9+
- Iterate through the marks one by one. For each loop check if the mark is less than or equal to 40. If so, then increment a count and after iterating through all marks in the loop return the count.
10+
11+
## 2. Top Marks
12+
13+
- Iterate through the list of marks one by one. If you find a mark which is more than x, just continue and go to the next mark.
14+
15+
## 3. First K Students.
16+
17+
- Start an index variable at 0 and loop through the elements till we reach the index variable equals to k. once that happens you can return the marks.
18+
19+
## 4. Full Marks
20+
21+
- There may be or may not be a student with full marks - 100. Either way you have to loop through all the marks and find whether we have a student with full marks else just return No Hundreds
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
You are a teacher and you are correcting papers of your students who wrote an exam you just conducted. You are tasked with the following activities.
2+
3+
## 1. Failed Students
4+
5+
Create the function `count_failed_students()` that takes one parameter: `student_marks`. This function should count the number of students who have failed the subject, returning the count as an integer. We say that a student has failed if their mark is less than or equal to **40**.
6+
7+
Note: `Iterate` through the student marks to find out your answer.
8+
9+
Result should be an `int`.
10+
11+
```python
12+
>>> count_failed_students(student_marks=[90,40,55,70,30]))
13+
2
14+
```
15+
16+
## 2. Top Marks
17+
18+
The Headmaster wants to find the group of top students. What qualifies student marks as `top marks` fluctuates, and you will need to find the student marks that are **greater than or equal to** the current threshold.
19+
20+
Create the function `above_threshold()` where `student_marks` and `threshold` are the two required parameters:
21+
22+
1. `student_marks` are a list of marks for each student
23+
2. `threshold` is the top mark threshold. Marks greater than or equal to this number are considered "top marks" .
24+
25+
This function should return a `list` of all marks that are greater than or equal to a scoring threshold.
26+
27+
**Note:** If you find a mark which is less than the threshold, you should `continue` to evaluating the next mark.​
28+
29+
```python
30+
>>> above_threshold(student_marks=[90,40,55,70,30], 70)
31+
[90, 70]
32+
```
33+
34+
## 3. First K Students.
35+
36+
Create the function `first_k_student_marks()` with parameters `student_marks` and `k`
37+
38+
1. Student marks are a list of marks of each student
39+
2. k is the number of students.
40+
41+
You need to return the first K number of student Marks. Once you reach K number of students, you should exit out of the loop.
42+
43+
```python
44+
>>> first_k_student_marks(student_marks=[90,80,100], k=1)
45+
[90]
46+
```
47+
48+
## 4. Full Marks
49+
50+
Create the function `perfect_score()` with parameter `student_info`.
51+
`student_info` is a dictionary containing the names and marks of the students `{"Charles": 90, "Tony": 80}`
52+
53+
Find if we have any students who scored "full marks"(_100%_)on the exam. If we don't find any "full marks" in `student_info`, we should return "No hundreds"
54+
55+
Return the first student who has scored full marks - 100.
56+
57+
```python
58+
>>> perfect_score(student_info={"Charles": 90, "Tony": 80, "Alex":100})
59+
Alex
60+
>>> perfect_score(student_info={"Charles": 90, "Tony": 80})
61+
No hundreds
62+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
## Loops in Python
2+
3+
There are 2 general ways in Python to loop through objects.
4+
5+
- `while` loop (_indefinite_, or _uncounted_)
6+
- `for` loop (_definite_, or _counted_)
7+
8+
## While loops
9+
10+
While loops are _uncounted_ and based on a `conditional` statement for looping through objects.
11+
12+
```
13+
while expression:
14+
set_of_statements
15+
```
16+
17+
When the statement evaluates to `True`, the loop advances and executes the code in the indented block - or "body" of the loop. Looping continues in this fashion until the conditional statement evaluates to `False`.
18+
19+
```python
20+
i = 0
21+
while i < 3:
22+
print(i)
23+
# => 0
24+
# => 1
25+
# => 2
26+
```
27+
28+
## For Loops
29+
30+
Unlike `while` loops, `for` loops are based on a counter. The Loop will execute until the counter/object being counted is exhausted. The counter in this case could be the indexes in a `list`, `string`, or `tuple` -- or the indexes in a `range()` object.
31+
32+
```python
33+
for item in countable_object:
34+
set_of_statements
35+
```
36+
37+
```python
38+
>>> numbers = [1, 2, 3, 4, 5]
39+
40+
>>> for number in numbers:
41+
print(number)
42+
#=> 1
43+
#=> 2
44+
#=> 3
45+
#=> 4
46+
#=> 5
47+
```
48+
49+
## Breaking from loops
50+
51+
Where you have a large set of objects that you want to loop through but would like to stop the loop execution when a certain condition is met, the `break` statement comes to the rescue.
52+
53+
```python
54+
list_of_items = [1, 2, 3, 4, 5, 6, 7, 8]
55+
for item in list_of_items:
56+
if item > 5:
57+
break
58+
print(item)
59+
# => 1
60+
# => 2
61+
# => 3
62+
# => 4
63+
# => 5
64+
```
65+
66+
## Continuing in loops
67+
68+
You will have important objects as well as non important ones. When you want to skip over the objects that you don't need to process you use the `continue` statement.
69+
70+
Example: you want to process only odd numbers in a loop and not the event numbers.
71+
72+
```python
73+
all_numbers = [1, 2, 3, 4, 5, 6, 7, 8]
74+
for num in all_numbers:
75+
if num % 2 == 0:
76+
continue
77+
print(num)
78+
# => 1
79+
# => 3
80+
# => 5
81+
# => 7
82+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"authors": [
3+
{
4+
"github_username": "mohanrajanr",
5+
"exercism_username": "mohanrajanr"
6+
},
7+
{
8+
"github_username": "BethanyG",
9+
"exercism_username": "BethanyG"
10+
}
11+
]
12+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Goal
2+
3+
This concept exercise should convey a basic understanding of how to handle the `for` loop and `while` in Python. This concept exercise should also teach how to interrupt or change the normal flow of a loop using the `break` statement and the `continue` statement.
4+
5+
## Learning objectives
6+
7+
- use `while` to make a loop: execute something as long as an expression/test is `true`
8+
- use `for` to iterate over an iterable object with the `for ... in` pattern. The Membership testing concept (in) is required to address this concept.
9+
- understand the differences between `while` (indefinite, condition-dependent) and `for` (definite, length or count dependent) loops
10+
- use `break` statement to terminate the nearest enclosing loop
11+
- use `continue` statement to continue with the next cycle of the nearest enclosing loop
12+
13+
## Out of scope
14+
15+
- async loops (advanced)
16+
17+
## Concepts
18+
19+
- `while` loops
20+
- `for` loops
21+
- `break` statement
22+
- `continue` statement
23+
24+
## Prerequisites
25+
26+
- `basics`
27+
- `numbers`
28+
- `arithmetic`
29+
30+
## Resources
31+
32+
- [The while key](https://yawpitchroll.com/posts/the-35-words-you-need-to-python/#while)
33+
- [The for loop](https://yawpitchroll.com/posts/the-35-words-you-need-to-python/#for)
34+
- [The break statement](https://yawpitchroll.com/posts/the-35-words-you-need-to-python/#break)
35+
- [The break statement python docs](https://docs.python.org/3/reference/simple_stmts.html#the-break-statement)
36+
- [The continue statement](https://yawpitchroll.com/posts/the-35-words-you-need-to-python/#continue)
37+
- [The continue statement python docs](https://docs.python.org/3/reference/simple_stmts.html#the-continue-statement)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def count_failed_students(student_marks):
2+
failed_marks = 0
3+
for mark in student_marks:
4+
if mark <=40:
5+
failed_marks +=1
6+
return failed_marks
7+
8+
def above_threshold(student_marks, x):
9+
above_marks = []
10+
for mark in student_marks:
11+
if mark < x:
12+
continue
13+
above_marks.append(mark)
14+
return above_marks
15+
16+
def first_k_student_marks(student_marks, k):
17+
i = 0
18+
marks = []
19+
while i <= k:
20+
marks.append(student_marks[i])
21+
return marks
22+
23+
def perfect_score(student_info):
24+
for name, mark in student_info.items():
25+
if mark == 100:
26+
return name
27+
else:
28+
return "No hundreds"

exercises/concept/loops/loops.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def count_failed_students(student_marks):
2+
pass
3+
4+
def above_threshold(student_marks, x):
5+
pass
6+
7+
def first_k_student_marks(student_marks, k):
8+
pass
9+
10+
def perfect_score(student_info):
11+
pass

0 commit comments

Comments
 (0)