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.
Fundamentals
long long next_bigger_number(long long n) { int digits[20]; int num = 0; long long temp = n; // Convert number to digit array while(temp > 0) { digits[num++] = temp % 10; temp /= 10; } if (num <= 1) { return -1; } // Reverse digits in array for(int i = 0; i < num / 2; ++i) { int t = digits[i]; digits[i] = digits[num - 1 - i]; digits[num - 1 - i] = t; } // Find next permutation (bigger number) int i = num - 2; while(i >= 0 && digits[i] >= digits[i + 1]) { i--; } int j = num - 1; while(digits[j] <= digits[i]) { j--; } // Swap and pivot int t = digits[i]; digits[i] = digits[j]; digits[j] = t; // Reverse the suffix for (int l = i + 1, r = num - 1; l < r; ++l, --r) { t = digits[l]; digits[l] = digits[r]; digits[r] = t; } // Convert back to number long long result = 0; for (int k = 0; k < num; ++k) { if (result > (9223372036854775807 - digits[k]) / 10) { return -1; // overflow } result = result * 10 + digits[k]; } return result; }
long long next_bigger_number(long long n) {return 0; //insert code here- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- long long next_bigger_number(long long n)
- {
- int digits[20];
- int num = 0;
- long long temp = n;
- // Convert number to digit array
- while(temp > 0)
- {
- digits[num++] = temp % 10;
- temp /= 10;
- }
- if (num <= 1)
- {
- return -1;
- }
- // Reverse digits in array
- for(int i = 0; i < num / 2; ++i)
- {
- int t = digits[i];
- digits[i] = digits[num - 1 - i];
- digits[num - 1 - i] = t;
- }
- // Find next permutation (bigger number)
- int i = num - 2;
- while(i >= 0 && digits[i] >= digits[i + 1])
- {
- i--;
- }
- int j = num - 1;
- while(digits[j] <= digits[i])
- {
- j--;
- }
- // Swap and pivot
- int t = digits[i];
- digits[i] = digits[j];
- digits[j] = t;
- // Reverse the suffix
- for (int l = i + 1, r = num - 1; l < r; ++l, --r)
- {
- t = digits[l];
- digits[l] = digits[r];
- digits[r] = t;
- }
- // Convert back to number
- long long result = 0;
- for (int k = 0; k < num; ++k)
- {
- if (result > (9223372036854775807 - digits[k]) / 10)
- {
- return -1; // overflow
- }
- result = result * 10 + digits[k];
- }
- return result;
- }
Knyazikvs.vajdagaborlast month
Recursive bomb search
export interface Box { code: string bomb?: boolean boxes?: Box[] } /** * Creates a box with optional boxes inside */ export const box = (code: string, boxes?: Box[]): Box => ({ code, ( boxes ? { boxes } : {} )}); /** * Returns a copy of a box with bomb */ export function withBomb(box: Box) { return { box, bomb: true } } /** * Finds the bomb in a box */ export function pathToBomb(box: Box, path: string[] = []): string[] { const newPath = [path, box.code] if (box.bomb) return newPath if (!Array.isArray(box.boxes)) return [] return traverseBoxes(box.boxes, newPath) } /** * Finds the bomb in an array of boxes */ export function traverseBoxes(boxes: Box[], path: string[] = []): string[] { return boxes.reduce((result: string[], box) => ( result.length > 0 ? result : pathToBomb(box, path) ), []) } /** * The main function */ export function findTheBomb(boxes: Box[]): string { if (boxes.length === 0) throw new Error('Empty array') return traverseBoxes(boxes).join(' > ') }
- export interface Box {
- code: string
- bomb?: boolean
- boxes?: Box[]
- }
- /**
- * Creates a box with optional boxes inside
- */
export function box(code: string, boxes?: Box[]): Box {return boxes ? { code, boxes } : { code }}- export const box = (code: string, boxes?: Box[]): Box => ({ code, ...( boxes ? { boxes } : {} )});
- /**
- * Returns a copy of a box with bomb
- */
- export function withBomb(box: Box) {
- return { ...box, bomb: true }
- }
- /**
- * Finds the bomb in a box
- */
- export function pathToBomb(box: Box, path: string[] = []): string[] {
- const newPath = [...path, box.code]
- if (box.bomb) return newPath
- if (!Array.isArray(box.boxes)) return []
- return traverseBoxes(box.boxes, newPath)
- }
- /**
- * Finds the bomb in an array of boxes
- */
- export function traverseBoxes(boxes: Box[], path: string[] = []): string[] {
- return boxes.reduce((result: string[], box) => (
- result.length > 0 ? result : pathToBomb(box, path)
- ), [])
- }
- /**
- * The main function
- */
- export function findTheBomb(boxes: Box[]): string {
- if (boxes.length === 0) throw new Error('Empty array')
- return traverseBoxes(boxes).join(' > ')
- }
def best_moves(score) # return the three best moves to reach 0 from the given score if (score.even? && score <= 40) return [Throw.new(score / 2, 'D')] end if score == 50 return [Throw.new(50)] end if score >= 182 return [Throw.new(20, 'T'), Throw.new(20, 'T'), Throw.new(20, 'T')] end if score == 170 return [Throw.new(20, 'T'), Throw.new(20, 'T'), Throw.new(50)] end if score == 141 return [Throw.new(19, 'T'), Throw.new(16, 'T'), Throw.new(18, 'D')] end end
- def best_moves(score)
- # return the three best moves to reach 0 from the given score
if (score.even? && score <= 40) || score == 50return [score]- if (score.even? && score <= 40)
- return [Throw.new(score / 2, 'D')]
- end
- if score == 50
- return [Throw.new(50)]
- end
- if score >= 182
return [60, 60, 60]- return [Throw.new(20, 'T'), Throw.new(20, 'T'), Throw.new(20, 'T')]
- end
- if score == 170
return [60,60,50]- return [Throw.new(20, 'T'), Throw.new(20, 'T'), Throw.new(50)]
- end
- if score == 141
- return [Throw.new(19, 'T'), Throw.new(16, 'T'), Throw.new(18, 'D')]
- end
if score == 21end- end
# From Ruby 3.0, RSpec is used under the hood. # See https://rspec.info/ # Defaults to the global `describe` for backwards compatibility, but `RSpec.desribe` works as well. describe "Example" do it "knows how to proceed quickly" do expect(best_moves(301)).to eq([Throw.new(20, 'T'), Throw.new(20, 'T'), Throw.new(20, 'T')]) end it "should return the optimal moves" do expect(best_moves(50)).to eq([Throw.new(50)]) expect(best_moves(170)).to eq([Throw.new(20, 'T'), Throw.new(20, 'T'), Throw.new(50)]) # expect(best_moves(5)).to needs to be either [1,4] or [1,2,2] end it "should return optimal moves for 141" do #expect(best_moves(141)).to eq([60,57,24]) expect(best_moves(141).last.last?).to be true expect(best_moves(141).map(&:value).sum).to eq(141) end end
- # From Ruby 3.0, RSpec is used under the hood.
- # See https://rspec.info/
- # Defaults to the global `describe` for backwards compatibility, but `RSpec.desribe` works as well.
- describe "Example" do
- it "knows how to proceed quickly" do
expect(best_moves(301)).to eq([60,60,60])- expect(best_moves(301)).to eq([Throw.new(20, 'T'), Throw.new(20, 'T'), Throw.new(20, 'T')])
- end
- it "should return the optimal moves" do
expect(best_moves(50)).to eq([50])- expect(best_moves(50)).to eq([Throw.new(50)])
- expect(best_moves(170)).to eq([Throw.new(20, 'T'), Throw.new(20, 'T'), Throw.new(50)])
expect(best_moves(170)).to eq([60,60,50])expect(best_moves(141)).to eq([60,57,24])expect(best_moves(141).last).to be_evenexpect(best_moves(141).last =< 50).to be_true- # expect(best_moves(5)).to needs to be either [1,4] or [1,2,2]
- end
- it "should return optimal moves for 141" do
- #expect(best_moves(141)).to eq([60,57,24])
- expect(best_moves(141).last.last?).to be true
- expect(best_moves(141).map(&:value).sum).to eq(141)
- end
- end
class Trie: def __init__(self): """ Initializes the trie object. """ self.children = {} self.is_word = False def insert(self, word: str) -> None: """ Inserts a word into the trie. """ # create a 'ptr' to the first node. node = self # loop through all the chars. for char in word: # insert a new instance of Trie if a letter isnt found. if char not in node.children: node.children[char] = Trie() # move to next node node = node.children[char] # once you get to the end mark this node as an end node. node.is_word = True def search(self, word: str) -> bool: """ Returns if the word is in the trie. """ # create a 'ptr' to the first node. node = self for char in word: # if at any point we dont find our current char. then we know the word is not in the Trie. if char not in node.children: return False # move to next node node = node.children[char] # if we get to the end then we know all the chars are in the trie. But we return weather or not we are at the end of a word. Since this could be a "starts with match". return node.is_word def startsWith(self, prefix: str) -> bool: """ Returns if there is any word in the trie that starts with the given prefix. """ # same as search()... node = self for char in prefix: if char not in node.children: return False node = node.children[char] #... except that regardless of the .is_word value we will return true. return True
- class Trie:
- def __init__(self):
- """
- Initializes the trie object.
- """
pass # Replace with your code- self.children = {}
- self.is_word = False
- def insert(self, word: str) -> None:
- """
- Inserts a word into the trie.
- """
pass # Replace with your code- # create a 'ptr' to the first node.
- node = self
- # loop through all the chars.
- for char in word:
- # insert a new instance of Trie if a letter isnt found.
- if char not in node.children:
- node.children[char] = Trie()
- # move to next node
- node = node.children[char]
- # once you get to the end mark this node as an end node.
- node.is_word = True
- def search(self, word: str) -> bool:
- """
- Returns if the word is in the trie.
- """
pass # Replace with your code- # create a 'ptr' to the first node.
- node = self
- for char in word:
- # if at any point we dont find our current char. then we know the word is not in the Trie.
- if char not in node.children:
- return False
- # move to next node
- node = node.children[char]
- # if we get to the end then we know all the chars are in the trie. But we return weather or not we are at the end of a word. Since this could be a "starts with match".
- return node.is_word
- def startsWith(self, prefix: str) -> bool:
- """
- Returns if there is any word in the trie that starts with the given prefix.
- """
pass # Replace with your code- # same as search()...
- node = self
- for char in prefix:
- if char not in node.children:
- return False
- node = node.children[char]
- #... except that regardless of the .is_word value we will return true.
- return True