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

Not sure whether it fits the theme - not using the phi constant.

The algorithm is an implementation of some 3kyu kata and is described there: https://mitpress.mit.edu/sicp/chapter1/node15.html The transform must be found by you yourself, as the p' and q' are not given. I did this by observing how individual transforms behave and concluding.

Code
Diff
  • def fib(n):
        if n<0:
            if n%2: return fib(-n)
            else: return -fib(-n)
        def transform(a,b,p,q,n):
            if n==0: return a
            if n%2==0:
                return transform(a,b,p*p+q*q,q*q+2*p*q,n/2)
            else:
                return transform(a*q+a*p+b*q,b*p+a*q,p,q,n-1)
        return transform(1,0,0,1,n)
    • from math import sqrt
    • def fib(x):
    • if x < 70:
    • a = 0.7236067977499789 # (sqrt(5) + 1) / (2 * sqrt(5))
    • b = 1.618033988749895 # (1 + sqrt(5)) / 2
    • return round(a * b**x)
    • elif x % 2 == 0:
    • a = fib(x / 2)
    • b = fib(x / 2 + 1)
    • return a * (2 * fib(b) - a)
    • else:
    • # x % 2 == 1, by elimination
    • a = fib((x - 1) / 2)
    • b = fib((x + 1) / 2)
    • return a * a + b * b
    • def fib(n):
    • if n<0:
    • if n%2: return fib(-n)
    • else: return -fib(-n)
    • def transform(a,b,p,q,n):
    • if n==0: return a
    • if n%2==0:
    • return transform(a,b,p*p+q*q,q*q+2*p*q,n/2)
    • else:
    • return transform(a*q+a*p+b*q,b*p+a*q,p,q,n-1)
    • return transform(1,0,0,1,n)
Stacks
Arrays
Data Types
Code
Diff
  • import java.util.*;
    
    public class MatchingBrackets {
      public static final class Brace {
          
            static final Map<Character, Character> enclosing = new HashMap<>();
            static {
              enclosing.put(')','(');
              enclosing.put('}','{');
              enclosing.put(']','[');
            }
            
            private Character braceChar;
            
            public Brace(Character braceChar) {
              this.braceChar = braceChar;
            }
          
            public boolean isEnclosingTo(Brace brace) {
              if (brace == null) {
                return false;
              }
              return brace.braceChar.equals(enclosing.get(this.braceChar));
            }
      }
      
      public static final class BraceStack {
          private Stack<Brace> stack = new Stack<>();
          
          public Brace getLastBraceOnStack() {
            if (stack.isEmpty()) {
              return null;
            }
            return stack.peek();
          }
          
          public Brace pop() {
            return stack.pop();
          }
          
          public void push(Brace brace) {
            stack.push(brace);
          }
          
          public boolean isEmpty() {
            return stack.isEmpty();
          }
      }
      public static boolean isBalanced(String braces) {
            BraceStack braceStack = new BraceStack();
            for (char nextChar : braces.toCharArray()) {
                final Brace nextBrace = new Brace(nextChar);
                
                if (nextBrace.isEnclosingTo(braceStack.getLastBraceOnStack())) {
                  braceStack.pop();
                } else {
                  braceStack.push(nextBrace);
                }
    
            }
            return braceStack.isEmpty();
        }
    }
    • import java.util.*;
    • public class MatchingBrackets {
    • public static final class Brace {
    • static final Map<Character, Character> enclosing = new HashMap<>();
    • static {
    • enclosing.put(')','(');
    • enclosing.put('}','{');
    • enclosing.put(']','[');
    • }
    • private Character braceChar;
    • public Brace(Character braceChar) {
    • this.braceChar = braceChar;
    • }
    • public boolean isEnclosingTo(Brace brace) {
    • if (brace == null) {
    • return false;
    • }
    • return brace.braceChar.equals(enclosing.get(this.braceChar));
    • }
    • }
    • public static boolean isBalanced(String braces) {
    • Stack<Brace> stack = new Stack<>();
    • public static final class BraceStack {
    • private Stack<Brace> stack = new Stack<>();
    • public Brace getLastBraceOnStack() {
    • if (stack.isEmpty()) {
    • return null;
    • }
    • return stack.peek();
    • }
    • public Brace pop() {
    • return stack.pop();
    • }
    • public void push(Brace brace) {
    • stack.push(brace);
    • }
    • public boolean isEmpty() {
    • return stack.isEmpty();
    • }
    • }
    • public static boolean isBalanced(String braces) {
    • BraceStack braceStack = new BraceStack();
    • for (char nextChar : braces.toCharArray()) {
    • final Brace nextBrace = new Brace(nextChar);
    • if (!stack.isEmpty()) {
    • Brace lastBraceOnStack = stack.peek();
    • if (nextBrace.isEnclosingTo(lastBraceOnStack)) {
    • stack.pop();
    • continue;
    • }
    • if (nextBrace.isEnclosingTo(braceStack.getLastBraceOnStack())) {
    • braceStack.pop();
    • } else {
    • braceStack.push(nextBrace);
    • }
    • stack.push(nextBrace);
    • }
    • return stack.isEmpty();
    • return braceStack.isEmpty();
    • }
    • }
wichuvs.wichuFailed Tests

F# Basics

Code
Diff
  • let add a b = a + b
    
    let square x = x * x
    
    let result = add 1 2 |> add 3 |> add 4 |> square
    
    printfn "%i" result
    • let add a b = a + b
    • let square x = x * x
    • let result = add 1 2 |> add 3 |> add 4 |> square
    • printfn result
    • printfn "%i" result
Code
Diff
  • import java.util.*;;
    
    class Solution {
      public static int peakIndex(int[] nums) {
        if (nums.length == 1) return 0;
        if (nums.length == 2) return nums[0] > nums[1] ? 0 : 1;
        
        boolean ascending = false;
        
        for (int i = 0; i < nums.length - 1; ++i) {
          if (nums[i] < nums[i + 1]) {
            ascending = true;
          } else {
            if (ascending && nums[i] > nums[i + 1]) {
              return i;
            }
            ascending = false;
          }
        }
        
        if (ascending) {
          return nums.length - 1;
        } else {
          return -1;
        }
      }
    }
    • import java.util.*;;
    • class Solution {
    • public static int peakIndex(int[] nums) {
    • int left = 0;
    • int right = nums.length - 1;
    • int peak = -1;
    • if (nums.length == 1) return 0;
    • if (nums.length == 2) return nums[0] > nums[1] ? 0 : 1;
    • while(left <= right) {
    • if(left == right) {
    • peak = left;
    • break;
    • }
    • int mid = (left + right) / 2;
    • if(nums[mid] < nums[mid + 1]) {
    • left = mid + 1;
    • boolean ascending = false;
    • for (int i = 0; i < nums.length - 1; ++i) {
    • if (nums[i] < nums[i + 1]) {
    • ascending = true;
    • } else {
    • right = mid;
    • if (ascending && nums[i] > nums[i + 1]) {
    • return i;
    • }
    • ascending = false;
    • }
    • }
    • return peak;
    • if (ascending) {
    • return nums.length - 1;
    • } else {
    • return -1;
    • }
    • }
    • }
Arrays
Data Types
Code
Diff
  • function remove(arr, n) {
      if (!arr[n]) return arr;
      arr.splice(n, 1);
      return arr;
    }
    • function remove(arr, n) {
    • const indexOutOfRange = n < 0 || n >= arr.length;
    • if (indexOutOfRange) return arr;
    • if (!arr[n]) return arr;
    • arr.splice(n, 1);
    • return arr;
    • }