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.
I used a different accumulation strategy. It's probably the same speed.
pub fn sort_desc(mut n: u64) -> u64 { let mut digit_counts = [0; 10]; while n > 0 { let digit = (n % 10) as usize; digit_counts[digit] += 1; n /= 10; } let mut result = 0; for digit in (0..10).rev() { for _ in 0..digit_counts[digit] { result *= 10; result += digit as u64; } } result }
- pub fn sort_desc(mut n: u64) -> u64 {
- let mut digit_counts = [0; 10];
- while n > 0 {
- let digit = (n % 10) as usize;
- digit_counts[digit] += 1;
- n /= 10;
- }
- let mut result = 0;
let mut fac = 1;for digit in 0..10 {- for digit in (0..10).rev() {
- for _ in 0..digit_counts[digit] {
result += (digit as u64) * fac;fac *= 10;- result *= 10;
- result += digit as u64;
- }
- }
- result
- }
The hack has been banished.
#include <stdlib.h> #include <string.h> /* Heap allocate the string and return it. */ char* remove_string(const char *input, const int n) { if (n <= 0 || input == NULL) return NULL; for (size_t i = 0, len = 0; i < (size_t)n; i++) { input += len; if ((len = strlen(input) + 1) == 1) return NULL; } return strdup(input); }
- #include <stdlib.h>
- #include <string.h>
- /*
- Heap allocate the string and return it.
- */
- char* remove_string(const char *input, const int n)
- {
- if (n <= 0 || input == NULL) return NULL;
// This is a BIG hack, and I am unsure why it works since in an edge// situation where "n" is greater than the number of '\0' in the string,// the pointer will wander into unallocated memory.for (int c = 1; c < n; input++, (*input == '\0' ? c++ && input++ : c));char* res = strdup(input);return *res == NULL ? NULL : res;- for (size_t i = 0, len = 0; i < (size_t)n; i++)
- {
- input += len;
- if ((len = strlen(input) + 1) == 1) return NULL;
- }
- return strdup(input);
- }
import random import time def not_even(n): pizza_time = time.ctime() if n % 2 == 0: return 'its not even', pizza_time else: return 'its still not even', pizza_time return pizza_time n = random.randint(0, 10000) print(not_even(n))
odd_even=lambda n:int(__import__('requests').get(f'https://api.isevenapi.xyz/api/iseven/{n}/').json()['iseven'])- import random
- import time
- def not_even(n):
- pizza_time = time.ctime()
- if n % 2 == 0:
- return 'its not even', pizza_time
- else:
- return 'its still not even', pizza_time
- return pizza_time
- n = random.randint(0, 10000)
- print(not_even(n))
# TODO Write tests import solution # or from solution import example import time # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): if time.ctime() == time.ctime(): test.assert_equals('its not even', 'its not even') or test.assert_equals('its still even', 'its still even')
import codewars_test as testfrom solution import odd_even- # TODO Write tests
- import solution # or from solution import example
- import time
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
@test.it("test case 1: Testing Odd Numbers")def test_case():for n in range(1, 10, 2):test.assert_equals(odd_even(n), 0)@test.it("test case 2: Testing Even Numbers")- @test.it("test case")
- def test_case():
for n in range(0, 10, 2):test.assert_equals(odd_even(n), 1)- if time.ctime() == time.ctime():
- test.assert_equals('its not even', 'its not even') or test.assert_equals('its still even', 'its still even')
fn mean(x: &[f64]) -> f64 { let x_len_float = x.len() as f64; return x.iter().sum::<f64>() / x_len_float; }
fn mean(x: &[u64]) -> u64 {x.iter().sum::<u64>() / x.len() as u64- fn mean(x: &[f64]) -> f64 {
- let x_len_float = x.len() as f64;
- return x.iter().sum::<f64>() / x_len_float;
- }
#[test] fn test_positive_integers() { assert_eq!(mean(&[4_f64, 8_f64, 4_f64, 8_f64]), 6_f64); } #[test] fn test_floats() { let actual = mean(&[0.48, 3.28, -5.77, 3.49]); let expected = 0.3700000; const EPSILON: f64 = 1e-7; let diff = (actual-expected).abs(); assert!(diff < EPSILON, "Difference between the computed mean: {} and expected: {} is greater than {}", actual, expected, EPSILON); }
- #[test]
fn test() {assert_eq!(mean(&[4, 8, 4, 8]), 6);- fn test_positive_integers() {
- assert_eq!(mean(&[4_f64, 8_f64, 4_f64, 8_f64]), 6_f64);
- }
- #[test]
- fn test_floats() {
- let actual = mean(&[0.48, 3.28, -5.77, 3.49]);
- let expected = 0.3700000;
- const EPSILON: f64 = 1e-7;
- let diff = (actual-expected).abs();
- assert!(diff < EPSILON, "Difference between the computed mean: {} and expected: {} is greater than {}", actual, expected, EPSILON);
- }
less char -> Short-Circuiting instead of ternary