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

Fixed errors in morse.
Updated the parsing logic.
Test cases improved to test more strictly.
Code updated to naming and stype conventions.

Code
Diff
  • DOT = '.'
    DASH = '-'
    SPACE = ' '
    
    
    MORSE = {
        ' ': [SPACE],
        'a': [DOT, DASH],
        'b': [DASH, DOT, DOT, DOT],
        'c': [DASH, DOT, DASH, DOT],
        'd': [DASH, DOT, DOT],
        'e': [DOT],
        'f': [DOT, DOT, DASH, DOT],
        'g': [DASH, DASH, DOT],
        'h': [DOT, DOT, DOT, DOT],
        'i': [DOT, DOT],
        'j': [DOT, DASH, DASH, DASH],
        'k': [DASH, DOT, DASH],
        'l': [DOT, DASH, DOT, DOT],
        'm': [DASH, DASH],
        'n': [DASH, DOT],
        'o': [DASH, DASH, DASH],
        'p': [DOT, DASH, DASH, DOT],
        'q': [DASH, DASH, DOT, DASH],
        'r': [DOT, DASH, DOT],
        's': [DOT, DOT, DOT],
        't': [DASH],
        'u': [DOT, DOT, DASH],
        'v': [DOT, DOT, DOT, DASH],
        'w': [DOT, DASH, DASH],
        'x': [DASH, DOT, DOT, DASH],
        'y': [DASH, DOT, DASH, DASH],
        'z': [DASH, DASH, DOT, DOT],
        '0': [DASH, DASH, DASH, DASH, DASH],
        '1': [DOT, DASH, DASH, DASH, DASH],
        '2': [DOT, DOT, DASH, DASH, DASH],
        '3': [DOT, DOT, DOT, DASH, DASH],
        '4': [DOT, DOT, DOT, DOT, DASH],
        '5': [DOT, DOT, DOT, DOT, DOT],
        '6': [DASH, DOT, DOT, DOT, DOT],
        '7': [DASH, DASH, DOT, DOT, DOT],
        '8': [DASH, DASH, DASH, DOT, DOT],
        '9': [DASH, DASH, DASH, DASH, DOT],
    }
    
    
    def morse_code(msg):
        return ''.join(map(lambda letter: ''.join(MORSE[letter]), msg.lower()))
    
    • dot='.'
    • dash='-'
    • morse = {':space:':[' '],'a':[dot,dash],'b':[dash,dot,dot,dot],'c':[dash,dot,dash,dot],'d':[dash,dot,dot],'e':[dot],'f':[dot,dot,dash,dot],'g':[dash,dash,dot],'h':[dot,dot,dot,dot],""
    • 'i':[dot,dot],'j':[dot,dash,dash,dash],'k':[dash,dot,dash],'l':[dot,dash,dot,dot],'m':[dash,dash],'n':[dash,dot],'o':[dash,dash,dash],'p':[dot,dash,dash],'q':[dash,dash,dot,dash],""
    • 'r':[dot,dash,dot],'s':[dot,dot,dot],'t':[dash],'u':[dot,dot,dash],'v':[dot,dot,dot,dash],'w':[dot,dash,dash],'x':[dash,dot,dot,dash],'y':[dash,dot,dash,dash],'z':[dash,dot,dot],""
    • '0':[dash,dash,dash,dash,dash],'1':[dot,dash,dash,dash,dash],'2':[dot,dot,dash,dash,dash],'3':[dot,dot,dot,dash,dash],'4':[dot,dot,dot,dot,dash],'5':[dot,dot,dot,dot,dot],""
    • '6':[dash,dot,dot,dot,dot],'7':[dash,dash,dot,dot,dot],'8':[dash,dash,dash,dot,dot],'9':[dash,dash,dash,dash,dot]}
    • DOT = '.'
    • DASH = '-'
    • SPACE = ' '
    • MORSE = {
    • ' ': [SPACE],
    • 'a': [DOT, DASH],
    • 'b': [DASH, DOT, DOT, DOT],
    • 'c': [DASH, DOT, DASH, DOT],
    • 'd': [DASH, DOT, DOT],
    • 'e': [DOT],
    • 'f': [DOT, DOT, DASH, DOT],
    • 'g': [DASH, DASH, DOT],
    • 'h': [DOT, DOT, DOT, DOT],
    • 'i': [DOT, DOT],
    • 'j': [DOT, DASH, DASH, DASH],
    • 'k': [DASH, DOT, DASH],
    • 'l': [DOT, DASH, DOT, DOT],
    • 'm': [DASH, DASH],
    • 'n': [DASH, DOT],
    • 'o': [DASH, DASH, DASH],
    • 'p': [DOT, DASH, DASH, DOT],
    • 'q': [DASH, DASH, DOT, DASH],
    • 'r': [DOT, DASH, DOT],
    • 's': [DOT, DOT, DOT],
    • 't': [DASH],
    • 'u': [DOT, DOT, DASH],
    • 'v': [DOT, DOT, DOT, DASH],
    • 'w': [DOT, DASH, DASH],
    • 'x': [DASH, DOT, DOT, DASH],
    • 'y': [DASH, DOT, DASH, DASH],
    • 'z': [DASH, DASH, DOT, DOT],
    • '0': [DASH, DASH, DASH, DASH, DASH],
    • '1': [DOT, DASH, DASH, DASH, DASH],
    • '2': [DOT, DOT, DASH, DASH, DASH],
    • '3': [DOT, DOT, DOT, DASH, DASH],
    • '4': [DOT, DOT, DOT, DOT, DASH],
    • '5': [DOT, DOT, DOT, DOT, DOT],
    • '6': [DASH, DOT, DOT, DOT, DOT],
    • '7': [DASH, DASH, DOT, DOT, DOT],
    • '8': [DASH, DASH, DASH, DOT, DOT],
    • '9': [DASH, DASH, DASH, DASH, DOT],
    • }
    • def morse_code(msg):
    • return ''.join(list(map(lambda i:''.join(morse[i.lower().replace(' ', ':space:')]),msg)))
    • print(morse_code('11 2 11'))
    • return ''.join(map(lambda letter: ''.join(MORSE[letter]), msg.lower()))
Interview Questions
Algorithms
Logic
Code
Diff
  • def swapPixel(array, index1, index2):
        temp = getPixel(array, index1)
        putPixel(array, index1, getPixel(array, index2))
        putPixel(array, index2, temp)
    
    def horizontalFlipPair(array, index):
        return (int((len(array)/2)+((len(array)/2)-int(index/len(array[0]))))-1)*len(array[0])+index%len(array[0])
    
    def simpleFlipPair(array, index):
        return int(2*(((len(array)-1)/2-int(index/len(array[0])))*len(array[0]))+index)
    
    def flipImage(array):
        for i in range(int(len(array)/2*len(array[0]))):
            swapPixel(array, i, simpleFlipPair(array, i))
    
        
        
    • def swapPixel(array, index1, index2):
    • temp = getPixel(array, index1)
    • putPixel(array, index1, getPixel(array, index2))
    • putPixel(array, index2, temp)
    • def horizontalFlipPair(array, index):
    • return (int((len(array)/2)+((len(array)/2)-int(index/len(array[0]))))-1)*len(array[0])+index%len(array[0])
    • def flipImage(array):
    • for i in range(int(len(array)*len(array[0])/2)):
    • swapPixel(array, i, horizontalFlipPair(array, i))
    • def simpleFlipPair(array, index):
    • w = len(array[0])
    • m = (len(array)-1)/2
    • l = int(index/w)
    • a = (m-l)*w
    • return int(2*(a)+index)
    • return int(2*(((len(array)-1)/2-int(index/len(array[0])))*len(array[0]))+index)
    • def flipImage(array):
    • for i in range(int(len(array)/2*len(array[0]))):
    • swapPixel(array, i, simpleFlipPair(array, i))

Changed Contract to be a struct because if the public fields are all comparable then you can just ask for equality. There is probably a better way to return the contract string though.

Code
Diff
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public enum Quality { SD, HD, UHD }
    
    public class TV
    {
        private struct Contract
        {
            public static Contract Premium = new Contract(4, Quality.UHD);
            public static Contract Standard = new Contract(2, Quality.HD);
            public static Contract Essential = new Contract(1, Quality.SD);
        
            public int Number;
            public Quality Quality;
        
            public Contract(int number, Quality quality) { Number = number; Quality = quality; }
        }
    
        public static string GetContract(int numDevices, Quality quality)
        {
            var desiredContract = new Contract(numDevices, quality);
        
            return desiredContract.Equals(Contract.Premium) ? "PREMIUM" :
              desiredContract.Equals(Contract.Standard) ? "STANDARD" :
              desiredContract.Equals(Contract.Essential) ? "ESSENTIAL" :
              "INVALID";
        }
    }
    • using System;
    • using System.Collections.Generic;
    • using System.Linq;
    • public enum Quality { SD, HD, UHD }
    • public class TV
    • {
    • private class Contract
    • private struct Contract
    • {
    • public string Type;
    • public Func<int, Quality, bool> Condition;
    • public static readonly IList<Contract> Types =
    • new List<Contract>
    • {
    • new Contract { Type = "PREMIUM", Condition = (numDevices, quality) => numDevices == 4 && quality == Quality.UHD },
    • new Contract { Type = "STANDARD", Condition = (numDevices, quality) => numDevices == 2 && quality == Quality.HD },
    • new Contract { Type = "ESSENTIAL", Condition = (numDevices, quality) => numDevices == 1 && quality == Quality.SD },
    • new Contract { Type = "INVALID", Condition = (numDevices, quality) => true }
    • };
    • public static Contract Premium = new Contract(4, Quality.UHD);
    • public static Contract Standard = new Contract(2, Quality.HD);
    • public static Contract Essential = new Contract(1, Quality.SD);
    • public int Number;
    • public Quality Quality;
    • public Contract(int number, Quality quality) { Number = number; Quality = quality; }
    • }
    • public static string GetContract(int numDevices, Quality quality)
    • {
    • return Contract.Types.First(contract => contract.Condition(numDevices, quality)).Type;
    • var desiredContract = new Contract(numDevices, quality);
    • return desiredContract.Equals(Contract.Premium) ? "PREMIUM" :
    • desiredContract.Equals(Contract.Standard) ? "STANDARD" :
    • desiredContract.Equals(Contract.Essential) ? "ESSENTIAL" :
    • "INVALID";
    • }
    • }

More pythonic version of your code.

Note: it's very inefficient solution. My fork of your previous solution takes 76 ms performing 100 tests on a string "A" * 10**6 while this one takes 10600 ms.

Code
Diff
  • import re
    
    def AmIYelling(input_sentence):
        return not len(re.sub('[^a-z]', '', input_sentence))
    • import re
    • def AmIYelling(input_sentence):
    • return True if(len(re.sub('[^a-z]','',input_sentence))==0) else False
    • return not len(re.sub('[^a-z]', '', input_sentence))
Fundamentals
Arrays
Data Types
Code
Diff
  • function findOnly(arr) {
      let result = 0;
      arr.forEach((i) => {
        result = i ^ result;
      });
      return result;
    }
    • function getSum(array) {
    • return array.reduce((total, num) => total += num);
    • function findOnly(arr) {
    • let result = 0;
    • arr.forEach((i) => {
    • result = i ^ result;
    • });
    • return result;
    • }
Code
Diff
  • import java.lang.Math;
    public class BiggerNum{
    
      /**
       * @param a integer of param1
       * @param b integer of param2
       * @return the bigger integer of a and b
       * If a equals b, return either one
       */
      public static int compare(int a, int b) {
        return Math.max(a,b);
      }
    }
    • import java.lang.Math;
    • public class BiggerNum{
    • /**
    • * @param a integer of param1
    • * @param b integer of param2
    • * @return the bigger integer of a and b
    • * If a equals b, return either one
    • */
    • public static int compare(int a, int b) {
    • return Math.max(a,b);
    • }
    • }