Skip to content

Commit 1af5185

Browse files
committed
added more problem loop related
1 parent 387e204 commit 1af5185

17 files changed

+265
-12
lines changed

Easy-ones/Floor-Division.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
## 1.4: Floor division
33

4-
**S-1: The problem**
4+
#### S-1: The problem
55
Find the floor division of two numbers.
66

77
<details>

Easy-ones/Math-Power.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
## 1.2: Math Power
33

4-
**S-1: The problem**
4+
#### S-1: The problem
55
Take two numbers from the users. Calculate the result of second number power of the first number.
66

77
<details>

Easy-ones/Random-Number.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
## 1.3: Create a random number
33

4-
**S-1: The problem**
4+
#### S-1: The problem
55
Create a random number between 0 to 10
66

77
<details>

Easy-ones/Temporary-variable.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
## 1.5: Swap two variables
33

4-
**S-1: The problem**
4+
#### S-1: The problem
55
> Swap two variables.<br><br>To swap two variables: the value of the first variable will become the value of the second variable. On the other hand, the value of the second variable will become the value of the first variable.
66
77
<details>

Easy-ones/User-input-to-Number.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
### 1.1: User input to Number
55

6-
**S-1: The problem**
6+
#### S-1: The problem
77
Take two inputs from the user. One will be an integer. The other will be a float number. Then multiply them to display the output.
88

99
<details>
File renamed without changes.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
## 3.2 Largest element of a list
3+
4+
#### S-1: The problem
5+
Find the largest element of a list.
6+
7+
<details>
8+
<summary><b>S-2: Click Here For Show Hints</b></summary>
9+
<p>Take the first element as the largest number. Then loop through the list and compare each element.
10+
</details>
11+
<br>
12+
13+
#### S-3: Solution
14+
```python
15+
def get_largest(nums):
16+
largest = nums[0]
17+
for num in nums:
18+
if num > largest:
19+
largest = num
20+
return largest
21+
22+
my_nums = [0,15,68,1,0,-55]
23+
24+
largest = get_largest(my_nums)
25+
26+
print('The largest number is: ', largest)
27+
```
28+
**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
29+
30+
#### S-4: Explanation
31+
In the beginning, consider the first element of the list as the largest element.
32+
33+
Don’t set the largest value to 0. If every number in the list is a negative number, your return will be wrong.
34+
35+
Then just loop through the list and compare. If the current number is greater than the largest number, set it as the largest number.
36+
37+
That’s it. Easy peasy.
38+
39+
#### S-5: Shortcut
40+
One shortcut is to pass a list of numbers to the max function. This will return the largest number in the list.
41+
42+
```python
43+
nums = [41, 69, 23, 45, 19, 8]
44+
45+
largest = max(nums)
46+
print(largest)
47+
```
48+
**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
49+
50+
#### S-6: Quiz
51+
What is the easiest way to find the largest number in a list?
52+
1. Use the max function
53+
2. Use the min function
54+
3. Use the sum function
55+
56+
#### S-7: Take Away
57+
58+
Use the max function to get the largest number in a list.
59+
60+
&nbsp;
61+
[![Next Page](../assets/next-button.png)](Sum-of-squares.md)
62+
&nbsp;
63+
64+
###### tags: `programmig-hero` `python` `float` `int` `math`
65+

Loop-Related/Remove-duplicate-Chars.md

Whitespace-only changes.

Loop-Related/Second-Largest.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
## 3.4 Second Largest
3+
4+
#### S-1: The problem
5+
For a list, find the second largest number in the list.
6+
7+
<details>
8+
<summary><b>S-2: Click Here For Show Hints</b></summary>
9+
<p>Finding the largest number was super easy. To find the second largest number, you have to keep track of two variables. Then compare.</p>
10+
</details>
11+
<br>
12+
13+
#### S-3: Solution
14+
```python
15+
def get_second_largest(nums):
16+
largest = nums[0]
17+
second_largest = nums[0]
18+
for i in range(1,len(nums)):
19+
if nums[i] > largest:
20+
second_largest = largest
21+
largest = nums[i]
22+
elif nums[i] > second_largest:
23+
second_largest = nums[i]
24+
return second_largest
25+
26+
my_nums = [44,11,83,29,25,76,88]
27+
second_largest = get_second_largest(my_nums)
28+
print("Second highest number is : ",second_largest)
29+
```
30+
**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
31+
32+
#### S-4: Explanation
33+
We declared two variables. One is called the largest. Another one is second_largest. We started both with the value of the first element of the list.
34+
35+
Then we ran a for loop on range. The range looks like range(1, len(nums)). We started the list with 1 because we already set the first value as the largest and second_largest. Then the upper value of the range is len(nums).
36+
37+
This means the range should run up to the length of the list.
38+
39+
Inside the loop, we have an if-else block. If the current element num[ i ] greater than the current largest element. This means the current largest element will become the new second largest element. And the current element will become the largest element.
40+
41+
On the other hand, if the current value is greater than the current second largest element, we just set the current value as the second largest element.
42+
43+
That's it.
44+
45+
#### S-5: Think Different
46+
Can you think about any way to solve this problem?
47+
48+
One clever solution could be,
49+
50+
Step 1: Use the max function to find the largest number.
51+
Step 2: Remove the max number from the list
52+
Step 3: From the remaining numbers, find the max number. As you already remove the previous max number, this max number will be the second-largest number.
53+
54+
How cool is this solution?
55+
56+
S-6: Clever Solution
57+
58+
```python
59+
nums = [5, 12, 54, 87, 55, 69, 23, 17]
60+
nums.remove(max(nums))
61+
second_largest = max(nums)
62+
print(second_largest)
63+
```
64+
**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
65+
66+
The answer is:
67+
68+
#### S-7: Take Away
69+
Clever ways to solve problems will make you happier.
70+
71+
&nbsp;
72+
[![Next Page](../assets/next-button.png)](Second-smallest.md)
73+
&nbsp;
74+
75+
###### tags: `programmig-hero` `python` `float` `int` `math`
76+

Loop-Related/Second-smallest.md

Whitespace-only changes.

0 commit comments

Comments
 (0)