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

This implementation of fibonacci follows https://www.nayuki.io/page/fast-fibonacci-algorithms, with a base case that uses math.pow.

Code
Diff
  • 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
    
    • # Fibonacci #
    • from math import sqrt
    • c1 = (sqrt(5) - 1) / (2 * sqrt(5))
    • c2 = (sqrt(5) + 1) / (2 * sqrt(5))
    • def f(x):
    • fibo = c1 * ((1 - sqrt(5)) / 2)**x + c2 * ((1 + sqrt(5)) / 2)**x
    • return int(round(fibo))
    • 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

Updated to test that it is working with new rust changes

Code
Diff
  • pub fn powermod(n: u64, p: u64, m: u64) -> u64 {
    	if p == 0 { return 1 % m }
    	if p == 1 { return n % m }
    
    	let mut r = powermod(n, p / 2, m);
    
    	r = r * r % m;
    	if p & 1 == 1 {
    		r = r * n % m;
    	}
    	
    	r
    }
    • pub fn powermod(n: u64, p: u64, m: u64) -> u64 {
    • if p == 0 { return 1 % m }
    • if p == 1 { return n % m }
    • let mut r = powermod(n, p / 2, m);
    • r = r * r % m;
    • if p & 1 == 1 {
    • r = r * n % m;
    • }
    • r
    • }
    • #[test]
    • fn test_powermod() {
    • assert_eq!(powermod(2, 999999, 147), 50);
    • }
Code
Diff
  • defmodule Piapprox do
    
      def iter_pi(epsilon) do
        pi = :math.pi
        leibniz_stream
          |> Stream.with_index
          |> Enum.reduce_while(0, fn
            ({i, _}, acc) when abs(pi - acc) >= epsilon ->
              {:cont, acc + i}
            ({_, n}, acc) ->
              {:halt, [n, Float.round(acc, 10)]}
          end)
      end
    
      defp leibniz_stream do
        Stream.unfold(1, fn
          n when rem(n,4) == 1 -> { 4/n, n+2 }
          n                    -> {-4/n, n+2 }
        end)
      end
    end
    
    • defmodule Piapprox do
    • def iter_pi(epsilon) do
    • leibniz_stream |>
    • Enum.reduce_while(0, fn {i, n}, acc ->
    • if abs(:math.pi - acc) >= epsilon do
    • { :cont, acc + i }
    • else
    • { :halt, [n, Float.round(acc, 10)] }
    • end
    • end)
    • pi = :math.pi
    • leibniz_stream
    • |> Stream.with_index
    • |> Enum.reduce_while(0, fn
    • ({i, _}, acc) when abs(pi - acc) >= epsilon ->
    • {:cont, acc + i}
    • ({_, n}, acc) ->
    • {:halt, [n, Float.round(acc, 10)]}
    • end)
    • end
    • defp leibniz_stream do
    • Stream.unfold(1, fn
    • n when rem(n,4) == 1 -> { 4/n, n+2 }
    • n -> {-4/n, n+2 }
    • end) |> Stream.with_index
    • end)
    • end
    • end

Passed and failing tests with imports etc

Code
Diff
  • class Person {
      String firstName;
      String lastName;
      
      Person(this.firstName,this.lastName);
      
      String get fullName => '$firstName $lastName';
    }
    • doubler(n) => n*2;
    • class Person {
    • String firstName;
    • String lastName;
    • Person(this.firstName,this.lastName);
    • String get fullName => '$firstName $lastName';
    • }
Code
Diff
  • module InfiniteSeq where
    
    import Data.List
    
    ones = repeat 1
    nats = [0..]
    
    merge = concat . transpose
    merge2 x y = merge [x, y]
    
    ints = 0: merge2 [1..] [(-1),(-2)..]
    
    fibs = 1 : scanl (+) 1 fibs
    • module InfiniteSeq where
    • import Data.List
    • ones = repeat 1
    • nats = [0..]
    • merge = concat . transpose
    • merge2 x y = merge [x, y]
    • ints = 0: merge2 [1..] [(-1),(-2)..]
    • fibs = 1 : scanl (+) 1 fibs

(Functional version with no loops)

Code
Diff
  • Number.prototype.times = function(f) { 
      const n = this.valueOf()
      const g = (_, i) => f(i)
      return [...Array(n)].map(g) 
    }
    
    • Number.prototype.times = function (f) {
    • for (let i = 0; i < this; i++) f(i);
    • }
    • Number.prototype.times = function(f) {
    • const n = this.valueOf()
    • const g = (_, i) => f(i)
    • return [...Array(n)].map(g)
    • }
Code
Diff
  • let greetLanguage = (greeting = "Hello", language = "Javascript") => `${greeting}, ${language}!`;
    • function greetLanguage(greeting = "Hello", language = "Javascript") {
    • var result = `${greeting}, ${language}!`;
    • console.log(result);
    • return result;
    • }
    • let greetLanguage = (greeting = "Hello", language = "Javascript") => `${greeting}, ${language}!`;

What are all the ways to output a string in Ruby?

Code
Diff
  • # What are all the ways to output a string in Ruby?
    def hello_ruby
      greet = "Hello Ruby!"
    
      print           greet, "\n"
      puts            greet
      $stdout.write   greet + "\n"
      $stdout.puts    greet
      $stdout.print   greet, "\n"
      $stdout <<      greet + "\n"
      (greet+"\n").each_char {|c| print c}
    end
    • greet = "Hello Ruby!"
    • # What are all the ways to output a string in Ruby?
    • def hello_ruby
    • greet = "Hello Ruby!"
    • print greet, "
    • "
    • puts greet
    • $stdout.write greet +"
    • "
    • $stdout.puts greet
    • $stdout.print greet, "
    • "
    • (greet+"
    • ").each_char {|c| print c}
    • print greet, "
    • "
    • puts greet
    • $stdout.write greet + "
    • "
    • $stdout.puts greet
    • $stdout.print greet, "
    • "
    • $stdout << greet + "\n"
    • (greet+"
    • ").each_char {|c| print c}
    • end