-
Arrays
Description I was bored.
Code int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; } int unique_sum(const int arr[/*size*/], size_t size) { int r = 0; qsort(arr, size, sizeof(int), cmp); for (size_t i = 0; i < size; ) { if ((i == 0 || arr[i] != arr[i - 1]) && (i == size - 1 || arr[i] != arr[i + 1])) r += arr[i]; for (int v = arr[i]; i < size && arr[i] == v; i++); } return r; }
Test Cases // TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example. int unique_sum(const int[], size_t); Test(unique_sum_of_numbers, should_do_something) { cr_assert_eq(unique_sum((const int[]){1, 2, 3, 2}, 4), 4); cr_assert_eq(unique_sum((const int[]){1, 1, 2, 3}, 4), 5); cr_assert_eq(unique_sum((const int[]){-1, -1, 0, 1}, 4), 1); } Test(unique_sum_of_numbers, should_do_something_other) { cr_assert_eq(unique_sum((const int[]){}, 0), 0); cr_assert_eq(unique_sum((const int[]){5, 5, 5, 5}, 4), 0); cr_assert_eq(unique_sum((const int[]){10, 20, 30, 20, 10}, 5), 30); cr_assert_eq(unique_sum((const int[]){1, 2, 3, 4, 5}, 5), 15); cr_assert_eq(unique_sum((const int[]){-5, -5, 0, 5, 5}, 5), 0); cr_assert_eq(unique_sum((const int[]){-1, 2, -1, 3, 4, 2}, 6), 7); cr_assert_eq(unique_sum((const int[]){1, 2, 3, 4, 5, 1, 2, 3}, 8), 9); cr_assert_eq(unique_sum((const int[]){100, 200, 300, 300, 200, 100}, 6), 0); }
Output:
-
Code #include <bits/stdc++.h>using namespace std;int unique_sum(const vector<int>& n) {auto nums = n;sort(nums.begin(), nums.end(), [](int a, int b) {return a < b;});int sum = 0;for(size_t i = 0; i < nums.size(); i++) {if(nums[i] == nums[i+1] || nums[i] == nums[i-1]) {continue;} else {sum += nums[i];}- #include <stdlib.h>
- int cmp(const void *a, const void *b)
- {
- return *(int *)a - *(int *)b;
- }
- int unique_sum(const int arr[/*size*/], size_t size)
- {
- int r = 0;
- qsort(arr, size, sizeof(int), cmp);
- for (size_t i = 0; i < size; )
- {
- if ((i == 0 || arr[i] != arr[i - 1]) &&
- (i == size - 1 || arr[i] != arr[i + 1]))
- r += arr[i];
- for (int v = arr[i]; i < size && arr[i] == v; i++);
- }
return sum;- return r;
- }
Test Cases // TODO: Replace examples and use TDD by writing your own tests- // TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example.
Describe(unique_sum_of_numbers)- #include <criterion/criterion.h>
- #include <stdlib.h>
- int unique_sum(const int[], size_t);
- Test(unique_sum_of_numbers, should_do_something)
- {
- cr_assert_eq(unique_sum((const int[]){1, 2, 3, 2}, 4), 4);
- cr_assert_eq(unique_sum((const int[]){1, 1, 2, 3}, 4), 5);
- cr_assert_eq(unique_sum((const int[]){-1, -1, 0, 1}, 4), 1);
- }
- Test(unique_sum_of_numbers, should_do_something_other)
- {
It(should_do_something){Assert::That(unique_sum({1,2,3,2}), Equals(4));Assert::That(unique_sum({1,1,2,3}), Equals(5));Assert::That(unique_sum({-1,-1,0,1}), Equals(1));};It(should_do_something_other){Assert::That(unique_sum({}), Equals(0));Assert::That(unique_sum({5, 5, 5, 5}), Equals(0));Assert::That(unique_sum({10, 20, 30, 20, 10}), Equals(30));Assert::That(unique_sum({1, 2, 3, 4, 5}), Equals(15));Assert::That(unique_sum({-5, -5, 0, 5, 5}), Equals(0));Assert::That(unique_sum({-1, 2, -1, 3, 4, 2}), Equals(7));Assert::That(unique_sum({1, 2, 3, 4, 5, 1, 2, 3}), Equals(9));Assert::That(unique_sum({100, 200, 300, 300, 200, 100}), Equals(0));}};- cr_assert_eq(unique_sum((const int[]){}, 0), 0);
- cr_assert_eq(unique_sum((const int[]){5, 5, 5, 5}, 4), 0);
- cr_assert_eq(unique_sum((const int[]){10, 20, 30, 20, 10}, 5), 30);
- cr_assert_eq(unique_sum((const int[]){1, 2, 3, 4, 5}, 5), 15);
- cr_assert_eq(unique_sum((const int[]){-5, -5, 0, 5, 5}, 5), 0);
- cr_assert_eq(unique_sum((const int[]){-1, 2, -1, 3, 4, 2}, 6), 7);
- cr_assert_eq(unique_sum((const int[]){1, 2, 3, 4, 5, 1, 2, 3}, 8), 9);
- cr_assert_eq(unique_sum((const int[]){100, 200, 300, 300, 200, 100}, 6), 0);
- }
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}