Search
• ###### Filter by Language:
• 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.

### Array#flip method

Recursion
Algorithms
Computability Theory
Theoretical Computer Science
Arrays
Methods
Functions
Object-oriented Programming
Control Flow
Basic Language Features
Fundamentals
Classes

# Array#flip method:

This kumite is a script that "flips" an Array's contents. Rather than simply `Array#reverse` it. Here's an example:

``````[1,2,3,[4,5,6],7,8,9].reverse #=> [9,8,7,[4,5,6],3,2,1]
[1,2,3,[4,5,6],7,8,9].flip    #=> [9,8,7,[6,5,4],3,2,1]
#                                          ^
``````

It's as if you've physically turned around an array. This goes for any smaller arrays within the Array given, so recursion may be on the cards.

I've done a neat little script (If you don't mind me saying!) carrying out this task. Please feel free to refactor, comment and feedback; I find critisism indispensible.

As a sidenote, maybe we could implement this for Hash as well? Maybe `{:hello => "world"}` should become `{"world" => :hello}`?

``````class Array # Opens up the Array class for method creation
def flip
result = []
me = self.reverse # Don't fret, I'll sort the Arrays out!

me.each do |x|

if x.class == Array then result << x.flip # Here's the recursion.
else result << x
end

end

return result
end
end``````

### Hello, Ruby!

``````greet = "Hello Ruby!"

print           greet, "\n"
puts            greet
\$stdout.write   greet +"\n"
\$stdout.puts    greet
\$stdout.print   greet, "\n"``````

### Hello Python!

Uses the 'end' tag on the print function to add a "!".

Code
Diff
• ``````from __future__ import print_function
print("Hello Python", end="!\n")``````
•  1 1 ```from __future__ import print_function ``` 2 − ```print("Hello Python!") ``` 2 + ```print("Hello Python", end="!\n") ```

Recent Moves:

### Hello C++

Code
Diff
• ``````#include <iostream>

int main(){

std::cout << "Hello C++" << std::endl;

}``````
•  1 1 ```#include ``` 2 2 3 3 ```int main(){ ``` 4 4 ``` ``` 5 − ``` std::cout << "Hello C++" << endl; ``` 5 + ``` std::cout << "Hello C++" << std::endl; ``` 6 6 ``` ``` 7 7 ```} ```

Recent Moves:

### Batch Interval Processor

Utilities

The idea here is to provide the ability to process a batch of callbacks. It provides the following capabilities:

• dynamic batch list, can be exanded at any time
• each callback is isolated, if an error is thrown it will not prevent the next callback from being called
• each callback has a timeout, so that if it does not call the next function itself, it will eventually be called automatically.
• batches can be ran at intervals, either forever, or up to configured amount of times
• events are provided to hook into batchStart, batchEnd, error and stopped
• nextTick can automatically be called for each item within the batch
``````// interval processor's job is to run a batch of callback items, one at a time. It runs these batches
// at a given interval, and will continue to run batches either forever or until a maxBatches value is reached.
var Processor = function(interval)
{
this.nextTick = true;
this.interval = interval;
this.timeout = 50;
this.items = [];
this.maxBatch = null;
this.events = {error: [], batchStart: [], batchEnd: [], stopped: [], timeout: []};
console.log('Interval Processor created with ' + interval + 'ms interval');
}

Processor.prototype.on = function on(event, callback)
{
this.events[event].push(callback);
}

// returns a new function that ensures that the passed in fn only gets called once no matter
// how many times the returned function is called.
function once(fn)
{
var called = false;
return function()
{
if (!called)
{
if (fn) fn();
called = true;
}
}
}

function fire(processor, event, arg)
{
processor.events[event].forEach(function(fn)
{
fn(processor, arg);
});
}

function nextTick(run)
{
return function(i)
{
process.nextTick(function()
{
run(i);
});
}
};

// add a new callback to the processor chain.
{
var self = this;
this.items.push(function(next)
{
var _next = once(function(){
clearTimeout(timeout);
next();
});

// set a timeout so that if for some reason the item fails and doesn't call its callback,
// we make sure that the chain doesn't break.
var timeout = setTimeout(function()
{
fire(self, 'timeout')
_next();
}, self.timeout);
try
{
item(_next);
}
catch(ex)
{
fire(self, 'error', ex);
_next();
}
});
}

// stop any additional iterations from happening
Processor.prototype.stop = function stop()
{
this.started = false;
clearTimeout(this._timeout);
fire(this, 'stopped');
}

// start the loop
Processor.prototype.start = function start()
{
// if not started yet and there are items to process
if (!this.started && this.items.length > 0)
{
var self = this;
self.started = true;
self.batch = 0;

function run(i)
{
i = i || 0;
// call the runStart event
if (i == 0)
{
self.batch++;
fire(self, 'batchStart');
}

// if not the end of the loop
if (i < self.items.length)
{
// run the current item, pass in the next run as the next function
self.items[i](function(){ run(++i);})
}
// if no more items to process then check if still running and if so then schedule the next batch
else if (self.started)
{
fire(self, 'batchEnd');

if (self.maxBatches && self.maxBatches == self.batch)
{
self.stop();
}
else
{
self._timeout = setTimeout(run, self.interval);
}
}
}

// if nextTick is true then wrap each run within a nextTick call
if (this.nextTick)
{
run = nextTick(run);
}

// run the first batch now
run();
return true;
}

return false;
}

module.exports = Processor;``````

### Number to Word

Numbers
Interview Questions

Converts a number to English. This is a horrendous mess...

``````(ns number-to-words
(:require [clojure.string :refer [join]]))

(def digit-to-word
{1 "One"
2 "Two"
3 "Three"
4 "Four"
5 "Five"
6 "Six"
7 "Seven"
8 "Eight"
9 "Nine"})

(def tens-to-word
{
20 "Twenty"
30 "Thirty"
40 "Fourty"
50 "Fifty"
60 "Sixty"
70 "Seventy"
80 "Eighty"
90 "Ninety"})

(def teen-to-word
{10 "Ten"
11 "Eleven"
12 "Twelve"
13 "Thirteen"
14 "Fourteen"
15 "Fifteen"
16 "Sixteen"
17 "Seventeen"
18 "Eighteen"
19 "Nineteen"})

(defn- small-number-to-words
[x]
(cond
(<= x 9) (digit-to-word x)
(< x 20) (teen-to-word x)
(< x 100)
(let [ones-part (mod x 10)
tens-part (- x ones-part)]
(str (tens-to-word tens-part) "-"
(digit-to-word ones-part)))
(<= x 999)
(let [small-part (mod x 100)
hundreds-digit (-> x (- small-part) (/ 100))]
(str (digit-to-word hundreds-digit) " Hundred " (small-number-to-words small-part)))))

(defn- digit-shift
[x shift]
(-> x (- (mod x shift)) (/ shift) int))

(defn convert
[x]
(loop [words [] x x]
(cond

(-> x (>= 1e6))
(recur (conj words "One Million")
(- x 1e6))

(-> x (>= 1e3))
(recur (conj words
(str
(-> x (digit-shift 1000) small-number-to-words)
" Thousand"))
(mod x 1000))

(-> x (> 0))
(recur (conj words (small-number-to-words x)) 0)

:else (join ", " words))))``````