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.
A more succinct version of the function but does require dependencies on tr, sed, and awk. I bet it could be reworked to remove tr and either sed or awk.
add_PATH() { t=$(echo $PATH | tr : '\n' | awk '!x[$0]++' | tr '\n' : | sed 's/.$//') echo -n "$t" unset t } echo "Original Path: $PATH" export PATH=$(add_PATH /usr/local/bin) echo "New Path: $PATH"
- add_PATH() {
oIFS=$IFSIFS=':'t=(${PATH})unset IFSt=("$1" ${t[@]%%"$1"})# output the new arrayIFS=':'echo -n "${t[*]}"unset tIFS=$oIFS- t=$(echo $PATH | tr : '\n' | awk '!x[$0]++' | tr '\n' : | sed 's/.$//')
- echo -n "$t"
- unset t
- }
- echo "Original Path: $PATH"
- export PATH=$(add_PATH /usr/local/bin)
- echo "New Path: $PATH"
In this version end
is protected so that it is only called once, and once its called next()
no longer does anything.
function Chain(){ this.links = []; } Chain.prototype.link = function link(cb){ this.links.push(cb); return this; } Chain.prototype.run = function run(end){ var self = this, ended = false, _end = function(){ end(); ended = true; }, next = function(){ if (!ended){ if(self.links.length) { self.links.shift()(next, _end); } else { _end(); } } }; next(); }
- function Chain(){
- this.links = [];
- }
- Chain.prototype.link = function link(cb){
- this.links.push(cb);
- return this;
- }
- Chain.prototype.run = function run(end){
if(this.links.length) {this.links.shift()(Chain.prototype.run.bind(this, end), end);} else {end();}- var self = this,
- ended = false,
- _end = function(){
- end();
- ended = true;
- },
- next = function(){
- if (!ended){
- if(self.links.length) {
- self.links.shift()(next, _end);
- } else {
- _end();
- }
- }
- };
- next();
- }
Write a generator that given a starting seed (which must be odd), will generate "random" numbers using the infamous RANDU generator, first value should be the seed itself.
def randu_seq(x): while True: yield x x = x * 65539 % 0x80000000
(ns randu)(defn randu-seq [x] (iterate #(mod (* 65539 %) 0x80000000) x))- def randu_seq(x):
- while True:
- yield x
- x = x * 65539 % 0x80000000
Test.describe("Tests") rand = randu_seq(1) Test.assert_equals(str(type(rand))[7:-2], "generator", "randu_seq should be a generator") for i in xrange(3, 20, 2): Test.assert_equals(randu_seq(i).next(), i, "The first value returned must be the seed") Test.assert_equals([rand.next() for _ in xrange(21)], [1, 65539, 393225, 1769499, 7077969, 26542323, 95552217, 334432395, 1146624417, 1722371299, 14608041, 1766175739, 1875647473, 1800754131, 366148473, 1022489195, 692115265, 1392739779, 2127401289, 229749723, 1559239569], "randu_seq(1) doesn't match OEIS A096555!")
(ns randu-test(:require [clojure.test :refer :all][randu :refer [randu-seq]]))- Test.describe("Tests")
(deftest test-oeis-a096555(is(=(take 21 (randu-seq 1))[1 65539 393225 1769499 7077969 2654232395552217 334432395 1146624417 172237129914608041 1766175739 1875647473 1800754131366148473 1022489195 692115265 13927397792127401289 229749723 1559239569])"(randu-seq 1) doesn't match OEIS A096555!"))- rand = randu_seq(1)
- Test.assert_equals(str(type(rand))[7:-2], "generator", "randu_seq should be a generator")
- for i in xrange(3, 20, 2):
- Test.assert_equals(randu_seq(i).next(), i, "The first value returned must be the seed")
- Test.assert_equals([rand.next() for _ in xrange(21)], [1, 65539, 393225, 1769499, 7077969, 26542323, 95552217, 334432395, 1146624417, 1722371299, 14608041, 1766175739, 1875647473, 1800754131, 366148473, 1022489195, 692115265, 1392739779, 2127401289, 229749723, 1559239569], "randu_seq(1) doesn't match OEIS A096555!")
def name = "John Cena" try { println "Your name in japanese is " + convertToJapanese(name) } catch(e) { System.err << e.getMessage() } static String convertToJapanese(String name) { name = name.toLowerCase() if(name == "" || !name.matches('^[a-z\\s]*$')) throw new Exception('ERROR: invalid name\n') def alphabet = [ 'ka','zu','mi','te','ku', 'lu','ji','ri','ki','zus', 'me','ta','rin','to','mo', 'no','ke','shi','ari','chi', 'do','ru','mei','na','fu','zi' ] String japaneseName = '' name.each { if(it in [' ', '\t', '\n']) japaneseName += ' ' else japaneseName += alphabet[((int)it) - 97] } japaneseName.split(' ').collect{it.capitalize()}.join(' ') }
- def name = "John Cena"
- try {
- println "Your name in japanese is " + convertToJapanese(name)
- } catch(e) {
- System.err << e.getMessage()
- }
- static String convertToJapanese(String name) {
- name = name.toLowerCase()
- if(name == "" || !name.matches('^[a-z\\s]*$'))
- throw new Exception('ERROR: invalid name\n')
- def alphabet = [
- 'ka','zu','mi','te','ku',
- 'lu','ji','ri','ki','zus',
- 'me','ta','rin','to','mo',
- 'no','ke','shi','ari','chi',
- 'do','ru','mei','na','fu','zi'
- ]
- String japaneseName = ''
- name.each {
- if(it in [' ', '\t', '\n'])
japanaseName += ' '- japaneseName += ' '
- else
japanaseName += alphabet[((int)it) - 97]- japaneseName += alphabet[((int)it) - 97]
- }
japanaseName.split(' ').collect{it.capitalize()}.join(' ')- japaneseName.split(' ').collect{it.capitalize()}.join(' ')
- }
import static org.junit.Assert.*; import org.junit.Test; import java.io.ByteArrayOutputStream; import java.io.PrintStream; public class TestT { @Test public void test1() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos); // IMPORTANT: Save the old System.out! PrintStream old = System.out; // Tell Java to use your special stream System.setOut(ps); Solution.main(null); // Put things back System.out.flush(); System.setOut(old); System.out.println("@" + baos.toString()+"@"); assertEquals("What is next?\n", baos.toString() ); } }
- import static org.junit.Assert.*;
- import org.junit.Test;
- import java.io.ByteArrayOutputStream;
- import java.io.PrintStream;
- public class TestT {
- @Test
- public void test1() {
assertEquals(1, 1);- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- PrintStream ps = new PrintStream(baos);
- // IMPORTANT: Save the old System.out!
- PrintStream old = System.out;
- // Tell Java to use your special stream
- System.setOut(ps);
- Solution.main(null);
- // Put things back
- System.out.flush();
- System.setOut(old);
- System.out.println("@" + baos.toString()+"@");
- assertEquals("What is next?\n", baos.toString() );
- }
- }
import java.util.*; public class MatchingBrackets { public static boolean isBalanced(String braces) { Stack<String> stack = new Stack<>(); Map<String, String> enclosing = new HashMap<>(); enclosing.put(")","("); enclosing.put("}","{"); enclosing.put("]","["); for (char brace : braces.toCharArray()) { final String strBrace = Character.toString(brace); if (!stack.isEmpty() && stack.peek().equals(enclosing.get(strBrace))) { stack.pop(); } else { stack.push(strBrace); } } return stack.isEmpty(); } }
/*** Check if brackets are matching.* Time Complexity: O(N), Space Complexity: O(N)** @author Jayesh Chandrapal*/- import java.util.*;
import java.lang.*;import java.io.*;class MatchingBrackets {/*** Checks if given input string contains matching brackets.** @params str input string to be checked* @returns boolean value indicating if given string contains matching brackets*/public static boolean isBalanced(String str) {boolean balanced = true;Stack<Character> stack = new Stack<Character>();for(Character c : str.toCharArray()) {if(c == '(' || c == '{' || c == '[') {stack.push(c);- public class MatchingBrackets {
- public static boolean isBalanced(String braces) {
- Stack<String> stack = new Stack<>();
- Map<String, String> enclosing = new HashMap<>();
- enclosing.put(")","(");
- enclosing.put("}","{");
- enclosing.put("]","[");
- for (char brace : braces.toCharArray()) {
- final String strBrace = Character.toString(brace);
- if (!stack.isEmpty() && stack.peek().equals(enclosing.get(strBrace))) {
- stack.pop();
- } else {
if(stack.isEmpty()) {balanced = false;break;}Character top = stack.peek();if((c == ')' && top == '(') || (c == '}' && top == '{') || (c == ']' && top == '[')) {stack.pop();} else {balanced = false;break;}- stack.push(strBrace);
- }
}return balanced && stack.isEmpty();}- }
- return stack.isEmpty();
- }
- }
Apart from using echo
to output in PHP, you could alternatively use the print
statement. Both behave similarly except for a few fundamental differences:
-
print
has a return value of1
;echo
does not -
echo
can take multiple arguments;print
cannot -
echo
executes faster thanprint
This illustrates how to sum natural numbers using tail recursion. In some languages, tail call optimization is performed, which enables recursive calls to take constant stack space.
def sum n def acc(n, s) return s if n <= 0 # Base case acc(n - 1, s + n) # Recursion step, with accumulator end acc(n, 0) end
- def sum n
return 1 if n == 1 # Base case of recursion method - must be defined; otherwise infinite recursion may occurn + sum(n - 1)- def acc(n, s)
- return s if n <= 0 # Base case
- acc(n - 1, s + n) # Recursion step, with accumulator
- end
- acc(n, 0)
- end