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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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.
+65
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

+76
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.

Loop-Related/Sum-of-squares.md

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
## 3.3 Sum of squares
3+
4+
#### S-1: The problem
5+
Take a number as input. Then get the sum of the numbers. If the number is n. Then get
6+
7+
02+12+22+32+42+.............+n2
8+
9+
<details>
10+
<summary><b>S-2: Click Here For Show Hints</b></summary>
11+
<p>Once again, run a for loop with a range. Inside the loop, use the power of 2. Then add that power to a sum variable. That’s it. </p>
12+
</details>
13+
<br>
14+
15+
#### S-3: Solution
16+
```python
17+
def square_sum(num) :
18+
sum = 0
19+
for i in range(num+1) :
20+
square = (i ** 2)
21+
sum = sum + square
22+
23+
return sum
24+
25+
num = int(input('Enter a number: '))
26+
sum = square_sum(num)
27+
28+
print('sum of square numbers is ', sum)
29+
```
30+
**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
31+
32+
#### S-4: Explanation
33+
This one is super easy. You declared a variable sum with an initial value 0.
34+
35+
Then you run a for loop. This loop will run for range (num +1).
36+
37+
The reason we are running it until num + 1. Because, we know that the range will stop just before the number inside it.
38+
39+
For example, if we want to run the loop including 10. We should write 11 inside the range function.
40+
41+
If needed, go to the code editor and run the following code.
42+
43+
```python
44+
for i in range(10):
45+
print(i)
46+
```
47+
**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
48+
And then run
49+
```python
50+
for i in range(11):
51+
print(i)
52+
```
53+
**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
54+
55+
56+
#### S-5: Shortcut
57+
Sometimes there could be a better and easier way to solve a problem. For example, there is a simple math formula to calculate the sum of the square.
58+
59+
This is the reason, you should search online and learn from different sources. This will make you stronger and better.
60+
61+
If you want to calculate the sum of the square of n numbers, the formula is-
62+
63+
n*(n+1)(2*n+1)/6
64+
65+
Now you can use this formula.
66+
```python
67+
def sum_of_square2(n):
68+
sum = n*(n+1)*(2*n+1)/6
69+
return sum
70+
71+
num = int(input('Enter a number: '))
72+
sum = sum_of_square2(num)
73+
print('Your sum of Square is: ', sum)
74+
```
75+
**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
76+
77+
#### S-6: Quiz
78+
79+
Why you should learn problem-solving from multiple sources?
80+
81+
1. To utilize the internet bill correctly
82+
2. To learn alternative solutions
83+
3. To pretend that you are busy
84+
85+
**The answer is: 2**
86+
87+
#### S-7: Take Away
88+
Knowing shortcut ways will make you smarter and faster.
89+
90+
&nbsp;
91+
[![Next Page](../assets/next-button.png)](Second-Largest.md)
92+
&nbsp;
93+
94+
###### tags: `programmig-hero` `python` `float` `int` `math`
95+

Number-Related/Average-of-numbers.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
## 2.3: Average of numbers
33

4-
**S-1: The problem**
4+
#### S-1: The problem
55
Take numbers from a user and show the average of the numbers the user entered.
66

77
<details>

Number-Related/Divisible-by-3-and-5.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
## 2.4: Divisible by 3 and 5
33

4-
**S-1: The problem**
4+
#### S-1: The problem
55
For a given number, find all the numbers smaller than the number. Numbers should be divisible by 3 and also by 5.
66

77
<details>

Number-Related/Max-of-three.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
## 2.2 max of three
33

4-
**S-1: The problem**
4+
#### S-1: The problem
55
Find the largest of the three numbers.
66

77
<details>

Number-Related/Sum-of-digits.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
## 3.1: Sum of Elements
33

4-
**S-1: The problem**
4+
#### S-1: The problem
55
For a given list, get the sum of each number in the list
66

77
<details>
@@ -63,7 +63,7 @@ What is the shortcut way to sum all the numbers in a list?
6363
Use the sum function to sum all the numbers in a list.
6464

6565
&nbsp;
66-
[![Next Page](../assets/next-button.png)](../README.md)
66+
[![Next Page](../assets/next-button.png)](Coin-sum.md)
6767
&nbsp;
6868

6969
###### tags: `programmig-hero` `python` `float` `int` `math`

Number-Related/max-of-two.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
## 2.1 max of two
33

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

77
<details>

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
* **[1.5](Easy-ones/Temporary-variable.md "Temporary variable")** - **[Temporary variable](/Easy-ones/Temporary-variable.md)**
1919

20+
21+
22+
2023
* **[2](Number-Relate/Number-Related.md "Number Related")** - &nbsp; **[Number Related](/Number-Related/Number-Related.md)**
2124

2225
* **[2.1](Number-Relate "Max of two")** - **[Max of two](/Number-Relate)**
@@ -29,7 +32,21 @@
2932

3033
* **[2.5](Number-Related/Sum-of-digits.md "Sum of digits")** - **[Sum of digits](Number-Related/Sum-of-digits.md)**
3134

32-
* **[2.6](Number-Related/Coin-sum.md "Coin sum")** - **[Coin sum](Number-Related/Coin-sum.md)**
35+
36+
37+
* **[3](Loop-Relate/Coin-sum.md "Loop Related")** - &nbsp; **[Loop Related](/Number-Related/Coin-sum.md)**
38+
39+
* **[3.1](Loop-Related/Coin-sum.md "Sum of elements")** - **[Sum of elements](Loop-Related/Coin-sum.md)**
40+
41+
* **[3.2](Loop-Related/Largest-element-of-a-list.md "Largest element of a list")** - **[Largest element of a list](Loop-Related/Largest-element-of-a-list.md)**
42+
43+
* **[3.3](Loop-Related/Sum-of-squares.md "Sum of squares")** - **[Sum of squares](Loop-Related/Sum-of-squares.md)**
44+
45+
* **[3.4](Loop-Related/Second-Largest.md "Second Largest")** - **[Second Largest](Loop-Related/Second-Largest.md)**
46+
47+
* **[3.5](Loop-Related/Second-smallest.md "Second Smallest")** - **[Second Smallest](Loop-Related/Second-smallest.md)**
48+
49+
* **[3.6](Loop-Related/Remove-duplicate-Chars.md "Remove duplicate Chars")** - **[Remove duplicate Chars](Loop-Related/Remove-duplicate-Chars.md)**
3350

3451
## 0: Introduction
3552

0 commit comments

Comments
 (0)