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.
You could also just test your strings length.
fn flip_the_number(x: &u64) -> u64 { let mut x = *x; let mut y = 0; while x != 0 { y = y * 10 + x % 10; x /= 10; } y }
- fn flip_the_number(x: &u64) -> u64 {
x.to_string().chars().rev().collect::<String>().parse::<u64>().unwrap()- let mut x = *x;
- let mut y = 0;
- while x != 0 {
- y = y * 10 + x % 10;
- x /= 10;
- }
- y
- }
#[test] fn test() { let nums = [ (1234, 4321), (1234567890, 987654321), (243, 342), (12, 21), (837583, 385738), (32851532, 23515823), (999999, 999999), (111, 111), (35832456789, 98765423853), (9223372036854775807, 7085774586302733229) ]; for num in nums.iter() { assert_eq!(num.1, flip_the_number(&num.0)); } }
- #[test]
- fn test() {
for num in NUMS.iter() {- let nums = [
- (1234, 4321),
- (1234567890, 987654321),
- (243, 342),
- (12, 21),
- (837583, 385738),
- (32851532, 23515823),
- (999999, 999999),
- (111, 111),
- (35832456789, 98765423853),
- (9223372036854775807, 7085774586302733229)
- ];
- for num in nums.iter() {
- assert_eq!(num.1, flip_the_number(&num.0));
- }
- }
use std::collections::BTreeMap; fn digits(n: u64) -> usize { let powers = (0..20).map(|n| ( if n == 0 { 0 } else { 10_u64.pow(n) }, n as usize + 1 )).collect::<BTreeMap<_, _>>(); return *powers.range(..=n).last().unwrap().1; }
- use std::collections::BTreeMap;
use std::ops::Bound::Unbounded;use std::ops::Bound::Included;- fn digits(n: u64) -> usize {
let mut powers = BTreeMap::new();powers.insert(0, 1);powers.insert(10, 2);powers.insert(100, 3);powers.insert(1000, 4);powers.insert(10000, 5);powers.insert(100000, 6);powers.insert(1000000, 7);powers.insert(10000000, 8);powers.insert(100000000, 9);powers.insert(1000000000, 10);powers.insert(10000000000, 11);powers.insert(100000000000, 12);powers.insert(1000000000000, 13);powers.insert(10000000000000, 14);powers.insert(100000000000000, 15);powers.insert(1000000000000000, 16);powers.insert(10000000000000000, 17);powers.insert(100000000000000000, 18);powers.insert(1000000000000000000, 19);powers.insert(10000000000000000000, 20);return *powers.range((Unbounded, Included(n))).last().unwrap().1;- let powers = (0..20).map(|n| (
- if n == 0 { 0 } else { 10_u64.pow(n) },
- n as usize + 1
- )).collect::<BTreeMap<_, _>>();
- return *powers.range(..=n).last().unwrap().1;
- }
Updated tests.
%timeit prime_checker(10000000019)
-> 13.4 ms ± 40.5 µs
def prime_checker(n): if n < 4: return n > 1 if not (n % 2 and n % 3): return False for i in range(5, int(n ** 0.5)+1, 6): if not ((n % i) and (n % (i + 2))): return False return True
"""Simple and fast, around 2-3ms"""# One line removed, one is edited with wit but actually works, you really should git gud < idk >- def prime_checker(n):
for i in range(3, int(n**.5)+1 if n > 0 else 10, 2):if n%i == 0: return Falsereturn (n > 2 and n%2 != 0) or n == 2- if n < 4:
- return n > 1
- if not (n % 2 and n % 3):
- return False
- for i in range(5, int(n ** 0.5)+1, 6):
- if not ((n % i) and (n % (i + 2))):
- return False
- return True
import codewars_test as test from solution import prime_checker @test.describe("Example") def test_group(): @test.it("test case") def test_case(): # primes from https://oeis.org/A000040/a000040.txt primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997} for n in range(-8, max(primes) + 1): if n in primes: test.assert_equals(prime_checker(n), True) else: test.assert_equals(prime_checker(n), False) test.assert_equals(prime_checker(1021), True) test.assert_equals(prime_checker(39 * 41), False) test.assert_equals(prime_checker(553_105_253), True) test.assert_equals(prime_checker(982_451_653), True)
- import codewars_test as test
- from solution import prime_checker
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
test.assert_equals(prime_checker(653), True)test.assert_equals(prime_checker(654), False)test.assert_equals(prime_checker(5), True)test.assert_equals(prime_checker(777), False)#test.assert_equals(prime_checker(977), True)test.assert_equals(prime_checker(125), False)#test.assert_equals(prime_checker(997), True)test.assert_equals(prime_checker(709), True)test.assert_equals(prime_checker(15), False)#test.assert_equals(prime_checker(11), True)test.assert_equals(prime_checker(13), True)test.assert_equals(prime_checker(17), True)test.assert_equals(prime_checker(19), True)test.assert_equals(prime_checker(23), True)test.assert_equals(prime_checker(29), True)- # primes from https://oeis.org/A000040/a000040.txt
- primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997}
- for n in range(-8, max(primes) + 1):
- if n in primes:
- test.assert_equals(prime_checker(n), True)
- else:
- test.assert_equals(prime_checker(n), False)
- test.assert_equals(prime_checker(1021), True)
- test.assert_equals(prime_checker(39 * 41), False)
- test.assert_equals(prime_checker(553_105_253), True)
test.assert_equals(prime_checker(982_451_653), True)test.assert_equals(prime_checker(-1), False)test.assert_equals(prime_checker(4), False)test.assert_equals(prime_checker(2), True)- test.assert_equals(prime_checker(982_451_653), True)
// True, sorting could take an almost infinite number of processes... wish there was a way without // using the Math class or a loop/recursion interface HighLow { static int[] findLargestAndSmallest(int[] nums) { if(nums == null || nums.length == 0) return null; int min = nums[0]; int max = nums[0]; int index = 0; while(index < nums.length) { min = (min < nums[index]) ? min : nums[index]; max = (max > nums[index]) ? max : nums[index]; index++; } return new int[]{min, max}; } }
interface HighLow {// IMHO with a function this simple and small,// seperating it to even more tiny functions is just overkill and makes the code less readable,// with such small functions you don't seperate for readability, but reusability. :)static int[] findLargestAndSmallest(int[] nums) {if (nums == null || nums.length == 0) return null;- // True, sorting could take an almost infinite number of processes... wish there was a way without
- // using the Math class or a loop/recursion
int max = nums[0];- interface HighLow {
- static int[] findLargestAndSmallest(int[] nums) {
- if(nums == null || nums.length == 0) return null;
- int min = nums[0];
- int max = nums[0];
- int index = 0;
for (int num : nums) {if (max < num) max = num;if (min > num) min = num;- while(index < nums.length) {
- min = (min < nums[index]) ? min : nums[index];
- max = (max > nums[index]) ? max : nums[index];
- index++;
- }
- return new int[]{min, max};
- }
export function transform(source: any) { const target = {}; Object.entries(source).forEach(function ([k, v]) { k.split("_").slice(0, -1).reduce(function (node: {[key: string]: any}, element: string) { return node[element] ??= {}; }, target)[k.slice(k.lastIndexOf("_") + 1)] = v; }); return target; }
export function transform(obj: any) {const _RegEx = new RegExp(/_/);const underscoredProp = Object.keys(obj).find(key =>_RegEx.test(key)) || '';const value = obj[underscoredProp];const propertList = underscoredProp.split('_').reverse();let toAdd = {};propertList.map((prop,index) => {toAdd = index==0 ?{[prop]:value} : {[prop]:toAdd}})return toAdd;- export function transform(source: any) {
- const target = {};
- Object.entries(source).forEach(function ([k, v]) {
- k.split("_").slice(0, -1).reduce(function (node: {[key: string]: any}, element: string) {
- return node[element] ??= {};
- }, target)[k.slice(k.lastIndexOf("_") + 1)] = v;
- });
- return target;
- }