Problem Description
The problem we will deal with today is “Sum of Even and Odd Numbers.” This problem requires distinguishing between even and odd numbers in a given list and calculating their respective sums.
This problem is a good example of utilizing basic control structures and list processing techniques in Python.
Problem: Sum of Even and Odd Numbers
Given a list of integers, write a program that calculates and outputs the sum of even numbers and the sum of odd numbers in the list.
Input
The first line contains integers separated by spaces, where integers fall within the range of (-106 ≤ integer ≤ 106).
Output
Output the sum of even numbers and the sum of odd numbers separated by a space on the first line.
Example Input
1 2 3 4 5 6 7 8 9 10
Example Output
30 25
Problem Solving Process
Step 1: Understand the Problem
The first thing to do when faced with a problem is to clearly understand the requirements.
You need to be able to distinguish between even and odd numbers and sum them up. It is important to carefully examine the form of input and output at this stage.
Step 2: Plan
To solve the problem, we follow these steps:
- Iterate through each number in the list.
- Determine whether the current number is even or odd.
- If it is even, add it to the even sum variable, and if it is odd, add it to the odd sum variable.
- Finally, output the sums of even and odd numbers.
Step 3: Coding
Now, let’s write the actual code based on the above plan. While writing the code, we set the variables and the basic structure that will be used in the project.
def sum_even_odd(numbers): even_sum = 0 odd_sum = 0 for num in numbers: if num % 2 == 0: even_sum += num else: odd_sum += num return even_sum, odd_sum # Input section input_numbers = list(map(int, input().split())) # Function call even_sum, odd_sum = sum_even_odd(input_numbers) # Output results print(even_sum, odd_sum)
Step 4: Debugging
After writing everything, we need to verify the accuracy of the code.
For instance, execute the code with various input values and check if the results match expectations.
It is also important to check whether there is handling for exceeding data ranges or exceptional situations.
Example of Error Occurrence
input_numbers = list(map(int, input().split())) # In the above code, if a string is entered, a ValueError may occur.
To prevent this, we can use a try-except block:
try: input_numbers = list(map(int, input().split())) except ValueError: print("Invalid input. Please enter integers only.")
Step 5: Optimization
The code can also be optimized. You can use list comprehension to make the code more concise. For example:
even_sum = sum(num for num in input_numbers if num % 2 == 0) odd_sum = sum(num for num in input_numbers if num % 2 != 0)
Conclusion
Through problems like this, we learned to easily identify and sum even and odd numbers.
Additionally, by writing and debugging the code ourselves, we could enhance our problem-solving skills.
Ultimately, I want to emphasize that writing efficient and concise code is of utmost importance.
You can also cultivate debugging skills through various problems and further improve algorithmic problem-solving capabilities.
Exercise
In a manner similar to the above problem, try solving the following problem. Calculate the sum of prime numbers and the sum of non-prime numbers from the input list.
Implementing Prime Check Function
def is_prime(n): if n <= 1: return False for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return True