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

Some additional robustness

Code
Diff
  • #include <stddef.h>
    
    const char* digit_to_text(unsigned digit)
    {
    	static const char* nums[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
      
    	if(digit > 9u)
    	{
    		return NULL;
    	}
      
    	return nums[digit];
    }
    • const char *nums[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    • #include <stddef.h>
    • const char* digit_to_text(int digit) {
    • return nums[digit];
    • const char* digit_to_text(unsigned digit)
    • {
    • static const char* nums[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    • if(digit > 9u)
    • {
    • return NULL;
    • }
    • return nums[digit];
    • }

Now in C!

Code
Diff
  • #include <stdint.h>
    #include <stdbool.h>
    
    #define MAX_ANGLE 180u
    
    typedef enum
    {
    	ACUTE,
    	RIGHT,
    	OBTUSE,
    } AngleType;
    
    typedef enum
    {
    	EQUILATERAL,
    	ISOSCELES,
    	SCALENE,
    } SideType;
    
    typedef struct
    {
    	union
    	{
    		struct
    		{
    			uint32_t a;
    			uint32_t b;
    			uint32_t c;
    		};
    		uint32_t angles[3];
    	};
    	
    } Triangle;
    
    bool TriangleIsValid(const Triangle* triangle);
    AngleType TriangleGetAngleType(const Triangle* triangle);
    SideType TriangleGetSideType(const Triangle* triangle);
    uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2);
    
    bool TriangleIsValid(const Triangle* triangle)
    {
        return triangle->a + triangle->b + triangle->c == 180u;
    }
    
    AngleType TriangleGetAngleType(const Triangle* triangle)
    {
    	for(unsigned i = 0u; i < 3u; i++)
    	{
    		if(triangle->angles[i] > 90u)
    		{
    			return OBTUSE;
    		}
    		if(triangle->angles[i] == 90u)
    		{
    			return RIGHT;
    		}
    	}
    	return ACUTE;
    }
    
    SideType TriangleGetSideType(const Triangle* triangle)
    {
    	uint32_t a = triangle->a;
    	uint32_t b = triangle->b;
    	uint32_t c = triangle->c;
    	
    	if(a == b && b == c)
    	{
    		return EQUILATERAL;
    	}
    	if(a == b || b == c || c == a)
    	{
    		return ISOSCELES;
    	}
    	return SCALENE;
    }
    
    uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2)
    {
    	return 180u - angle1 - angle2;
    }
    • use std::marker::PhantomData;
    • #include <stdint.h>
    • #include <stdbool.h>
    • #[derive(Debug, PartialEq, Eq)]
    • enum AngleType {
    • Acute,
    • Right,
    • Obtuse
    • }
    • #[derive(Debug, PartialEq, Eq)]
    • enum SideType {
    • Equilateral,
    • Isoceles,
    • Scalene
    • }
    • struct Complete;
    • struct Incomplete;
    • #define MAX_ANGLE 180u
    • struct Triangle<T> {
    • angles: [u32; 3],
    • phantom: PhantomData<T>
    • typedef enum
    • {
    • ACUTE,
    • RIGHT,
    • OBTUSE,
    • } AngleType;
    • typedef enum
    • {
    • EQUILATERAL,
    • ISOSCELES,
    • SCALENE,
    • } SideType;
    • typedef struct
    • {
    • union
    • {
    • struct
    • {
    • uint32_t a;
    • uint32_t b;
    • uint32_t c;
    • };
    • uint32_t angles[3];
    • };
    • } Triangle;
    • bool TriangleIsValid(const Triangle* triangle);
    • AngleType TriangleGetAngleType(const Triangle* triangle);
    • SideType TriangleGetSideType(const Triangle* triangle);
    • uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2);
    • bool TriangleIsValid(const Triangle* triangle)
    • {
    • return triangle->a + triangle->b + triangle->c == 180u;
    • }
    • impl<T> Triangle<T> {
    • fn angle_type(&self) -> AngleType {
    • for angle in self.angles {
    • if angle > 90 {
    • return AngleType::Obtuse;
    • }
    • if angle == 90 {
    • return AngleType::Right;
    • }
    • }
    • AngleType::Acute
    • }
    • fn side_type(&self) -> SideType {
    • let [a, b, c] = self.angles;
    • if a == b && b == c {
    • SideType::Equilateral
    • } else if a == b || b == c || c == a {
    • SideType::Isoceles
    • } else {
    • SideType::Scalene
    • }
    • }
    • AngleType TriangleGetAngleType(const Triangle* triangle)
    • {
    • for(unsigned i = 0u; i < 3u; i++)
    • {
    • if(triangle->angles[i] > 90u)
    • {
    • return OBTUSE;
    • }
    • if(triangle->angles[i] == 90u)
    • {
    • return RIGHT;
    • }
    • }
    • return ACUTE;
    • }
    • impl Triangle<Complete> {
    • fn new(a: u32, b: u32, c: u32) -> Self {
    • Self { angles: [a, b, c], phantom: PhantomData::<Complete> }
    • }
    • SideType TriangleGetSideType(const Triangle* triangle)
    • {
    • uint32_t a = triangle->a;
    • uint32_t b = triangle->b;
    • uint32_t c = triangle->c;
    • if(a == b && b == c)
    • {
    • return EQUILATERAL;
    • }
    • if(a == b || b == c || c == a)
    • {
    • return ISOSCELES;
    • }
    • return SCALENE;
    • }
    • impl Triangle<Incomplete> {
    • fn new(a: u32, b: u32) -> Self {
    • Self { angles: [a, b, 180 - a - b], phantom: PhantomData::<Incomplete> }
    • }
    • fn other_angle(&self) -> u32 {
    • self.angles[2]
    • }
    • uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2)
    • {
    • return 180u - angle1 - angle2;
    • }

'1' and '2' are technically of the same case + it seems more intuitive TBH

Code
Diff
  • using System;
    
    public static class Kata
    {
        public static int SameCase(char a, char b)
        {
            if (!char.IsLetter(a) && !char.IsLetter(b))
                  return 1;  
            if (!char.IsLetter(a) || !char.IsLetter(b))
                  return -1;
    
            return (char.IsUpper(a) == char.IsUpper(b)) ? 1 : 0;
        }
    }
    • using System;
    • public static class Kata
    • {
    • public static int SameCase(char a, char b)
    • {
    • if (!char.IsLetter(a) && !char.IsLetter(b))
    • return 1;
    • if (!char.IsLetter(a) || !char.IsLetter(b))
    • return -1;
    • return -1;
    • return (char.IsUpper(a) == char.IsUpper(b)) ? 1 : 0;
    • }
    • }

made it fit on one line :)

Code
Diff
  • max_sequence = lambda arr :max([sum(arr)] + [sum(arr[j:j+i]) for i in range(len(arr)) for j in range(len(arr) - i + 1)])
    • def max_sequence(arr):
    • # Code to find maximum sum of subarray
    • return max(
    • [sum(arr)] +
    • [sum(arr[j:j+i]) for i in range(len(arr)) for j in range(len(arr) - i + 1)]
    • )
    • max_sequence = lambda arr :max([sum(arr)] + [sum(arr[j:j+i]) for i in range(len(arr)) for j in range(len(arr) - i + 1)])
Code
Diff
  • function sum(a,b) {
      return a+b; // wrong returning
    }
    • function sum(a,b) {
    • return 1; // wrong returning
    • return a+b; // wrong returning
    • }