Skip to content

Commit 88beea7

Browse files
committed
Update code formatting and fix bug in function XYZ
1 parent 39a6050 commit 88beea7

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
### Code Snippet: Nth Largest and Smallest Element Finder
2+
3+
#### Inputs
4+
The code snippet prompts the user to enter a list of numbers separated by commas and the position 'N' from the largest or smallest element.
5+
6+
#### Flow
7+
1. The code snippet takes input from the user, expecting a list of numbers separated by commas and the position 'N'.
8+
2. It splits the input string to obtain the list of numbers.
9+
3. It converts the list of strings to a list of integers.
10+
4. It sorts the list in descending order to find the Nth largest element and in ascending order to find the Nth smallest element.
11+
5. It prints the Nth largest and Nth smallest elements.
12+
13+
#### Outputs
14+
The code snippet outputs the Nth largest and Nth smallest elements from the entered list.
15+
16+
```python
17+
# Getting the input from the user
18+
L = input("Enter list of numbers separated by comma: ").split(",")
19+
N = int(input("Enter Nth position from largest or smallest: "))
20+
21+
# Converting string to int
22+
L = [int(i) for i in L]
23+
24+
# Finding Nth largest element
25+
L.sort(reverse=True)
26+
largest = L[N-1]
27+
print(f"{N} largest is = {largest}")
28+
29+
# Finding Nth smallest element
30+
L.sort()
31+
smallest = L[N-1]
32+
print(f"{N} smallest is = {smallest}")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
L = input("Enter list of numbers separated by comma: ").split(",")
2+
N = int(input("Enter Nth position from largest or smallest: "))
3+
4+
# Converting string to int
5+
L = [int(i) for i in L]
6+
7+
# Finding Nth largest element
8+
L.sort(reverse=True)
9+
largest = L[N-1]
10+
print(f"{N} largest is = {largest}")
11+
12+
# Finding Nth smallest element
13+
L.sort()
14+
smallest = L[N-1]
15+
print(f"{N} smallest is={smallest}")
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
### Code Snippet: Remove Duplicates from List
2+
3+
#### Inputs
4+
The code snippet prompts the user to enter a list of elements separated by space.
5+
6+
#### Flow
7+
1. The code snippet takes input from the user, expecting a string of elements separated by spaces.
8+
2. It converts the input string into a list of strings.
9+
3. It converts the list of strings into a list of integers.
10+
4. It creates a set from the list to remove duplicate elements.
11+
5. It converts the set back to a list to retain the distinct elements.
12+
6. It prints the list containing only the distinct elements.
13+
14+
#### Outputs
15+
The code snippet outputs a list with duplicate elements removed.
16+
17+
```python
18+
# getting the input from the user
19+
data = input("Enter the list elements separated by space: ")
20+
21+
# converting the string to list
22+
list_data = data.split()
23+
24+
# converting the list to integers
25+
list_data = [int(i) for i in list_data]
26+
27+
# creating a set to remove the duplicates
28+
unique_set = set(list_data)
29+
30+
# converting the set back to list
31+
distinct_list = list(unique_set)
32+
33+
# printing the list
34+
print(distinct_list)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
# getting the input from the user
3+
data = input("Enter the list elements separated by space: ")
4+
5+
# converting the string to list
6+
list_data = data.split()
7+
8+
# converting the list to integers
9+
list_data = [int(i) for i in list_data]
10+
11+
# creating a set to remove the duplicates
12+
unique_set = set(list_data)
13+
14+
# converting the set back to list
15+
distinct_list = list(unique_set)
16+
17+
# printing the list
18+
print(distinct_list)
19+
20+
21+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
### Code Snippet: Even and Odd Numbers Separator
2+
3+
#### Inputs
4+
The code snippet prompts the user to enter elements separated by spaces.
5+
6+
#### Flow
7+
1. The code snippet takes input from the user, expecting a list of elements separated by spaces.
8+
2. It converts the input string to a list of integers.
9+
3. It initializes two empty lists: `even_numbers` and `odd_numbers`.
10+
4. It iterates through the list of numbers and appends even numbers to the `even_numbers` list and odd numbers to the `odd_numbers` list.
11+
5. It prints the lists containing even and odd numbers, respectively.
12+
13+
#### Outputs
14+
The code snippet outputs two lists: one containing even numbers and the other containing odd numbers from the entered list.
15+
16+
```python
17+
# Getting input from the user
18+
L = [int(x) for x in input("Enter elements separated by space: ").split()]
19+
20+
even_numbers = list()
21+
odd_numbers = list()
22+
23+
# Separating even and odd numbers
24+
for i in L:
25+
if i % 2 == 0:
26+
even_numbers.append(i)
27+
else:
28+
odd_numbers.append(i)
29+
30+
# Printing even and odd numbers
31+
print(f"Even numbers: {even_numbers}")
32+
print(f"Odd numbers: {odd_numbers}")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
L=[int(x) for x in input("Enter elements separated by space: ").split()]
2+
3+
even_numbers=list()
4+
odd_numbers=list()
5+
6+
for i in L:
7+
if i%2==0:
8+
even_numbers.append(i)
9+
else:
10+
odd_numbers.append(i)
11+
12+
print(f"Even numbers: {even_numbers}")
13+
print(f"Odd numbers: {odd_numbers}")
14+

0 commit comments

Comments
 (0)