Task:
UGNTU student contacted me for help:
1. Calculate the arithmetic mean of even elements of the last quarter of the array
2. Find the two largest positive elements of the first third
3. Sort the even-numbered elements of the second half of the array in ascending squares
Solution:
To perform all three tasks in Python, first create functions for each of them, and then apply them to a specific array.
def average_even_last_quarter(arr): last_quarter = arr[int(3 * len(arr)/4):] even_elements = [elem for elem in last_quarter if elem % 2 == 0] return sum(even_elements)/len(even_elements) if even_elements else 0 def two_largest_positive_first_third(arr): first_third = arr[:int(len(arr)/3)] positive_elements = [elem for elem in first_third if elem > 0] return sorted(positive_elements)[-2:] if len(positive_elements) > 1 else [] def sort_even_indices_second_half(arr): second_half = arr[int(len(arr)/2):] even_indices = [i for i in range(len(second_half)) if i % 2 == 0] even_values = [second_half[i] for i in even_indices] sorted_values = sorted(even_values, key=lambda x: x ** 2) for index, value in zip(even_indices, sorted_values): second_half[index] = value return arr[:int(len(arr)/2)] + second_half # Example array arr = [5, 3, 8, -2, 7, 6, 12, -1, 9, 0, 15, 20, 3, 10, 14, 18] avg_even_last_quarter = average_even_last_quarter(arr) two_largest_first_third = two_largest_positive_first_third(arr) sorted_arr = sort_even_indices_second_half(arr) print("The arithmetic mean of the even elements of the last quarter of the array:", avg_even_last_quarter) print("Two most positive element of the first third of the array:", two_largest_first_third) print("An array with second half elements sorted by number in ascending squares:", sorted_arr)
This is what the solution to all three problems looks like applied to one array. You need to replace the example array with your own array to get the appropriate results.
How it would look like in C++
First, let's create functions for each of them, and then apply them to a specific array.
#include <iostream> #include <vector> #include <algorithm> #include <numeric> double average_even_last_quarter(const std::vector<int>& arr) { int start_index = 3 * arr.size()/4; std::vector<int> even_elements; for (int i = start_index; i < arr.size(); ++i) { if (arr[i] % 2 == 0) { even_elements.push_back(arr[i]); } } return even_elements.empty() ? 0 : std::accumulate(even_elements.begin(), even_elements.end(), 0.0)/even_elements.size(); } std::vector<int> two_largest_positive_first_third(const std::vector<int>& arr) { int end_index = arr.size()/3; std::vector<int> positive_elements; for (int i = 0; i < end_index; ++i) { if (arr[i] > 0) { positive_elements.push_back(arr[i]); } } if (positive_elements.size() < 2) { return {}; } std::partial_sort(positive_elements.begin(), positive_elements.begin() + 2, positive_elements.end(), std::greater<int>()); return {positive_elements[0], positive_elements[1]}; } std::vector<int> sort_even_indices_second_half(const std::vector<int>& arr) { int start_index = arr.size()/2; std::vector<int> second_half(arr.begin() + start_index, arr.end()); std::vector<int> even_indices; for (int i = 0; i < second_half.size(); ++i) { if (i % 2 == 0) { even_indices.push_back(i); } } std::sort(even_indices.begin(), even_indices.end(), [&](int i1, int i2) { return second_half[ i1] * second_half[i1] < second_half[i2] * second_half[i2]; }); for (int i = 0; i < even_indices.size(); ++i) { second_half[even_indices[i]] = arr[start_index + even_indices[i]]; } return std::vector<int>(arr.begin(), arr.begin() + start_index) + second_half; } int main() { std::vector<int> arr = {5, 3, 8, -2, 7, 6, 12, -1, 9, 0, 15, 20, 3, 10, 14, 18}; double avg_even_last_quarter = average_even_last_quarter(arr); std::vector<int> two_largest_first_third = two_largest_positive_first_third(arr); std::vector<int> sorted_arr = sort_even_indices_second_half(arr); std::cout<< "The arithmetic mean of the even elements of the last quarter of the array: " << avg_even_last_quarter << std::endl; std::cout<< "The two largest positive elements of the first third of the array: "; for (int elem : two_largest_first_third) { std::cout<< element << ' '; } std::cout << std::endl; std::cout<< "An array with even-numbered second half elements sorted in ascending squares: "; for (int elem : sorted_arr) { std::cout<< element << ' '; } std::cout << std::endl; return 0; }
Now you can copy the code into your project, replace the sample array with your own array, and run the code to see the results.
- Если Вам понравилась статья, рекомендуем почитать
- Microsoft Corporation brought a new vulnerability to UEFI loading
- Linux 6.13-RC7 has already been released-the release of Linux 6.13 Stable in a few days