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

Version with no modulus or division operators at all.

Uses this fact:

Even powers of two are 3k - 1 (2^5 = 32 = 33 - 1, etc.) for some k
Odd powers of two are 3k + 1 (2^4 = 16 = 15 + 1, etc.) for some k

Any given binary number is a sum of powers of 2; some are odd powers and some are even powers.

10011 = 1 * (3a - 1) + 0 * (3b + 1) + 0 * (3c - 1) + 1 * (3d + 1) + 1 * (3e - 1) = (3a - 1) + (3c - 1) + (3d + 1) = 3(a+c+d) - 1

It stands to reason that this is not divisible by 3 (and 10011 = 19 which is not divisible by 3)

For any binary number, it is 3k + count_odd_bits - count_even_bits for some integer k. Thus, it is only divisible by 3 if count_odd_bits - count_even bits is divisible by three

Code
Diff
  • def divisibility_by_3(x):
        e, o = 0, 0
        while x > 0:
            e += x & 1
            o += (x >> 1) & 1
            x >>= 2
            if x == 0:
                x, e, o = abs(e - o), 0, 0
                if x == 1 or x == 2:
                    return False
        return True
        
    • def divisibility_by_3(x):
    • list_of_values = [int(d) for d in str(x)]
    • summation = sum(list_of_values)
    • if summation % 3 == 0:
    • return True
    • else:
    • return False
    • e, o = 0, 0
    • while x > 0:
    • e += x & 1
    • o += (x >> 1) & 1
    • x >>= 2
    • if x == 0:
    • x, e, o = abs(e - o), 0, 0
    • if x == 1 or x == 2:
    • return False
    • return True
Code
Diff
  • async function a() {
      return 'ok';
    }
    
    async function b() {
      try {
        let res = await a();
        console.log(res);
      } catch(err){
        console.log(err);
      };
    }
    • function a() {
    • return new Promise(function (resolve, reject) {
    • resolve('ok');
    • });
    • async function a() {
    • return 'ok';
    • }
    • function b() {
    • a().then(function(res) {
    • async function b() {
    • try {
    • let res = await a();
    • console.log(res);
    • }).catch(function(err) {
    • } catch(err){
    • console.log(err);
    • })
    • };
    • }
Code
Diff
  • function monteCarlo() {
      return new Promise(function (resolve, reject) {
        resolve([{
          titulo: 'UOL - O Melhor conteúdo'
        }]);
      });
    }
    
    async function main() {
    
      // converter para ES6
      let result = await monteCarlo();
      
      return result[0].titulo;
    }
    • function monteCarlo() {
    • return new Promise(function (resolve, reject) {
    • resolve([{
    • titulo: 'UOL - O Melhor conteúdo'
    • }]);
    • });
    • }
    • async function main() {
    • // converter para ES6
    • monteCarlo().then(function(response) {
    • console.log(response)
    • });
    • let result = await monteCarlo();
    • return true;
    • return result[0].titulo;
    • }
Code
Diff
  • class Component {
      constructor(dom) {
        console.log('Parent class constructor executed!');
        this.dom = dom;
      }
      
      onCreate() {
        return true;
      }
    }
    
    class Collection extends Component {
      constructor(dom,created){
        super(dom);
      }
    }
    
    new Collection('#body',true);
    • class Component {
    • constructor(dom) {
    • console.log('Parent class constructor executed!');
    • this.dom = dom;
    • }
    • onCreate() {
    • return true;
    • }
    • }
    • class Collection {
    • //
    • }
    • class Collection extends Component {
    • constructor(dom,created){
    • super(dom);
    • }
    • }
    • new Collection('#body',true);
Code
Diff
  • class Component {
      constructor(dom) {
          this.dom = dom;
      }
      
      onCreate() {
        console.log('onCreate from parent class');
        return 'missing';
      }
      
      static on(event, callback) {
        callback();
      }
      
      async emit(event, data) {}
    }
    
    class Title extends Component {
      onCreate() {
        super.onCreate()
        return 'super!'
      }
      
    }
    
    
    
    • class Component {
    • constructor(dom) {
    • this.dom = dom;
    • }
    • onCreate() {
    • console.log('onCreate from parent class');
    • return 'missing';
    • }
    • static on(event, callback) {
    • callback();
    • }
    • async emit(event, data) {}
    • }
    • class Title extends Component {
    • onCreate() {
    • super.onCreate()
    • return 'super!'
    • }
    • }
Code
Diff
  • {
      function todo() {
        return 'Walk';
      }
      
      // reescrever em ES6
      {
      
        function todo() {
          return 'Run';
        }
      }
    }
    • function todo() {
    • return 'Walk';
    • }
    • // reescrever em ES6
    • (function() {
    • {
    • function todo() {
    • return 'Run';
    • return 'Walk';
    • }
    • })();
    • // reescrever em ES6
    • {
    • function todo() {
    • return 'Run';
    • }
    • }
    • }
Code
Diff
  • // reescreva usando ES6
    
    var prop = 'myProp';
    
    var obj = {
      [prop]: 123,
      myFunc() {
        return this[prop];
      } 
    };
    • // reescreva usando ES6
    • var prop = 'myProp';
    • var obj = {
    • myFunc: function() {
    • [prop]: 123,
    • myFunc() {
    • return this[prop];
    • }
    • };
    • obj[prop] = 123;
    • };
Code
Diff
  • var title = 'UOL - O melhor conteúdo';
    
    var share = {
      fb: {
        title
      },
      twitter: {
        tweet: title
      }
    };
    • var title = 'UOL - O melhor conteúdo';
    • var share = {
    • fb: {
    • title: title
    • title
    • },
    • twitter: {
    • tweet: title
    • }
    • };
Code
Diff
  • let results = [
      {materia: {conteudo: {titulo: 'São Paulo'}}, tags: [1, 2, 3]},
      {materia: {conteudo: {}}, tags: [3, 2]},
      {materia: {conteudo: {titulo: 'Rio de Janeiro'}}, tags: [3, 2]},
    ];
    
    for (const result of results) {
      
      const {materia: {conteudo: {titulo = 'Brasil'}}} = result
    }
    • let results = [
    • {materia: {conteudo: {titulo: 'São Paulo'}}, tags: [1, 2, 3]},
    • {materia: {conteudo: {}}, tags: [3, 2]},
    • {materia: {conteudo: {titulo: 'Rio de Janeiro'}}, tags: [3, 2]},
    • ];
    • for (const result of results) {
    • let titulo = result.materia.conteudo.titulo || 'Brasil';
    • console.log(titulo);
    • const {materia: {conteudo: {titulo = 'Brasil'}}} = result
    • }
Code
Diff
  • var materia = {
      conteudo: {
        titulo: 'UOL',
      },
      tags: ['São Paulo', 'SP', 'Sudeste', 'Brasil', 'América Latina']
    };
    
    // var uf = materia.tags[1];
    // var regiao = materia.tags[2];
    
    const { conteudo: { titulo }, tags: [,uf,regiao]} = materia;
    • var materia = {
    • conteudo: {
    • titulo: 'UOL',
    • },
    • tags: ['São Paulo', 'SP', 'Sudeste', 'Brasil', 'América Latina']
    • };
    • var uf = materia.tags[1];
    • var regiao = materia.tags[2];
    • // var uf = materia.tags[1];
    • // var regiao = materia.tags[2];
    • var titulo = materia.conteudo.titulo;
    • const { conteudo: { titulo }, tags: [,uf,regiao]} = materia;