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.
use std::collections::HashMap; fn first_non_repeating_letter(s: &str) -> Option<char> { let mut counts = HashMap::new(); // Count occurrences using lowercase chars to avoid String allocations for c in s.chars() { let lower = c.to_lowercase().next().unwrap_or(c); *counts.entry(lower).or_insert(0) += 1; } // Find the first char in the original string that has a count of 1 s.chars().find(|&c| { let lower = c.to_lowercase().next().unwrap_or(c); counts[&lower] == 1 }) }- use std::collections::HashMap;
- fn first_non_repeating_letter(s: &str) -> Option<char> {
let normalize = |c: char| c.to_lowercase().to_string();let mut appears_twice = HashMap::new();- let mut counts = HashMap::new();
- // Count occurrences using lowercase chars to avoid String allocations
- for c in s.chars() {
appears_twice.entry(normalize(c)).and_modify(|e| *e = true).or_insert(false);- let lower = c.to_lowercase().next().unwrap_or(c);
- *counts.entry(lower).or_insert(0) += 1;
- }
s.chars().find(|&c| !appears_twice[&normalize(c)])- // Find the first char in the original string that has a count of 1
- s.chars().find(|&c| {
- let lower = c.to_lowercase().next().unwrap_or(c);
- counts[&lower] == 1
- })
- }
The string slice indexing makes it a bit clunky, but this works too.
fn get_grade(score: usize) -> &'static str { if score <= 100 { &"FFFFFFDCBAA"[score/10..=score/10] } else { "Invalid" } }class Kata {public static String getGrade(int s) {return s >= 0 && s <= 100 ? "" + "FFFFFFDCBAA".charAt(s / 10) : "Invalid";}- fn get_grade(score: usize) -> &'static str {
- if score <= 100 { &"FFFFFFDCBAA"[score/10..=score/10] } else { "Invalid" }
- }
#[cfg(test)] mod tests { use super::get_grade; #[test] fn basic_tests() { assert_eq!(get_grade(95), "A"); assert_eq!(get_grade(90), "A"); assert_eq!(get_grade(100),"A"); assert_eq!(get_grade(82), "B"); assert_eq!(get_grade(80), "B"); assert_eq!(get_grade(89), "B"); assert_eq!(get_grade(74), "C"); assert_eq!(get_grade(70), "C"); assert_eq!(get_grade(79), "C"); assert_eq!(get_grade(65), "D"); assert_eq!(get_grade(60), "D"); assert_eq!(get_grade(69), "D"); assert_eq!(get_grade(45), "F"); assert_eq!(get_grade(59), "F"); assert_eq!(get_grade(0), "F"); } #[test] fn edge_cases() { assert_eq!(get_grade(100), "A"); assert_eq!(get_grade(89), "B"); assert_eq!(get_grade(79), "C"); assert_eq!(get_grade(69), "D"); assert_eq!(get_grade(59), "F"); } #[test] fn invalid_scores() { assert_eq!(get_grade(101), "Invalid"); assert_eq!(get_grade(150), "Invalid"); assert_eq!(get_grade(999), "Invalid"); } }import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.assertEquals;public class KataTest {@Testpublic void basicTests() {assertEquals("A", Kata.getGrade(95));assertEquals("A", Kata.getGrade(90));assertEquals("A", Kata.getGrade(100));assertEquals("B", Kata.getGrade(82));assertEquals("B", Kata.getGrade(80));assertEquals("B", Kata.getGrade(89));assertEquals("C", Kata.getGrade(74));assertEquals("C", Kata.getGrade(70));assertEquals("C", Kata.getGrade(79));assertEquals("D", Kata.getGrade(65));assertEquals("D", Kata.getGrade(60));assertEquals("D", Kata.getGrade(69));assertEquals("F", Kata.getGrade(45));assertEquals("F", Kata.getGrade(59));assertEquals("F", Kata.getGrade(0));}@Testpublic void edgeCases() {// Граничные значения внутри допустимого диапазона// assertEquals("A", Kata.getGrade(100));// assertEquals("B", Kata.getGrade(89));// assertEquals("C", Kata.getGrade(79));// assertEquals("D", Kata.getGrade(69));// assertEquals("F", Kata.getGrade(59));assertEquals("Invalid", Kata.getGrade(-2));}@Testpublic void invalidScores() {assertEquals("Invalid", Kata.getGrade(101));assertEquals("Invalid", Kata.getGrade(150));assertEquals("Invalid", Kata.getGrade(999));assertEquals("Invalid", Kata.getGrade(-1));assertEquals("Invalid", Kata.getGrade(-5));assertEquals("Invalid", Kata.getGrade(-100));}// Важно: Codewars очень любит случайные тесты, чтобы нельзя было захардкодить ответы@Testpublic void randomTests() {for (int i = 0; i < 50; i++) {int score = (int) (Math.random() * 220) - 60; // от -60 до 159String expected = referenceGetGrade(score);assertEquals(Kata.getGrade(score), expected, "Failed for score = " + score);}}// Эталонная реализация для проверки случайных тестовprivate String referenceGetGrade(int score) {if (score < 0 || score > 100) {return "Invalid";} else if (score >= 90) {return "A";} else if (score >= 80) {return "B";} else if (score >= 70) {return "C";} else if (score >= 60) {return "D";} else {return "F";}}- #[cfg(test)]
- mod tests {
- use super::get_grade;
- #[test]
- fn basic_tests() {
- assert_eq!(get_grade(95), "A");
- assert_eq!(get_grade(90), "A");
- assert_eq!(get_grade(100),"A");
- assert_eq!(get_grade(82), "B");
- assert_eq!(get_grade(80), "B");
- assert_eq!(get_grade(89), "B");
- assert_eq!(get_grade(74), "C");
- assert_eq!(get_grade(70), "C");
- assert_eq!(get_grade(79), "C");
- assert_eq!(get_grade(65), "D");
- assert_eq!(get_grade(60), "D");
- assert_eq!(get_grade(69), "D");
- assert_eq!(get_grade(45), "F");
- assert_eq!(get_grade(59), "F");
- assert_eq!(get_grade(0), "F");
- }
- #[test]
- fn edge_cases() {
- assert_eq!(get_grade(100), "A");
- assert_eq!(get_grade(89), "B");
- assert_eq!(get_grade(79), "C");
- assert_eq!(get_grade(69), "D");
- assert_eq!(get_grade(59), "F");
- }
- #[test]
- fn invalid_scores() {
- assert_eq!(get_grade(101), "Invalid");
- assert_eq!(get_grade(150), "Invalid");
- assert_eq!(get_grade(999), "Invalid");
- }
- }
This code is completely nonsense please ignor it
from re import fullmatch from functools import partial, reduce def compose(*funcs): compose_ = lambda f, g: lambda x: f(g(x)) return reduce(compose_, funcs) count_valid_usernames = partial(compose(sum, partial(map, bool), partial(map, partial(fullmatch, r"[a-z][a-z0-9]{3,11}"))))- from re import fullmatch
from functools import partial- from functools import partial, reduce
def count_valid_usernames(usernames):return sum(map(bool, map(partial(fullmatch, r"[a-z][a-z0-9]{3,11}"), usernames)))- def compose(*funcs):
- compose_ = lambda f, g: lambda x: f(g(x))
- return reduce(compose_, funcs)
- count_valid_usernames = partial(compose(sum, partial(map, bool), partial(map, partial(fullmatch, r"[a-z][a-z0-9]{3,11}"))))