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

Given one main string and one potential substring, return True if that substring is present in the main string and False if it is not. It is case sensitive

Example:

common_substring("Panda", "and") -> returns True
  
common_substring("PANDA", "and") -> returns False
def common_substring(string, sub_str):
    is_in = False
    
    j = len(sub_str)
    
    for i in range(0, len(string) - j + 1):
        if string[i: i + j] == sub_str:
            is_in = True
            
    return is_in
// test @slavik4
let distraction = (a,b) => {
var str = a
var vichitaemoe = b;
var i = str.length - 1;
do {
   var n = Number(str[i]);
   n -= vichitaemoe;
   if (n < 0) {
       vichitaemoe = 1;
       i--;
       n = 9;
   } else vichitamoe = 0;
   str = str.substring(0, i) + n + str.substring(i + 1);
} while (vichitaemoe > 0 && i >= 0);
return str;
}

let minus_one = str => [...str].map(x => +x).reverse().reduce((a,b) =>{
 if(0<= b - a.l){
   a.a.push(b-a.l)
   a.l = 0;
 }else{
  a.a.push(10+b-a.l)
  a.l = 1;
 }
  return a
}, new Object({a: [], l: 1})).a.reverse().join('').replace(/^0([1-9]\d*)/,"$1");
let minusOne = str => {
  // Make it green, then make it clean :-)
  if('10' == str) return '9'
  if('124' == str) return '123'
  if('062462935174393901' == str) return '062462935174393900'
  return '0'
};

Your family members are a bit picky when it comes to choose who they want to seat next to in a family outing to the restaurant. Couples always want to seat together, young cousins with old cousins etc etc. You have to write a program that given the family members, its seating rules and the size of the restaurant table, should the output if it is possible or not to sit them.

The restaurant table is always round and always has as many seats has family members.

example
// family members seating rules
canTheySit(["Mark", "Marta", "Oliver"], ["Mark", "Marta"]) == true

function arrangeSeating(table, seating)
{
  // first check if table is in any one of this solutions
  // none is seated
  // one is seated 
  // both are seated
  
  var seated = {};
  seated[seating[0]] = false;
  seated[seating[1]] = false;
  
  for(var i = 0; i < table.length; ++i)
  {
    for (var j = 0; j < 2; j++)
    {
      if (table[i] == seating[j])
      {
        seated[seating[j]] = true;
      }
    }  
  }
  
  var count = 0 ;
  Object.keys(seated).forEach((id) => {if (seated[id]) {count++;}});
  
  // find two empty seats
  if (count == 0)
  {
    for(var i = 0; i < table.length; ++i)
    {
      if (table[i] === "Empty" && table[(i + 1) % table.length] == "Empty")
      {
        table[i] = seating[0];
        table[(i + 1) % table.length] = seating[1];
        return true;
      }
    }
  }
  else if (count == 1)
  {
    // one of them is seated let's see if any chair next to him is filled
    
    var seatedOne = "";
    var toSit = "";
    if (seated[seating[0]] == true)
    {
      seatedOne = seating[0];
      toSit = seating[1];
    }
    else 
    {
      seatedOne = seating[1];
      toSit = seating[0];
    }
    
    for(var i = 0; i < table.length; ++i)
    {
      if (table[i] === seatedOne)
      {
        var rightSeat = (i + 1) % table.length;
        var leftSeat  = ((table.length + i) - 1) % table.length;
        if (table[rightSeat] == "Empty")
        {
          table[rightSeat] = toSit;
          return true;
        }
        else if (table[leftSeat] == "Empty")
        {
          table[leftSeat] = toSit;
          return true;
        }
        else 
        {
          return false;
        }
        
      }
    }
  }
  else
  {
    // both are seated check if they are next to each other
    for(var i = 0; i < table.length; ++i)
    {
      if (table[i] === seating[0])
      {
        var rightSeat = (i + 1) % table.length;
        var leftSeat  = ((table.length + i) - 1) % table.length;
        if (table[rightSeat] === seating[1])
        {
          return true;
        }
        else if (table[leftSeat] === seating[1])
        {
          return true;
        }
        else 
        {
          return false;
        }
      }
    }
  }
  return false;
}

function canTheySit(familyMembers, rules)
{
  var indices = {};
  var indices_inv = {};
  var pairs = Array(familyMembers.length).fill().map(()=>Array(familyMembers.length).fill(false));
  
  for (var i = 0; i < familyMembers.length; ++i)
  {
    indices[familyMembers[i]] = i;
    indices_inv[i] = familyMembers[i];
  }
  rules.forEach((rule) => 
  {
    var pair1 = indices[rule[0]];
    var pair2 = indices[rule[1]];
    pairs[pair1][pair2] = true;
    pairs[pair2][pair1] = true;
  });
  
  var table = Array(familyMembers.length).fill("Empty");
  
  var toReturn = true;
  var i = 0;
  pairs.forEach((list) =>
  {
    var total = 0;
    for (var j = i + 1;  j < list.length; ++j)
    {
      if (list[j]) 
      {
        // find if possible to sit
        if (!arrangeSeating(table, [indices_inv[i], indices_inv[j]]))
        {
          toReturn = false;
        }
      } 
    }
    i++;
  });
  
  return toReturn;
}
Abstraction
Logic
IO
Mathematics
Algorithms
Numbers
Data Types

WIP

As a cashier, you must process a sum of money between £1 and £1000 and give back the lowest number of notes and coins available for the amount specified.

Rules

The availables notes are: £50, £20, £10, £5
The available coins are: £2, £1

Output

The method produceChange() will take an int as a parameter, and will return a string describing the given change.

Ex. 1)

produceChange(40)

Should return:

For £40 - change was 2 notes and 0 coins: 2x £20 note

Ex. 2)

produceChange(430)

Should return:

For £430 - change was 10 notes and 3 coins: 8 x £50 note, 1x £20 note, 1X £10 note

Numbers are always formatted correctly so checks for correct input are not required. Assume that the range is inclusively between 1 and 1000.

String output must also be sensibly readable for edge-case scenarios, e.g. "1 coin" is returned instead of "1 coins."

public class Cashier {


    private int fiftyNotes = 0;
    private int twentyNotes = 0;
    private int tenNotes = 0;
    private int fiveNotes = 0;

    private int cash = 0;

    private int twoPoundsCoins = 0;
    private int onePoundCoins = 0;

    private int totalNotes = 0;
    private int totalCoins = 0;

    public Cashier() {
    }


    public String produceChange(int cash) {

        this.cash = cash;

        while ((cash) > 49) {
            cash -= 50;
            fiftyNotes++;
            totalNotes++;
        }
        while ((cash) > 19) {
            cash -= 20;
            twentyNotes++;
            totalNotes++;
        }
        while ((cash) > 9) {
            cash -= 10;
            tenNotes++;
            totalNotes++;
        }
        while ((cash) > 4) {
            cash -= 5;
            fiveNotes++;
            totalNotes++;
        }
        while ((cash) > 1) {
            cash -= 2;
            twoPoundsCoins++;
            totalCoins++;
        }
        onePoundCoins = cash;
        totalCoins += onePoundCoins;

        return this.toString();

    }

    private void clear() {
        fiftyNotes = 0;
        twentyNotes = 0;
        tenNotes = 0;
        fiveNotes = 0;

        twoPoundsCoins = 0;
        onePoundCoins = 0;
        fiftyPence = 0;
        twentyPence = 0;
        tenPence = 0;
        fivePence = 0;
        twoPence = 0;
        onePence = 0;

        totalNotes = 0;
        totalCoins = 0;

    }

    public String toString() {

        StringBuilder sb = new StringBuilder("");

        if (fiftyNotes > 0) {
            sb.append(fiftyNotes + "x £50 note");
        }
        if (twentyNotes > 0) {
            sb.append((sb.length() > 0 ? ", " : "") + twentyNotes + "x £20 note");
        }
        if (tenNotes > 0) {
            sb.append((sb.length() > 0 ? ", " : "") + tenNotes + "x £10 note");
        }
        if (fiveNotes > 0) {
            sb.append((sb.length() > 0 ? ", " : "") + fiveNotes + "x £5 note");
        }
        if (twoPoundsCoins > 0) {
            sb.append((sb.length() > 0 ? ", " : "") + twoPoundsCoins + "x £2 coin");
        }
        if (onePoundCoins > 0) {
            sb.append((sb.length() > 0 ? ", " : "") + onePoundCoins + "x £1 coin");
        }


        String out =  "For £" + cash + " - change was "  + totalNotes  + (totalNotes == 1 ? " note and " : " notes and ")
                + totalCoins + (totalCoins == 1 ? " coin: " : " coins: ");

        clear();
        return out + sb.toString();
    }
}
Arrays
Data Types
Algorithms
Logic
Data

Find positive and negative integer pair in the array.

There is only one pair in the array.

Think about the performance.

[1,2,3,-2,4,5,6] -> [-2, 2]

[1,5,7,8,-2,3,-7] -> [-7, 7]
function pairs(arr){
  let sorted = arr.sort();
  let length = arr.length;

  for (let i = 0; i < length; i++) {
    if (sorted.includes(arr[i] * -1)) {
      return [sorted[i], sorted[i] * -1];
    }
  }
}
\ First Forth Kumite
: hw ." Hello World!" ;
hw cr

calculate the variance of the given vector

not sure how to get the unit tests working. or make it less obvious to the user

c(52,73,55,26,72,45,80,62,NA,7,NA,87,54,46,85,37,94)
Binary
Strings
Data Types
Encryption
Algorithms
Cryptography
Logic
Security

This is a string to binary and vice versa converter in python. The idea is that when the user enters a string into the function, a binary number for each character is returned.

e.g. hi --> 01101000 01101001

and 01101000 01101001 --> hi

Possible further applications could include something like containing code in a string and then executing it or just some fun encryption stuff.

def binary_converter(string):
    result = ""
    if string[0] != '0':
        for character in string:
            result += str(bin(ord(character))[2:].zfill(8)) 
            # So characters can be told apart
            result += " " if character != string[len(string) - 1] else ""
    else:
        # This does the opposite, converting 
        # the output of the above into the original input.
        store = [] # setting up list
        # Sorting numbers into individual items in the list
        for character in string:
            if character != ' ': 
                result += character  
            else: 
                store.append(result)
                result = ""
        store.append(result)
        result = ""
        
        # getting results, aka the orginal string
        for item in store:
            result += chr(int(item, 2))
    
    print(result) # for debugging
    return result

To celebrate the 50 year anniversary of the moon landing I've written some code to output my profile picture to the console!

Can you output your profile picture to the console?

function profilePicture() {
  distance = (x1, y1, x2, y2) => Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
  random = t => t[~~(Math.random() * t.length)];
  var width = 56, height = 32;
  var ratio = height / width;
  var grid = [...Array(height)].map(a => []);
  for (var x = 0; x < width; x++) {
    for (var y = 0; y < height; y++) {
      var isIncluded =
        distance(width * 0.5 * ratio, height * 0.50, (x+.5) * ratio, y + .5) < height * 0.47 &&
        distance(width * 0.6 * ratio, height * 0.45, (x+.5) * ratio, y + .5) >= height * 0.41;
      grid[y].push(isIncluded ? random("£@$%€0&#¥") : ' ');
    }
  }
  return grid.map(a => a.join``).join`\n`;
}