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

What's new:

  • Imported separate std::-s instead of entire namespace std
  • Implemented testing using string IO
Code
Diff
  • #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <vector>
    #include <algorithm>
    #include <cmath>
    #include <random>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::istream;
    using std::make_pair;
    using std::mt19937;
    using std::ostream;
    using std::pair;
    using std::random_device;
    using std::string;
    using std::vector;
    
    string generateRandomNumber() {
        vector<char> digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    
        // Initialize random number generator
        random_device rd;  // Obtain a random number from hardware
        mt19937 g(rd());  // Seed the generator
    
        // Shuffle digits
        shuffle(digits.begin(), digits.end(), g);
    
        // Return the first 4 digits as a string
        return string(digits.begin(), digits.begin() + 4);
    }
    
    bool hasDuplicateDigits(const string &number) {
        vector<bool> seen(10, false);
        for (const auto c : number) {
            if (seen[c - '0']) return true;
            seen[c - '0'] = true;
        }
        return false;
    }
    
    bool isNumeric(const string &s) {
        return all_of(s.begin(), s.end(), [](char c){ return isdigit(c); });
    }
    
    string getUserGuess(istream &in = cin, ostream &out = cout) {
        string guess;
        out << "Enter your guess (a 4-digit number with non-repeating digits): ";
        in >> guess;
        return guess;
    }
    
    pair<int, int> checkGuess(const string &randomNumber, const string &userGuess) {
        int correctNumbers = 0;
        int correctPosition = 0;
    
        for (int i = 0; i < 4; ++i) {
            if (randomNumber[i] == userGuess[i]) {
                correctNumbers++;
                correctPosition++;
            } else if (count(randomNumber.begin(), randomNumber.end(), userGuess[i])) {
                correctNumbers++;
            }
        }
    
        return make_pair(correctNumbers, correctPosition);
    }
    
    int acceptGuess(const string &expected, istream &in = cin, ostream &out = cout) {
        int attempts = 0;
        while (true) {
            string userGuess = getUserGuess(in, out);
            if (userGuess.length() != 4 || hasDuplicateDigits(userGuess) || !isNumeric(userGuess)) {
                out << "Invalid input. Please enter a 4-digit number with non-repeating digits." << endl;
                continue;
            }
            auto [corNum, corPos] = checkGuess(expected, userGuess);
            out << "Correct numbers: " << corNum << " Correct position: " << corPos << endl;
            attempts++;
            if (corPos == 4) {
                out << "Congratulations! You guessed the number " << expected << " correctly in " << attempts << " attempts!" << endl;
                break;
            }
        }
        return attempts;
    }
    
    int _main(istream &in = cin, ostream &out = cout) {
        // Seed rand since random_shuffle _probably_ uses it.
        srand(static_cast<unsigned>(time(0)));
        string randomNumber = generateRandomNumber();
        acceptGuess(randomNumber, in, out);
        return 0;
    }
    
    • #include <iostream>
    • #include <cstdlib>
    • #include <ctime>
    • #include <vector>
    • #include <algorithm>
    • #include <cmath>
    • #include <random>
    • using namespace std;
    • using std::cin;
    • using std::cout;
    • using std::endl;
    • using std::istream;
    • using std::make_pair;
    • using std::mt19937;
    • using std::ostream;
    • using std::pair;
    • using std::random_device;
    • using std::string;
    • using std::vector;
    • string generateRandomNumber() {
    • vector<char> digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    • // Initialize random number generator
    • random_device rd; // Obtain a random number from hardware
    • mt19937 g(rd()); // Seed the generator
    • // Shuffle digits
    • shuffle(digits.begin(), digits.end(), g);
    • // Return the first 4 digits as a string
    • return string(digits.begin(), digits.begin() + 4);
    • }
    • bool hasDuplicateDigits(const string &number) {
    • vector<bool> seen(10, false);
    • for (const auto c : number) {
    • if (seen[c - '0']) return true;
    • seen[c - '0'] = true;
    • }
    • return false;
    • }
    • bool isNumeric(const string &s) {
    • return all_of(s.begin(), s.end(), [](char c){ return isdigit(c); });
    • }
    • string getUserGuess() {
    • string getUserGuess(istream &in = cin, ostream &out = cout) {
    • string guess;
    • cout << "Enter your guess (a 4-digit number with non-repeating digits): ";
    • cin >> guess;
    • out << "Enter your guess (a 4-digit number with non-repeating digits): ";
    • in >> guess;
    • return guess;
    • }
    • pair<int, int> checkGuess(const string &randomNumber, const string &userGuess) {
    • int correctNumbers = 0;
    • int correctPosition = 0;
    • for (int i = 0; i < 4; ++i) {
    • if (randomNumber[i] == userGuess[i]) {
    • correctNumbers++;
    • correctPosition++;
    • } else if (count(randomNumber.begin(), randomNumber.end(), userGuess[i])) {
    • correctNumbers++;
    • }
    • }
    • return make_pair(correctNumbers, correctPosition);
    • }
    • int _main() {
    • // Seed rand since random_shuffle _probably_ uses it.
    • srand(static_cast<unsigned>(time(0)));
    • string randomNumber = generateRandomNumber();
    • int acceptGuess(const string &expected, istream &in = cin, ostream &out = cout) {
    • int attempts = 0;
    • while (true) {
    • string userGuess = getUserGuess();
    • string userGuess = getUserGuess(in, out);
    • if (userGuess.length() != 4 || hasDuplicateDigits(userGuess) || !isNumeric(userGuess)) {
    • cout << "Invalid input. Please enter a 4-digit number with non-repeating digits." << endl;
    • out << "Invalid input. Please enter a 4-digit number with non-repeating digits." << endl;
    • continue;
    • }
    • auto [corNum, corPos] = checkGuess(randomNumber, userGuess);
    • cout << "Correct numbers: " << corNum << " Correct position: " << corPos << endl;
    • auto [corNum, corPos] = checkGuess(expected, userGuess);
    • out << "Correct numbers: " << corNum << " Correct position: " << corPos << endl;
    • attempts++;
    • if (corPos == 4) {
    • cout << "Congratulations! You guessed the number " << randomNumber << " correctly in " << attempts << " attempts!" << endl;
    • out << "Congratulations! You guessed the number " << expected << " correctly in " << attempts << " attempts!" << endl;
    • break;
    • }
    • }
    • return attempts;
    • }
    • int _main(istream &in = cin, ostream &out = cout) {
    • // Seed rand since random_shuffle _probably_ uses it.
    • srand(static_cast<unsigned>(time(0)));
    • string randomNumber = generateRandomNumber();
    • acceptGuess(randomNumber, in, out);
    • return 0;
    • }

its a static amout of numbers, so it can be just a list, duh

Code
Diff
  • get_fibs=[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050, 3416454622906707, 5527939700884757, 8944394323791464, 14472334024676221, 23416728348467685, 37889062373143906, 61305790721611591, 99194853094755497, 160500643816367088, 259695496911122585, 420196140727489673, 679891637638612258, 1100087778366101931, 1779979416004714189, 2880067194370816120, 4660046610375530309, 7540113804746346429, 12200160415121876738, 19740274219868223167, 31940434634990099905, 51680708854858323072, 83621143489848422977, 135301852344706746049, 218922995834555169026, 354224848179261915075]
    • get_fibs=lambda a=1,b=1:[(t:=a,a:=b,b:=b+t)[0]for _ in range(100)]
    • get_fibs=[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050, 3416454622906707, 5527939700884757, 8944394323791464, 14472334024676221, 23416728348467685, 37889062373143906, 61305790721611591, 99194853094755497, 160500643816367088, 259695496911122585, 420196140727489673, 679891637638612258, 1100087778366101931, 1779979416004714189, 2880067194370816120, 4660046610375530309, 7540113804746346429, 12200160415121876738, 19740274219868223167, 31940434634990099905, 51680708854858323072, 83621143489848422977, 135301852344706746049, 218922995834555169026, 354224848179261915075]
Code
Diff
  • def twin_sum_solutions(array)
      duplicates = []
      find_duplicates(array).map{|e| e*2 }
    end
    
    def find_duplicates(array)
      duplicates = []
      array.each do |element|
        duplicates << element if array.count(element) > 1
      end
      duplicates.uniq
    end
    • def twin_sum_solutions(array)
    • duplicates = []
    • find_duplicates(array).map{|e| e*2 }
    • end
    • def find_duplicates(array)
    • duplicates = []
    • array.each do |element|
    • duplicates << element if array.count(element) > 1
    • end
    • duplicates.uniq
    • end
Fundamentals
Arrays
Code
Diff
  • function Sum-OfPositive($NumberArray)
    {
      [Func[int,bool]]$Delegate = {param($v); return $v -ge 0}
      [Linq.Enumerable]::Sum([Linq.Enumerable]::Where([int[]]$NumberArray,$Delegate))
    }
    
    • function Sum-OfPositive($NumberArray)
    • {
    • ($NumberArray | ?{$_ -gt 0} | Measure -sum).Sum
    • [Func[int,bool]]$Delegate = {param($v); return $v -ge 0}
    • [Linq.Enumerable]::Sum([Linq.Enumerable]::Where([int[]]$NumberArray,$Delegate))
    • }

Added a small benchmark, the output can be shown with "cargo test -- --nocapture" locally.

Code
Diff
  • pub fn sort_desc(mut n: u64) -> u64 {
        let mut digit_counts = [0; 10];
        while n > 0 {
            let digit = (n % 10) as usize;
            digit_counts[digit] += 1;
            n /= 10;
        }
        let mut result = 0;
        let mut fac = 1;
        for digit in 0..10 {
            for _ in 0..digit_counts[digit] {
                result += (digit as u64) * fac;
                fac *= 10;
            }
        }
        result
    }
    use std::iter::repeat;
    
    pub fn sort_desc_it(mut n: u64) -> u64 {
        let mut digit_counts = [0; 10];
        while n > 0 {
            digit_counts[n as usize % 10] += 1;
            n /= 10;
        }
        digit_counts
            .into_iter()
            .enumerate()
            .rev()
            .flat_map(|(digit, count)| repeat(digit as u64).take(count))
            .fold(0, |result, digit| result * 10 + digit)
    }
    use std::time::Instant;
    
    fn benchmark<F>(func: F, input: u64, iterations: u32) -> (u64, f64)
    where
        F: Fn(u64) -> u64,
    {
        let mut total_duration = 0.0;
        let mut result = 0;
        for _ in 0..iterations {
            let start = Instant::now();
            result = func(input);
            let duration = start.elapsed();
            total_duration += duration.as_secs_f64();
        }
        let avg_duration = total_duration / iterations as f64;
        (result, avg_duration)
    }
    
    • pub fn sort_desc(mut n: u64) -> u64 {
    • let mut digit_counts = [0; 10];
    • while n > 0 {
    • let digit = (n % 10) as usize;
    • digit_counts[digit] += 1;
    • n /= 10;
    • }
    • let mut result = 0;
    • let mut fac = 1;
    • for digit in 0..10 {
    • for _ in 0..digit_counts[digit] {
    • result += (digit as u64) * fac;
    • fac *= 10;
    • }
    • }
    • result
    • }
    • use std::iter::repeat;
    • pub fn sort_desc(mut n: u64) -> u64 {
    • pub fn sort_desc_it(mut n: u64) -> u64 {
    • let mut digit_counts = [0; 10];
    • while n > 0 {
    • digit_counts[n as usize % 10] += 1;
    • n /= 10;
    • }
    • digit_counts
    • .into_iter()
    • .enumerate()
    • .rev()
    • .flat_map(|(digit, count)| repeat(digit as u64).take(count))
    • .fold(0, |result, digit| result * 10 + digit)
    • }
    • }
    • use std::time::Instant;
    • fn benchmark<F>(func: F, input: u64, iterations: u32) -> (u64, f64)
    • where
    • F: Fn(u64) -> u64,
    • {
    • let mut total_duration = 0.0;
    • let mut result = 0;
    • for _ in 0..iterations {
    • let start = Instant::now();
    • result = func(input);
    • let duration = start.elapsed();
    • total_duration += duration.as_secs_f64();
    • }
    • let avg_duration = total_duration / iterations as f64;
    • (result, avg_duration)
    • }