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
Code
Diff
  • package com.mystuff.juststuff;
    
    import java.text.*;
    import java.util.*;
    
    public class Palindrome {
    
    	private static final SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
    
    	public Set<Date> countDatePalindromes(final Date startDate, final Date endDate) {
    		final Set<Date> set = new TreeSet<>();
    		final GregorianCalendar startCal = new GregorianCalendar();
    		final GregorianCalendar endCal = new GregorianCalendar();
    
        // If end date is before start date, flip them.
        startCal.setTime(startDate.before(endDate) ? startDate : endDate);
        endCal.setTime(startDate.after(endDate) ? startDate : endDate);
        
    		for (; startCal.before(endCal) || startCal.equals(endCal); startCal.add(Calendar.DAY_OF_MONTH, 1)) {   
    			final String forward = sdf.format(startCal.getTime());
          final String back = new StringBuilder(forward).reverse().toString();            
    			if (forward.equals(back)) {
            set.add(startCal.getTime()); // Date string looks same both ways	
          }
    		}	
        
    		return set;
    	}
    	
    }
    
    • package com.mystuff.juststuff;
    • import java.text.*;
    • import java.util.Calendar;
    • import java.util.Date;
    • import java.util.GregorianCalendar;
    • import java.util.TreeSet;
    • import java.util.*;
    • public class Palindrome {
    • SimpleDateFormat sdf;
    • public Palindrome() {
    • this.sdf = new SimpleDateFormat("MMddyyyy");
    • sdf.setLenient(false);
    • }
    • public TreeSet<Date> countDatePalindromes(Date startDate, Date endDate) {
    • TreeSet<Date> palSet = new TreeSet<Date>();
    • GregorianCalendar startCal = new GregorianCalendar();
    • GregorianCalendar endCal = new GregorianCalendar();
    • private static final SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
    • /*
    • end date is chronologically before start date. flip them.
    • NOTE: lines 23-30 should be a pre-condition to the countDatePalindromes method,
    • so they should be deleted.
    • */
    • if (startDate.getTime() > endDate.getTime()) {
    • startCal.setTime(endDate);
    • endCal.setTime(startDate);
    • }else {
    • startCal.setTime(startDate);
    • endCal.setTime(endDate);
    • }
    • String regularDate = null;
    • String palDate = null;
    • while ( (startCal.before(endCal)) || (startCal.equals(endCal)) ) {
    • regularDate = sdf.format(startCal.getTime());
    • palDate = palindromify(startCal.getTime());
    • // a date palindrome was found
    • if (regularDate.equals(palDate))
    • palSet.add(startCal.getTime());
    • startCal.add(Calendar.DAY_OF_MONTH, 1);
    • public Set<Date> countDatePalindromes(final Date startDate, final Date endDate) {
    • final Set<Date> set = new TreeSet<>();
    • final GregorianCalendar startCal = new GregorianCalendar();
    • final GregorianCalendar endCal = new GregorianCalendar();
    • // If end date is before start date, flip them.
    • startCal.setTime(startDate.before(endDate) ? startDate : endDate);
    • endCal.setTime(startDate.after(endDate) ? startDate : endDate);
    • for (; startCal.before(endCal) || startCal.equals(endCal); startCal.add(Calendar.DAY_OF_MONTH, 1)) {
    • final String forward = sdf.format(startCal.getTime());
    • final String back = new StringBuilder(forward).reverse().toString();
    • if (forward.equals(back)) {
    • set.add(startCal.getTime()); // Date string looks same both ways
    • }
    • }
    • return palSet;
    • return set;
    • }
    • private String palindromify(final Date arg) {
    • StringBuffer palBuff = new StringBuffer(8);
    • palBuff = sdf.format(arg, palBuff, new FieldPosition(0));
    • return palBuff.reverse().toString();
    • }
    • }
Code
Diff
  • function getFactors (n) {
      if (!Number.isInteger(n) || n < 1) return [];
      
      var divisors = new Set(); 
      var step = n % 2 == 0 ? 1 : 2;
      for (let i = 1; i <= Math.sqrt(n); i += step) {
        if (n % i == 0) divisors.add(i).add(n/i)
      }
      return Array.from(divisors).sort((a,b)=>a-b)
    }
    • function getFactors (n) {
    • if (!Number.isInteger(n) || n < 1) return [];
    • var divisors = new Set();
    • for (let i = 1; i <= Math.sqrt(n); i++) {
    • var divisors = new Set();
    • var step = n % 2 == 0 ? 1 : 2;
    • for (let i = 1; i <= Math.sqrt(n); i += step) {
    • if (n % i == 0) divisors.add(i).add(n/i)
    • }
    • return Array.from(divisors)
    • return Array.from(divisors).sort((a,b)=>a-b)
    • }

math.sqrt(n) = n**.5

although that may lead unreadable code.

for python 3 replace long(...) with int(...)

Code
Diff
  • fib = lambda n: long((((1+5**.5)/2)**(n+1) - ((1-5**.5)/2)**(n+1))/5**.5)
    • import math
    • def fib(n):
    • return long((((1+math.sqrt(5))/2)**(n+1) - ((1-math.sqrt(5))/2)**(n+1))/math.sqrt(5))
    • fib = lambda n: long((((1+5**.5)/2)**(n+1) - ((1-5**.5)/2)**(n+1))/5**.5)
Arrays
Data Types
Map/Reduce
Algorithms
Logic
Code
Diff
  • const duplicates = a => a.reduce((r, v, i, l) => !r.includes(v) && l.indexOf(v) !== i ? [...r, v] : r, []);
    • function duplicates(arr) {
    • var out = [];
    • for(var x=0;x<arr.length-1;x++)
    • {
    • var ch = arr[x];
    • for(var y=x+1;y<arr.length;y++)
    • {
    • var comp = arr[y];
    • if (comp === ch && out.indexOf(comp) === -1)
    • {
    • out.push(comp);
    • break;
    • }
    • }
    • }
    • out.sort();
    • return out;
    • }
    • const duplicates = a => a.reduce((r, v, i, l) => !r.includes(v) && l.indexOf(v) !== i ? [...r, v] : r, []);
Code
Diff
  • const middleCharacter = s => s.slice(s.length/2 - !(s.length % 2), s.length/2+1);
    • var middleCharacter = (str) => str.slice(str.length/2 - !(str.length % 2), str.length/2+1);
    • const middleCharacter = s => s.slice(s.length/2 - !(s.length % 2), s.length/2+1);
Code
Diff
  • const convert = n => Number(n.toFixed(2));
    • const convert = (num) => {
    • return parseFloat(num.toFixed(2));
    • };
    • const convert = n => Number(n.toFixed(2));
Code
Diff
  • const greetLanguage = (a = "Hello", b = "Javascript") => `${a}, ${b}!`
    • let greetLanguage = (a = "Hello", b = "Javascript") => `${a}, ${b}!`
    • const greetLanguage = (a = "Hello", b = "Javascript") => `${a}, ${b}!`