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
Code
Diff
  • export function validateString(input: string, index: number): boolean {
      const validator = new BracketsValidator();
      
      for (let i = index; i < input.length; i++) {
        const char = input[i];
        
       if (!validator.isValidChar(char)) {
         return false;
       }
      }
      
      return true;
    }
    
    
    
    class BracketsValidator {
      private readonly bracketPairs: Record<string, string> = {
        '{': '}',
        '[': ']',
        '(': ')',
      };
      private readonly openBrackets: Set<string> = new Set(Object.keys(this.bracketPairs));
      private readonly closingBrackets: Set<string> = new Set(Object.values(this.bracketPairs));
      private stack: string[] = [];
      
      public isValidChar(char: string): boolean {
        if (this.isNotBracket(char)) {
          return true;
        }
        
        if (this.isOpeningBracket(char)) {
          this.push(char);
          return true;
        }
        
        if (this.hasMatchFor(char)) {
          this.pop();
          return true;
        }
        
        return false;
      }
      
      private push(bracket: string): void {
        this.stack.push(bracket);
      } 
      
      private pop(): string|undefined {
        return this.stack.pop();
      }
      
      private lastFromStack(): string|undefined {
        return this.stack.slice(-1)[0];
      }
      
      private hasMatchFor(bracket: string): boolean {
        const previousBracket: string|undefined = this.lastFromStack();
        
        if (undefined === previousBracket) {
          return false;
        }
        
        const match = this.matchFor(bracket);
        
        if (previousBracket !== match) {
          return false;
        }
        
        return true;
      }
      
      private isOpeningBracket(char: string): boolean {
        if (this.openBrackets.has(char)) {
          return true;
        }
    
        return false;
      }
      
      private isClosingBracket(char: string): boolean {
        if (this.closingBrackets.has(char)) {
          return true;
        }
    
        return false;
      }
      
      private isNotBracket(char: string): boolean {
        if (this.isOpeningBracket(char)) {
          return false;
        }
        
        if (this.isClosingBracket(char)) {
          return false;
        }
        
        return true;
      }
      
      private matchFor(bracket: string): string {
        const index: number = Object.values(this.bracketPairs).indexOf(bracket);
        return Object.keys(this.bracketPairs)[index];
      }
    }
    
    • const bracketPairs: Record<string, string> = {
    • '{': '}',
    • '[': ']',
    • '(': ')',
    • }
    • const openBrackets: Set<string> = new Set(Object.keys(bracketPairs));
    • const closingBrackets: Set<string> = new Set(Object.values(bracketPairs));
    • export function validateString(input: string, index: number): boolean {
    • const stack: string[] = [];
    • for (let i = index, len = input.length; i < len; i++) {
    • const validator = new BracketsValidator();
    • for (let i = index; i < input.length; i++) {
    • const char = input[i];
    • if (openBrackets.has(char)) {
    • stack.push(bracketPairs[char])
    • } else if (closingBrackets.has(char)) {
    • const match = stack.pop();
    • if (match !== char) {
    • return false;
    • }
    • }
    • if (!validator.isValidChar(char)) {
    • return false;
    • }
    • }
    • return true;
    • }
    • }
    • class BracketsValidator {
    • private readonly bracketPairs: Record<string, string> = {
    • '{': '}',
    • '[': ']',
    • '(': ')',
    • };
    • private readonly openBrackets: Set<string> = new Set(Object.keys(this.bracketPairs));
    • private readonly closingBrackets: Set<string> = new Set(Object.values(this.bracketPairs));
    • private stack: string[] = [];
    • public isValidChar(char: string): boolean {
    • if (this.isNotBracket(char)) {
    • return true;
    • }
    • if (this.isOpeningBracket(char)) {
    • this.push(char);
    • return true;
    • }
    • if (this.hasMatchFor(char)) {
    • this.pop();
    • return true;
    • }
    • return false;
    • }
    • private push(bracket: string): void {
    • this.stack.push(bracket);
    • }
    • private pop(): string|undefined {
    • return this.stack.pop();
    • }
    • private lastFromStack(): string|undefined {
    • return this.stack.slice(-1)[0];
    • }
    • private hasMatchFor(bracket: string): boolean {
    • const previousBracket: string|undefined = this.lastFromStack();
    • if (undefined === previousBracket) {
    • return false;
    • }
    • const match = this.matchFor(bracket);
    • if (previousBracket !== match) {
    • return false;
    • }
    • return true;
    • }
    • private isOpeningBracket(char: string): boolean {
    • if (this.openBrackets.has(char)) {
    • return true;
    • }
    • return false;
    • }
    • private isClosingBracket(char: string): boolean {
    • if (this.closingBrackets.has(char)) {
    • return true;
    • }
    • return false;
    • }
    • private isNotBracket(char: string): boolean {
    • if (this.isOpeningBracket(char)) {
    • return false;
    • }
    • if (this.isClosingBracket(char)) {
    • return false;
    • }
    • return true;
    • }
    • private matchFor(bracket: string): string {
    • const index: number = Object.values(this.bracketPairs).indexOf(bracket);
    • return Object.keys(this.bracketPairs)[index];
    • }
    • }
Code
Diff
  • f=lambda n:[n-10,91][n<101]
    • f=lambda n: 91 if n <= 100 else n - 10
    • f=lambda n:[n-10,91][n<101]
Fundamentals
Strings
Data Types
Algorithms
Logic

A few less spaces...

Code
Diff
  • palindrome=lambda w:str(w)==str(w)[::-1]
    
    • palindrome = lambda w: str(w)==str(w)[::-1]
    • palindrome=lambda w:str(w)==str(w)[::-1]

Assumes entry lists are sorted !!

Code
Diff
  • #include <list>
    
    auto merge_list(const std::list<int>& a, const std::list<int>& b) {
      std::list<int> o{a};
      for(auto i : b) o.insert(std::lower_bound(o.begin(), o.end(), i), i);
      return o;
    }
    • #include <algorithm>
    • #include <list>
    • #include <execution>
    • #include <iterator>
    • auto merge_list(const std::list<int>& a, const std::list<int>& b) {
    • std::list<int> o;
    • std::merge(
    • std::execution::seq,
    • a.begin(),
    • a.end(),
    • b.begin(),
    • b.end(),
    • std::back_inserter(o)
    • // std::less is default sort
    • );
    • std::list<int> o{a};
    • for(auto i : b) o.insert(std::lower_bound(o.begin(), o.end(), i), i);
    • return o;
    • }
Fundamentals
Functional Programming
Declarative Programming
Programming Paradigms
Higher-order Functions
Functions
Control Flow
Basic Language Features
  • Added 3 more test cases
  • Follwing problem description:
    Counting from 1 to n, then back to 1
  • Wait, why am I doing this?
Code
Diff
  • module NumberPrint where
    
    numberprint :: Int -> Integer
    numberprint n = read . concat $ show <$> [1 .. pred n] <> [n, pred n .. 1]
    • module NumberPrint where
    • numberprint :: Int -> Integer
    • numberprint = read . (>>= id) . ((++) <*> (reverse . init)) . (\x -> show <$> [1..x])
    • numberprint n = read . concat $ show <$> [1 .. pred n] <> [n, pred n .. 1]
Games
Algorithms
Logic
Combinators
Functional Programming
Combinatory Logic
Functions
Declarative Programming
Programming Paradigms
Computational Science
Theoretical Computer Science
Control Flow
Basic Language Features
Fundamentals
Design Patterns

Combinators are useful.
I'd like a safe tail function though.
EDIT: drop could be used instead.

<*> is S combinator.

  • For A <*> B, parameter is piped to both A and B
  • and then, they are applied
  • i.e. A <*> B = \x -> (A x) (B x)
Code
Diff
  • module Platforms where
    
    platforms :: [Integer] -> Integer
    platforms = sum . fmap abs . (zipWith (-) <*> drop 1)
    • module Platforms where
    • platforms :: [Integer] -> Integer
    • platforms (x:y:xs) = abs (x - y) + platforms (y:xs)
    • platforms _ = 0
    • platforms = sum . fmap abs . (zipWith (-) <*> drop 1)