Ad
Code
Diff
  • #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));
    • }
Code
Diff
  • #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 0
    • n = 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;
    • }
Ciphers
Fundamentals
Code
Diff
  • #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 in
    • let 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;
    • }
Lists
Code
Diff
  • #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);
    • }
Fundamentals

Due to the great demand (joking) for a better test case to detect foul play, I've implemented just that.

Code
Diff
  • #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];
    • }
Fundamentals

Written in C.

Code
Diff
  • #include <stdlib.h>
    
    const char *roll(const char **faces, size_t len)
    {
      return faces[rand() % len];
    }
    • import random
    • dice=lambda:random.choice("⚀⚁⚂⚃⚄⚅")
    • #include <stdlib.h>
    • const char *roll(const char **faces, size_t len)
    • {
    • return faces[rand() % len];
    • }

no need to assign strlen to a seperate variable, saved some cycles.

Code
Diff
  • #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;
    • }
Code
Diff
  • _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);
    • }
  • Eliminated unnecessary variable allocation (i inside the for loop).
  • Moved null terminator assignment into the loop, removing the need for an external assignment.
Code
Diff
  • #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;
    • }
Code
Diff
  • #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;
    • }
Code
Diff
  • #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;
    • }
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;
    • }
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];
    • }
Loading more items...