We usually find problems where they tell us to separate the odd and even numbers from the list. But this question is unique as you must find the first-ever even number you encounter and the first-ever odd number you encounter inside the list.
Here are the example inputs and outputs.
Example Input 1:
33,44,55,77,88
Example Output 1:
[33, 44, 55, 77, 88]
First even: 44 and first odd: 33
Example Input 2:
12,44,86,32,66,92,100
Example Output 2:
[12, 44, 86, 32, 66, 92, 100]
First even: 12 and first odd: Not found
Let’s set up some steps for this problem:
- Implementing user input as a list.
- Setting up variables.
- Searching the odd and even numbers.
To solve this problem, we must take input from the user and convert it into a list. There are various ways of making a list by taking input from the user, but we usually use some techniques that could be more neat. The messiest way to make a list would be — setting up the number of elements for a list. The to type in each element while pressing enter after those elements. This is a messy way of taking input from the user and making it a list.
Step 1:
Let’s code an effective way of handling input as a list.
my_list = input().split(",")
for n in range(0, len(my_list)):
my_list[n] = int(my_list[n])
print(my_list)
#I'm printing the list to
#show you how it works compared to the above messy example.
Now, will you look at that!
Step 2:
We would then set 2 variables named comment
and comment_2
and set them as “Not Found”. These comment variables will also act like a medium of output if there are no even or odd numbers in the list.
comment = "Not found"
comment_2 = "Not found"
#I have taken two comment variables, one for even and one for odd.
Step 3:
Now, we will search the even numbers and the odd numbers from the list with a simple modulus “%” operator in Python.
We will put the list through a for loop, and if I see that my element is odd or even, I will swap the value of comment
or comment_2
with the item I have just found from the list.
# Check for even numbers
for items in my_list:
if items % 2 == 0:
comment = items #Notice that I have assigned the variable to items
break
We will repeat the same process for odd numbers, and finally, we are done 🎉🎉💥💥
Final Code:
my_list = input().split(",")
for n in range(0, len(my_list)):
my_list[n] = int(my_list[n])
#This code above solves the problem.
#We have taken the list as an input in a neat way.
comment = "Not found"
comment_2 = "Not found"
#I have taken two comment variables,
#one for even and one for odd.
# Next, we check for even numbers
for items in my_list:
if items % 2 == 0:
comment = items #Notice that I have assigned the variable to items
break
#checking for odd numbers and repeating the whole process
for items_2 in my_list:
if items_2 % 2 != 0:
comment_2 = items_2
break
print(my_list)
print("First even: " + str(comment), "and first odd: " + str(comment_2))
You can find the full code from my GitHub from here.
Note for the readers.
I’m genuinely grateful for all the support you all kind souls have shown in my previous article. With all the support you guys have shown me, I couldn’t stop writing another Python Programming article for you guys 💜!
Could you all react to show your support and keep my enthusiasm going? This will ignite my passion, and I will continue giving my best! 🔥
FYI — You can all find me on Twitter (X) and LinkedIn.
Top comments (0)