#include <stddef.h> #include <stdlib.h> int * twin_sum_solutions (int *input, size_t input_len, size_t *output_len) { int *res = malloc (input_len * sizeof (int)); for (size_t i = *output_len = 0; i < input_len - 1; i++) if (input[i] == input[i + 1]) res[(*output_len)++] = input[i] * 2, i++; return realloc (res, *output_len * sizeof (int)); }
fn twin_sum_solutions(array: &[i32]) -> Vec<i32> {array.windows(2).filter(|w| w[0] == w[1]).map(|w| w[0] + w[1]).collect()- #include <stddef.h>
- #include <stdlib.h>
- int *
- twin_sum_solutions (int *input, size_t input_len, size_t *output_len)
- {
- int *res = malloc (input_len * sizeof (int));
- for (size_t i = *output_len = 0; i < input_len - 1; i++)
- if (input[i] == input[i + 1])
- res[(*output_len)++] = input[i] * 2, i++;
- return realloc (res, *output_len * sizeof (int));
- }
#include <criterion/criterion.h> #include <stddef.h> int *twin_sum_solutions (int *input, size_t input_len, size_t *output_len); void tester (int *input, size_t input_len, int *expected, size_t expected_len) { size_t actual_len; int *actual = twin_sum_solutions (input, input_len, &actual_len); cr_assert_eq (expected_len, actual_len); if (!expected_len) cr_assert_eq (actual, 0); else cr_assert_arr_eq (expected, actual, expected_len); free (actual); } Test (example, simple) { tester ((int[]){ 2, 2, 4, 4 }, 4, (int[]){ 4, 8 }, 2); tester ((int[]){ 3, 2, 6, 6 }, 4, (int[]){ 12 }, 1); tester ((int[]){ 1, 2, 3, 4 }, 4, (int[]){}, 0); }
#[test]fn test() {assert_eq!(twin_sum_solutions(&[2, 2, 4, 4]), [4, 8]);assert_eq!(twin_sum_solutions(&[3, 2, 6, 6]), [12]);assert_eq!(twin_sum_solutions(&[1, 2, 3, 4]), []);- #include <criterion/criterion.h>
- #include <stddef.h>
- int *twin_sum_solutions (int *input, size_t input_len, size_t *output_len);
- void
- tester (int *input, size_t input_len, int *expected, size_t expected_len)
- {
- size_t actual_len;
- int *actual = twin_sum_solutions (input, input_len, &actual_len);
- cr_assert_eq (expected_len, actual_len);
- if (!expected_len)
- cr_assert_eq (actual, 0);
- else
- cr_assert_arr_eq (expected, actual, expected_len);
- free (actual);
- }
- Test (example, simple)
- {
- tester ((int[]){ 2, 2, 4, 4 }, 4, (int[]){ 4, 8 }, 2);
- tester ((int[]){ 3, 2, 6, 6 }, 4, (int[]){ 12 }, 1);
- tester ((int[]){ 1, 2, 3, 4 }, 4, (int[]){}, 0);
- }
#include <math.h> #include <stdlib.h> int closest_to_zero (int nums[/* len */], size_t len) { if (len == 0) return 0; int r = nums[0], ar = abs (r); for (size_t i = 1; i < len; i++) { int n = nums[i], an = abs (n); if (an < ar || (an == ar && n > r)) r = n, ar = abs (r); } return r; }
def closest_to_zero(nums):if not nums:return 0n = min(nums, key=abs)if n < 0 and abs(n) in nums:return abs(n)return n- #include <math.h>
- #include <stdlib.h>
- int
- closest_to_zero (int nums[/* len */], size_t len)
- {
- if (len == 0)
- return 0;
- int r = nums[0], ar = abs (r);
- for (size_t i = 1; i < len; i++)
- {
- int n = nums[i], an = abs (n);
- if (an < ar || (an == ar && n > r))
- r = n, ar = abs (r);
- }
- return r;
- }
#include <criterion/criterion.h> #include <stdlib.h> #define ARR(...) \ (int[]) { __VA_ARGS__ } int closest_to_zero (int nums[/* len */], size_t len); Test (Example, test_case) { cr_assert_eq (closest_to_zero (ARR (7, 5, 9, 1, 4), 5), 1); cr_assert_eq (closest_to_zero (ARR (7, -4, -3, -12, 5, 9, -2, 4), 8), -2); cr_assert_eq (closest_to_zero (ARR (-5, -5), 2), -5); cr_assert_eq (closest_to_zero (ARR (), 0), 0); cr_assert_eq (closest_to_zero (ARR (-5, 0, 1, 5), 4), 0); cr_assert_eq (closest_to_zero (ARR (-5, -1, 1, 5), 4), 1); }
import codewars_test as testfrom solution import closest_to_zero- #include <criterion/criterion.h>
- #include <stdlib.h>
@test.describe("Example")def test_group():@test.it("test case")def test_case():test.assert_equals(closest_to_zero([7, 5, 9, 1, 4]), 1)test.assert_equals(closest_to_zero([7,-4, -3, -12, 5, 9, -2, 4]), -2)test.assert_equals(closest_to_zero([-5, -5]), -5)test.assert_equals(closest_to_zero([]), 0)test.assert_equals(closest_to_zero([-5, 0, 1, 5]), 0)test.assert_equals(closest_to_zero([-5, -1, 1, 5]), 1)- #define ARR(...) \
- (int[]) { __VA_ARGS__ }
- int closest_to_zero (int nums[/* len */], size_t len);
- Test (Example, test_case)
- {
- cr_assert_eq (closest_to_zero (ARR (7, 5, 9, 1, 4), 5), 1);
- cr_assert_eq (closest_to_zero (ARR (7, -4, -3, -12, 5, 9, -2, 4), 8), -2);
- cr_assert_eq (closest_to_zero (ARR (-5, -5), 2), -5);
- cr_assert_eq (closest_to_zero (ARR (), 0), 0);
- cr_assert_eq (closest_to_zero (ARR (-5, 0, 1, 5), 4), 0);
- cr_assert_eq (closest_to_zero (ARR (-5, -1, 1, 5), 4), 1);
- }
Ciphers
Fundamentals
#include <ctype.h> #include <stdbool.h> #include <stdlib.h> #include <string.h> char * caesar_cypher (const char *input, int amount, bool reversed, bool encode, bool ignore_special_chars, bool match_case) { char *res = malloc (strlen(input) + 1); char *p = res; amount = (amount % 26) * (2 * (encode ^ reversed) - 1); for (; *input; input++) { char c = *input; if (!isalpha (c)) { if (!ignore_special_chars) *p++ = c; continue; } char s = ((toupper (c) + amount - 39) % 26) + 65; *p++ = match_case && islower (c) ? tolower (s) : s; } return *p = '\0', res; }
func caesarCypher(_ input: String,amount: Int,reversed: Bool = false,encode: Bool = true,ignoreSpecialChars: Bool = false,matchCase: Bool = true) -> String {let alphabet = Array("ABCDEFGHIJKLMNOPQRSTUVWXYZ")return input.map { character inlet char = Character(character.uppercased())guard alphabet.contains(char) else {return ignoreSpecialChars ? "" : String(char)}var newIndex = alphabet.firstIndex(of: char)!switch encode {case true:newIndex += (reversed ? -amount : amount)case false:newIndex += (reversed ? amount : -amount)- #include <ctype.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <string.h>
- char *
- caesar_cypher (const char *input, int amount, bool reversed, bool encode,
- bool ignore_special_chars, bool match_case)
- {
- char *res = malloc (strlen(input) + 1);
- char *p = res;
- amount = (amount % 26) * (2 * (encode ^ reversed) - 1);
- for (; *input; input++)
- {
- char c = *input;
- if (!isalpha (c))
- {
- if (!ignore_special_chars)
- *p++ = c;
- continue;
- }
(newIndex < 0) ? (newIndex += 26) : ()(newIndex >= 26) ? (newIndex -= 26) : ()return (char.isLowercase && matchCase)? alphabet[newIndex].lowercased() : alphabet[newIndex].uppercased()}.joined()- char s = ((toupper (c) + amount - 39) % 26) + 65;
- *p++ = match_case && islower (c) ? tolower (s) : s;
- }
- return *p = '\0', res;
- }
#include <criterion/criterion.h> #include <stdbool.h> char *caesar_cypher (const char *input, int amount, bool reversed, bool encode, bool ignore_special_chars, bool match_case); Test (fixed_tests, simple) { const char *input = "The Quick Brown Fox Jumps Over The Lazy Dog"; char *submited = caesar_cypher (input, 11, false, true, true, false); const char *expected = "ESPBFTNVMCZHYQZIUFXADZGPCESPWLKJOZR"; cr_assert_str_eq (submited, expected); free (submited); }
import XCTest- #include <criterion/criterion.h>
- #include <stdbool.h>
class SolutionTest: XCTestCase {static var allTests = [("Fixed Tests", fixedTests),]- char *caesar_cypher (const char *input, int amount, bool reversed, bool encode,
- bool ignore_special_chars, bool match_case);
func fixedTests() {let input = "The Quick Brown Fox Jumps Over The Lazy Dog"XCTAssertEqual(caesarCypher(input, amount: 11, ignoreSpecialChars: true, matchCase: false), "ESPBFTNVMCZHYQZIUFXADZGPCESPWLKJOZR")}}XCTMain([testCase(SolutionTest.allTests)])- Test (fixed_tests, simple)
- {
- const char *input = "The Quick Brown Fox Jumps Over The Lazy Dog";
- char *submited = caesar_cypher (input, 11, false, true, true, false);
- const char *expected = "ESPBFTNVMCZHYQZIUFXADZGPCESPWLKJOZR";
- cr_assert_str_eq (submited, expected);
- free (submited);
- }
Lists
#include <stdlib.h> int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; } void re_arrange(int data[/* len */], size_t len) { size_t e = 0; for (size_t i = 0; i < len; i++) { if (!(data[i] & 1)) { int t = data[i]; data[i] = data[e]; data[e++] = t; } } qsort(data, e, sizeof(int), cmp); qsort(&data[e], len - e, sizeof(int), cmp); }
#include <vector>- #include <stdlib.h>
void re_arrange(std::vector<int>& data) {std::vector<int> even, odd;std::copy_if (data.cbegin(), data.cend(), std::back_inserter(even), [](int num){return num%2 == 0;});std::copy_if (data.cbegin(), data.cend(), std::back_inserter(odd), [](int num){return num%2 == 1;});sort(even.begin(), even.end());sort(odd.begin(), odd.end());std::vector<int> result = even;result.insert(result.cend(), odd.cbegin(), odd.cend());data = result;- int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; }
- void re_arrange(int data[/* len */], size_t len)
- {
- size_t e = 0;
- for (size_t i = 0; i < len; i++)
- {
- if (!(data[i] & 1))
- {
- int t = data[i];
- data[i] = data[e];
- data[e++] = t;
- }
- }
- qsort(data, e, sizeof(int), cmp);
- qsort(&data[e], len - e, sizeof(int), cmp);
- }
#include <criterion/criterion.h> #include <stdlib.h> void re_arrange(int data[], size_t len); _Bool is_arranged(int data[], int expected[], size_t len) { for (size_t i = 0; i < len; i++) if (data[i] != expected[i]) return 0; return 1; } Test(simple_example, re_arrange_simple) { int data[] = {1, 2, 5, 0, 9, 4, 7}; int expected[] = {0, 2, 4, 1, 5, 7, 9}; size_t len = sizeof(data) / sizeof(data[0]); re_arrange(data, len); cr_assert(is_arranged(data, expected, len), "Arrays do not match!"); }
Describe(simple_example)- #include <criterion/criterion.h>
- #include <stdlib.h>
- void re_arrange(int data[], size_t len);
- _Bool is_arranged(int data[], int expected[], size_t len)
- {
It(re_arrange_simple){auto data = std::vector<int>{1,2,5,0,9,4,7};re_arrange(data);Assert::That(data, Equals(std::vector<int>{0,2,4,1,5,7,9}));}};- for (size_t i = 0; i < len; i++)
- if (data[i] != expected[i])
- return 0;
- return 1;
- }
- Test(simple_example, re_arrange_simple)
- {
- int data[] = {1, 2, 5, 0, 9, 4, 7};
- int expected[] = {0, 2, 4, 1, 5, 7, 9};
- size_t len = sizeof(data) / sizeof(data[0]);
- re_arrange(data, len);
- cr_assert(is_arranged(data, expected, len), "Arrays do not match!");
- }
Fundamentals
Due to the great demand (joking) for a better test case to detect foul play, I've implemented just that.
#include <stdlib.h> const char *roll(const char **faces, size_t len) { return faces[rand() % len]; }
- #include <stdlib.h>
const char *roll(const char **faces, size_t len){return faces[0];}- const char *roll(const char **faces, size_t len)
- {
- return faces[rand() % len];
- }
#include <criterion/criterion.h> #include <time.h> #include <stdio.h> #include <stdlib.h> #define NUM_OF_ROLLS 60000 #define LEN 6 const char *faces[LEN] = {"⚀", "⚁", "⚂", "⚃", "⚄", "⚅"}; const char *roll(const char **faces, size_t len); Test(dice, basic_tests) { srand(time(NULL)); int distribution[LEN] = {0}; int expected = NUM_OF_ROLLS / LEN; for (size_t i = 0; i < NUM_OF_ROLLS; i++) { const char *res = roll(faces, LEN); for (size_t j = 0; j < LEN; j++) { if (res == faces[j]) { distribution[j]++; break; } } } for (size_t i = 0; i < LEN; i++) { printf("Face %s rolled %d times\n", faces[i], distribution[i]); cr_assert(abs(distribution[i] - expected) < expected * 0.05, "Face %s is not within expected range: %d", faces[i], distribution[i]); } }
- #include <criterion/criterion.h>
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
#define LEN 6- #define NUM_OF_ROLLS 60000
- #define LEN 6
- const char *faces[LEN] = {"⚀", "⚁", "⚂", "⚃", "⚄", "⚅"};
- const char *roll(const char **faces, size_t len);
_Bool is_valid_roll(const char *res){for (size_t i = 0; i < LEN; i++)if (strcmp(res, faces[i]) == 0)return 1;return 0;}- Test(dice, basic_tests)
- {
- srand(time(NULL));
for (size_t i = 0; i < 100; i++)- int distribution[LEN] = {0};
- int expected = NUM_OF_ROLLS / LEN;
- for (size_t i = 0; i < NUM_OF_ROLLS; i++)
- {
- const char *res = roll(faces, LEN);
cr_assert(is_valid_roll(res), "Not a valid roll");printf("%s\n", res);- for (size_t j = 0; j < LEN; j++)
- {
- if (res == faces[j])
- {
- distribution[j]++;
- break;
- }
- }
- }
- for (size_t i = 0; i < LEN; i++)
- {
- printf("Face %s rolled %d times\n", faces[i], distribution[i]);
- cr_assert(abs(distribution[i] - expected) < expected * 0.05,
- "Face %s is not within expected range: %d",
- faces[i], distribution[i]);
- }
- }
Fundamentals
Written in C.
#include <stdlib.h> const char *roll(const char **faces, size_t len) { return faces[rand() % len]; }
import randomdice=lambda:random.choice("⚀⚁⚂⚃⚄⚅")- #include <stdlib.h>
- const char *roll(const char **faces, size_t len)
- {
- return faces[rand() % len];
- }
#include <criterion/criterion.h> #include <time.h> #include <stdio.h> #include <stdlib.h> #define LEN 6 const char *faces[LEN] = {"⚀", "⚁", "⚂", "⚃", "⚄", "⚅"}; const char *roll(const char **faces, size_t len); _Bool is_valid_roll(const char *res) { for (size_t i = 0; i < LEN; i++) if (strcmp(res, faces[i]) == 0) return 1; return 0; } Test(dice, basic_tests) { srand(time(NULL)); for (size_t i = 0; i < 100; i++) { const char *res = roll(faces, LEN); cr_assert(is_valid_roll(res), "Not a valid roll"); printf("%s\n", res); } }
import codewars_test as test# TODO Write testsimport solution # or from solution import example- #include <criterion/criterion.h>
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
@test.it("Basic tests")def basic_tests():valid = ["⚀", "⚁", "⚂", "⚃", "⚄", "⚅"]# roll 100 timesfor _ in range(100):result = dice()test.expect(result in valid, f"Got {result} but expected one of {valid}")- #define LEN 6
- const char *faces[LEN] = {"⚀", "⚁", "⚂", "⚃", "⚄", "⚅"};
- const char *roll(const char **faces, size_t len);
- _Bool is_valid_roll(const char *res)
- {
- for (size_t i = 0; i < LEN; i++)
- if (strcmp(res, faces[i]) == 0)
- return 1;
- return 0;
- }
- Test(dice, basic_tests)
- {
- srand(time(NULL));
- for (size_t i = 0; i < 100; i++)
- {
- const char *res = roll(faces, LEN);
- cr_assert(is_valid_roll(res), "Not a valid roll");
- printf("%s\n", res);
- }
- }
no need to assign strlen
to a seperate variable, saved some cycles.
#include <stdlib.h> #include <string.h> #include <ctype.h> char *fun(const char *input) { char *result = malloc(strlen(input) + 1); char *p = result; while (*p++ = toupper(*input++)); return result; }
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
char *fun(const char *input) {size_t len = strlen(input);char *result = malloc(len + 1);char *p = result;while ((*p++ = toupper(*input++)));return result;- char *fun(const char *input)
- {
- char *result = malloc(strlen(input) + 1);
- char *p = result;
- while (*p++ = toupper(*input++));
- return result;
- }
_Bool above_two(int *arg) { return *arg > 2 ? 1 : (*arg = 3, 0); }
template<typename T>bool above_two(T& arg){static_assert(std::is_arithmetic<T>::value, "arg must be a arithmetic type");if(arg <= T(2)){arg = T(3);}return true;- _Bool above_two(int *arg)
- {
- return *arg > 2 ? 1 : (*arg = 3, 0);
- }
#include <criterion/criterion.h> _Bool above_two(int *arg); void tester(int arg) { cr_assert_eq(arg > 2, above_two(&arg)); cr_assert(arg > 2); } Test(example, test_case) { tester(0); tester(1); tester(2); tester(3); tester(4); tester(5); tester(6); tester(7); tester(8); tester(9); tester(10); }
template<typename T>void tester(T arg){if(arg > 2){auto expected = arg;Assert::That(above_two(arg), Equals(true));Assert::That(arg, Equals(expected));}else{Assert::That(above_two(arg), Equals(true));Assert::That(arg, Is().GreaterThan(2));}- #include <criterion/criterion.h>
- _Bool above_two(int *arg);
- void tester(int arg)
- {
- cr_assert_eq(arg > 2, above_two(&arg));
- cr_assert(arg > 2);
- }
Describe(example)- Test(example, test_case)
- {
It(test_case_int){tester(0);tester(1);tester(2);tester(3);tester(4);tester(5);tester(6);tester(7);tester(8);tester(9);tester(10);}It(test_case_double){tester(0.0);tester(1.0);tester(2.0);tester(3.0);tester(4.0);tester(5.0);tester(6.0);tester(7.0);tester(8.0);tester(9.0);tester(10.0);}};- tester(0);
- tester(1);
- tester(2);
- tester(3);
- tester(4);
- tester(5);
- tester(6);
- tester(7);
- tester(8);
- tester(9);
- tester(10);
- }
- Eliminated unnecessary variable allocation (
i
inside the for loop). - Moved null terminator assignment into the loop, removing the need for an external assignment.
#include <stdlib.h> #include <string.h> #include <ctype.h> char *fun(const char *input) { size_t len = strlen(input); char *result = malloc(++len), *p = result; while (len--) *p++ = toupper(*input++); return result; }
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- char *fun(const char *input)
- {
- size_t len = strlen(input);
char *result = malloc(len + 1), *p = result;for (size_t i = 0; i < len; i++)- char *result = malloc(++len), *p = result;
- while (len--)
- *p++ = toupper(*input++);
*p = '\0';- return result;
- }
#include <stdlib.h> #include <string.h> #include <ctype.h> char *fun(const char *input) { size_t len = strlen(input); char *result = malloc(len + 1); for (size_t i = 0; input[i]; i++) result[i] = toupper(input[i]); return result[len] = '\0', result; }
def fun(s): return s.upper()- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- char *fun(const char *input)
- {
- size_t len = strlen(input);
- char *result = malloc(len + 1);
- for (size_t i = 0; input[i]; i++)
- result[i] = toupper(input[i]);
- return result[len] = '\0', result;
- }
#include <criterion/criterion.h> #include <stdlib.h> char *fun(const char *input); void tester(const char *input, const char *expected) { char *result = fun(input); cr_assert_str_eq(result, expected); free(result); } Test(Example, test_case) { tester("Welcome", "WELCOME"); tester("hello", "HELLO"); tester("yellow", "YELLOW"); tester("blue", "BLUE"); }
import codewars_test as test# TODO Write testsimport solution # or from solution import example- #include <criterion/criterion.h>
- #include <stdlib.h>
# test.assert_equals(actual, expected, [optional] message)@test.describe("Example")def test_group():@test.it("test case")def test_case():test.assert_equals(fun('Welcome'), 'WELCOME')test.assert_equals(fun('hello'), 'HELLO')test.assert_equals(fun('yellow'), 'YELLOW')test.assert_equals(fun('blue'), 'BLUE')- char *fun(const char *input);
- void tester(const char *input, const char *expected)
- {
- char *result = fun(input);
- cr_assert_str_eq(result, expected);
- free(result);
- }
- Test(Example, test_case)
- {
- tester("Welcome", "WELCOME");
- tester("hello", "HELLO");
- tester("yellow", "YELLOW");
- tester("blue", "BLUE");
- }
#include <stdlib.h> #include <string.h> char *reverse_string(char *word) { size_t len = strlen(word); char *res = malloc(len + 1); for (size_t i = 0; i < len; i++) res[i] = word[len - i - 1]; return res[len] = '\0', res; }
#include <string>std::string reverse_string(std::string_view word){return {word.rbegin(), word.rend()};- #include <stdlib.h>
- #include <string.h>
- char *reverse_string(char *word)
- {
- size_t len = strlen(word);
- char *res = malloc(len + 1);
- for (size_t i = 0; i < len; i++)
- res[i] = word[len - i - 1];
- return res[len] = '\0', res;
- }
#include <criterion/criterion.h> char *reverse_string(char *word); void tester(char *word, char *expected) { char *res = reverse_string(word); cr_assert_str_eq(res, expected); free(res); } Test(Sample_Tests, Valid_Input_Test) { tester("monkey", "yeknom"); tester("home", "emoh"); tester("pneumonoultramicroscopicsilicovolcanoconiosis", "sisoinoconaclovociliscipocsorcimartluonomuenp"); }
Describe(Sample_Tests)- #include <criterion/criterion.h>
- char *reverse_string(char *word);
- void tester(char *word, char *expected)
- {
It(Valid_Input_Test){Assert::That(reverse_string("monkey"), Equals("yeknom"));Assert::That(reverse_string("home"), Equals("emoh"));Assert::That(reverse_string("pneumonoultramicroscopicsilicovolcanoconiosis"), Equals("sisoinoconaclovociliscipocsorcimartluonomuenp"));}};- char *res = reverse_string(word);
- cr_assert_str_eq(res, expected);
- free(res);
- }
- Test(Sample_Tests, Valid_Input_Test)
- {
- tester("monkey", "yeknom");
- tester("home", "emoh");
- tester("pneumonoultramicroscopicsilicovolcanoconiosis", "sisoinoconaclovociliscipocsorcimartluonomuenp");
- }
int sum_of_numbers(int a, int b) { return a + b; }
sum_of_numbers = lambda a, b: a + b- int sum_of_numbers(int a, int b)
- {
- return a + b;
- }
#include <criterion/criterion.h> int sum_of_numbers(int a, int b); Test(Sum_of_Numbers_Tests, Basic_Test_Cases) { cr_assert_eq(sum_of_numbers(1, 2), 3); cr_assert_eq(sum_of_numbers(-1, 1), 0); cr_assert_eq(sum_of_numbers(0, 0), 0); cr_assert_eq(sum_of_numbers(100, 200), 300); cr_assert_eq(sum_of_numbers(-50, -50), -100); }
import codewars_test as test # Import Codewars test framework- #include <criterion/criterion.h>
@test.describe("Sum of Numbers Tests")def test_group():@test.it("Basic Test Cases")def basic_tests():test.assert_equals(sum_of_numbers(1, 2), 3)test.assert_equals(sum_of_numbers(-1, 1), 0)test.assert_equals(sum_of_numbers(0, 0), 0)test.assert_equals(sum_of_numbers(100, 200), 300)test.assert_equals(sum_of_numbers(-50, -50), -100)- int sum_of_numbers(int a, int b);
- Test(Sum_of_Numbers_Tests, Basic_Test_Cases)
- {
- cr_assert_eq(sum_of_numbers(1, 2), 3);
- cr_assert_eq(sum_of_numbers(-1, 1), 0);
- cr_assert_eq(sum_of_numbers(0, 0), 0);
- cr_assert_eq(sum_of_numbers(100, 200), 300);
- cr_assert_eq(sum_of_numbers(-50, -50), -100);
- }
someone had to do it
int multiply_and_add_one(int a, int b) { int res; __asm__ ( "imul %1, %2;" "add $1, %2;" "mov %2, %0;" : "=r" (res) : "r" (a), "r" (b) : ); return res; }
def multiply_and_add_one(a, b):return a * b + 1- int multiply_and_add_one(int a, int b)
- {
- int res;
- __asm__
- (
- "imul %1, %2;"
- "add $1, %2;"
- "mov %2, %0;"
- : "=r" (res)
- : "r" (a), "r" (b)
- :
- );
- return res;
- }
#include <criterion/criterion.h> int multiply_and_add_one(int a, int b); Test(multiply_and_add_one_tests, basic_tests) { cr_assert_eq(multiply_and_add_one(2, 3), 7); cr_assert_eq(multiply_and_add_one(4, 5), 21); cr_assert_eq(multiply_and_add_one(0, 10), 1); cr_assert_eq(multiply_and_add_one(-1, 5), -4); }
import codewars_test as testfrom solution import multiply_and_add_one- #include <criterion/criterion.h>
@test.describe("Multiply and Add One Tests")def _():@test.it("Basic tests")def test_case():test.assert_equals(multiply_and_add_one(2, 3), 7)test.assert_equals(multiply_and_add_one(4, 5), 21)test.assert_equals(multiply_and_add_one(0, 10), 1)test.assert_equals(multiply_and_add_one(-1, 5), -4)- int multiply_and_add_one(int a, int b);
- Test(multiply_and_add_one_tests, basic_tests)
- {
- cr_assert_eq(multiply_and_add_one(2, 3), 7);
- cr_assert_eq(multiply_and_add_one(4, 5), 21);
- cr_assert_eq(multiply_and_add_one(0, 10), 1);
- cr_assert_eq(multiply_and_add_one(-1, 5), -4);
- }
Arrays
I was bored.
#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 r; }
#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;
- }
// TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example. #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) { 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); }
// 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);
- }