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.
Fixed version of original, taking divisibility into account
(even though rowcased's solution is probably better)
(I'll take a victory on readability though!)
def validateKey(key): segments = key.split('-') if len(segments) != 2: return False end_ints = list(map(lambda x: int(x), segments[1])) illegalSites = ["333", "444", "555", "666", "777", "888", "999"] illegalEnds = ["0", "8", "9"] return segments[0].isnumeric() and segments[1].isnumeric() and len(segments[0]) == 3 and len(segments[1]) == 7 and not segments[0] in illegalSites and not segments[1][-1:] in illegalEnds and sum(end_ints) % 7 == 0
- def validateKey(key):
- segments = key.split('-')
- if len(segments) != 2: return False
- end_ints = list(map(lambda x: int(x), segments[1]))
- illegalSites = ["333", "444", "555", "666", "777", "888", "999"]
- illegalEnds = ["0", "8", "9"]
return len(segments) == 2 and len(segments[0]) == 3 and len(segments[1]) == 7 and not segments[0] in illegalSites and not segments[1][-1:] in illegalEnds- return segments[0].isnumeric() and segments[1].isnumeric() and len(segments[0]) == 3 and len(segments[1]) == 7 and not segments[0] in illegalSites and not segments[1][-1:] in illegalEnds and sum(end_ints) % 7 == 0
# TODO: Add your tests here # Starting from Node 10.x, [Mocha](https://mochajs.org) is used instead of our custom test framework. # [Codewars' assertion methods](https://github.com/Codewars/codewars.com/wiki/Codewars-JavaScript-Test-Framework) # are still available for now. # # For new tests, using [Chai](https://chaijs.com/) is recommended. # You can use it by requiring: # const assert = require("chai").assert; # If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted. import unittest class MyTest(unittest.TestCase): def test0(self): self.assertEqual(validateKey('879-5676524'), True); def test1(self): self.assertEqual(validateKey('879-5676529'), False); def test2(self): self.assertEqual(validateKey('535-5676524'), True); def test3(self): self.assertEqual(validateKey('000-5676524'), True); def test4(self): self.assertEqual(validateKey('999-5676524'), False); def test5(self): self.assertEqual(validateKey('764-2365839'), False); def test6(self): self.assertEqual(validateKey('3-5676524'), False); def test7(self): self.assertEqual(validateKey('364-17688'), False); def test8(self): self.assertEqual(validateKey('5676524'), False); def test9(self): self.assertEqual(validateKey('3-'), False); def test10(self): self.assertEquals(validateKey("111-1111112"), False); if __name__ == '__main__': unittest.main()
- # TODO: Add your tests here
- # Starting from Node 10.x, [Mocha](https://mochajs.org) is used instead of our custom test framework.
- # [Codewars' assertion methods](https://github.com/Codewars/codewars.com/wiki/Codewars-JavaScript-Test-Framework)
- # are still available for now.
- #
- # For new tests, using [Chai](https://chaijs.com/) is recommended.
- # You can use it by requiring:
- # const assert = require("chai").assert;
- # If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted.
- import unittest
- class MyTest(unittest.TestCase):
- def test0(self):
- self.assertEqual(validateKey('879-5676524'), True);
- def test1(self):
- self.assertEqual(validateKey('879-5676529'), False);
- def test2(self):
- self.assertEqual(validateKey('535-5676524'), True);
- def test3(self):
- self.assertEqual(validateKey('000-5676524'), True);
- def test4(self):
- self.assertEqual(validateKey('999-5676524'), False);
- def test5(self):
- self.assertEqual(validateKey('764-2365839'), False);
- def test6(self):
- self.assertEqual(validateKey('3-5676524'), False);
- def test7(self):
- self.assertEqual(validateKey('364-17688'), False);
- def test8(self):
- self.assertEqual(validateKey('5676524'), False);
- def test9(self):
- self.assertEqual(validateKey('3-'), False);
- def test10(self):
- self.assertEquals(validateKey("111-1111112"), False);
- if __name__ == '__main__':
- unittest.main()
Faster version using the closed form of the Fibonacci sequence given by Binet's formula
This version is prone to round-off error for large enough n, but it gets the right answer up to the 32-bit signed integer cut-off.
public class NthFib { public static final double phi = (1 + Math.sqrt(5))/2.0; public static final double psi = (1 - Math.sqrt(5))/2.0; public static int fib(int n) { return (int)((Math.pow(phi, n) - Math.pow(psi, n))/Math.sqrt(5)); } }
class NthFib {static int fib(int n) {return n > 1 ? fib(--n) + fib(--n) : n;- public class NthFib {
- public static final double phi = (1 + Math.sqrt(5))/2.0;
- public static final double psi = (1 - Math.sqrt(5))/2.0;
- public static int fib(int n) {
- return (int)((Math.pow(phi, n) - Math.pow(psi, n))/Math.sqrt(5));
- }
- }
Although just using the variance function works, I feel like this is more in the spirit of what this was supposed to be.
x <- c(52,73,55,26,72,45,80,62,NA,7,NA,87,54,46,85,37,94) #x = x is true for everything except NA edited <- x[which(x == x)] mean <- sum(edited)/length(edited) normal <- edited - mean squares <- normal * normal sum_of_squares = sum(squares) var <- sum_of_squares/(length(edited) - 1) print(var)
c(52,73,55,26,72,45,80,62,NA,7,NA,87,54,46,85,37,94)- x <- c(52,73,55,26,72,45,80,62,NA,7,NA,87,54,46,85,37,94)
- #x = x is true for everything except NA
- edited <- x[which(x == x)]
- mean <- sum(edited)/length(edited)
- normal <- edited - mean
- squares <- normal * normal
- sum_of_squares = sum(squares)
- var <- sum_of_squares/(length(edited) - 1)
- print(var)
I can shorten the time to a bit more than 1/4 of the given time, by getting the complement of a divisor. If the divisor exists in the list already, we can break.
However, the list must be sorted, which will affect the time complexity.
The algorithm also suffers when the number is prime; I cannot find a faster way.
By doing this, the algorithm can run 200 (example solutions ran and user solution) times in 12 seconds, in the number range of 1e6 to 2e6.
def divisors(n): res=[] for i in range(1,int(n*0.5)+1): if i in res: break if n % i == 0: res.append(i) res.append(int(n / i)) return sorted(list(set(res)))
def divisors(number):dividers = [number]num = int(number * 0.5)for i in range(1,num+1):if(number % i == 0):dividers.append(i)dividers.sort()return dividers- def divisors(n):
- res=[]
- for i in range(1,int(n*0.5)+1):
- if i in res: break
- if n % i == 0:
- res.append(i)
- res.append(int(n / i))
- return sorted(list(set(res)))
from random import randint def example(n): res=[] for i in range(1,n): if i in res: break if n % i == 0: res.append(i) res.append(int(n / i)) return sorted(list(set(res))) test.assert_equals(divisors (5*4*3), [1,2,3,4,5,6,10,12,15,20,30,60]) test.assert_equals(divisors (4), [1,2,4]) test.assert_equals(divisors (5*3), [1,3,5,15]) test.assert_equals(divisors (101), [1,101]) @test.describe('Random') def random_tests(): for i in range(100): rand = randint(1000000,2000000) test.assert_equals(divisors(rand),example(rand))
- from random import randint
- def example(n):
- res=[]
- for i in range(1,n):
- if i in res: break
- if n % i == 0:
- res.append(i)
- res.append(int(n / i))
- return sorted(list(set(res)))
- test.assert_equals(divisors (5*4*3), [1,2,3,4,5,6,10,12,15,20,30,60])
- test.assert_equals(divisors (4), [1,2,4])
- test.assert_equals(divisors (5*3), [1,3,5,15])
- test.assert_equals(divisors (101), [1,101])
- @test.describe('Random')
- def random_tests():
- for i in range(100):
- rand = randint(1000000,2000000)
- test.assert_equals(divisors(rand),example(rand))
numberplatereader=lambda*p:__import__('re').match("[A-Z]{2}[0-9]{2}\s[A-Z]{3}",*p)!=None
def numberplatereader(plate):import reif re.match("[A-Z]{2}[0-9]{2}\s[A-Z]{3}", plate) != None:return Trueelse:return False- numberplatereader=lambda*p:__import__('re').match("[A-Z]{2}[0-9]{2}\s[A-Z]{3}",*p)!=None
def find_special(jar, special): if not isinstance(jar, list) or special not in jar: return return [special, *filter(special.__ne__, jar)]
- def find_special(jar, special):
if not isinstance(jar, list) or not isinstance(special, (int, float)) or special not in jar:return Nonejar = jar[:]jar.remove(special)jar.insert(0, special)return jar- if not isinstance(jar, list) or special not in jar:
- return
- return [special, *filter(special.__ne__, jar)]