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

O(n) solution

Code
Diff
  • /*
    Brainstorming steps (jumping in without thinking was a disaster)
    1. take while positive (current sum)
    2. note as maximum sum
    3. take while negative
    4. take while positive
    5. if current sum is less than negatives
        discard and continue with positives as current sum
    otherwise
        add negatives and positives to current sum
    6. update max
    7. repeat 2-6 until done
    */
    
    fn max_sequence(arr: &[i32]) -> i32 {
        if arr.is_empty() {
            return 0;
        }
        
        let mut max;
        let mut current = 0;
        let mut i = 0;
        
        while i < arr.len() && arr[i].is_positive() {
            current += arr[i];
            i += 1;
        }
        
        max = current;
        
        while i < arr.len() {
            let mut new_negatives = 0;
            while i < arr.len() && arr[i].is_negative() {
                new_negatives += -arr[i];
                i += 1;
            }
    
            let mut new_positives = 0;
            while i < arr.len() && arr[i].is_positive() {
                new_positives += arr[i];
                i += 1;
            }
            
            if current < new_negatives {
                current = new_positives;
            } else {
                current -= new_negatives;
                current += new_positives;
            }
    
            max = current.max(max);
        }
        
        max
    }
    
    • /*
    • Brainstorming steps (jumping in without thinking was a disaster)
    • 1. take while positive (current sum)
    • 2. note as maximum sum
    • 3. take while negative
    • 4. take while positive
    • 5. if current sum is less than negatives
    • discard and continue with positives as current sum
    • otherwise
    • add negatives and positives to current sum
    • 6. update max
    • 7. repeat 2-6 until done
    • */
    • fn max_sequence(arr: &[i32]) -> i32 {
    • (0..arr.len()).flat_map(|start| {
    • (start..=arr.len()).map(move |end| {
    • arr[start..end].iter().sum()
    • })
    • }).max().unwrap_or(0)
    • if arr.is_empty() {
    • return 0;
    • }
    • let mut max;
    • let mut current = 0;
    • let mut i = 0;
    • while i < arr.len() && arr[i].is_positive() {
    • current += arr[i];
    • i += 1;
    • }
    • max = current;
    • while i < arr.len() {
    • let mut new_negatives = 0;
    • while i < arr.len() && arr[i].is_negative() {
    • new_negatives += -arr[i];
    • i += 1;
    • }
    • let mut new_positives = 0;
    • while i < arr.len() && arr[i].is_positive() {
    • new_positives += arr[i];
    • i += 1;
    • }
    • if current < new_negatives {
    • current = new_positives;
    • } else {
    • current -= new_negatives;
    • current += new_positives;
    • }
    • max = current.max(max);
    • }
    • max
    • }
Code
Diff
  • const nums = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    function digitToText(digit) {
      return nums[digit]
    }
    • const nums = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    • function digitToText(digit) {
    • if (digit == 1) {
    • return 'one'
    • }
    • if (digit == 2) {
    • return 'two'
    • }
    • if (digit == 3) {
    • return 'three'
    • }
    • if (digit == 4) {
    • return 'four'
    • }
    • if (digit == 5) {
    • return 'five'
    • }
    • if (digit == 6) {
    • return 'six'
    • }
    • if (digit == 7) {
    • return 'seven'
    • }
    • if (digit == 8) {
    • return 'eight'
    • }
    • if (digit == 9) {
    • return 'nine'
    • }
    • if (digit == 0) {
    • return 'zero'
    • }
    • return nums[digit]
    • }
Code
Diff
  • class Triangle:
    
        @staticmethod
        def other_angle(a, b): 
            return 180 - (a + b)
        
    
        @staticmethod
        def triangle_type_angle(a, b, c):
            if all( angle < 90 for angle in (a, b ,c)):
                return "Acute Triangle"
            if any(angle == 90 for angle in (a, b, c)):
                return "Right Triangle"
            return "Obtuse Triangle"
        
        @staticmethod
        def triangle_type_sides(s1, s2, s3):
            if s1 == s2 == s3:
                return "Equilateral Triangle"
            elif s1 == s2 or s2 == s3 or s1 == s3:
                return "Isoceles Triangle"
            return "Scalene Triangle"
            
    
    • class Triangle:
    • @staticmethod
    • def other_angle(a, b):
    • return 180 - (a + b)
    • @staticmethod
    • def triangle_type_angle(a, b, c):
    • if all( angle < 90 for angle in (a, b ,c)):
    • return "Acute Triangle"
    • if any(angle == 90 for angle in (a, b, c)):
    • return "Right Triangle"
    • return "Obtuse Triangle"
    • @staticmethod
    • def triangle_type_sides(s1, s2, s3):
    • if s1 == s2 == s3:
    • return "Equilateral Triangle"
    • elif s1 == s2 or s2 == s3 or s1 == s3:
    • return "Isoceles Triangle"
    • return "Scalene Triangle"
Code
Diff
  • function sum(a,b) {
      return a+b; // wrong returning
    }
    • function sum(a,b) {
    • return 1; // wrong returning
    • return a+b; // wrong returning
    • }
Fundamentals
Algorithms
Code
Diff
  • const BLOCK_WIDTH: i32 = 274;
    const BLOCK_HEIGHT: i32 = 80;
    const PROBATION_LIMIT: i32 = 2000;
    
    fn goes_to_jail(directions: &[[i32;4]]) -> bool {
        let (mut x, mut y) = (0, 0);
        directions.iter().any(|[north, east, south, west]| {
            x += (east - west) * BLOCK_WIDTH;
            y += (north - south) * BLOCK_HEIGHT;
            x.pow(2) + y.pow(2) > PROBATION_LIMIT.pow(2)      
        })
    }
    • const BLOCK_WIDTH: i32 = 274;
    • const BLOCK_HEIGHT: i32 = 80;
    • const PROBATION_LIMIT: i32 = 2000;
    • fn goes_to_jail(directions: &[[i32;4]]) -> bool {
    • let mut x = 0;
    • let mut y = 0;
    • for [north, east, south, west] in directions {
    • let (mut x, mut y) = (0, 0);
    • directions.iter().any(|[north, east, south, west]| {
    • x += (east - west) * BLOCK_WIDTH;
    • y += (north - south) * BLOCK_HEIGHT;
    • if x.pow(2) + y.pow(2) > PROBATION_LIMIT.pow(2) {
    • return true
    • }
    • }
    • false
    • x.pow(2) + y.pow(2) > PROBATION_LIMIT.pow(2)
    • })
    • }

Added tests for negative integers, modified solution so it doesn't rely on Show instance of Int.

Code
Diff
  • module AreThereThree where
    
    solution :: Int -> Bool
    solution = go . abs
      where
        go 0 = False
        go x = let (x', y) = x `divMod` 10 in y == 3 || go x'
    
    • module AreThereThree where
    • solution :: Int -> Bool
    • solution = elem '3' . show
    • solution = go . abs
    • where
    • go 0 = False
    • go x = let (x', y) = x `divMod` 10 in y == 3 || go x'
Code
Diff
  • pub struct Account {
        password: String,
        secret: String,
    }
    
    impl Account {
        pub fn new(password: String, secret: String) -> Self {
            Self { password, secret }
        }
    
        pub fn request_secret(&self, input: &str) -> Option<&str> {
            if input == self.password {
                Some(&self.secret)
            } else {
                None
            }
        }
    }
    • mod preloaded;
    • use preloaded::Account;
    • pub struct Account {
    • password: String,
    • secret: String,
    • }
    • impl Account {
    • pub fn new(password: String, secret: String) -> Self {
    • Self { password, secret }
    • }
    • pub fn request_secret(&self, input: &str) -> Option<&str> {
    • if input == self.password {
    • Some(&self.secret)
    • } else {
    • None
    • }
    • }
    • }
Code
Diff
  • let [min, max] = [iter.next()?;2]; let minmax = iter.fold((min, max), |(min, max), num| (num.min(min), num.max(max))); Some(minmax)}
    
    
        
        
         
        
    
    
    • fn find_largest_and_smallest(nums: &[i32]) -> Option<(i32, i32)> {
    • let mut iter = nums.iter().copied();
    • let [min, max] = [iter.next()?;2];
    • let minmax = iter.fold((min, max), |(min, max), num| (num.min(min), num.max(max)));
    • Some(minmax)
    • }
    • let [min, max] = [iter.next()?;2]; let minmax = iter.fold((min, max), |(min, max), num| (num.min(min), num.max(max))); Some(minmax)}