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

To make Hello World return string modifiable, can always allocate memory for the string.
Here the code should produce "Xello World." when return string modified.

Code
Diff
  • char* Hi (void)
    {
      char* ans=malloc(13);
      strcpy(ans,"Hello World.");
      return ans;
    }
    
    • const char* Hi (void)
    • char* Hi (void)
    • {
    • return("Hello World.");
    • }
    • char *oopsHi(void)
    • {
    • return ("Hello World.");
    • char* ans=malloc(13);
    • strcpy(ans,"Hello World.");
    • return ans;
    • }

Some of what I did may or may not ruin the language generalizability of the solution :/

Code
Diff
  • // e.g.:
    // simplify( 15,   5) == [  3,  1]
    // simplify(-50,  20) == [- 5,  2]
    // simplify( 20, - 2) == [-10,  1]
    // simplify(- 6, - 3) == [  2,  1]
    // simplify(- 0,   3) == [  0,  1]
    // simplify(  0,   0) == undefined
    // simplify(  5, - 0) == undefined
    
    function simplify(n, d){
      if (d === 0)
        return undefined;
      if (n === 0)
        return [0, 1];
    
      // fraction_sign contains the sign of the fraction (1 if positive, -1 if negative)
      const fraction_sign = Math.sign(n * d);
      // fraction_gcd contains the greatest common divisor of n and d
      const fraction_gcd = gcd(Math.abs(n), Math.abs(d));
      // we calculate the reduced numerator (it has the same sign as the fraction)
      const result_n = fraction_sign * Math.abs(n) / fraction_gcd;
      // we calculate the reduced denominator
      const result_d = Math.abs(d) / fraction_gcd;
    
      return [result_n, result_d];
    }
    
    
    
    // gcd(x, y) calculates the greatest common divisor of x and y
    // x and y must be non-negative integers
    // USED ALGORITHM: binary method
    // BASED ON: https://en.wikipedia.org/wiki/Binary_GCD_algorithm
    
    function gcd(x, y){
      if (x === y)
        return x;
      if (x > y)
        return gcd(y, x);
      if (x === 0)
        return y;
    
      switch(2 * (x % 2) + (y % 2)) {
        case 0:                           // both are divisible by 2
          return gcd(x >> 1, y >> 1) << 1;
        case 1:                           // only y is divisible by 2
          return gcd(x >> 1, y);
        case 2:                           // only x is divisible by 2
          return gcd(x, y >> 1);
        case 3:                           // neither are divisible by 2
          return gcd((y - x) >> 1, x);
        default:                          // default case should not run
          return 0;
      }
    }
    • // e.g.:
    • // simplify( 15, 5) == [ 3, 1]
    • // simplify(-50, 20) == [- 5, 2]
    • // simplify( 20, - 2) == [-10, 1]
    • // simplify(- 6, - 3) == [ 2, 1]
    • // simplify(- 0, 3) == [ 0, 1]
    • // simplify( 0, 0) == undefined
    • // simplify( 5, - 0) == undefined
    • function simplify(n, d){
    • if (d === 0){
    • if (d === 0)
    • return undefined;
    • }
    • if (n === 0)
    • return [0, 1];
    • // fraction_sign contains the sign of the fraction (1 if positive, -1 if negative)
    • const fraction_sign = ((n < 0) ? -1 : 1) * ((d < 0) ? -1 : 1);
    • const fraction_sign = Math.sign(n * d);
    • // fraction_gcd contains the greatest common divisor of n and d
    • const fraction_gcd = gcd(Math.abs(n), Math.abs(d));
    • // we calculate the reduced numerator (it has the same sign as the fraction)
    • const result_n = fraction_sign * Math.abs(n) / fraction_gcd;
    • // we calculate the reduced denominator
    • const result_d = Math.abs(d) / fraction_gcd;
    • return [result_n, result_d];
    • }
    • // gcd(x, y) calculates the greatest common divisor of x and y
    • // x and y must be non-negative integers
    • // USED ALGORITHM: binary method
    • // BASED ON: https://en.wikipedia.org/wiki/Binary_GCD_algorithm
    • function gcd(x, y){
    • if (x === y){
    • if (x === y)
    • return x;
    • }
    • if (x === 0){
    • if (x > y)
    • return gcd(y, x);
    • if (x === 0)
    • return y;
    • }
    • if (y === 0){
    • return x;
    • }
    • if (x % 2 === 0){
    • if (y % 2 === 1){
    • return gcd(x >> 1, y);
    • }
    • else {
    • switch(2 * (x % 2) + (y % 2)) {
    • case 0: // both are divisible by 2
    • return gcd(x >> 1, y >> 1) << 1;
    • }
    • }
    • if (y % 2 === 0){
    • return gcd(x, y >> 1);
    • }
    • if (x > y){
    • return gcd((x - y) >> 1, y);
    • case 1: // only y is divisible by 2
    • return gcd(x >> 1, y);
    • case 2: // only x is divisible by 2
    • return gcd(x, y >> 1);
    • case 3: // neither are divisible by 2
    • return gcd((y - x) >> 1, x);
    • default: // default case should not run
    • return 0;
    • }
    • return gcd((y - x) >> 1, x);
    • }

Fixed test cases.

Code
Diff
  • var nextGeneration = grid =>
      grid.map((row, rowIndex) =>
        row.map((cell, colIndex) => {
          let neighbours = -cell;
          [-1, 0, 1].forEach(dr =>
            [-1, 0, 1].forEach(dc =>
              neighbours += (grid[rowIndex + dr] || [])[colIndex + dc] || 0
            )
          );
    
          if(neighbours == 3)
            return 1;
          if(neighbours != 2)
            return 0;
          return cell;
        })
      );
    • function nextGeneration(grid) {
    • return grid.map((row, rowIndex) => {
    • return row.map((cell, colIndex) => {
    • // Non circular grid
    • if(!rowIndex || !colIndex || rowIndex == grid.length - 1 || colIndex == row.length - 1) {
    • return 0;
    • }
    • const neighbours = grid[rowIndex ][colIndex -1] +
    • grid[rowIndex ][colIndex +1] +
    • grid[rowIndex -1][colIndex -1] +
    • grid[rowIndex -1][colIndex ] +
    • grid[rowIndex -1][colIndex +1] +
    • grid[rowIndex +1][colIndex -1] +
    • grid[rowIndex +1][colIndex ] +
    • grid[rowIndex +1][colIndex +1];
    • var nextGeneration = grid =>
    • grid.map((row, rowIndex) =>
    • row.map((cell, colIndex) => {
    • let neighbours = -cell;
    • [-1, 0, 1].forEach(dr =>
    • [-1, 0, 1].forEach(dc =>
    • neighbours += (grid[rowIndex + dr] || [])[colIndex + dc] || 0
    • )
    • );
    • if(neighbours == 3) return 1;
    • else if(neighbours != 2) return 0;
    • if(neighbours == 3)
    • return 1;
    • if(neighbours != 2)
    • return 0;
    • return cell;
    • });
    • });
    • }
    • })
    • );
Code
Diff
  • class Person
      attr_accessor :name
    
      def initialize(name)
        self.name = name
      end
    end
    
    def change_name(new_name, thing)
      thing.name = new_name if thing.respond_to?(:name=)
    end
    
    
    • class Person
    • attr_accessor :name
    • def initialize(name)
    • @name = name
    • self.name = name
    • end
    • end
    • def change_name(new_name, thing)
    • if thing.respond_to?(:name=)
    • # do something
    • thing.name = new_name
    • else
    • nil
    • end
    • thing.name = new_name if thing.respond_to?(:name=)
    • end

I prefer looking for overlaps on a min and max of the opposing range.

Code
Diff
  • static void normalize(int & num)
    {
        num %= 24;
    }
    
    static bool willTheyMeet(
            int startHour1,
            int endHour1,
            int startHour2,
            int endHour2) {
        normalize(startHour1);
        normalize(startHour2);
        normalize(endHour1);
        normalize(endHour2);
        
        return (startHour2 >= startHour1 && startHour2 <= endHour1) ||
                (endHour2 >= startHour1 && endHour2 <= endHour1) ||
                (startHour1 >= startHour2 && startHour1 <= endHour2) ||
                (endHour1 >= startHour2 && endHour1 <= endHour2);
    }
    • #include <cmath>
    • static float hourToRad(int hour)
    • static void normalize(int & num)
    • {
    • return (float) hour * (M_PI / 12.0);
    • };
    • num %= 24;
    • }
    • static bool willTheyMeet(
    • int startHour1,
    • int endHour1,
    • int startHour2,
    • int endHour2)
    • {
    • auto a1 = hourToRad(startHour1);
    • auto a2 = hourToRad(endHour1);
    • auto b1 = hourToRad(startHour2);
    • auto b2 = hourToRad(endHour2);
    • if (a1 == b2 || a2 == b1)
    • return false;
    • float da = (a2 - a1) / 2.0;
    • float db = (b2 - b1) / 2.0;
    • float ma = (a2 + a1) / 2.0;
    • float mb = (b2 + b1) / 2.0;
    • float cda = cos(da);
    • float cdb = cos(db);
    • return cos(ma - b1) >= cda ||
    • cos(ma - b2) >= cda ||
    • cos(mb - a1) >= cdb ||
    • cos(mb - a2) >= cdb;
    • };
    • int startHour1,
    • int endHour1,
    • int startHour2,
    • int endHour2) {
    • normalize(startHour1);
    • normalize(startHour2);
    • normalize(endHour1);
    • normalize(endHour2);
    • return (startHour2 >= startHour1 && startHour2 <= endHour1) ||
    • (endHour2 >= startHour1 && endHour2 <= endHour1) ||
    • (startHour1 >= startHour2 && startHour1 <= endHour2) ||
    • (endHour1 >= startHour2 && endHour1 <= endHour2);
    • }

While your lambdas are a cool language feature, they serve no purpose outside of being cool and they take the ability away from the compiler to optimize your code. If you take your test conditions and invert them, then use them as the escape conditions, you reduce your compiled assember instruction count from 104 instructions to 61 instructions, according to GodBolt. If you take out the individual ifs and use a single if with the original logic you had, not-inverted, then you get 51 instructions.

Check them out here: https://godbolt.org/z/R_Of7C

Code
Diff
  • #include <string>
    bool testPassword(std::string password) // 58 instructions
    {
        if (password.size() < 8) return false;
        if (password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") == std::string::npos) return false;
        if (password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") == std::string::npos) return false;
        if (password.find_first_of("0123456789") == std::string::npos) return false;
        return true;
    }
    /* 51 instructions
    #include <string>
    bool testPassword(std::string password) 
    {
        if (password.size() >= 8 &&
            password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos &&
            password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") != std::string::npos &&
            password.find_first_of("0123456789") != std::string::npos) 
            return true;
        return false;
    }
    */
    
    • #include <string>
    • bool testPassword(std::string password)
    • bool testPassword(std::string password) // 58 instructions
    • {
    • auto const has_length{[&]() { return password.size() >= 8; }};
    • auto const has_upper{[&]() { return password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos; }};
    • auto const has_special{[&]() { return password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") != std::string::npos; }};
    • auto const has_digit{[&]() { return password.find_first_of("0123456789") != std::string::npos; }};
    • return has_length() && has_upper() && has_special() && has_digit();
    • }
    • if (password.size() < 8) return false;
    • if (password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") == std::string::npos) return false;
    • if (password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") == std::string::npos) return false;
    • if (password.find_first_of("0123456789") == std::string::npos) return false;
    • return true;
    • }
    • /* 51 instructions
    • #include <string>
    • bool testPassword(std::string password)
    • {
    • if (password.size() >= 8 &&
    • password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos &&
    • password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") != std::string::npos &&
    • password.find_first_of("0123456789") != std::string::npos)
    • return true;
    • return false;
    • }
    • */
Code
Diff
  • def decrypt(code, amount):
        return ''.join([chr((ord(c)-amount+65)%26+65) if c.isalpha() else c for c in code])
    • def decrypt(code,amount):
    • word = ""
    • for i in code:
    • if i==" ":
    • word+=" "
    • continue
    • tmp = ord(i)-amount
    • if tmp<65:
    • tmp+=26
    • word+=chr(tmp)
    • return word
    • def decrypt(code, amount):
    • return ''.join([chr((ord(c)-amount+65)%26+65) if c.isalpha() else c for c in code])
Code
Diff
  • def PotatoCode(o):
        i= len(o)-3
        return o[2:i]*2+o[:i-1]+" tato"
        
    • def PotatoCode(input):
    • output = input[2:len(input)-3]*2+str(input[:len(input)-4])+" tato"
    • return str(output)
    • def PotatoCode(o):
    • i= len(o)-3
    • return o[2:i]*2+o[:i-1]+" tato"