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

Uses async library import in the preload

Code
Diff
  • Stream nums = new Stream.fromIterable([1,2,3,4,5]);
    
    • class Person {
    • String firstName;
    • String lastName;
    • Person(this.firstName,this.lastName);
    • }
    • Stream nums = new Stream.fromIterable([1,2,3,4,5]);

Random numbers in PHP

Not sure if I'm doing this right but anyway here's my take at random numbers in PHP.

lcg_value

Accepts no arguments and returns a random float between 0 (inclusive) and 1 (exclusive).

rand

Optionally accepts 2 arguments. If no arguments provided, returns a random integer from 0 to get_rand_max(). If both arguments provided, returns random integer between the stated minimum and maximum (both inclusive).

Code
Diff
  • echo "<DESCRIBE::>Random Numbers in PHP\n";
    echo "<IT::>Demonstrating lcg_value()\n";
    for ($i = 0; $i < 100; $i++) {
      echo "<PASSED::>" . lcg_value() . "\n";
    }
    echo "<COMPLETEDIN::>\n";
    echo "<IT::>Demonstrating rand() with no arguments\n";
    for ($i = 0; $i < 100; $i++) {
      echo "<PASSED::>" . rand() . "\n";
    }
    echo "<COMPLETEDIN::>\n";
    echo '<IT::>Demonstrating rand($min, $max) with $min = 0 and $max = 100 (like how the random_number/randomNumber method generates random integers in most Codewars testing frameworks)' . "\n";
    for ($i = 0; $i < 100; $i++) {
      echo "<PASSED::>" . rand(0, 100) . "\n";
    }
    echo "<COMPLETEDIN::>";
    echo "<COMPLETEDIN::>";
    • #include <algorithm>
    • #include <iterator>
    • #include <random>
    • #include <vector>
    • void run() {
    • using namespace std;
    • // this is a source of true random numbers
    • random_device rd;
    • // it's used to generate a seed for an engine
    • default_random_engine eng(rd());
    • // distribution is applied to a generator to generate random numbers
    • uniform_int_distribution<int> dist(1, 10);
    • vector<int> xs;
    • for (unsigned i = 0; i < 20; i++)
    • xs.push_back(dist(eng));
    • for (int x : xs)
    • cout << x << ' ';
    • cout << '\n';
    • echo "<DESCRIBE::>Random Numbers in PHP\n";
    • echo "<IT::>Demonstrating lcg_value()\n";
    • for ($i = 0; $i < 100; $i++) {
    • echo "<PASSED::>" . lcg_value() . "\n";
    • }
    • void run2() {
    • using namespace std;
    • default_random_engine eng{random_device()()};
    • uniform_int_distribution<int> dist(1, 10);
    • auto rand10 = [&eng, &dist]() { return dist(eng); };
    • vector<int> xs;
    • generate_n(back_inserter(xs), 20, rand10);
    • copy(cbegin(xs), cend(xs), ostream_iterator<int>(cout, " "));
    • cout << '\n';
    • echo "<COMPLETEDIN::>\n";
    • echo "<IT::>Demonstrating rand() with no arguments\n";
    • for ($i = 0; $i < 100; $i++) {
    • echo "<PASSED::>" . rand() . "\n";
    • }
    • echo "<COMPLETEDIN::>\n";
    • echo '<IT::>Demonstrating rand($min, $max) with $min = 0 and $max = 100 (like how the random_number/randomNumber method generates random integers in most Codewars testing frameworks)' . "\n";
    • for ($i = 0; $i < 100; $i++) {
    • echo "<PASSED::>" . rand(0, 100) . "\n";
    • }
    • echo "<COMPLETEDIN::>";
    • echo "<COMPLETEDIN::>";
Fundamentals
Code
Diff
  • (ns power.core)
    
    (defn power [n e]
      (condp = e
        0 1
        (* n (power n (dec e)))))
    • function power_of(int $x, int $y) {
    • return $x ** $y;
    • }
    • (ns power.core)
    • $test->describe("Basic Power Function", function () {
    • global $test;
    • $test->it("should always return when the power is 0", function () {
    • global $test;
    • $test->assert_equals(power_of(1, 0), 1);
    • $test->assert_equals(power_of(2, 0), 1);
    • $test->assert_equals(power_of(6, 0), 1);
    • $test->assert_equals(power_of(100, 0), 1);
    • $test->assert_equals(power_of(1000, 0), 1);
    • $test->assert_equals(power_of(99999, 0), 1);
    • $test->assert_equals(power_of(666, 0), 1);
    • $test->assert_equals(power_of(33, 0), 1);
    • $test->assert_equals(power_of(123456789, 0), 1);
    • $test->assert_equals(power_of(911, 0), 1);
    • });
    • $test->it("should work for other powers", function () {
    • global $test;
    • $test->assert_equals(power_of(1, 1), 1);
    • $test->assert_equals(power_of(4, 2), 16);
    • $test->assert_equals(power_of(2, 4), 16);
    • $test->assert_equals(power_of(5, 3), 125);
    • $test->assert_equals(power_of(2, 9), 512);
    • $test->assert_equals(power_of(7, 3), 343);
    • $test->assert_equals(power_of(6, 3), 216);
    • $test->assert_equals(power_of(3, 6), 729);
    • $test->assert_equals(power_of(9, 3), 729);
    • $test->assert_equals(power_of(10, 10), 10000000000);
    • $test->assert_equals(power_of(2, 53), 9007199254740992);
    • });
    • });
    • (defn power [n e]
    • (condp = e
    • 0 1
    • (* n (power n (dec e)))))

Taking a look at Igloo testing framework.

I think there must be some automatical header generation going on here...

Now we get error: 'auto' not allowed in function prototype. Gcc compiles this, clang doesn't. It's not standard C++ yet, but maybe C++17.

Code
Diff
  • auto add(const auto& x, const auto& y) {
      return x + y;
    }
    • template <class T, class U>
    • auto add(const T& x, const U& y) {
    • auto add(const auto& x, const auto& y) {
    • return x + y;
    • }
Fundamentals
Code
Diff
  • function greet_language($lang = "PHP") {
      print "Hello $lang!\n";
    }
    greet_language();
    greet_language("Javascript");
    greet_language("Ruby");
    • (ns hello.core)
    • (println "Hello Clojure")
    • function greet_language($lang = "PHP") {
    • print "Hello $lang!\n";
    • }
    • greet_language();
    • greet_language("Javascript");
    • greet_language("Ruby");
Code
Diff
  • #include <string>
    #include <stdio.h>
    
    int Main()
    {
      int arg1, arg2, add, sub, mul, quo, rem ;
    
        printf( "Enter two integer numbers : " );
        scanf( "%d%d", &arg1, &arg2 ); //no scanf provided hope to get memory garbage as number
    
        /* Perform Addition, Subtraction, Multiplication & Division */
        __asm__ ( "addl %%ebx, %%eax;" : "=a" (add) : "a" (arg1) , "b" (arg2) );
        __asm__ ( "subl %%ebx, %%eax;" : "=a" (sub) : "a" (arg1) , "b" (arg2) );
        __asm__ ( "imull %%ebx, %%eax;" : "=a" (mul) : "a" (arg1) , "b" (arg2) );
    
        __asm__ ( "movl $0x0, %%edx;"
                  "movl %2, %%eax;"
                  "movl %3, %%ebx;"
                   "idivl %%ebx;" : "=a" (quo), "=d" (rem) : "g" (arg1), "g" (arg2) );
    
        printf( "%d + %d = %d\n", arg1, arg2, add );
        printf( "%d - %d = %d\n", arg1, arg2, sub );
        printf( "%d * %d = %d\n", arg1, arg2, mul );
        printf( "%d / %d = %d\n", arg1, arg2, quo );
        printf( "%d %% %d = %d\n", arg1, arg2, rem );
    
        return 0 ;
    }
    • int main()
    • {
    • printf("%d\n", test());
    • }
    • #include <string>
    • #include <stdio.h>
    • int test()
    • int Main()
    • {
    • __asm__("movl $1337, %eax");
    • int arg1, arg2, add, sub, mul, quo, rem ;
    • printf( "Enter two integer numbers : " );
    • scanf( "%d%d", &arg1, &arg2 ); //no scanf provided hope to get memory garbage as number
    • /* Perform Addition, Subtraction, Multiplication & Division */
    • __asm__ ( "addl %%ebx, %%eax;" : "=a" (add) : "a" (arg1) , "b" (arg2) );
    • __asm__ ( "subl %%ebx, %%eax;" : "=a" (sub) : "a" (arg1) , "b" (arg2) );
    • __asm__ ( "imull %%ebx, %%eax;" : "=a" (mul) : "a" (arg1) , "b" (arg2) );
    • __asm__ ( "movl $0x0, %%edx;"
    • "movl %2, %%eax;"
    • "movl %3, %%ebx;"
    • "idivl %%ebx;" : "=a" (quo), "=d" (rem) : "g" (arg1), "g" (arg2) );
    • printf( "%d + %d = %d\n", arg1, arg2, add );
    • printf( "%d - %d = %d\n", arg1, arg2, sub );
    • printf( "%d * %d = %d\n", arg1, arg2, mul );
    • printf( "%d / %d = %d\n", arg1, arg2, quo );
    • printf( "%d %% %d = %d\n", arg1, arg2, rem );
    • return 0 ;
    • }
Code
Diff
  • (ns basics.core)
    
    (defn add [a b] (+ a b))
    (defn square [x] (* x x))
     
    (def result (-> (add 1 2) (add 3) (add 4) (square)))
    
    (println result)
    
    ;; It is better (without def), but it is more difficult to test:
    ; (let [result (-> (add 1 2)
    ;                  (add 3)
    ;                  (add 4)
    ;                  (square))]
    ;  (print result))
    
    ;; Shorter:
    ; (def add #(+ %1 %2))
    ; (def square #(* %1 %1))
    
    ;; Or maybe just:
    ; (def add +)
    • add = -> a, b { a + b }
    • square = -> x { x * x }
    • (ns basics.core)
    • # sigh, if only Ruby supported pipes!
    • result = square.(add.(add.(add.(1,2), 3), 4))
    • (defn add [a b] (+ a b))
    • (defn square [x] (* x x))
    • (def result (-> (add 1 2) (add 3) (add 4) (square)))
    • puts result
    • (println result)
    • ;; It is better (without def), but it is more difficult to test:
    • ; (let [result (-> (add 1 2)
    • ; (add 3)
    • ; (add 4)
    • ; (square))]
    • ; (print result))
    • ;; Shorter:
    • ; (def add #(+ %1 %2))
    • ; (def square #(* %1 %1))
    • ;; Or maybe just:
    • ; (def add +)