|
| 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 | + |
| 72 | +[](Second-smallest.md) |
| 73 | + |
| 74 | + |
| 75 | +###### tags: `programmig-hero` `python` `float` `int` `math` |
| 76 | + |
0 commit comments