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

Rewrote in Rust using match and enum language features. I find it more elegant, save that floating-point values can't be coerced from integer literals and must be explicitly written as a floating-point literal.

Sorting when there are two roots was not strictly necessary, but made test cases easier to grok.

Code
Diff
  • use std::cmp::Ordering;
    
    #[derive(Debug, PartialEq)]
    enum Roots {
        None,
        One(f64),
        Two(f64, f64)
    }
    
    fn quadratic_formula(a: f64, b: f64, c: f64) -> Roots {
        let delta = b * b - 4.0 * a * c;
        match delta.partial_cmp(&0.0).unwrap() {
            Ordering::Less => Roots::None,
            Ordering::Equal => Roots::One(-b / (2.0 * a)),
            Ordering::Greater => {
                let s1 = (-b + delta.sqrt()) / (2.0 * a);
                let s2 = (-b - delta.sqrt()) / (2.0 * a);
                Roots::Two(s1.min(s2), s1.max(s2))
            }
        }
    }
    • //IamQuan :3
    • use std::cmp::Ordering;
    • //Libraly
    • #include<iostream>
    • #include <math.h>
    • using namespace std;
    • //Code
    • //Ham Phu de goi an cho a b c
    • double GoiAn()
    • {
    • double ABC{};
    • std::cin >> ABC;
    • return ABC;
    • #[derive(Debug, PartialEq)]
    • enum Roots {
    • None,
    • One(f64),
    • Two(f64, f64)
    • }
    • //Code
    • int main()
    • {
    • //Khai bao cho DK
    • int A{};
    • //Khai bao cho tinh toan
    • double x1, x2;
    • //--------------------------------------------------
    • // User nhap a b c
    • cout << "Chuong trinh tinh phuong trinh bac 2 mot an\n";
    • cout << "Vui long nhap cac he so a , b , c\n";
    • cout << "---------------------------------------------" << endl;
    • //Nhap he so a
    • cout << "Nhap he so a: " ;
    • double a{GoiAn()};
    • //Dieu kien
    • while (a == 0 && A < 1000)
    • {
    • cout << "Phuong trinh da chuyen ve \nphuong trinh bac nhat mot an" << endl;
    • cout << "Ban co muon nhap lai ham so a ?\nNeu muon thi nhap lai.\na:" << endl;
    • cin >> a;
    • A++;
    • }
    • //Nhap he so b
    • cout << "Nhap he so b: ";
    • double b{ GoiAn() };
    • //Nhap he so c
    • cout << "Nhap he so c: ";
    • double c{ GoiAn() };
    • // Thong bao cho user
    • cout << "Dang tinh delta va nghiem ......." << endl;
    • //Khai bao Delta
    • double delta{ (b * b) - 4 * a * c };
    • //Tinh delta
    • if (delta < 0)
    • {
    • cout << "Delta = " << delta << "\n";
    • cout << " => Phuong trinh vo nghiem " << endl;
    • }
    • else if (delta == 0)
    • {
    • cout << "Delta = " << delta << "\n";
    • cout << " => Phuong trinh co nghiem kep " << endl;
    • }
    • else if (delta > 0)
    • {
    • cout << "Delta = " << delta << "\n";
    • cout << " => Phuong trinh co 2 nghiem phan biet " << endl;
    • }
    • cout << "\n\n";
    • //Tinh nghiem
    • if (delta > 0)
    • {
    • x1 = (-b - sqrt(delta)) / (2 * a);
    • x2 = (-b + sqrt(delta)) / (2 * a);
    • cout << "Nghiem cua phuong trinh la: \n";
    • cout << "x1 = " << x1 << endl;
    • cout << "x2 = " << x2 << endl;
    • }
    • else if (delta == 0) {
    • x1 = x2 = -b / (2 * a);
    • cout << "Nghiem cua phuong trinh la: \n";
    • cout << "x1 = x2 = " << x1 << endl;
    • }
    • else
    • {
    • cout << "Vi phuong trinh vo nghiem nen khong co nghiem" << endl;
    • }
    • system("pause");
    • return 0;
    • fn quadratic_formula(a: f64, b: f64, c: f64) -> Roots {
    • let delta = b * b - 4.0 * a * c;
    • match delta.partial_cmp(&0.0).unwrap() {
    • Ordering::Less => Roots::None,
    • Ordering::Equal => Roots::One(-b / (2.0 * a)),
    • Ordering::Greater => {
    • let s1 = (-b + delta.sqrt()) / (2.0 * a);
    • let s2 = (-b - delta.sqrt()) / (2.0 * a);
    • Roots::Two(s1.min(s2), s1.max(s2))
    • }
    • }
    • }
Code
Diff
  • public static class Kata
    {
    		public static int SameCase(char a, char b) {
    			bool areBothCharsLetter = char.IsLetter(a) && char.IsLetter(b);
    			bool areSameLetters = char.IsUpper(a) == char.IsUpper(b);
    
    			if (areBothCharsLetter)
    			{
    				if (areSameLetters)
    				{
    					return 1;
    				}
    				else
    				{
    					return 0;
    				}
    			}
    
    			return -1;
    		}
    }
    • public static class Kata
    • {
    • public static int SameCase(char a, char b) =>
    • char.IsLetter(a) && char.IsLetter(b) ?
    • (char.IsUpper(a) == char.IsUpper(b) ? 1 : 0)
    • : -1;
    • public static int SameCase(char a, char b) {
    • bool areBothCharsLetter = char.IsLetter(a) && char.IsLetter(b);
    • bool areSameLetters = char.IsUpper(a) == char.IsUpper(b);
    • if (areBothCharsLetter)
    • {
    • if (areSameLetters)
    • {
    • return 1;
    • }
    • else
    • {
    • return 0;
    • }
    • }
    • return -1;
    • }
    • }

Translation to Python??

Code
Diff
  • def other_angle(a, b):
        req_angle = 180 - (a + b)
        return req_angle
    
    
    • fn other_angle(a: u32, b: u32) -> u32 {
    • 180 - (a + b)
    • }
    • def other_angle(a, b):
    • req_angle = 180 - (a + b)
    • return req_angle
Mathematics
Regular Expressions
Parsing
Code
Diff
  • fn parse_quaternion(quaternion: &str) -> [f64; 4] {
        let mut iter = quaternion.chars().filter(|&c| c != ' ').peekable();
        let mut terms = Vec::new();
        while let Some(first) = iter.next() {
            let mut term = first.to_string();
            while iter.peek().map_or(false, |&c| !"+-".contains(c)) {
                term.push(iter.next().unwrap());
            }
            terms.push(term);
        }
        let mut real = 0.0;
        let mut imaginary = 0.0;
        let mut jmaginary = 0.0;
        let mut kmaginary = 0.0;
        for term in terms {
            match term.chars().last().unwrap() {
                'i' => imaginary += parse_term(&term),
                'j' => jmaginary += parse_term(&term),
                'k' => kmaginary += parse_term(&term),
                _ => real += parse_term(&term)
            }
        }
        [real, imaginary, jmaginary, kmaginary]
    }
    
    fn parse_term(mut term: &str) -> f64 {
        if term.chars().last().map_or(false, |c| c.is_ascii_alphabetic()) {
            term = &term[..term.len()-1];
            if !term.chars().any(|c| c.is_ascii_digit()) {
                return format!("{term}1").parse().unwrap();
            }
        }
        term.parse().unwrap()
    }
    • #include <array>
    • #include <string>
    • #include <regex>
    • #include <algorithm>
    • std::array<float, 4> ParseQuaternion(const std::string& t_quaternion)
    • {
    • std::smatch matches;
    • std::regex realCompPattern("(-?)([0-9.]+)*[^ijk]([+]|-|$)");
    • std::regex_search(t_quaternion, matches, realCompPattern);
    • float w = matches.empty() ? 0 : std::stof(matches[0]);
    • auto g = [&](const std::string& t_letter) -> float {
    • const std::regex pattern(R"((-?)([ (0-9).]+)?)" + t_letter);
    • std::string result;
    • if (std::regex_search(t_quaternion, matches, pattern))
    • {
    • if (matches[2].matched && matches[2] != " ")
    • {
    • auto matchStr = matches[2].str();
    • matchStr.erase(std::remove(matchStr.begin(), matchStr.end(), ' '), matchStr.end());
    • result = matches[1].str() + matchStr;
    • }
    • else
    • {
    • result = matches[1].str() + "1";
    • }
    • fn parse_quaternion(quaternion: &str) -> [f64; 4] {
    • let mut iter = quaternion.chars().filter(|&c| c != ' ').peekable();
    • let mut terms = Vec::new();
    • while let Some(first) = iter.next() {
    • let mut term = first.to_string();
    • while iter.peek().map_or(false, |&c| !"+-".contains(c)) {
    • term.push(iter.next().unwrap());
    • }
    • else
    • {
    • result = "0";
    • terms.push(term);
    • }
    • let mut real = 0.0;
    • let mut imaginary = 0.0;
    • let mut jmaginary = 0.0;
    • let mut kmaginary = 0.0;
    • for term in terms {
    • match term.chars().last().unwrap() {
    • 'i' => imaginary += parse_term(&term),
    • 'j' => jmaginary += parse_term(&term),
    • 'k' => kmaginary += parse_term(&term),
    • _ => real += parse_term(&term)
    • }
    • return std::stof(result);
    • };
    • }
    • [real, imaginary, jmaginary, kmaginary]
    • }
    • auto i = g("i");
    • auto j = g("j");
    • auto k = g("k");
    • return { w, i, j, k };
    • fn parse_term(mut term: &str) -> f64 {
    • if term.chars().last().map_or(false, |c| c.is_ascii_alphabetic()) {
    • term = &term[..term.len()-1];
    • if !term.chars().any(|c| c.is_ascii_digit()) {
    • return format!("{term}1").parse().unwrap();
    • }
    • }
    • term.parse().unwrap()
    • }
Code
Diff
  • def max_sequence(arr):
        # Code to find maximum sum of subarray
    	l_a = len(arr)
    	sum_b = sum(arr)
    	
    	for i in range(l_a):
    		for k in range(l_a-i+1):
    			sum_c = sum(arr[k:k+i])
    			if sum_c > sum_b: sum_b = sum_c
                    
    	return(sum_b)
    
    • def max_sequence(arr):
    • # Code to find maximum sum of subarray
    • l_a = len(arr)
    • counter = 0
    • for m in arr:
    • if m < 0:
    • counter = counter
    • else:
    • counter += 1
    • if counter == 0:
    • return(0)
    • sum_b = sum(arr)
    • for i in range(l_a):
    • if i == 0:
    • sum_b = sum(arr)
    • else:
    • for k in range(l_a-i+1):
    • sum_c = sum(arr[k:k+i])
    • if sum_c > sum_b: sum_b = sum_c
    • for k in range(l_a-i+1):
    • sum_c = sum(arr[k:k+i])
    • if sum_c > sum_b: sum_b = sum_c
    • return(sum_b)
Fundamentals
Strings
Code
Diff
  • reverse_string = lambda string: string[::-1]
    • def reverse_string(string): return string[::-1]
    • reverse_string = lambda string: string[::-1]
Code
Diff
  • #include <string>
    #include <numeric>
    #include <algorithm>
    #include <string_view>
    
    class StringComparer {
    public:
        static inline auto verifySum(std::string_view w1, std::string_view w2) -> bool {
            return sumOfCharacters(w1) == sumOfCharacters(w2);
        }
      
        static inline auto sumOfCharacters(std::string_view word) -> int {
            return std::accumulate(std::begin(word), std::end(word), 0, [](int sum,  char ch) {
                return sum + ch;
            });
        }
    };
    • #include <string>
    • #include <numeric>
    • #include <algorithm>
    • #include <string_view>
    • class StringComparer {
    • public:
    • static inline auto verifySum(std::string_view w1, std::string_view w2) -> bool {
    • return sumOfCharacters(w1) == sumOfCharacters(w2);
    • }
    • static inline auto sumOfCharacters(std::string_view word) -> int {
    • return std::accumulate(std::begin(word), std::end(word), 0, [](int sum, const char ch) {
    • return sum + static_cast<int>(ch);
    • return std::accumulate(std::begin(word), std::end(word), 0, [](int sum, char ch) {
    • return sum + ch;
    • });
    • }
    • };