Skip to content

Commit a119bb4

Browse files
committed
added loop related problem
1 parent 1af5185 commit a119bb4

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
## 3.6 Remove duplicate characters
3+
4+
#### S-1: The problem
5+
For a given string, remove all duplicate characters from that string.
6+
7+
<details>
8+
<summary><b>S-2: Click Here For Show Hints</b></summary>
9+
<p>Create a result string. Then loop through the string and check whether the current character not in the string. If it is not, then add it. Otherwise, it’s already there adding it will make it duplicate. </p>
10+
</details>
11+
<br>
12+
13+
#### S-3: Solution
14+
```python
15+
def remove_duplicate(your_str):
16+
result = ''
17+
for char in your_str:
18+
if char not in result:
19+
result += char
20+
return result
21+
22+
user_input = input('what is your string:')
23+
24+
no_duplicate = remove_duplicate(user_input)
25+
print('Without duplicate: ', no_duplicate)
26+
```
27+
**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
28+
29+
#### S-4: Explanation
30+
We just created a function. In the function, we are taking a string as an input.
31+
32+
Inside the function, we created a variable named result with initial value as an empty string.
33+
34+
Our task is to loop through the input string and then for each character/letter check whether the character (char) not in the result. If it is not in the result, we add it to the result string.
35+
36+
If it is already added in the result string, we don’t add it to it. Because, if we add it again, it will become a duplicate.
37+
38+
So, the overall task is very simple.
39+
40+
If needed, go to the code playground type the code multiple times and you will understand it.
41+
42+
#### S-5: Quiz
43+
How would you check whether a character exists in a string?
44+
45+
1. not in
46+
2. ==
47+
3. index
48+
49+
**The answer is: 1**
50+
51+
#### S-6: Take Away
52+
The not in is just the opposite check of in.
53+
54+
55+
&nbsp;
56+
[![Next Page](../assets/next-button.png)](../README.md)
57+
&nbsp;
58+
59+
###### tags: `programmig-hero` `python` `float` `int` `math`
60+

Loop-Related/Second-smallest.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
## 3.5 Second smallest element
3+
4+
#### S-1: The problem
5+
For a list, find the second smallest element in the list
6+
7+
<details>
8+
<summary><b>S-2: Click Here For Show Hints</b></summary>
9+
<p>If you understand the process of finding the second largest element, this will be a piece of cake for you.</p>
10+
</details>
11+
<br>
12+
13+
#### S-3: Solution
14+
```python
15+
def get_second_smallest(nums):
16+
smallest = nums[0]
17+
second_smallest = nums[0]
18+
for i in range(1,len(nums)):
19+
if nums[i] < smallest:
20+
second_smallest = smallest
21+
smallest = nums[i]
22+
elif nums[i] < second_smallest:
23+
second_smallest = nums[i]
24+
return second_smallest
25+
26+
my_nums = [44,11,83,29,25,76,88]
27+
second_smallest = get_second_smallest(my_nums)
28+
print("Second smallest number is : ", second_smallest)
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 smallest. Another one is second_smallest. We started both with the value of the first element of the list.
34+
35+
Then we ran a for loop on the range. The range looks like range(1, len(nums)). We started the list with 1 because we already set the first value as the smallest and the second smallest. Then the upper value of the range is len(num).
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 ] smaller than the current smallest element. This means the current smallest element will become the new second smallest element. And the current element will become the smallest element.
40+
41+
On the other hand, if the current value is smaller than the current second smallest element, we just set the current value as the second smallest element.
42+
43+
That's it.
44+
45+
#### S-5: Think DIfferent
46+
47+
You can use the previous clever solution to find the second smallest number as well.
48+
49+
Please note that, by removing an element from the list, you are changing the original list. If you don’t want to modify the list (you might need the list for other operations), you should use the comparison method.
50+
51+
#### S-6: Clever Solution
52+
53+
```python
54+
nums = [2, 15, 14, 71, 52, 209, 551]
55+
nums.remove(min(nums))
56+
second_smallest = min(nums)
57+
print(second_smallest)
58+
```
59+
60+
**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
61+
62+
#### S-7: Take Away
63+
If you know multiple solutions to a problem, you can apply the right solution based on the situation.
64+
65+
&nbsp;
66+
[![Next Page](../assets/next-button.png)](Remove-duplicate-Chars.md)
67+
&nbsp;
68+
69+
###### tags: `programmig-hero` `python` `float` `int` `math`
70+

0 commit comments

Comments
 (0)