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
Algorithms
Logic
Fundamentals

Fixed version of original, taking divisibility into account

(even though rowcased's solution is probably better)

(I'll take a victory on readability though!)

Code
Diff
  • def validateKey(key):
    
      segments = key.split('-')
      if len(segments) != 2: return False
      
      end_ints = list(map(lambda x: int(x), segments[1]))
      
      illegalSites = ["333", "444", "555", "666", "777", "888", "999"]
      illegalEnds = ["0", "8", "9"]
      
      return segments[0].isnumeric() and segments[1].isnumeric() and len(segments[0]) == 3 and len(segments[1]) == 7 and not segments[0] in illegalSites and not segments[1][-1:] in illegalEnds and sum(end_ints) % 7 == 0
    
    • def validateKey(key):
    • segments = key.split('-')
    • if len(segments) != 2: return False
    • end_ints = list(map(lambda x: int(x), segments[1]))
    • illegalSites = ["333", "444", "555", "666", "777", "888", "999"]
    • illegalEnds = ["0", "8", "9"]
    • return len(segments) == 2 and len(segments[0]) == 3 and len(segments[1]) == 7 and not segments[0] in illegalSites and not segments[1][-1:] in illegalEnds
    • return segments[0].isnumeric() and segments[1].isnumeric() and len(segments[0]) == 3 and len(segments[1]) == 7 and not segments[0] in illegalSites and not segments[1][-1:] in illegalEnds and sum(end_ints) % 7 == 0

Faster version using the closed form of the Fibonacci sequence given by Binet's formula

This version is prone to round-off error for large enough n, but it gets the right answer up to the 32-bit signed integer cut-off.

Code
Diff
  • public class NthFib {
      public static final double phi = (1 + Math.sqrt(5))/2.0;
      public static final double psi = (1 - Math.sqrt(5))/2.0;
      
      public static int fib(int n) {
        return (int)((Math.pow(phi, n) - Math.pow(psi, n))/Math.sqrt(5));
      }
    }
    
    • class NthFib {
    • static int fib(int n) {
    • return n > 1 ? fib(--n) + fib(--n) : n;
    • public class NthFib {
    • public static final double phi = (1 + Math.sqrt(5))/2.0;
    • public static final double psi = (1 - Math.sqrt(5))/2.0;
    • public static int fib(int n) {
    • return (int)((Math.pow(phi, n) - Math.pow(psi, n))/Math.sqrt(5));
    • }
    • }

Although just using the variance function works, I feel like this is more in the spirit of what this was supposed to be.

Code
Diff
  • x <- c(52,73,55,26,72,45,80,62,NA,7,NA,87,54,46,85,37,94)
    
    #x = x is true for everything except NA
    edited <- x[which(x == x)]
    
    mean <- sum(edited)/length(edited)
    normal <- edited - mean
    
    squares <- normal * normal
    sum_of_squares = sum(squares)
    
    var <- sum_of_squares/(length(edited) - 1)
    print(var)
    • c(52,73,55,26,72,45,80,62,NA,7,NA,87,54,46,85,37,94)
    • x <- c(52,73,55,26,72,45,80,62,NA,7,NA,87,54,46,85,37,94)
    • #x = x is true for everything except NA
    • edited <- x[which(x == x)]
    • mean <- sum(edited)/length(edited)
    • normal <- edited - mean
    • squares <- normal * normal
    • sum_of_squares = sum(squares)
    • var <- sum_of_squares/(length(edited) - 1)
    • print(var)
Performance

I can shorten the time to a bit more than 1/4 of the given time, by getting the complement of a divisor. If the divisor exists in the list already, we can break.

However, the list must be sorted, which will affect the time complexity.

The algorithm also suffers when the number is prime; I cannot find a faster way.

By doing this, the algorithm can run 200 (example solutions ran and user solution) times in 12 seconds, in the number range of 1e6 to 2e6.

Code
Diff
  • def divisors(n):
        res=[]
        for i in range(1,int(n*0.5)+1):
            if i in res: break
            if n % i == 0: 
                res.append(i)
                res.append(int(n / i))
        
        return sorted(list(set(res)))
    • def divisors(number):
    • dividers = [number]
    • num = int(number * 0.5)
    • for i in range(1,num+1):
    • if(number % i == 0):
    • dividers.append(i)
    • dividers.sort()
    • return dividers
    • def divisors(n):
    • res=[]
    • for i in range(1,int(n*0.5)+1):
    • if i in res: break
    • if n % i == 0:
    • res.append(i)
    • res.append(int(n / i))
    • return sorted(list(set(res)))
Code
Diff
  • numberplatereader=lambda*p:__import__('re').match("[A-Z]{2}[0-9]{2}\s[A-Z]{3}",*p)!=None
    • def numberplatereader(plate):
    • import re
    • if re.match("[A-Z]{2}[0-9]{2}\s[A-Z]{3}", plate) != None:
    • return True
    • else:
    • return False
    • numberplatereader=lambda*p:__import__('re').match("[A-Z]{2}[0-9]{2}\s[A-Z]{3}",*p)!=None
Code
Diff
  • def max(list):
        list.sort()
        return list[-1]
    • #
    • def max(list):
    • list.sort()
    • return list[-1]
Fundamentals
Code
Diff
  • def find_special(jar, special):
        if not isinstance(jar, list) or special not in jar:
            return
        return [special, *filter(special.__ne__, jar)]
    • def find_special(jar, special):
    • if not isinstance(jar, list) or not isinstance(special, (int, float)) or special not in jar:
    • return None
    • jar = jar[:]
    • jar.remove(special)
    • jar.insert(0, special)
    • return jar
    • if not isinstance(jar, list) or special not in jar:
    • return
    • return [special, *filter(special.__ne__, jar)]