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 function claculates the area of a Triangle;

float AreaOfTriangle(float w, float h) {
    float A = (w * h) / 2;
    return A;
}
USING: kernel combinators locals formatting lint literals
       sequences assocs strings arrays
       math math.functions math.order ;
IN: rosetta-code.bt
CONSTANT: addlookup {
  { 0 CHAR: 0 }
  { 1 CHAR: + }
  { -1 CHAR: - }
}

<PRIVATE

: bt-add-digits ( a b c -- d e ) 
  + + 3 +
  { { 0 -1 } { 1 -1 } { -1 0 } { 0 0 } { 1 0 } { -1 1 } { 0 1 } } 
  nth first2
;

PRIVATE>

! Conversion
: bt>integer ( seq -- x ) 0 [ swap 3 * + ] reduce ;
: integer>bt ( x -- x ) [ dup zero? not ] [
    dup 3 rem {
      { 0 [ 3 / 0 ] }
      { 1 [ 3 / round 1 ] }
      { 2 [ 1 + 3 / round -1 ] }
    } case
  ] produce nip reverse
;
: bt>string ( seq -- str ) [ addlookup at ] map >string ;
: string>bt ( str -- seq ) [ addlookup value-at ] { } map-as ;

! Arithmetic
: bt-neg ( a -- -a ) [ neg ] map ;
:: bt-add ( u v -- w ) 
  u v max-length :> maxl
  u v [ maxl 0 pad-head reverse ] bi@ :> ( u v )
  0 :> carry!
  u v { } [ carry bt-add-digits carry! prefix ] 2reduce
  carry prefix [ zero? ] trim-head
;
: bt-sub ( u v -- w ) bt-neg bt-add ;
:: bt-mul ( u v -- w ) u { } [
    {
      { -1 [ v bt-neg ] }
      { 0  [ { } ] }
      { 1  [ v ] }
    } case bt-add 0 suffix
  ] reduce
  1 head*
;

[let
  "+-0++0+" string>bt :> a
  -436 integer>bt     :> b
  "+-++-" string>bt   :> c
  b c bt-sub a bt-mul :> d
  "a" a bt>integer a bt>string "%s: %d, %s\n" printf
  "b" b bt>integer b bt>string "%s: %d, %s\n" printf
  "c" c bt>integer c bt>string "%s: %d, %s\n" printf
  "a*(b-c)" d bt>integer d bt>string "%s: %d, %s\n" printf
]
Strings

If you were to run this function through a terminal (not all terminals support ANSI escape code colors, try downloading the new windows terminal or a different terminal of your preference if it does not work) the text would be a different color.

ColorAdder.AddColor(color in string, colored text)

Info:
https://en.wikipedia.org/wiki/ANSI_escape_code
List of escape codes: https://gist.github.com/Prakasaka/219fe5695beeb4d6311583e79933a009

I don't know C# that well so if you could fix the warnings for me that would be awesome.

Feel free to translate and develop on the concept.

public class ColorAdder
{
    public static string AddColor(string color, string text)
    {
      string Reset = "\033[0m";
      string Red = "\033[31m";
      string Green = "\033[32m";
      string Yellow = "\033[33m";
      string Blue = "\033[34m";
      string Magenta = "\033[35m";
      string Cyan = "\033[36m";
      string White = "\033[37m";

      switch(color) {
        case "Red":
          return string.Concat(string.Concat(Red, text), Reset);
          break;
        case "Green":
          return string.Concat(string.Concat(Green, text), Reset);
          break;
        case "Yellow":
          return string.Concat(string.Concat(Yellow, text), Reset);
          break;
        case "Blue":
          return string.Concat(string.Concat(Blue, text), Reset);
          break;
        case "Magenta":
          return string.Concat(string.Concat(Magenta, text), Reset);
          break;
        case "Cyan":
          return string.Concat(string.Concat(Cyan, text), Reset);
          break;
        case "White":
          return string.Concat(string.Concat(White, text), Reset);
          break;
        default:
          return "Invalid Color";
          break;
      }
    }
}
function sudoku(puzzle) {
  while (puzzle.some( l => l.some( p => p===0 ) ))  
    for (let y=0; y<9; y++)                         
      for (let x=0; x<9; x++)
        if (puzzle[y][x]===0) {                  
          let p = [true,true,true,true,true,      
                   true,true,true,true,true];
          for (let i = 0; i<9; i++) {              
            p[puzzle[y][i]] = false;             
            p[puzzle[i][x]] = false;              
          }
          for (let i=3*~~(x/3); i<3*(~~(x/3)+1); i++)   
            for (let j=3*~~(y/3); j<3*(~~(y/3)+1); j++) 
              p[puzzle[j][i]] = false;                 
          if (p.reduce( (p,c) => p+(c?1:0) ) === 1) 
            puzzle[y][x] = p.indexOf(true);         
        }
  return puzzle;
}

ABRACADABRA is a magical word of unknown origins often used by stage magicians. Several folk etymologies associate the word from phrases in Hebrew that mean:

"I will create as I speak."

def magical_incantation() -> str:
    """
    Prints the word ABRACADABRA inside a magick triangle, and
    returns the word as a string.
    """
    magick_runes = '>=-><>;>=->'
    alchemy = [chr(int(bin(int(rune, 2) ^ (2 ** (len(rune) + 1) - 1))[3:], 2)) for rune in
               ['0' + bin(ord(i))[2:] for i in magick_runes]]
    the_great_void = ""
    for i in range(len(alchemy), 0, -1):
        print(the_great_void, end="")
        for spell in alchemy[0:i]:
            print(spell, end=' ')
        print()
        the_great_void += " "

    return ''.join(alchemy)

Implement a function named count_digits that takes an integer n as a parameter. This function should return the number of digits of n without using the len() function ~ Good luck!

from math import log10


count_digits = lambda n: int(log10(n)) + 1

Overview

"99 Bottles of Beer" is a sea chantey dating to the mid-20th century. It is a traditional reverse counting song popular in both the United States and Canada. Review the Test Case if you are unfamilar with the song.

Task

  • Minimize the code....
def beer_song_verse() -> str:
    """Returns 99 Bottles of Beer of song."""
    b, m = 'bottles of beer on the wall.', 'no more'  
    r = ''
    for i in range(99, -1, -1):
        if i == 0:
            r += f'No more {b[:-1]}, {m} {b[:15]}\nGo to the store and buy some more, 99 {b}\n'
        else:
            r += f'{i} {(f"{b[:6]}", f"{b[:7]}")[i > 1]} {b[:15]}, {i} {b[:15]}.\nTake it down and pass it around, {i - 1} {b}\n\n'
    return r

A handful of utility words for specialised testing. Not often needed, but occasionally very helpful.

USING: tools.testest kernel io continuations sequences locals io namespaces ;
IN: tools.testest.extras

! Simple assertions, for when stack comparisons don't make sense
: pass-test ( -- ) "<PASSED::>" write test-passed. get call( -- ) nl ;
: fail-test ( msg -- ) "<FAILED::>" prepend write nl ;

! Testing exception behaviours.
! Could be split into two words, one with a specific error, one without
:: expect-error ( quot ?error -- )
  [ quot call( -- ) "Test Failed: Expected Error to be thrown" fail-test ]
  [ :> got-err
    ?error [
      <{ got-err -> ?error }>
    ] [ pass-test ] if
  ] recover ;
USING: math ;
FROM: example.preloaded => ERROR: error <{ ;
IN: example

ERROR: bad msg ;
C: <bad> bad

: solve ( -- bad ) "Exception" <bad> ;
: solve* ( -- * ) "!Exception" bad ;
: solve/0* ( -- * ) 1 0 / "!Exception" bad ;
struct Test {
    const int t = 42;
};

template <class T>
struct TestTemplateThis : T {
    int get_t() const { return this->t; }
};

template <class T>
struct TestTemplateNoThis : T {
    int get_t() const { return t; }
};