Start a new Kumite
AllAgda (Beta)BF (Beta)CCFML (Beta)ClojureCOBOL (Beta)CoffeeScriptCommonLisp (Beta)CoqC++CrystalC#D (Beta)DartElixirElm (Beta)Erlang (Beta)Factor (Beta)Forth (Beta)Fortran (Beta)F#GoGroovyHaskellHaxe (Beta)Idris (Beta)JavaJavaScriptJulia (Beta)Kotlinλ Calculus (Beta)LeanLuaNASMNim (Beta)Objective-C (Beta)OCaml (Beta)Pascal (Beta)Perl (Beta)PHPPowerShell (Beta)Prolog (Beta)PureScript (Beta)PythonR (Beta)RacketRaku (Beta)Reason (Beta)RISC-V (Beta)RubyRustScalaShellSolidity (Beta)SQLSwiftTypeScriptVB (Beta)
Show only mine

Kumite (ko͞omiˌtā) is the practice of taking techniques learned from Kata and applying them through the act of freestyle sparring.

You can create a new kumite by providing some initial code and optionally some test cases. From there other warriors can spar with you, by enhancing, refactoring and translating your code. There is no limit to how many warriors you can spar with.

A great use for kumite is to begin an idea for a kata as one. You can collaborate with other code warriors until you have it right, then you can convert it to a kata.

Ad
Ad

No matter what arg is it would always return true,

because in the original code

def above_two(arg): return (arg < 2) or (arg >= 2)

it will return true if arg is greater or equal than two,
if arg is less than two it will also return true

Code
Diff
  • #Onelined again...
    def above_two(arg): return True
    • #Onelined again...
    • def above_two(arg): return arg > 2 if arg > 2 else not arg > 2
    • def above_two(arg): return True
Algorithms

Added memory ordering

Increment should just need relaxed, as it does not controll other states. It either keeps the potential object from being reclaimed, in which case there is no change, or the object is already reclaimed and you don't touch it anyway.

all else is more or less acq_rel to sync possible last access to an object, and it's destructor

Code
Diff
  • #include <atomic>
    
    struct Counter {
      static constexpr uint64_t is_zero = 1ull << 63;
      static constexpr uint64_t helped = 1ull << 62;
      std::atomic<uint64_t> counter{1};
      
      bool increment_if_not_zero(){
        return (counter.fetch_add(1, std::memory_order_relaxed) & is_zero) == 0;
      }
      
      bool decrement(){
        if(counter.fetch_sub(1, std::memory_order_acq_rel) == 1){
          uint64_t e = 0;
          if(counter.compare_exchange_strong(e, is_zero, std::memory_order_acq_rel, std::memory_order_acquire)) return true;
          else if((e & helped) && (counter.exchange(is_zero, std::memory_order_acq_rel) & helped)) return true;
        }
        return false;
      }
      
      uint64_t read(){
        auto val = counter.load(std::memory_order_acquire);
        if(val == 0 && counter.compare_exchange_strong(val, is_zero | helped, std::memory_order_acq_rel, std::memory_order_acquire)) return 0;
        return (val & is_zero) ? 0 : val;
      }
    };
    • #include <atomic>
    • struct Counter {
    • static constexpr uint64_t is_zero = 1ull << 63;
    • static constexpr uint64_t helped = 1ull << 62;
    • std::atomic<uint64_t> counter{1};
    • bool increment_if_not_zero(){
    • return (counter.fetch_add(1) & is_zero) == 0;
    • return (counter.fetch_add(1, std::memory_order_relaxed) & is_zero) == 0;
    • }
    • bool decrement(){
    • if(counter.fetch_sub(1) == 1){
    • if(counter.fetch_sub(1, std::memory_order_acq_rel) == 1){
    • uint64_t e = 0;
    • if(counter.compare_exchange_strong(e, is_zero)) return true;
    • else if((e & helped) && (counter.exchange(is_zero) & helped)) return true;
    • if(counter.compare_exchange_strong(e, is_zero, std::memory_order_acq_rel, std::memory_order_acquire)) return true;
    • else if((e & helped) && (counter.exchange(is_zero, std::memory_order_acq_rel) & helped)) return true;
    • }
    • return false;
    • }
    • uint64_t read(){
    • auto val = counter.load();
    • if(val == 0 && counter.compare_exchange_strong(val, is_zero | helped)) return 0;
    • auto val = counter.load(std::memory_order_acquire);
    • if(val == 0 && counter.compare_exchange_strong(val, is_zero | helped, std::memory_order_acq_rel, std::memory_order_acquire)) return 0;
    • return (val & is_zero) ? 0 : val;
    • }
    • };
Arrays
Code
Diff
  • #include <vector>
    #include <unordered_map>
    #include <numeric>
    
    int unique_sum(const std::vector<int>& n)
    {
      int sum{0};
      std::unordered_map<int,int> frequencies;
      for (auto i : n) {
        auto &v=frequencies[i];
        if (v==0)      sum+=i;
        else if (v==1) sum-=i;
        v++;
      }
      return sum;
    }
    • #include <vector>
    • #include <unordered_map>
    • #include <numeric>
    • int unique_sum(const std::vector<int>& n)
    • {
    • int sum{0};
    • std::unordered_map<int,int> frequencies;
    • for (auto i : n)
    • ++frequencies[i];
    • return std::accumulate(begin(n), end(n), 0, [&](auto sum, auto i){
    • return frequencies[i] == 1 ? sum + i : sum;
    • });
    • for (auto i : n) {
    • auto &v=frequencies[i];
    • if (v==0) sum+=i;
    • else if (v==1) sum-=i;
    • v++;
    • }
    • return sum;
    • }
Code
Diff
  • #include <stdlib.h>
    #include <string.h>
    
    char *reverse_string(const char *word) {
        if (!word) return NULL;
    
        size_t len = strlen(word);
        char *res = malloc(len + 1);
        if (!res) return NULL;
    
        for (size_t i = 0; i < len; i++)
            res[i] = word[len - i - 1];
    
        res[len] = '\0';
        return res;
    }
    
    • #include <stdlib.h>
    • #include <string.h>
    • char *reverse_string(const 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;
    • }
    • char *reverse_string(const char *word) {
    • if (!word) return NULL;
    • size_t len = strlen(word);
    • char *res = malloc(len + 1);
    • if (!res) return NULL;
    • for (size_t i = 0; i < len; i++)
    • res[i] = word[len - i - 1];
    • res[len] = '\0';
    • return res;
    • }
Code
Diff
  • def slow_sum(lst):
        return sum(lst) if lst else 0
    • import random
    • from random import randrange
    • def slow_sum(lst):
    • if lst:
    • s = sum(lst)
    • else:
    • s = 0
    • randrange(s-1, s+1)
    • while True:
    • temp = randrange(s-10000, s+10000)
    • if temp == s:
    • return temp
    • return sum(lst) if lst else 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;
    • }