Given an integer n
return whether or not the integer is even. Your code may not contain any numbers, and it may contain no more than a single space (' '
) character.
E.g.:
is_even(2) # True
is_even(5) # False
is_even(123456) # True
is_even(654321) # False
#is_even=lambda n:not(n%(ord('ࠀ')>>ord('\n'))) # left in the single space here # this also works with more regexes: def is_even(n): return not(n%(ord('ࠀ')>>ord('\n')))
is_even=lambda*z:not(z[ord('`')-ord('`')]%(ord('a')//ord('!')))- #is_even=lambda n:not(n%(ord('ࠀ')>>ord('\n'))) # left in the single space here
- # this also works with more regexes:
- def is_even(n):
- return not(n%(ord('ࠀ')>>ord('\n')))
import codewars_test as test import re, random @test.describe('Fixed Tests') def example_tests(): @test.it('Submission Tests') def check_code(): # regex subs for line comments txt = re.sub('(\s?#.*)','', (open('/workspace/solution.txt').read())) txt = re.sub('^\s+', '', txt, flags=re.MULTILINE) txt = txt.split(':',maxsplit=1)[-1] # dangerous if they use type hinting print("Evaluated code:\n\t{}".format('\n\t'.join(repr(line)for line in txt.split('\n') if line))) if any(n in txt for n in "0123456789"): test.fail("Your code may contain any digits (0-9)!") if txt.count(' ') > 1: test.fail("Your code may only contain one space (' ') character!") test.pass_() @test.it('Test Cases') def simple_tests(): for i in range(1,21): test.assert_equals(is_even(i), i%2==0, f'{i} should equal {i%2==0}') @test.describe('Random Tests') def random_tests(): @test.it("Random Small Numbers") def small_ints(): for _ in range(20): i = random.randint(0,101) test.assert_equals(is_even(i), i%2==0, f'{i} should equal {i%2==0}') @test.it("Random Big Numbers") def big_ints(): for _ in range(20): i = random.randint(1e9,10e9) test.assert_equals(is_even(i), i%2==0, f'{i} should equal {i%2==0}')
- import codewars_test as test
- import re, random
- @test.describe('Fixed Tests')
- def example_tests():
@test.it('Code Test Case')- @test.it('Submission Tests')
- def check_code():
txt = open('/workspace/solution.txt').read()if any(n in txt for n in "0123456789 "):test.fail("Your code may not contain any digits (0-9) or spaces!")- # regex subs for line comments
- txt = re.sub('(\s?#.*)','', (open('/workspace/solution.txt').read()))
- txt = re.sub('^\s+', '', txt, flags=re.MULTILINE)
- txt = txt.split(':',maxsplit=1)[-1] # dangerous if they use type hinting
- print("Evaluated code:\n\t{}".format('\n\t'.join(repr(line)for line in txt.split('\n') if line)))
- if any(n in txt for n in "0123456789"):
- test.fail("Your code may contain any digits (0-9)!")
- if txt.count(' ') > 1:
- test.fail("Your code may only contain one space (' ') character!")
- test.pass_()
@test.it('Example Test Case')- @test.it('Test Cases')
- def simple_tests():
- for i in range(1,21):
- test.assert_equals(is_even(i), i%2==0, f'{i} should equal {i%2==0}')
- @test.describe('Random Tests')
- def random_tests():
- @test.it("Random Small Numbers")
- def small_ints():
- for _ in range(20):
- i = random.randint(0,101)
- test.assert_equals(is_even(i), i%2==0, f'{i} should equal {i%2==0}')
- @test.it("Random Big Numbers")
- def big_ints():
- for _ in range(20):
- i = random.randint(1e9,10e9)
- test.assert_equals(is_even(i), i%2==0, f'{i} should equal {i%2==0}')
Given an integer n
return whether or not the integer is even. Your code may not contain any numbers, and it may contain no more than a single space (' '
) character.
E.g.:
is_even(2) # True
is_even(5) # False
is_even(123456) # True
is_even(654321) # False
is_even=lambda n:not(n%(ord('ࠀ')>>ord('\n'))) # I can't get out the final space!
is_even(){__asm("mov%rdi,%rax\nshr%rax\nshl%rax\ncmp%rax,%rdi\nsete%al\nleave\nret");}- is_even=lambda n:not(n%(ord('ࠀ')>>ord('\n'))) # I can't get out the final space!
import codewars_test as test import re, random @test.describe('Fixed Tests') def example_tests(): @test.it('Submission Tests') def check_code(): # regex subs for line comments txt = re.sub('(\s#.*)','', (open('/workspace/solution.txt').read())) txt = re.sub('^\s+', '', txt, flags=re.MULTILINE) txt = txt.split(':',maxsplit=1)[1] print("Evaluated code:\n\t{}".format('\n\t'.join(repr(line)for line in txt.split('\n')))) if any(n in txt for n in "0123456789"): test.fail("Your code may contain any digits (0-9)!") if txt.count(' ') > 1: test.fail("Your code may only contain one space (' ') character!") test.pass_() @test.it('Test Cases') def simple_tests(): for i in range(1,21): test.assert_equals(is_even(i), i%2==0, f'{i} should equal {i%2==0}') @test.describe('Random Tests') def random_tests(): @test.it("Random Small Numbers") def small_ints(): for _ in range(20): i = random.randint(0,101) test.assert_equals(is_even(i), i%2==0, f'{i} should equal {i%2==0}') @test.it("Random Big Numbers") def big_ints(): for _ in range(20): i = random.randint(1e9,10e9) test.assert_equals(is_even(i), i%2==0, f'{i} should equal {i%2==0}')
#include <criterion/criterion.h>#include <stdio.h>#include <stdlib.h>#include <ctype.h>- import codewars_test as test
- import re, random
_Bool is_even(int);Test(the_even_checker, return_true_for_even_numbers) {int evens[] = {2, 456, 987654};cr_assert(is_even(evens[0]));cr_assert(is_even(evens[1]));cr_assert(is_even(evens[2]));}Test(the_even_checker, return_false_for_odd_numbers) {int odds[] = {3, 123, 987654321};cr_assert_not(is_even(0[odds]));cr_assert_not(is_even(1[odds]));cr_assert_not(is_even(2[odds]));}Test(the_even_checker, should_not_contain_spaces_or_numbers) {FILE *fp = fopen("/workspace/solution.c", "r");if (!fp) exit(8);int c;while ((c = getc(fp)) != EOF) {if (isspace(c) | isdigit(c)) {fclose(fp);cr_assert_fail("Spaces and numbers are not allowed here");}}fclose(fp);cr_assert(1);}- @test.describe('Fixed Tests')
- def example_tests():
- @test.it('Submission Tests')
- def check_code():
- # regex subs for line comments
- txt = re.sub('(\s#.*)','', (open('/workspace/solution.txt').read()))
- txt = re.sub('^\s+', '', txt, flags=re.MULTILINE)
- txt = txt.split(':',maxsplit=1)[1]
- print("Evaluated code:\n\t{}".format('\n\t'.join(repr(line)for line in txt.split('\n'))))
- if any(n in txt for n in "0123456789"):
- test.fail("Your code may contain any digits (0-9)!")
- if txt.count(' ') > 1:
- test.fail("Your code may only contain one space (' ') character!")
- test.pass_()
- @test.it('Test Cases')
- def simple_tests():
- for i in range(1,21):
- test.assert_equals(is_even(i), i%2==0, f'{i} should equal {i%2==0}')
- @test.describe('Random Tests')
- def random_tests():
- @test.it("Random Small Numbers")
- def small_ints():
- for _ in range(20):
- i = random.randint(0,101)
- test.assert_equals(is_even(i), i%2==0, f'{i} should equal {i%2==0}')
- @test.it("Random Big Numbers")
- def big_ints():
- for _ in range(20):
- i = random.randint(1e9,10e9)
- test.assert_equals(is_even(i), i%2==0, f'{i} should equal {i%2==0}')
Given an integer n
, create a function that returns true
if that number is even, false
if that number is odd.
bool isEven(int n) { return n % 2 == 0; }
function isEven(input) {return true;- bool isEven(int n)
- {
- return n % 2 == 0;
- }
void testEqual(bool answer, bool solution) { Assert::That(answer, Equals(solution)); } Describe(isEven_Tests) { It(test_even_numbers) { testEqual(isEven(2), true); testEqual(isEven(456), true); testEqual(isEven(987654), true); } It(test_odd_numbers) { testEqual(isEven(3), false); testEqual(isEven(123), false); testEqual(isEven(987654321), false); } };
const expect = require("chai").expect;describe("Solution", function() {it("should be true for 4", function() {expect(isEven(4)).to.equal(true);});});- void testEqual(bool answer, bool solution)
- {
- Assert::That(answer, Equals(solution));
- }
- Describe(isEven_Tests)
- {
- It(test_even_numbers)
- {
- testEqual(isEven(2), true);
- testEqual(isEven(456), true);
- testEqual(isEven(987654), true);
- }
- It(test_odd_numbers)
- {
- testEqual(isEven(3), false);
- testEqual(isEven(123), false);
- testEqual(isEven(987654321), false);
- }
- };
public class Kata { public static boolean[] populateBathroom(boolean[] urinals, int numOfPeople) { // Implement the urinal problem, described as "given x number of people walking // into a bathroom that contains y number of urinals, describe the optimate // placement of people so that none is standing next to the other" // In this case, the urinals are represented by a boolean array with // true representing a person at a urinal, and false meaning the urinal is empty int pointer = 0; for (int i = numOfPeople; i > 0; i--) { if (i == 1) { urinals[urinals.length-1] = true; } else { urinals[pointer] = true; pointer += 1 + (urinals.length - pointer) / i; } } return urinals; } }
- public class Kata {
- public static boolean[] populateBathroom(boolean[] urinals, int numOfPeople) {
- // Implement the urinal problem, described as "given x number of people walking
- // into a bathroom that contains y number of urinals, describe the optimate
- // placement of people so that none is standing next to the other"
- // In this case, the urinals are represented by a boolean array with
- // true representing a person at a urinal, and false meaning the urinal is empty
if (urinals.length < numOfPeople) {throw new RuntimeException("Cursed input data.");}int peopleRemain = numOfPeople;int peoplePlacePointer = 0;boolean[] result = urinals;while(peopleRemain > 0) {if (peopleRemain == 1) {// only one person is not at the urinal// we'll place him to far corner of the toilet :)result[urinals.length - 1] = true;peopleRemain--;} else {// if more than one person is not at the urinal// place first of them at the urinal with number peoplePlacePointerresult[peoplePlacePointer] = true;// then increments the peoplePlacePoiner by value// which depends on empty urinals count from rigth side of peoplePlacePointer and remain people count// the point is to distribute resulting empty urinals so that almost equals spaces will be between peoplepeoplePlacePointer += 1 + (urinals.length - peoplePlacePointer) / peopleRemain;// one less leftpeopleRemain--;- int pointer = 0;
- for (int i = numOfPeople; i > 0; i--) {
- if (i == 1) { urinals[urinals.length-1] = true; }
- else {
- urinals[pointer] = true;
- pointer += 1 + (urinals.length - pointer) / i;
- }
- }
return result;- return urinals;
- }
- }