Ad
Code
Diff
  • 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;
    • }

someone had to do it

Code
Diff
  • 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;
    • }
Test Cases
Diff
  • #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 test
    • from 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.

Code
Diff
  • #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;
    • }
Code
Diff
  • #include<stdbool.h>
    
    const char *bool_to_word (bool value)
    {
        return (const char*[]){"No", "Yes"}[value];
    }
    • #include<stdbool.h>
    • const char *bool_to_word (bool value)
    • {
    • return value ? "Yes" : "No";
    • return (const char*[]){"No", "Yes"}[value];
    • }

Why use Python when we can use Assembly 😎.

Code
Diff
  • int multiply(int a, int b)
    {
      int res;
      __asm__
      (
        "imull      %1, %2     \n\t"
        "movl       %2, %0"
        : "=r" (res)
        : "r" (a), "r" (b)
      );
      return res;
    }
    • def multiply(a, b):
    • return a*b
    • int multiply(int a, int b)
    • {
    • int res;
    • __asm__
    • (
    • "imull %1, %2 \n\t"
    • "movl %2, %0"
    • : "=r" (res)
    • : "r" (a), "r" (b)
    • );
    • return res;
    • }
Code
Diff
  • #include <stdlib.h>
    #include <string.h>
    
    char *fanis(const char *input, int value)
    {
      char *r = malloc(strlen(input) + 1), *p = r;
      while (*input) *p++ = *input++ + value;
      return *p = '\0', r;
    }
    • #include <stdlib.h>
    • #include <string.h>
    • char *fanis(char *r, int k) {
    • char *w = malloc(strlen(r) + 1), *p = w;
    • while(w && *r) *p++ = k + *r++;
    • return w;
    • char *fanis(const char *input, int value)
    • {
    • char *r = malloc(strlen(input) + 1), *p = r;
    • while (*input) *p++ = *input++ + value;
    • return *p = '\0', r;
    • }
Code
Diff
  • #include <stdlib.h>
    #include <string.h>
    
    char *disemvowel(char *string)
    {
      char *res = (char*)malloc(strlen(string)), *s = res;
      for (; *string != '\0'; string++) if (!strchr("aeiouAEIOU", *string)) *res++ = *string;
      *res = '\0';
      return s;
    }
    • def disemvowel(string_):
    • vowels = ["a", "e", "i", "o", "u"]
    • new_txt = ""
    • for i in range(len(string_)):
    • if string_[i].lower() in vowels:
    • continue
    • else:
    • new_txt += string_[i]
    • return new_txt
    • #include <stdlib.h>
    • #include <string.h>
    • char *disemvowel(char *string)
    • {
    • char *res = (char*)malloc(strlen(string)), *s = res;
    • for (; *string != '\0'; string++) if (!strchr("aeiouAEIOU", *string)) *res++ = *string;
    • *res = '\0';
    • return s;
    • }
Code
Diff
  • int findMissingNumber(int arr[], int n)
    {
      int exp = (n + 2) * (n + 1) / 2, act = 0;
      for (int i = 0; i < n; i++) act += arr[i];    
      return exp - act;
    }
    • class Solution {
    • public:
    • int missingNumber(vector<int>& nums) {
    • }
    • };
    • int findMissingNumber(int arr[], int n)
    • {
    • int exp = (n + 2) * (n + 1) / 2, act = 0;
    • for (int i = 0; i < n; i++) act += arr[i];
    • return exp - act;
    • }
Algorithms
Strings

The hack has been banished.

Code
Diff
  • #include <stdlib.h>
    #include <string.h>
    
    /*
      Heap allocate the string and return it.
    */
    
    char* remove_string(const char *input, const int n)
    {
      if (n <= 0 || input == NULL) return NULL;
      for (size_t i = 0, len = 0; i < (size_t)n; i++)
      {
        input += len;
        if ((len = strlen(input) + 1) == 1) return NULL;
      }  
      return strdup(input);
    }
    • #include <stdlib.h>
    • #include <string.h>
    • /*
    • Heap allocate the string and return it.
    • */
    • char* remove_string(const char *input, const int n)
    • {
    • if (n <= 0 || input == NULL) return NULL;
    • // This is a BIG hack, and I am unsure why it works since in an edge
    • // situation where "n" is greater than the number of '\0' in the string,
    • // the pointer will wander into unallocated memory.
    • for (int c = 1; c < n; input++, (*input == '\0' ? c++ && input++ : c));
    • char* res = strdup(input);
    • return *res == NULL ? NULL : res;
    • for (size_t i = 0, len = 0; i < (size_t)n; i++)
    • {
    • input += len;
    • if ((len = strlen(input) + 1) == 1) return NULL;
    • }
    • return strdup(input);
    • }
Algorithms
Strings

Only God knows the sins that I have committed by writting this code.

Code
Diff
  • #include <stdlib.h>
    #include <string.h>
    
    /*
      Heap allocate the string and return it.
    */
    
    char* remove_string(const char *input, const int n)
    {
      if (n <= 0 || input == NULL) return NULL;
      // This is a BIG hack, and I am unsure why it works since in an edge
      // situation where "n" is greater than the number of '\0' in the string,
      // the pointer will wander into unallocated memory.
      for (int c = 1; c < n; input++, (*input == '\0' ? c++ && input++ : c));
      char* res = strdup(input);
      return *res == NULL ? NULL : res;
    }
    • #include <stdlib.h>
    • #include <string.h>
    • /*
    • Heap allocate the string and return it.
    • */
    • char* remove_string(const char *input, const int n)
    • {
    • if (n <= 0 || input == NULL) return NULL;
    • size_t len = 0;
    • for (size_t i = 0; i < (size_t)n; ++i)
    • {
    • input += len;
    • len = strlen(input) + 1;
    • if (len == 1) return NULL;
    • }
    • size_t index = 0;
    • char *res = (char*)malloc(sizeof(char) * (len + 1));
    • for (; *input != '\0'; input++) res[index++] = *input; res[index] = '\0';
    • return res;
    • // This is a BIG hack, and I am unsure why it works since in an edge
    • // situation where "n" is greater than the number of '\0' in the string,
    • // the pointer will wander into unallocated memory.
    • for (int c = 1; c < n; input++, (*input == '\0' ? c++ && input++ : c));
    • char* res = strdup(input);
    • return *res == NULL ? NULL : res;
    • }
Algorithms
Strings

This challenge involves removing the n-th string from a character array labeled "input" and then returning it. It is important to consider edge cases where the value of "n" can be zero or greater than the total number of strings available. In such cases, the function should return NULL.

#include <stdlib.h>
#include <string.h>

/*
  Heap allocate the string and return it.
*/

char* remove_string(const char *input, const int n)
{
  if (n <= 0 || input == NULL) return NULL;
  size_t len = 0;
  for (size_t i = 0; i < (size_t)n; ++i) 
  {
    input += len;
    len = strlen(input) + 1;
    if (len == 1) return NULL;
  }
  
  size_t index = 0;
  char  *res   = (char*)malloc(sizeof(char) * (len + 1));  
  
  for (; *input != '\0'; input++) res[index++] = *input; res[index] = '\0';
  return res;
}
Algorithms

Just use 'stdint.h' :D

Code
Diff
  • #include <stdint.h>
    number_of_digits(uint64_t n){uint8_t d=0;for(;n!=0;n/=10)d+=1;return d;}
    • #include <math.h>
    • #include <stdint.h>
    • uint8_t number_of_digits(uint64_t n)
    • {
    • return log10(n) + 1;
    • }
    • number_of_digits(uint64_t n){uint8_t d=0;for(;n!=0;n/=10)d+=1;return d;}

One liner.

Code
Diff
  • #include <stdio.h>
    
    int prnt_mltply(int m, int n) {
        for (int i = 0; i < m; i++) for (int j = 1; j <= n; j++) printf("%d * %d = %d\n", i + 1, j, (i+1) * j);
        return m * n;
    }
    • #include <stdio.h>
    • int prnt_mltply(int m, int n) {
    • int i = 1;
    • while(i <= m) {
    • int j = 1;
    • while(j <= n) {
    • printf("%d * %d = %d
    • ", i, j, i * j);
    • j++;
    • }
    • i++;
    • printf("\n");
    • }
    • for (int i = 0; i < m; i++) for (int j = 1; j <= n; j++) printf("%d * %d = %d
    • ", i + 1, j, (i+1) * j);
    • return m * n;
    • }